karansky
karansky

Reputation: 558

Android - How to change the attributes of multiple child views

I have a LinearLayout with a lot of textViews and my application can change its theme according to user's preference. How to change all of the textView's textColor and font type attribute in this particular LinearLayout? I cannot use

<style name="Theme1" parent="android:Theme.Light.NoTitleBar">
        <item name="android:textViewStyle">@style/theme1TextStyle/item>
</style>

in the styles.xml file because I have other textViews in another layout which need different font color and font type. The only way that I can think of is to declare a custom attribute (customTextViewStyle) myself and use it in every textViews (by inserting style="?attr/customTextViewStyle") in that LinearLayout and do the following in styles.xml:

<style name="Theme1" parent="android:Theme.Light.NoTitleBar">
        <item name="android:customTextViewStyle">@style/theme1TextStyle/item>
</style>

Is there a more efficient way to do this? My objective is to make this code readable and easy to modify.

Upvotes: 0

Views: 1976

Answers (1)

Pankaj Nimgade
Pankaj Nimgade

Reputation: 4549

make these styles in the styles.xml, you can see the working example which is uploaded on github

<style name="TestOne">
    <item name="android:textColor">#00aa22</item>
    <item name="android:fontFamily">sans-serif</item>
</style>

<style name="TestTwo">
    <item name="android:textColor">#00aacc</item>
    <item name="android:fontFamily">sans-serif-light</item>
</style>

<style name="TestThree">
    <item name="android:textColor">#ddaa00</item>
    <item name="android:fontFamily">sans-serif-condensed</item>
</style>

<style name="TestFour">
    <item name="android:textColor">#bbaa00</item>
    <item name="android:fontFamily">sans-serif-thin</item>
</style>

<style name="TestFive">
    <item name="android:textColor">#00aabb</item>
    <item name="android:fontFamily">sans-serif-medium</item>
</style>

and these can be used like this to a textView

textView.setTextAppearance(getApplicationContext(),R.style.TestOne);

I have written an example which you can check

public class TestTextViewActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;

    int[] id = {R.style.TestOne, R.style.TestTwo, R.style.TestThree, R.style.TestFour, R.style.TestFive};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_text_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_custom_toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initializeUI();
    }

    private void initializeUI() {
        textView = (TextView) findViewById(R.id.TestTextViewActivity_textView);
        button = (Button) findViewById(R.id.TestTextViewActivity_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = (new Random()).nextInt(6);
                textView.setTextAppearance(getApplicationContext(),id[count]);
            }
        });
    }

}

Output

enter image description here

Upvotes: 1

Related Questions