Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index of particular view OR ViewGroup which is added to the ViewGroup(layout)

Tags:

android

<TextView android:layout_width="wrap_content"     android:layout_height="wrap_content" android:layout_margin="24dip"     android:text="Add Notes" /> <TableLayout android:layout_width="fill_parent"     android:layout_height="wrap_content" android:layout_marginLeft="24dip"     android:layout_marginRight="24dip" android:id="@+id/tlNotes"     android:stretchColumns="0"> </TableLayout>  <Button android:id="@+id/bAddNoteLine"     android:layout_marginLeft="24dip" android:layout_width="wrap_content"     android:layout_height="wrap_content" android:text="ADD"> </Button>  <LinearLayout android:id="@+id/llIndex"     android:orientation="horizontal" android:layout_width="fill_parent"     android:layout_height="wrap_content" android:layout_margin="21dip"     android:gravity="center">     <Button android:id="@+id/bSaveSubjectiveNote"         android:layout_width="192dip" android:layout_height="wrap_content"         android:text="Save" />     <Button android:id="@+id/bDiscardSubjectiveNote"         android:layout_width="192dip" android:layout_height="wrap_content"         android:layout_marginLeft="48dip" android:background="@drawable/button"         android:text="Discard" /> </LinearLayout> 

how to retrieve the index of linearLayout which has "llIndex" as id. Thanks

like image 252
user373885 Avatar asked Jul 26 '10 12:07

user373885


People also ask

Which method is used to access a view element of a layout?

When we have created the layout, we need to load the XML layout resource from our activity onCreate() callback method and access the UI element from the XML using findViewById. Here, we can observe the above code and finds out that we are calling our layout using the setContentView method in the form of R.

What is the difference between View and ViewGroup in android?

View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views (child views) and other ViewGroup.

What provides the layout in which you can order the appearance and sequence of views?

A ViewGroup provides the android layout in which we can order the appearance and sequence of views. Examples of ViewGroup are LinearLayout , FrameLayout , RelativeLayout etc.


1 Answers

Very simple - call the indexOfChild function on the view's parent:

LinearLayout addNoteLayout = (LinearLayout) findViewById(R.id.llAddNote); int index = ((ViewGroup) addNoteLayout.getParent()).indexOfChild(addNoteLayout); 
like image 97
Romain Guy Avatar answered Oct 14 '22 21:10

Romain Guy