Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTextAppearance() is not working on textviews

I know that there are a lot of discussions about setTextAppearance, but I searched and I did't find anything that worked for me...

I'm programmatically setting the code for a textView, that is inside a tablerow.
But when I'm trying to call the style from styles.xml on the textView it dosn't apply.

From styles.xml:

<style name="ElemTabArtikl">
    <item name="android:layout_marginLeft">10dp</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>

The code in java:

TextView tvNazArt = new TextView(this);
    tvNazArt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.4f));
    tvNazArt.setTextAppearance(this, R.style.ElemTabArtikl);
    tvNazArt.setText(NazivArt);
    tvNazArt.setPadding(10, 0, 0, 0);
artikliVrstica.addView(tvNazArt);
like image 555
BRap Avatar asked Dec 02 '25 09:12

BRap


1 Answers

If you are only using two entries in your style, why not just do it all programatically?

Change:

tvNazArt.setTextAppearance(this, R.style.ElemTabArtikl);

to:

float density = getResources().getDisplayMetrics().density;
int padding = (int)(10 * density);
tvNazArt.setPadding (padding, int top, int right, int bottom)
tvNazArt.setTextAppearance(this, android.R.style.textAppearance_Medium);

If you want to save keystrokes, or must do this a number of times, create a function called setstyle(), or something of that nature, and call the extra code using that.

i.e.

void setstyle(Context context)
{
    float density = context.getResources().getDisplayMetrics().density;
    int padding = (int)(10 * density);
    tvNazArt.setPadding (padding, int top, int right, int bottom)
    tvNazArt.setTextAppearance(context, android.R.style.textAppearance_Medium);
}

of course, tvNazArt would have to be global.

like image 65
dberm22 Avatar answered Dec 03 '25 22:12

dberm22



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!