Reputation: 21
I am trying to make some text views with checkboxes in LinearLayout. I want for result like this:
TextView Ckeckbox
TextViewwwwww Ckeckbox
TextV Ckeckbox
But currently I have this:
TextView Checkbox
TextViewwwww Checkbox
TextView Checkbox
Here is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</LinearLayout>
And this xml gives me same result:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"/>
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"/>
</LinearLayout>
Upvotes: 2
Views: 7516
Reputation: 247
Try this:
<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:orientation="vertical"
tools:context="com.example.ipdemo.MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 5146
You can use the layout_weight
attribute on your TextView
only to automatically designate the TextView
to fill any space which the CheckBox
does not occupy. For example, the following will set your checkboxes to the end (provided they have no text associated with them) and then lets the TextView fill the rest of the layout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Additionally, you should use match_parent
instead of fill_parent
as fill_parent
is deprecated.
Upvotes: 7
Reputation: 5136
check the following use android:layout_weight="1" in both view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTitle"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/checkItem"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
Upvotes: 0
Reputation: 11487
Use layout_gravity="right"
instead of gravity="end"
if you're working with vertical
layouts.
Your CheckBox
tag should look like:
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
EDIT: On horizontal
layouts, try layout_weight="1"
, that will make both of your elements fill half of the layout space.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Upvotes: 1
Reputation: 10651
You should be using layout_weight attribute to achieve the effect
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
So horizontally both the views will be occupying half of the screen size. You could adjust layout_weight to achieve the desired outcome.
There maybe other ways to do this, but I think this method is the best way.
Upvotes: 0
Reputation: 8073
Use layout weights and try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="5">
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="wdjhwdjwo"/>
<CheckBox
android:id="@+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"/>
</LinearLayout>
Upvotes: 0