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

  • Home
  • SEARCH
  • 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 9227323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:02:42+00:00 2026-06-18T05:02:42+00:00

what i need is to access the buttons of the clicked item(layout); this code

  • 0

what i need is to access the buttons of the clicked item(layout); this code works but just display all buttons,TextView,…of all layouts not the clicked one.
if you want to run this code you can add other layouts with buttons for the test
thank you for your help
here is my layout:

This is activity_main.xml

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listView_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
       <TextView        
          android:id="@+id/myTextView"    
          android:layout_width="fill_parent"    
         android:layout_height="25dp"    
         android:textSize="23sp" />
    </LinearLayout>

Then my MainActivity:

    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.View.OnClickListener;
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));
        }
    }

And eventually my class MySimpleArrayAdapter:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.troisieme, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.troisieme, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.myTextView);
    //ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // Change the icon for Windows and iPhone
    String s = values[position];


    return rowView;
  }
} 
  • 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-18T05:02:44+00:00Added an answer on June 18, 2026 at 5:02 am

    To get all the Buttons from a single layout:

    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));
        }
    }
    

    from your MainActivity you call

    ArrayList<Button> allButtonsOnCurrentLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
    

    Now you have an ArrayList with all Buttons from the current layout. You can iterate the list and fetch their ID’s with the reflection you already displayed, or show their tags, et cetera.

    Edit: My last attempt to help, extend the code below with your own

    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);
            for(Field field : R.layout.class.getFields()){
                try {
                    int id = field.getInt(null);
                    String name = getResources().getResourceEntryName(id);
                    layoutIds.put(name, id);
                    arrayForArrayAdapter.add(name);
                } 
                catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    
        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));
            }
        }
    }
    

    If this doens’t help you, I do not know what will. Now use allButtonsInLayout to show the Button id’s, or whatever it is you want to do with all the buttons.

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

Sidebar

Related Questions

I need to access topBox's grandchildren and determine whether they're buttons or not. This
I need access to the uint64_t typedef from stdint.h in some wrapper code that
I know that's bad design, but I need access to the view from my
I need to access camera from a remote html page. I load this html
I need to access a label control in a listview when i've clicked a
I have two different modules that need access to a single file (One will
I've got an app running maximized in a borderless window and need access to
I am working on a theme for Opencart and am finding I need access
I need to access a components tag attribute like: <h:inputtext id=input_age/> from a backing
I need to access the commandline from within a C# application. I can't find

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.