0

I am trying to learn about Animation and am using following layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:padding="16dp">

        <ImageButton
            android:id="@+id/thumb_button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher"
            android:scaleType="centerCrop"
            android:layout_gravity="center"
            android:background="@null"
            android:contentDescription="@string/description_image_1" />

    </LinearLayout>
</FrameLayout>

However, this will not center the ImageButton vertically. It does it only horizontally. I tried layout_gravity "center", "center_vertical", "center_horizontal" without success. The best I got is this: enter image description here

but I need to get this:

enter image description here

Much appreciated

pixel
  • 9,653
  • 16
  • 82
  • 149

2 Answers2

1

Try android:gravity="center" to your LinearLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:padding="16dp"
       android:gravity="center">

        <ImageButton
            android:id="@+id/thumb_button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher"
            android:scaleType="centerCrop"
            android:layout_gravity="center"
            android:background="@null"
            android:contentDescription="@string/description_image_1" />

    </LinearLayout>

difference between gravity and layout_gravity

Community
  • 1
  • 1
Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
0
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:padding="16dp">

    <ImageButton
        android:id="@+id/thumb_button_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:src="@mipmap/ic_launcher"
        android:scaleType="centerCrop"
        android:layout_gravity="center"
        android:background="@null"
        android:contentDescription="@string/description_image_1" />

</LinearLayout>

There is problem in wrap_content in layout height and width in your imagebutton widget

Sathish Kumar
  • 919
  • 1
  • 13
  • 27