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.
source share