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 9220445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:21:00+00:00 2026-06-18T03:21:00+00:00

i’ve got an answer: accessing buttons of every layout about how to display all

  • 0

i’ve got an answer:accessing buttons of every layout
about how to display all the buttons of a specific layout but i don’t understand the code
here is how i try to make it run
the rest of the project is excatly the same as the link above
here is what i changed:

package com.example.adapterlist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    Map<String, Integer> layoutIds = new HashMap<String, Integer>();
    ArrayList<String> arrayForArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView listView = (ListView) findViewById(R.id.mylist);
        java.lang.reflect.Field[] ID_Fields = R.layout.class.getFields();
        String []values=new String[ID_Fields.length];
        int[] resArray = new int[ID_Fields.length];
        for(int i = 0; i < ID_Fields.length; i++){
            try {
                resArray[i] = ID_Fields[i].getInt(null);
                values[i]=getResources().getResourceEntryName(resArray[i]);
                Log.v("resArray[i]  " , getResources().getResourceEntryName(resArray[i]));

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_list_item_1, android.R.id.text1, values);
             listView.setAdapter(adapter);
             listView.setOnItemClickListener(new OnItemClickListener(){
             @Override
             public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    iJustClickedAnItemInTheList((String) listView.getSelectedItem());



                }
             });


    }

    private void iJustClickedAnItemInTheList(String idname) {
        setContentView(layoutIds.get(idname));
        ArrayList<Button> allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
    }

    public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
        ArrayList<T> result = new ArrayList<T>();
        for (View view : getAllViewsFromRoots(root)) 
            if (clazz.isInstance(view)) 
                result.add(clazz.cast(view));
        return result;
    }

    public static ArrayList<View> getAllViewsFromRoots(View...roots) {
        ArrayList<View> result = new ArrayList<View>();
        for (View root : roots)
            getAllViews(result, root);
        return result;
    }

    private static void getAllViews(ArrayList<View> allviews, View parent) {
        allviews.add(parent);
        if (parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup)parent;
            for (int i = 0; i < viewGroup.getChildCount(); i++)
                getAllViews(allviews, viewGroup.getChildAt(i));
        }
      }


    }
  • 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-18T03:21:02+00:00Added an answer on June 18, 2026 at 3:21 am

    How about this? only thing is, you need a layout (/res/layout/listactivity.xml) with a listview whose id is listView.

    public class MainActivity extends Activity {
        ListView lv;
        String[] layoutNames;
        int[] layoutIds;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listactivity);
            lv = (ListView)findViewById(R.id.listView);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            fill();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, layoutNames);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                    iJustClickedAnItemInTheList(arg2);
                }
            });
        }
    
        void fill() {
            java.lang.reflect.Field[] layoutFields = R.layout.class.getFields();
            layoutNames = new String[layoutFields.length];
            layoutIds = new int[layoutFields.length];
            for(int i = 0; i < layoutFields.length; i++){
                try {
                    layoutIds[i] = layoutFields[i].getInt(null);
                    layoutNames[i] = getResources().getResourceEntryName(layoutIds[i]);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    
        private void iJustClickedAnItemInTheList(int position) {
            setContentView(layoutIds[position]);
            ArrayList<Button> allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
            Toast.makeText(this, "" + allButtonsInLayout.size(), Toast.LENGTH_SHORT).show();
        }
    
        public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
            ArrayList<T> result = new ArrayList<T>();
            for (View view : getAllViewsFromRoots(root)) 
                if (clazz.isInstance(view)) 
                    result.add(clazz.cast(view));
            return result;
        }
    
        public static ArrayList<View> getAllViewsFromRoots(View...roots) {
            ArrayList<View> result = new ArrayList<View>();
            for (View root : roots)
                getAllViews(result, root);
            return result;
        }
    
        private static void getAllViews(ArrayList<View> allviews, View parent) {
            allviews.add(parent);
            if (parent instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup)parent;
                for (int i = 0; i < viewGroup.getChildCount(); i++)
                    getAllViews(allviews, viewGroup.getChildAt(i));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.