0

I'm making an app exclusively for android tablets and part of it has buttons dynamically created based on various news headlines. I have a single dimens.xml and I use the scaling factor from DisplayMetrics to make the sizes appropriate for different densities. The problem is that when I change the size in my xml it has absolutely no effect on the dimension that the method returns.

RelativeLayout.LayoutParams announcementButtonLayout = new   RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
announcementButtonLayout.topMargin = topMargin;
announcementButtonLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
announcementButtonLayout.height =     this.getResources().getDimensionPixelSize(R.dimen.news_button_height);
announcementButtonLayout.width = getResources().getDimensionPixelSize(R.dimen.dashboard_right_width);

Button button = new Button (this.getActivity());
button.setWidth(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width));
button.setBackgroundResource(R.drawable.photo_edited);
button.setTextColor(getResources().getColor(R.drawable.news_poll_item_text_color));
float f =Utilities.getPixelScaleMultiplier();
f *= getResources().getDimension(R.dimen.news_item_text);
button.setTextSize(f);
button.setText(announcement.getDescription());
button.setMaxLines(1);
button.setTag(announcement);
button.setOnClickListener(this);            
button.setLayoutParams(announcementButtonLayout);
button.setId(index);

Based on my button code above, what could be causing this breach of trust?

Thanks,

Adurnari

EDIT: Included my LayoutParams as reference

Adurnari
  • 9
  • 5

3 Answers3

1

Found the solution:

"Cleaning" was taking the values that I had changed in my xml and adding them on a later portion of the page where they were located previously. The lower ones were dictating the size.

Adurnari
  • 9
  • 5
0

Use LayoutParams to set view width and height, then add it to viewgroup.

Button button = new Button (this.getActivity());
//button.setWidth(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width));
button.setBackgroundResource(R.drawable.photo_edited);
button.setTextColor(getResources().getColor(R.drawable.news_poll_item_text_color));
float f =Utilities.getPixelScaleMultiplier();
f *= getResources().getDimension(R.dimen.news_item_text);
button.setTextSize(f);
button.setText(announcement.getDescription());
button.setMaxLines(1);
button.setTag(announcement);
button.setOnClickListener(this);    
LayoutParams announcementButtonLayout = new LayoutParams(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width), LayoutParams.WRAP_CONTENT);        
button.setLayoutParams(announcementButtonLayout);
button.setId(index);

Clean the project after changing dimens.xml value, maybe the R file is not getting updated in your case.

himanshurb
  • 1,115
  • 1
  • 12
  • 22
  • Sorry hbansal, cleaning didn't work, but what is "params" supposed to do? Should I use those parameters for my announcementButtonLayout? – Adurnari Jan 09 '14 at 18:32
  • LayoutParams define the view bounds (height, width, margin etc), during layout and measure pass. Edited my answer – himanshurb Jan 09 '14 at 19:13
  • Well I meant the second one you defined. All that stuff is already set above. Would defining the width as a parameter instead of through the field really change it? – Adurnari Jan 09 '14 at 19:44
0

Try to work with button.setBounds

Mansouritta
  • 166
  • 1
  • 1
  • 10