0

I need to change the size of the buttons programmatically. I read some answers and I do the next to change the button size.

button.setLayoutParams(new RelativeLayout.LayoutParams(150, 50)); 

This code change the size correctly but the problem is that the button not stay in the position I want, it moves the button to the top left corner.

I need to change the button size but I don't want to change de position.

How can I fix this?

2 Answers 2

1

You have to use the existing RelativeLayout Rules as well,i.e whether the item is center aligned,align parent left etc. Or you can make use of the existing Params and modify the width and height as follows the RelativeLayout rules will not be modified.

 RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) button.getLayoutParams(); layoutParams.width=150; layoutParams.height=50; 
Sign up to request clarification or add additional context in comments.

2 Comments

but if i have like 10 buttons, i have to do the same for those ten? There is nothing generic?
Use some other layout as your parent layout...maybe a linearLayout
1
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams(); params.width = 500; params.height = 500; button.setLayoutParams(params); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.