Android : android layout weight creates gap

on Sunday, April 19, 2015


I am new to android developing. I try to iterate through a Hashmap to create vertical linear layouts which always include one ImageView and one Button. This works fine so far. To allocate the available space equal to all linear layouts i try to use the weight attribute. This also works. But unfortunately it creates a huge gap vertically between the Image and the button. I dont understand why. Any Idea?



Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if(count%3==0){
if(linear!=null){
layout.addView(linear);
}
linear=new LinearLayout(getApplicationContext());
linear.setOrientation(LinearLayout.HORIZONTAL);
linear.setLayoutParams(layoutParams);
}
LinearLayout linear2=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
lp1.setMargins(15, 0, 15, 0);
linear2.setLayoutParams(lp1);
linear2.setOrientation(LinearLayout.VERTICAL);
final String finalstring=(String)pair.getKey();
Button button = new Button(getApplicationContext());
button.setText((String)pair.getKey());
button.setClickable(false);
button.setBackgroundColor(getResources().getColor(R.color.friking_blue));

ImageView image = new ImageView(getApplicationContext());
new DownloadImageTask(image).execute((String)pair.getValue());

linear2.addView(image);
linear2.addView(button);
linear.addView(linear2);
linear2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(getApplicationContext(), Mycontacts.class);
intent.putExtra("category", finalstring);
startActivity(intent);
}
});
count++;
it.remove(); // avoids a ConcurrentModificationException
}


Since I am not allowed to post images i will just post the Link.


Link to image


Thanks for any help!


0 comments:

Post a Comment