Question

i'm new at Android world, and i have a doubt, is there any method that give me the name of the id's i create in main.xml? For example i have this:

main.xml

<TextView android:id="@+id/text1"
       android:layout_width="70px"
       android:layout_height="70px"
       android:text="Google"
       />

<TextView android:id="@+id/text2"
       android:layout_width="70px"
       android:layout_height="70px"
       android:text="As"
       />

And what i want is the id name from the two TextView, is there any method that i can use in my class .java that give me for this example the id? In this case i want the (text1 and text2).

Thanks and forgive mi English.

Was it helpful?

Solution

Set an id to the parent layout and try something like this:

    LinearLayout ll = findViewById(R.id.yourLayout);
    for(int i=0; i<ll.getChildCount(); i++){
        View v = ll.getChildAt(i);
        int idView = v.getId();
            //Do something with idView
    }

OTHER TIPS

inflate the file

linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = linflater.inflate(R.layout.main, null);

then run findViewById to turn TextView into an object

TextView text1 = (TextView)main.findViewById(R.id.text1);

then you can work with object text1 later like this

text1.setText("foo");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top