Oreo
Oreo

Reputation: 2594

How do I align Radio Buttons center in LinearLayout

How do I center align Radio button inside LinearLayout

For the TextViews:

<LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1.4">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:padding="2dp"
                android:textColor="#000000"
                android:text="Often"
                />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:textColor="#000000"
                android:paddingRight="5dp"
                android:paddingLeft="2dp"
                android:text="Sometimes"
                />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:textColor="#000000"
                android:padding="2dp"
                android:text="Rarely"
                />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:textColor="#000000"
                android:text="Never"
                />

        </LinearLayout>

For the RadioButtons:

<LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1.4">

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:padding="2dp"
                />

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:paddingRight="5dp"
                android:paddingLeft="2dp"
                android:layout_gravity="center"
                />

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:padding="2dp"
                />

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                />

        </LinearLayout>

This is still it looks like :

enter image description here

May I know where i am doing mistake, I already tried with layout_gravity = "center" and gravity = "center"

How can i align it to Center, where i have to make change ?

Upvotes: 1

Views: 3542

Answers (5)

codein
codein

Reputation: 11

Use the following line in the radio button's block which you want to align at the center:

android:layout_centerInParent="true"

Upvotes: 0

Ashish Vora
Ashish Vora

Reputation: 571

Try with this one. It will work for no of buttons you want. Surely its works:

  <HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Often"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Sometimes"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Rarely"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Never"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Often"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Sometimes"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Rarely"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp"
                android:text="Never"
                android:textColor="#000000" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="2dp" />
        </LinearLayout>
    </LinearLayout>
</HorizontalScrollView>

Upvotes: 0

Shvet
Shvet

Reputation: 1201

Check this It will make both checkbox and textview in center.

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:text="Often"
        android:textColor="#000000"
        />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:text="Often"
        android:textColor="#000000"
        />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:text="Often"
        android:textColor="#000000"
        />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:text="Often"
        android:textColor="#000000"
        />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        />
</LinearLayout>

PIC

Upvotes: 0

Nikos Mitropoulos
Nikos Mitropoulos

Reputation: 163

If I understand correctly what you want to do..i suggest you the below layout..

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

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

</LinearLayout>

Upvotes: 1

Suman Dey
Suman Dey

Reputation: 66

Try with this one:

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingLeft="2dp"
        android:paddingRight="5dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

Upvotes: 1

Related Questions