Create Android button dynamically and fill the layout

I create a button dynamically. The number of buttons depends on the size of the arraylist. the problem is that after creating the button, I will add to the layout using the addview method. The problem is that I use linear layout, because the default orientation for the linear layout is horizontal, so the button will fill the layout horizontally. Because of this, some buttons are not visible. What I'm trying to achieve looks like this.

multiple button in android messaging contact

My code is as follows:

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 the linear layout vertically, the button will fill vertically. Therefore, if there is any solution to create a button dynamically and fill the layout both horizontal and vertical, as shown in the image.

+4
source share
6 answers

After 2 days, struggling with the thought of this problem, finally, I found a solution. I tried to put my entire contact list, save it in an arraylist and create a button for each item, and I am quite satisfied with the result after being displayed on the screen. This is how I do the trick. I really appreciate any comments 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 an onClick event button;

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

the drawlayout method is where I add the button and arrange 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 is pretty dirty + I don't fully use array size for

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

but it works for me.

+6
source

There is no ready-made layout in the SDK that does exactly what you are aiming for (that is, lay as many children horizontally as you place, then go to the next line to lay out a little more), so you will need to create a custom ViewGroup that will execute this task. Luckily for you, Romain Guy created one on-screen during a presentation at Devoxx.

Here is the link to the video.

Here is a link to sample code and slides .

NTN

+7
source

use TableLayout instead of LinearLayout , this tutorial hope this helps you understand the idea

0
source

Have you set android:layout_width="fill_parent" ? Do this if you do not.

0
source

Well, you can try to use a more complex way. You can create a horizontal linear layout and add buttons to it. Each time you try to add a new button, you check to see if there is room for it by finding the difference between the width of the layout and the width of the buttons.

Each time your horizontal layout is filled, you add it to another vertical layout and create another horizontal layout to save the buttons on the left.

I used this trick in my applications.

0
source

try this operating mode

this.row = (LinearLayout) findViewById (R.id.tags); this.row.setOrientation (LinearLayout.VERTICAL); LinearLayout one = new LinearLayout (this);

  //get the size of the screen Display display = getWindowManager().getDefaultDisplay(); this.screenWidth = display.getWidth(); // deprecated this.screenHeight = display.getHeight();// depreceted for(int i=0; i<6; i++) { one.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); this.button = new Button(this); button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); if(i==0) { this.button.setText("Muhammad Aamir"); } else if(i==1) { this.button.setText("Ahsan"); } else if(i==2) { this.button.setText("Mujahid"); } else if(i==3) { this.button.setText("Waqas"); } else if(i==4) { this.button.setText("Ali"); } else { this.button.setText("Ahmer"); } //get the size of the button text Paint mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextSize(button.getTextSize()); mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.NORMAL)); float size = mPaint.measureText(button.getText().toString(), 0, button.getText().toString().length()); size = size+14; this.totalTextWidth += size; if(totalTextWidth < screenWidth) { one.addView(button); } else { this.row.addView(one); one = new LinearLayout(this); one.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); one.addView(button); this.totalTextWidth = size; } } this.row.addView(one); } 
0
source

Source: https://habr.com/ru/post/1391090/


All Articles