Andrew
Andrew

Reputation: 1067

WebView in RelativeLayout doesn't center

I have a layout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <WebView
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

When looking the the Android Studio layout designer, the WebView is centred as expected. When pushed onto a device or run in the emulator, the WebView is left justified, ignoring the centerHorizontal tag. Below is a screenshot with show view bounds enabled.

Example output with show view bounds enabled

How can I get the WebView to centre?

Upvotes: 0

Views: 435

Answers (3)

Vanilla Boy
Vanilla Boy

Reputation: 258

if you want to make a view centre aligned, You can use android:centre on the parent view.It will work. So,the code will change as

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="centre">
    <WebView
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

i hope this will help you :)

Upvotes: 0

A. K. M. Tariqul Islam
A. K. M. Tariqul Islam

Reputation: 2834

Try this

<WebView
    android:layout_width="400dp"
    android:layout_height="match_parent"
    android:layout_centerInParent="true" />

Upvotes: 0

Ahmad Alkhatib
Ahmad Alkhatib

Reputation: 1226

Make sure the parent was set as match_parent like this .

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

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

    <WebView
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" />
       </RelativeLayout>

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

Upvotes: 1

Related Questions