Android: layout_centerInParent not working

I have an image that I want to place in the center of the screen. Despite my expectations, it is centered horizontally, but not vertically. That is, the image touches the top of the screen.

The same layout shows OK in the sandbox project, but, unfortunately, not in real.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- ... --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/progress2" android:src="@drawable/wait2" android:layout_centerInParent="true" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:visibility="visible" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 

Lines

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

Of course, they are not needed, but in any case it does not work.

Any suggestions are welcome.

EDIT: It looks like the view is shown using InputMethodService .

+4
source share
1 answer

I had the same problem. I used

 android:layout_centerInParent 

and it worked on a preview of AndroidStudio and on a Nexus 4 device with 4.3. But on Galaxy S1 with 2.3 this did not work.

I fixed it by wrapping a layout that I would like to center in RelativeLayout, and a center that with a gravity attribute:

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <RelativeLayout ... 

This worked on both devices.

+3
source

All Articles