Android Creating button dynamically and fill layout

前端 未结 6 1514
长情又很酷
长情又很酷 2021-01-01 05:25

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

相关标签:
6条回答
  • 2021-01-01 05:45

    There is not a canned layout in the SDK that does exactly what you are aiming for (i.e. lay out as many children horizontally as will fit, then flow to the next line to lay out some more), so you will need to create a custom ViewGroup that accomplishes this purpose. Luckily for you, Romain Guy created one live on-screen during a presentation at Devoxx.

    Here is a link to that presentation video.

    Here is a link to the sample code and slides.

    HTH

    0 讨论(0)
  • 2021-01-01 05:49

    Well, you can try using more sophisticated way. You can create horizontal linear layout, and add buttons to it. Every time you're attempting to add new button, you check if there is place for it, by finding difference between layout's and buttons widths.

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

    I used that trick in my apps.

    0 讨论(0)
  • 2021-01-01 05:51

    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 讨论(0)
  • 2021-01-01 05:51

    Does you set android:layout_width="fill_parent"? Do this if you don't.

    0 讨论(0)
  • 2021-01-01 05:56

    try this its working fine

    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 讨论(0)
  • 2021-01-01 05:57

    use TableLayout instead of LinearLayout this is tutorial hope this will help you to get the idea

    0 讨论(0)
提交回复
热议问题