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() .


UselesssCat Points 2575


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;
        } else if(child instanceof ViewGroup){

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



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: