3 votes

Change text size of several textviews at the same time from code

I want to have several buttons that change the text size of the whole application.

I know that in the layout.xml I can change the size manually or by referencing a dimension (@dimen/size_letter), I also know that from the code I can create a reference to the object (widget) and use the method setTextSize() but the problem is that there are hundreds of TextView and I need to change the text size to all of them at the same time.

I don't want to have to make a thousand references of all the TextViews and then use the method setTextSize() .

1voto

UselesssCat Points 2575

Good.

Have you tried to recursively traverse the container of your buttons? This code is an example, so it might not work:

private void changeTextViewTextSize(ViewGroup v){
    for (int i = 0, length = v.getChildCount(); i < length; i++){
        Object child = v.getChildAt(i); 

        if (child instanceof TextView){
            TextView tv = (EditText)child;
            tv.setTextSize(12)
        } else if(child instanceof ViewGroup){
            changeTextViewTextSize((ViewGroup)child); 
        }
    }
}

To call this function you must pass as parameter the container of all your textviews:

changeTextViewTextSize(R.id.textviewscontainer);

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X