Center the Image View in in Android Studio

I want to center the image view in the android studio. Here is the code

Image View Code:

enter image description here

Output of the code:

enter image description here

2 answers

  • answered 2021-06-10 11:00 Rahul Singh

    if you are using LinearLayout with android:orientation="vertical"
    then do this

       <ImageView
        android:id="@+id/imageView"
        android:layout_width="348dp"
        android:layout_height="267dp"
        android:layout_gravity="center"
        tools:srcCompat="@tools:sample/avatars" />
    

    full code

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="348dp"
            android:layout_height="267dp"
            android:layout_gravity="center"
            tools:srcCompat="@tools:sample/avatars" />
    </LinearLayout>
    

  • answered 2021-06-10 11:13 ApkChanlee

    If your layout is LinearLayout, then use this

    android:layout_gravity="center"
    

    or RelativeLayout, then use any of this for horizontal, vertical or center in parent layout

    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    

    Can you give image's parent layout with this question, then only we can identify the problem