I have a LinearLayout
, which contains several child TextViews
. How can I get child views of that LinerLayout using a loop?
Get all child views inside LinearLayout at once
Asked Answered
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.
// …
}
Hi Yashwanth Kumar, can i get all TextViews in that Linearlayout? –
Warranty
@hai-nguyen: You can use if (v instanceof TextView) {...} for that. –
Mickiemickle
what if one of the child is a ViewGroup and we want to get all those childs as well? –
Allantoid
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. –
Usanis ((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);
}
}
}
Just to clarify, doing it recursively is because
getChildAt
returns only direct children –
Remainderman Wouldn't it cause a stackoverflow error if we call "too" recursively? –
Quitt
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.
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()));
}
}
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;
}
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)
}
© 2022 - 2024 — McMap. All rights reserved.