0

I have an UI like this:

<RelativeLayout>
       <TextView />
       <RelativeLayout>
             <TextView />
             <TextView />
             <LinearLayout>
                  <TextView />
             </LinearLayout>
       </RelativeLayout>
</RelativeLayout>

I want to access all TextViews programmatically and change some attributes but I don't want use id and findViewById, because I have a multi layout which I want to perform the same changes in them, i.e.: change the fonts of TextViews.

How can I access all the TextViews whether direct or hierarchy of a view, like RelativeLayout in a loop or in a list?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ali kiani
  • 877
  • 1
  • 14
  • 29
  • 1
    It would be trivial to write a recursive (or even a linear) algorithm to walk over the view hierarchy. Most of the solution is already outlined [here](http://stackoverflow.com/questions/18668897/android-get-all-children-elements-of-a-viewgroup/). All you'll need to do is add some type checking for the particular view type you're interested in. – MH. Mar 23 '15 at 08:17

1 Answers1

-1
    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.my_linear_layout);

    for(i = 0; i <= mLinearLayout.getChildCount(); i++){ 
        View view = mLinearLayout.getChildAt(i); 
        if (view instanceof TextView){ 
            TextView textView = (TextView) view;
            // TODO do your stuff 
        } 
    }

if you have ViewGroups in your RelativeLayout then you need to write a recursive method.

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • *"If you have ViewGroups in your RelativeLayout (...)"* - based on the question, there's not much of an *if* here... There is a `RelativeLayout` in the first level, and a `LinearLayout` in the second. – MH. Mar 23 '15 at 08:21
  • He should research some to improve himself :P – Emre Aktürk Mar 23 '15 at 08:22