Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:25:21+00:00 2026-06-03T17:25:21+00:00

I have a pretty annoying problem here, as Im still trying to internalize each

  • 0

I have a pretty annoying problem here, as Im still trying to internalize each and every bits of what I am doing,

I currently have a LinearLayout, then upon onCreate of the Activity, I will populate or inflate several other LinearLayout’s with Buttons, my problem is that when I try to access the button, it seems that I’m not getting any close or deeper from the LinearLayout, all I can get is the LinearLayout(Parent) and the other LinearLayout(Children), I believe there is a way, Im just totally confused how to do it.

LinearLayout
 ->LinearLayout(Child1)->Button1, Button2, Button3
 ->LinearLayout(Child2)->Button4, Button5, Button6

How would I be able to access and get the Buttons?

My source;

for (int x=0; x<ll.getChildCount(); x++){
  View v = ll.getChildAt(x);
  Class c = v.getClass();
  if(c == LinearLayout.class){
    for(int y=0; y< ; y++){
      **I know there is something that must be done here, likewise, is this the most
      efficient way of doing things?
    }
  }
 Log.i("test", c.getName());
}

Only the LinearLayout(Parent) exist in the XML, others are inflated run-time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:25:23+00:00Added an answer on June 3, 2026 at 5:25 pm

    You should be able to simply cast v to a LinearLayout, then access its children just like you did with its parent. Something like:

    for (int x=0; x<ll.getChildCount(); x++){
      View v = ll.getChildAt(x);
      Class c = v.getClass();
      if(c == LinearLayout.class){
        //Cast to LinearLayout since View doesn't expose a way to access children
        LinearLayout innerLayout = (LinearLayout)v;
        for(int y=0; y<innerLayout.getChildCount() ; y++){
          Button b = (Button)innerLayout.getChildAt(y);
    
          //Do something with b
        }
      }
     Log.i("test", c.getName());
    }
    

    Depending on your exact hierarchy you could possibly simplify this by removing the reflection and simply doing a null check (if needed, wrap it in a try/catch and catch a ClassCastException). I’ve typically done something like this in situations where I need to traverse a layout tree that’s dynamically generated:

    for (int i = 0; i < outerLayout.getChildCount(); ++i)
    {
        try
        {
            LinearLayout innerLayout = (LinearLayout)outerLayout.getChildAt(i);
    
            if (innerLayout != null)
            {
                for (int j = 0; j < innerLayout.getChildCount(); ++j)
                {
                    Button btn = (Button)innerLayout.getChildAt(j);
    
                    //Do something with btn
                }
            }
        }
        catch (ClassCastException cEx)
        {
            Log.w("WARN", "Unexpected child type in outerLayout. " + cEx.getMessage());
        }
    }
    

    This is an untested example (could need better exception handling depending on your requirements and layouts) but hopefully it gives you the general idea. If you want to be a little bit more type-agnostic you could also use a cast to ViewGroup instead. That would allow you to potentially use different kinds of layout containers as children, if needed, as they are subclasses of ViewGroup (which is where they inherit getChildAt() and getChildCount() from).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty annoying problem with the IntelliSense in Visual Studio 2010: Every
I have a pretty annoying compiling problem. I am trying to do a System.loadlibrary
I have a pretty monstrous Java app in development here. As of now, it's
I have a problem with hibernate where it is doing N+1 SELECTS when using
I'm running across a very annoying problem regarding transitive const in D. I have
And this is pretty annoying since we have it set up to continuously check
I've got an annoying problem here. I've recently upgraded to VS 2010. I really
i have my listview activity that works pretty fine but i have an annoying
I have been trying to load a text file into a combobox, and then
I have pretty good skills in PHP , Mysql and Javascript for a junior

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.