BP233
BP233

Reputation: 85

Android-How to center my custom View in LinearLayout?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">

    <com.example.root.howold.MyRing
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

I also try to set gravity and layout_gravity, but they don't work actually.

How can I make MyRing(my custom View) center in Layout?

Upvotes: 2

Views: 2636

Answers (4)

Kanagalingam
Kanagalingam

Reputation: 2184

If you are using Linear Layout,

Option 1: Add android:gravity to the parent view, to center all its child
views. Example:

<LinearLayout
           android:gravity="center">
           <TextView/>   
           <TextView/>  // Both these textviews will be aligned center
           </LinearLayout>

Option 2: If you want to center only a child view, then add the attribute android:layout_gravity="center" to the childview tag.

<LinearLayout>
           <TextView
            android:layout_gravity="center"  // Only this textview will be aligned center
           />
           <TextView/>
           </LinearLayout>

Upvotes: 0

Pranay Narvankar
Pranay Narvankar

Reputation: 52

Try this:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.root.howold.MyRing
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

Upvotes: 4

Saritha G
Saritha G

Reputation: 2608

If your using root layout as linear layout. You can use below text that will set your custom view to center.

Depends on your requirement you can user

 layout_gravity="center"

(or)

layout_gravity="center_vertical" 

(or)

layout_gravity="center_horizontal"

Below is the xml which has custom view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

 <!-- Here you may some other views -->

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <com.purpletalk.root.sampledemo.MyRing
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="hellooooo" />
   </LinearLayout>
</LinearLayout>

Upvotes: 0

Stanley Ko
Stanley Ko

Reputation: 3497

Read this: Gravity and layout_gravity on Android

To make your view aligned center, you can:

LinearLayout case1:
"android:gravity" is for it's children views. If this doesn't works, check it's parent view's width and height. And, set parent's width and height to bigger size.

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST" />
</LinearLayout>


LinearLayout case2:
"android:layout_gravity" is for it self, if parent view has empty spaces.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="TEST" />
</LinearLayout>


RelativeLayout
You can use "android:layout_centerInParent" for RelativeLayout.

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="TEST" />
</RelativeLayout>

Upvotes: 0

Related Questions