I have a LinearLayout
, which contains several child TextViews
. How can I get child views of that LinerLayout using a loop?
Asked
Active
Viewed 1.2e+01k times
151
6 Answers
310
Use getChildCount()
and getChildAt(int index)
.
Example:
LinearLayout ll = …
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
// Do something with v.
// …
}

Benny
- 2,233
- 1
- 22
- 27

Yashwanth Kumar
- 28,931
- 15
- 65
- 69
-
Hi Yashwanth Kumar, can i get all TextViews in that Linearlayout? – Hai nguyen Dec 18 '13 at 09:22
-
15@hai-nguyen: You can use if (v instanceof TextView) {...} for that. – Anoop Apr 04 '14 at 10:54
-
3what if one of the child is a ViewGroup and we want to get all those childs as well? – Hendra Anggrian Jul 25 '16 at 00:49
-
How can i get total no of `buttons` added in linear layout and devide it by *2* ? My purpose is to show 2 rows of buttons using *linear layout*. – Jay Rathod Dec 01 '16 at 08:39
49
((ViewGroup) findViewById(android.R.id.content));// you can use this in an Activity to get your layout root view, then pass it to findAllEdittexts() method below.
Here I am iterating only EdiTexts, if you want all Views you can replace EditText with View.
SparseArray<EditText> array = new SparseArray<EditText>();
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof EditText) {
EditText edittext = (EditText) view;
array.put(editText.getId(), editText);
}
}
}
-
1Just to clarify, doing it recursively is because `getChildAt` returns only direct children – YoussefDir May 10 '20 at 06:42
-
Wouldn't it cause a stackoverflow error if we call "too" recursively? – FEBRYAN ASA PERDANA Aug 02 '21 at 21:30
10
It is easier with Kotlin using for-in loop:
for (childView in ll.children) {
//childView is a child of ll
}
Here ll
is id
of LinearLayout
defined in layout XML.

Malwinder Singh
- 6,644
- 14
- 65
- 103
8
use this
final int childCount = mainL.getChildCount();
for (int i = 0; i < childCount; i++) {
View element = mainL.getChildAt(i);
// EditText
if (element instanceof EditText) {
EditText editText = (EditText)element;
System.out.println("ELEMENTS EditText getId=>"+editText.getId()+ " getTag=>"+element.getTag()+
" getText=>"+editText.getText());
}
// CheckBox
if (element instanceof CheckBox) {
CheckBox checkBox = (CheckBox)element;
System.out.println("ELEMENTS CheckBox getId=>"+checkBox.getId()+ " getTag=>"+checkBox.getTag()+
" getText=>"+checkBox.getText()+" isChecked=>"+checkBox.isChecked());
}
// DatePicker
if (element instanceof DatePicker) {
DatePicker datePicker = (DatePicker)element;
System.out.println("ELEMENTS DatePicker getId=>"+datePicker.getId()+ " getTag=>"+datePicker.getTag()+
" getDayOfMonth=>"+datePicker.getDayOfMonth());
}
// Spinner
if (element instanceof Spinner) {
Spinner spinner = (Spinner)element;
System.out.println("ELEMENTS Spinner getId=>"+spinner.getId()+ " getTag=>"+spinner.getTag()+
" getSelectedItemId=>"+spinner.getSelectedItemId()+
" getSelectedItemPosition=>"+spinner.getSelectedItemPosition()+
" getTag(key)=>"+spinner.getTag(spinner.getSelectedItemPosition()));
}
}

Роман Зыков
- 574
- 4
- 7
1
Get all views from any type of layout
public List<View> getAllViews(ViewGroup layout){
List<View> views = new ArrayList<>();
for(int i =0; i< layout.getChildCount(); i++){
views.add(layout.getChildAt(i));
}
return views;
}
Get all TextView from any type of layout
public List<TextView> getAllTextViews(ViewGroup layout){
List<TextView> views = new ArrayList<>();
for(int i =0; i< layout.getChildCount(); i++){
View v =layout.getChildAt(i);
if(v instanceof TextView){
views.add((TextView)v);
}
}
return views;
}

NJY404
- 349
- 3
- 14
-1
Get all views of a view plus its children recursively in Kotlin:
private fun View.getAllViews(): List<View> {
if (this !is ViewGroup || childCount == 0) return listOf(this)
return children
.toList()
.flatMap { it.getAllViews() }
.plus(this as View)
}

Stanislav Kinzl
- 370
- 4
- 6