Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:layout_centerInParent does not work

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

The same layout shows ok in a sandbox project, but unfortunately not in the real one.

<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>

The lines

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

are of course unnecessary, but the thing does not work in either case.

Any suggestions are welcome.

EDIT: it looks like it's relevant that the view is shown from an InputMethodService.

like image 232
18446744073709551615 Avatar asked Apr 25 '13 08:04

18446744073709551615


People also ask

How to use Relative Layout in Android?

Positioning ViewsRelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.

What is Layout_above Android?

android:layout_above : Places the element above the specified element. android:layout_below : Places the element below the specified element. android:layout_toLeftOf : Places the element to the left of the specified element. android:layout_toRightOf : Places the element to the right of the specified element.

Where to find Relative Layout in Android studio?

Navigate to the app > res > layout > activity_main. xml and add the below code to that file. Below is the code for the activity_main. xml file.


1 Answers

I just had the same Problem. I used

android:layout_centerInParent

and it worked on the AndroidStudio preview and on an Nexus 4 with 4.3 device. But on Galaxy S1 with 2.3 it did not work.

I fixed it by wrapping the Layout i wanted to center in an RelativeLayout and center that with the gravity attribute:

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

<RelativeLayout
    ...

That worked on both devices.

like image 169
Nico Avatar answered Oct 16 '22 06:10

Nico