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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:45:44+00:00 2026-06-07T15:45:44+00:00

for(int i=0;i<n;i++) { Button btn = new Button(this); btn.setLayoutParams(new LinearLayout.LayoutParams(150, 45)); btn.setId(i); linearLayout.addView(btn); try

  • 0
for(int i=0;i<n;i++)
        {
        Button btn = new Button(this); 
        btn.setLayoutParams(new LinearLayout.LayoutParams(150, 45));
        btn.setId(i);
        linearLayout.addView(btn); 
        try {
           btn.setOnClickListener(new OnClickListener() {

                public void onClick(View v){
                    Intent userTry = new Intent(Ed.this,Op.class);
                    startActivity(userTry);
                }
            });
        } catch (ActivityNotFoundException anfe) {
            Log.e("onCreate", "Activity Not Found", anfe);
        }

Android : I want to dynamically create button such that it passes it id to the next activity it goes to. But i am getting an error. Thanks for the help

  • 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-07T15:45:46+00:00Added an answer on June 7, 2026 at 3:45 pm

    You’ll have to pass the Bundle with the id:

    Intent userTry = new Intent(Ed.this,Op.class);
    
    Bundle extras = new Bundle();
    extras.putInt("EXTRA_PARAM1", yourParam);
    userTry.putExtras(extras);
    
    startActivity(userTry);
    

    Also, you are setting the id in a wrong way. The method setId (int) is expecting an id which is registered in your resources (e.g, R.id.itemId)

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

Sidebar

Related Questions

I'm creating buttons dynamically ... for(int i=0; i<colSize;i++){ final Button btn = new Button(this);
public View getView(int position, View convertView, ViewGroup parent) { Button btn; if (convertView ==
I have something like the code below: for(int i=0;i<10;i++){ button=new JButton(buttons[i]); button.addActionListener(new ActionListener(){ public
I need to have only one button in showConfirmDialog . I tried this: int
for ( int i = 0; i < responses.size(); i++ ) { Button btn
public void onClick_addContact(View v) { LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear); layout.addView(linearlayout(_intMyLineCount)); _intMyLineCount++; } private
This is my code to add a new linear layout: public void onClick_addContact(View v)
int* p_bob = new int; *p_bob = 78; The above code makes sense to
int a[2]; This in memory actually looks like: //Assuming int is 2 bytes add=2000,
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Log.d(LOG_TAG,

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.