Ben Ben - 4 months ago 39
Android Question

android set style in code

I'm trying to use the TextView constructor with style like this:

TextView myText = new TextView(MyActivity.this, null, );

however, when i do this, the text view does not appear to take the style (I verified the style by setting it on a static object).

I've also tried using
but it also doesn't work


I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
        android:text="This is a template"
        style="@style/my_style" />

then inflate this to instantiate your new TextView:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);

Hope this helps.