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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:25:59+00:00 2026-05-28T05:25:59+00:00

I’m creating a button dynamically. The number of button is depend on the size

  • 0

I’m creating a button dynamically. The number of button is depend on the size of arraylist. the problem is, after creating the button I will add to the layout using addview method. The problem is I’m using linear layout, as by default orientation for linear layout is horizontal, so the button will fill the layout horizontally. Because of that some of the button is not visible. What I’m trying to achieve is something look like this

multiple button in android messaging contact

My code is like below:

Button[] tv = new Button[arraylist.size()];
for(int i=0;i<arraylist.size();i++){                                
    tv[i] = new Button(getApplicationContext());
    tv[i].setText(arraylist.get(i).toString());
    tv[i].setTextColor(Color.parseColor("#000000"));
    tv[i].setTextSize(20);
    tv[i].setPadding(15, 5, 15, 5);     
    linearlayout.addView(tv[i]);    
}

If I set the orientation of linear layout to vertical the button will fill vertically. So if there any solution to create the button dynamically and fill the layout both horizontal and vertical as shown by image.

  • 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-28T05:26:00+00:00Added an answer on May 28, 2026 at 5:26 am

    After 2 days struggling thinking bout this problem finally I’ve found the solution. I’ve try put all my contact list, store it in arraylist and create button for each element and I’m quite satisfy with the result after display on the screen. Here is how I do the trick. I really appreciate for any comment from others.

    variable declaration;

    int currWidth;
    int currCounter;
    boolean isNewLine;
    LinkedList<HashMap<String,Object>> button;
    ArrayList<String> nameNumber = new ArrayList<String>();
    contactWrapper = (LinearLayout) findViewById(R.id.multiple_selection);
    

    create button onClick event;

    for(int i=0;i<nameNumber.size();i++){                               
                tv[i] = new Button(getApplicationContext());                            
                String[] namePhone = nameNumber.get(i).toString().split("@@");
                phoneNumber.add(namePhone[1]);
                tv[i].setText(namePhone[0]);
                tv[i].setTag(namePhone[1]);
                tv[i].setTextColor(Color.parseColor("#000000"));                                
                tv[i].setTextSize(20);
                tv[i].setPadding(15, 5, 15, 5);                                 
                tv[i].measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                HashMap<String, Object> map = new HashMap<String,Object>();
                map.put("button", tv[i]);
                map.put("width", tv[i].getMeasuredWidth());                             
                button.add(map);
            }
            drawLayout();
    

    drawlayout method is where I add button and arrange accordingly to fit the layout;

    public void drawLayout(){
            int counter=0;
            contactWrapper.setOrientation(LinearLayout.VERTICAL);
            currCounter=0;
            currWidth=0;
            isNewLine=false;
            LinearLayout[] row = new LinearLayout[nameNumber.size()];
            row[currCounter] = new LinearLayout(getApplicationContext());
            @SuppressWarnings("rawtypes")       
            Iterator it = button.iterator();
            for(int i = 0; i<button.size(); i++){   
                it.next();  
                row[currCounter].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                currWidth += Integer.parseInt(button.get(i).get("width").toString());
                if(isNewLine){              
                    if(currWidth < contactWrapper.getWidth()){  
                        row[currCounter].addView((View) button.get(i).get("button"));
                        if(!it.hasNext()){                      
                            contactWrapper.addView(row[currCounter]);
                        }else{                      
                            if(contactWrapper.getWidth()<(currWidth+Integer.parseInt(button.get(i+1).get("width").toString()))){
                                isNewLine=true;
                                contactWrapper.addView(row[currCounter]);
                                currCounter+=1; 
                                row[currCounter] = new LinearLayout(getApplicationContext());
                                currWidth=0;
                            }else{
                                isNewLine=false;
                            }
                        }
                    }else{
                        isNewLine=true;
                        contactWrapper.addView(row[currCounter]);
                        currCounter+=1; 
                        row[currCounter] = new LinearLayout(getApplicationContext());
                        currWidth=0;
                    }                                               
                }else{
                    if(currWidth < contactWrapper.getWidth()){                                      
                        if(!it.hasNext()){
                            row[currCounter].addView((View) button.get(i).get("button"));
                            contactWrapper.addView(row[currCounter]);
                        }else{
                            row[currCounter].addView((View) button.get(i).get("button"));
                            if(contactWrapper.getWidth()<(currWidth+Integer.parseInt(button.get(i+1).get("width").toString()))){
                                isNewLine=true;
                                contactWrapper.addView(row[currCounter]);
                                currCounter+=1; 
                                row[currCounter] = new LinearLayout(getApplicationContext());
                                currWidth=0;
                            }else{
                                isNewLine=false;
                            }
                        }
                    }else{
                        isNewLine=true;
                        contactWrapper.addView(row[currCounter]);
                        currCounter+=1; 
                        row[currCounter] = new LinearLayout(getApplicationContext());
                        currWidth=0;
                    }
                }           
                counter++;
            }            
        }
    

    this code quite messy + I’m not fully utilize the size of array for

    LinearLayout[] row = new LinearLayout[nameNumber.size()];
    

    but it work for me.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.