Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Center ImageButton in LinearLayout

Tags:

android

How can I center ImageButton in the this LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main"
>

ImageButton

              <ImageButton android:id="@+id/btnBut"
                      android:background="@drawable/button"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />

I don't want to use fixed sizes.

Thanks

like image 639
Jovan Avatar asked Sep 27 '10 17:09

Jovan


People also ask

How to set LinearLayout in android studio?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

Which of the following layout and line of code will align a button at the center in an activity?

If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.


1 Answers

Use android:layout_gravity attribute:

 <ImageButton android:id="@+id/btnBut"
    android:background="@drawable/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />
like image 106
Konstantin Burov Avatar answered Sep 29 '22 08:09

Konstantin Burov