user3786731
user3786731

Reputation: 27

How to get arbitrary TextViews so that I can change their text size

I am making an android app and I want it to have a option in the action bar which allows the user to increase or decrease the text size in the current activity (or the whole app - would like to know how to do both).

I have defined a base activity which specifies the action bar. In the onOptionsItemSelected method I need to be able to get all the TextViews in the activity and change their text size. How would I go about doing this? Also how would I go about making the change persistent across the app?

Upvotes: 0

Views: 28

Answers (1)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

You can navigate trough the View hierarchy from the root and get all TextViews.

void setAllTextSize(ViewGroup root, int size){
    for(int i = 0; i < root.getChildCount();i++){
        View child = root.getChildAt(i);
        if(child instanceof TextView)
            ((TextView) child).setTextSize(TypedValue.COMPLEX_UNIT_PT, size);

        if(child instanceof ViewGroup)
            setAllTextSize((ViewGroup) child, size);
    }
}

Then you must call this method passing the root of your layout.

Upvotes: 1

Related Questions