I’ve got an imagebutton defined in an xml-file. During runtime, I want to make it visible (possible with setVisibility(), so it was no problem) but I also want to center it horizontally, like the XML attribute android:layout_gravity="center_horizontal". How do you do that during runtime, in javacode, though? I couldn’t find any methods to use to set the gravity of the imagebutton programmatically.
EDIT: am using a RelativeLayout in the xml file
EDIT2: the XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center_horizontal">
<TextView android:text="Unknown" android:layout_width="wrap_content" android:id="@+id/screen_p2pcall_status_text" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_centerHorizontal="true"></TextView>
<ImageView android:layout_width="wrap_content" android:src="@drawable/android_avatar_big" android:id="@+id/screen_p2pcall_android" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_status_icon" android:layout_centerHorizontal="true" android:layout_marginTop="36dp"></ImageView>
<ImageView android:layout_width="wrap_content" android:src="@android:drawable/presence_invisible" android:id="@+id/screen_p2pcall_status_icon" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_status_text" android:layout_centerHorizontal="true"></ImageView>
<TextView android:text="Unknown" android:id="@+id/screen_p2pcall_peer" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_android" android:layout_centerHorizontal="true"></TextView>
<ImageButton android:layout_width="120dp" android:id="@+id/screen_p2pcall_hangup" android:src="@drawable/phone_hang_up_48" android:layout_height="50dp" android:layout_below="@+id/screen_p2pcall_peer" android:layout_toRightOf="@+id/screen_p2pcall_status_icon" android:layout_marginTop="18dp"></ImageButton>
<ImageButton android:layout_width="120dp" android:id="@+id/screen_p2pcall_answer" android:src="@android:drawable/sym_action_call" android:layout_height="50dp" android:layout_alignTop="@+id/screen_p2pcall_hangup" android:layout_toLeftOf="@+id/screen_p2pcall_hangup"></ImageButton>
</RelativeLayout>
The java code, where I want to change to center the @+id/screen_p2pcall_hangup horizontally in runtime:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_p2pcall);
final ImageButton hangup = (ImageButton)findViewById(R.id.screen_p2pcall_hangup);
// ????
}
Thanks
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
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,
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 dont 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
This should work.