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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:12:02+00:00 2026-05-31T23:12:02+00:00

I am inflating an xml having button, multiple times and i am able to

  • 0

I am inflating an xml having button, multiple times and i am able to do so perfectly but the problem is when I click the button,i want to show which button is clicked.

   public class InflateExActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    Button b;


    LinearLayout lLayout;
    LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for (int i = 0; i < 3; i++) {

             inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             b = (Button) inflater.inflate(R.layout.buttons, null);
            t = (TextView) inflater.inflate(R.layout.texts, null);


            b.setTag(i); // you'll get 0,1,2 as

            lLayout = (LinearLayout) findViewById(R.id.layout1);
            lLayout.addView(b);

            b.setOnClickListener(this);

        }

    }

    public void onClick(View v) {

        }

}
  • 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-05-31T23:12:03+00:00Added an answer on May 31, 2026 at 11:12 pm

    items you are adding programmatically, you must have to assign ids to them.

    b.setId(1);
    

    EDITED:

    public class DynamicLayoutActivity extends Activity implements OnClickListener{
    private static final int MY_BUTTON = 9000;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
    
        // add button
        Button b = new Button(this);
        b.setText("Button added dynamically!");
        b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,   LayoutParams.WRAP_CONTENT));
        b.setId(MY_BUTTON);
        b.setOnClickListener(this);
        ll.addView(b);
    }
     public void onClick(View v) {
            Toast toast;
            Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId());
            switch (v.getId()) {
            case MY_BUTTON:
                toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400);
                toast.show();    
            }
        }
    

    LATEST:

    public class InflateExActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            LinearLayout lLayout;
            Button b = null;
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            for(int i=0;i<3;i++){
                final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 b = (Button) inflater.inflate(R.layout.buttons, null);
                 b.setId(i);
                lLayout = (LinearLayout) findViewById(R.id.layout1);
                lLayout.addView(b);
                b.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    Toast.makeText(InflateExActivity.this, "Button Clicked :"+v.getId(),
                            Toast.LENGTH_LONG).show();
                }
            });
        }
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am extending a Relative layout in which i am Inflating an xml.I added
I want to inflate a XML-Layout-File in a custom ViewGroup Class, my Problem is
Hiiii there, I have an xml layout and when a button is clicked I
Am getting Error inflating fragment error while running calling an Activity which having fragments.
So I'm having pretty much exactly the problem described here: http://code.google.com/p/android/issues/detail?id=6191 and until the
I am dynamically inflating following xml <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=wrap_content android:layout_height=wrap_content android:layout_margin=10dip
I am trying to set shared_prefs from an xml file but, when simulating, app
I have been attempting to use an android.preference.DialogPreference inflated from XML, but the documentation
I am having a problem with one of my apps and I was wondering
For a widget I use a simple layout which contains a TableLayout <?xml version=1.0

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.