Reputation: 831
I'm struggling with TextViews
. I want them next to each other but in the opposite screen sides. First should be on the left of the screen and the second on the right. This is my code:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/unit"
android:textSize="22sp"
android:text="Unit"/>
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/unitName"
android:text="Km"
android:textColor="@android:color/darker_gray"
android:textSize="22sp"/>
</LinearLayout>
I tried layout_gravity
and gravity
and it doesn't work. I was experimenting with wrap_content
and match_parent
but still my TextViews
are just next to each other. I want them in opposite screen sides. What should I do?
Upvotes: 0
Views: 45
Reputation: 69734
try this use android:layout_weight=""
and android:gravity=""
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="left"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="left"
android:layout_weight="1"/>
</LinearLayout>
Upvotes: 0
Reputation: 8237
Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/unit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="Unit"
android:textSize="22sp"/>
<TextView
android:id="@+id/unitName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right"
android:text="Km"
android:textColor="@android:color/darker_gray"
android:textSize="22sp"/>
</LinearLayout>
Upvotes: 0
Reputation: 6899
I have made some changes in your layout please take a look
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/unit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".50"
android:text="Unit"
android:textSize="22sp" />
<TextView
android:id="@+id/unitName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".50"
android:gravity="right"
android:text="Km"
android:textColor="@android:color/darker_gray"
android:textSize="22sp" />
</LinearLayout>
Upvotes: 0
Reputation: 4141
Change your width to Wrap_content and Linear layout to RelativeLayout and then set alignParent attributes
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/unit"
android:layout_alignParentLeft="true"
android:textSize="22sp"
android:text="Unit"/>
<TextView
android:id="@+id/unitName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:layout_alignParentRight="true"
android:text="Km"
android:textColor="@android:color/darker_gray"
android:textSize="22sp" />
</RelativeLayout>
Upvotes: 1