matghazaryan
matghazaryan

Reputation: 5896

How to put checkbox on the right side of the screen

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/groups_massegesTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:textColor="@color/list_text_color" />

    <CheckBox
        android:id="@+id/groups_massegescheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/checkbox_background"
        android:button="@drawable/checkbox"
        android:focusable="false"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="right"
        />
</LinearLayout>

I am using linearlayout and I want set checkbox in the right of screen. I don't want to use relativelayout. Is there a way do this in linearlayout. Checkbox is always to right of textView.

Upvotes: 0

Views: 4643

Answers (4)

Adiyat Mubarak
Adiyat Mubarak

Reputation: 10529

I have simillar issue and I solved my problem by adding weighSum as 2 on the linearlayout, then I set my textview's layout_weight to 2 and the checkbox's layout_weight to 0

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:orientation="horizontal"
android:weightSum="2">

<TextView
    android:id="@+id/groups_massegesTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="10dp"
    android:textColor="@color/list_text_color"
    android:layout_weight="2" />

<CheckBox
    android:id="@+id/groups_massegescheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/checkbox_background"
    android:button="@drawable/checkbox"
    android:focusable="false"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="5dp"
    android:layout_gravity="right"
    android:layout_weight="0"
    />

Upvotes: 0

Abhinava
Abhinava

Reputation: 1030

First, why don't you want to use a RelativeLayout?

Second, in case you want to do it, then just put layout_width="fill_parent" for the checkbox!

Upvotes: 1

Android
Android

Reputation: 2393

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="check box"    
    />
<CheckBox  
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_gravity="right"
>
</CheckBox>
</LinearLayout>

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67286

Apply android:layout_weight="1" to the TextView.

Upvotes: 5

Related Questions