030
030

Reputation: 11679

How to tab in Android?

The aim is to tab the options to the right. The option android:gravity="right" is not able to accomplish this.

Current Outcome:

enter image description here

Expected Outcome:

enter image description here

Code

C:\workspace-adt\Helloworld\res\layout\activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.helloworld.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:text="@string/fractions"
        android:textSize="30sp" />

    <RadioGroup
        android:id="@+id/fractions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/fraction_true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingRight="15dip"
            android:text="@string/fraction_true"
            android:textSize="30sp"
            android:gravity="right"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/fraction_false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="15dip"
            android:text="@string/fraction_false"
            android:textSize="30sp" />
    </RadioGroup>

</LinearLayout>

C:\workspace-adt\Helloworld\res\values\strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Helloworld</string>
    <string name="fractions"><b>Fractions</b></string>
    <string name="action_settings">Settings</string>
    <string name="fraction_true">True</string>
    <string name="fraction_false">False</string>

</resources>

Upvotes: 0

Views: 48

Answers (2)

MobileMon
MobileMon

Reputation: 8651

Use a nested ViewGroup:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.helloworld.MainActivity" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:text="@string/fractions"
    android:textSize="30sp" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioGroup
    android:alignParentRight = "true"
    android:id="@+id/fractions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <RadioButton
        android:id="@+id/fraction_true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:paddingRight="15dip"
        android:text="@string/fraction_true"
        android:textSize="30sp"
        android:gravity="right"
        android:textStyle="bold" />

    <RadioButton
        android:id="@+id/fraction_false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="15dip"
        android:text="@string/fraction_false"
        android:textSize="30sp" />
</RadioGroup>
</RelativeLayout>
</LinearLayout>

Upvotes: 0

Zeeshan Zulfiqar
Zeeshan Zulfiqar

Reputation: 91

Use weight sum technique for layouts, so that the controls in your each line consumes the assigned percentage of space ( there won't be any need to put them in Grid or other UI Controls)

Upvotes: 1

Related Questions