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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:24:05+00:00 2026-06-01T20:24:05+00:00

I want to add buttons programmatically on the screen and I am getting the

  • 0

I want to add buttons programmatically on the screen and I am getting the value by parsing an API and now I want to display the buttons according to the length of an array. I am doing this but I am only getting the last button displayed, but inside the for loop I’m getting all values correct but displaying only the last button. This is my code:

RelativeLayout relate;

//...
relate = (RelativeLayout)findViewById(R.id.relative);

protected void onPostExecute(Void result) {
    if(dialog.isShowing() == true) {
        dialog.dismiss();
    }

    //int width = 100, height =50, x = 10, y = 20;

    for (int i =0;i<adapt_obj.city_name_array.length;i++){
        b1 = new Button(myref);

        b1.setText(adapt_obj.city_name_array[i]);

        relate.addView(b1);

        //relate.addView(b1, i,  new RelativeLayout.LayoutParams(width,height));

        //height = height+80;
    }

    listlocation.setAdapter(adapt_obj);
    adapt_obj.notifyDataSetChanged();
}
  • 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-01T20:24:05+00:00Added an answer on June 1, 2026 at 8:24 pm

    A RelativeLayout will stack the views you add to it at the top-let corner if you don’t specify some placement rules. Your buttons are added to the layout but they are placed one on top of each other and so the only visible is the last one you add. Here are some modification of your for loop:

    RelativeLayout relate; relate = (RelativeLayout)findViewById(R.id.relative);
    for (int i = 0; i < adapt_obj.city_name_array.length; i++){
        Button b1 = new Button(myref);
        b1.setId(100 + i);
        b1.setText(adapt_obj.city_name_array[i]);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (i > 0) {
            lp.addRule(RelativeLayout.BELOW, b1.getId() - 1);
        }   
        b1.setLayoutParams(lp);
        relate.addView(b1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add haptic feedback to my application's buttons and control them programmatically
i want to add buttons on to my tab bar programmatically in my view...
I want to add a subview programmatically to my view, FirstViewController.xib . For now
I have been developing an Android RSS news reader.I want to add two buttons
I have a loop displaying records, and I want to add submit buttons to
I tried to add buttons, textboxes and text to tableview. I want the row
I am using C#.net I want to add custom edit/delete buttons to my GridView1
I want to programmatically add multiple UIButtons to a view - the number of
I have an HBox with width=500. I actually want to add two arrows buttons
I'm developing images in view with button programmatically in iphone. now I want to

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.