Android - center imagebutton in runtime?

When you use Relative Layout you should add rules to the layout params to center child views. To the relative layout params object you should add the the rule like this rlp. AddRule(RelativeLayout.

CENTER_HORIZONTAL,RelativeLayout. TRUE) Other rules can be added according to your requirement. Gravity is not possible on relative layout I assume this is the whole layout xml for the activity.

If so, this should work ImageButton hangupButton = (ImageButton)findViewById(R.id. Screen_p2pcall_hangup); int width = (int) TypedValue. ApplyDimension(TypedValue.

COMPLEX_UNIT_DIP, (float) 120, getResources(). GetDisplayMetrics()); int height = (int) TypedValue. ApplyDimension(TypedValue.

COMPLEX_UNIT_DIP, (float) 50, getResources(). GetDisplayMetrics()); RelativeLayout. LayoutParams rlp = new RelativeLayout.

LayoutParams(width,height); rlp. AddRule(RelativeLayout. CENTER_HORIZONTAL,RelativeLayout.

TRUE); hangupButton. SetLayoutParams(rlp); hangupButton. SetVisibility(RelativeLayout.

VISIBLE) Edit:Solution with Code I've edited you xml code a bit to test in my project. There are attributes used that are not usable, such as 'orientation'. It only applies in a linearlayout.

Next time when you post a question please break your xml into the next line so that we don't need to scroll left and right to read the whole thing layout xml? Xml version="1.0" encoding="utf-8"? > Screen_p2pcall_hangup); int width = (int) TypedValue.

ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 120, getResources(). GetDisplayMetrics()); int height = (int) TypedValue.

ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 50, getResources(). GetDisplayMetrics()); int marginTop = (int) TypedValue.

ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 18, getResources(). GetDisplayMetrics()); RelativeLayout.

LayoutParams rlp = new RelativeLayout. LayoutParams(width,height); rlp. AddRule(RelativeLayout.

CENTER_HORIZONTAL,RelativeLayout. TRUE); rlp. AddRule(RelativeLayout.

BELOW, R.id. Screen_p2pcall_peer); rlp. TopMargin = marginTop; hangupButton.

SetLayoutParams(rlp); ((ImageButton)findViewById(R.id. Screen_p2pcall_answer)) . SetVisibility(RelativeLayout.

INVISIBLE) This should work.

When you use Relative Layout you should add rules to the layout params to center child views. To the relative layout params object you should add the the rule like this rlp. AddRule(RelativeLayout.

CENTER_HORIZONTAL,RelativeLayout. TRUE); Other rules can be added according to your requirement. Gravity is not possible on relative layout.

I assume this is the whole layout xml for the activity. If so, this should work, ImageButton hangupButton = (ImageButton)findViewById(R.id. Screen_p2pcall_hangup); int width = (int) TypedValue.

ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 120, getResources(). GetDisplayMetrics()); int height = (int) TypedValue.

ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 50, getResources(). GetDisplayMetrics()); RelativeLayout.

LayoutParams rlp = new RelativeLayout. LayoutParams(width,height); rlp. AddRule(RelativeLayout.

CENTER_HORIZONTAL,RelativeLayout. TRUE); hangupButton. SetLayoutParams(rlp); hangupButton.

SetVisibility(RelativeLayout. VISIBLE); Edit:Solution with Code I've edited you xml code a bit to test in my project. There are attributes used that are not usable, such as 'orientation'.

It only applies in a linearlayout. Next time when you post a question please break your xml into the next line so that we don't need to scroll left and right to read the whole thing. Layout xml Now I used this code in my onCreate method and it hides the left bottom button and horizontally centers the right bottom button ImageButton hangupButton = (ImageButton)findViewById(R.id.

Screen_p2pcall_hangup); int width = (int) TypedValue. ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 120, getResources().

GetDisplayMetrics()); int height = (int) TypedValue. ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 50, getResources().

GetDisplayMetrics()); int marginTop = (int) TypedValue. ApplyDimension(TypedValue. COMPLEX_UNIT_DIP, (float) 18, getResources().

GetDisplayMetrics()); RelativeLayout. LayoutParams rlp = new RelativeLayout. LayoutParams(width,height); rlp.

AddRule(RelativeLayout. CENTER_HORIZONTAL,RelativeLayout. TRUE); rlp.

AddRule(RelativeLayout. BELOW, R.id. Screen_p2pcall_peer); rlp.

TopMargin = marginTop; hangupButton. SetLayoutParams(rlp); ((ImageButton)findViewById(R.id. Screen_p2pcall_answer)) .

SetVisibility(RelativeLayout. INVISIBLE); This should work.

– KaiserJohaan Oct 3 at 13:28 put up your java and xml code. Lets see what you have got so far. – blessenm Oct 3 at 14:14 I've added it into OP – KaiserJohaan Oct 3 at 14:42 Ive updated my answer.

– blessenm Oct 3 at 14:54 I've tried it but for some reason all objects try to align themselves to the right of the screen. Why is this? – KaiserJohaan Oct 37 at 8:30.

The Holo theme in your app, declare android:theme="@android:style/Theme. Manifest's element. For information about using the Holo theme while supporting older devices.

To customize individual buttons with a different background, specify the android:background attribute with a drawable or color resource. HTML styles to define multiple style properties such as the background, font, size, and others. For more information about applying styles, see Styles and Themes.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions