Find controls in any view in Android

Source: Internet
Author: User

1, in many cases, we may not know the ID of the control, but we would like to find it in the view containing the control, you can use the following practices:

Example: Find all of the button controls in the activity's root view

private void Findbutton (ViewGroup group, list<button> result)
{
if (group! = null)
{
for (int i = 0, j = group.getchildcount (); i < J; i++)
{
View child = Group.getchildat (i);
if (Child instanceof Button)
{
Result.add (Button) child);
} else if (child instanceof ViewGroup)
{
Findbutton ((viewgroup) child, result);
}
}
}
}

Called in the activity:

list<button> result = new arraylist<button> ();
This.findbutton ((ViewGroup) This.getwindow (). Getdecorview (), result);

This method is in fact recursively from the root view to find the entire control tree, finally find the control that meets the requirements, a little rewrite can meet the needs of a variety of search control.

Find controls in any view in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.