Home » Android » android linear layout place two items next to each other

android linear layout place two items next to each other

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have the following layout:

<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"
android:orientation="vertical"
tools:context=".MainActivity" >

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Status:"
    android:layout_gravity="left"
    android:textAppearance="?android:attr/textAppearanceMedium" />
  <ImageView
                android:id="@+id/txcpluss_imageView_refresh"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="right"
                android:clickable="true"
                android:contentDescription="icon"
                android:gravity="right"
                android:src="@drawable/refresh_big" />
  </LinearLayout>
 </LinearLayout>

But my refresh icon is not in the top right hand corner where I want it. It sits right next to the text view.

How can I get the text view to be on the left and the refresh icon on the right with white space between them?

How to&Answers:

Use the Following code

<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"
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=".MainActivity" >

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:text="Status:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/txcpluss_imageView_refresh"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:clickable="true"
        android:contentDescription="icon"
        android:gravity="right"
        android:src="@drawable/refresh_big" />
</LinearLayout>

I added weight for the text view.

Answer:

same idea but it makes more sense to split weights evenly

   <LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button_capture"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_weight="0.5"
        android:background="?android:attr/selectableItemBackground"
        android:onClick="snapIt"
        android:text="@string/Capture" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="?android:attr/selectableItemBackground"
        android:onClick="snapIt"
        android:text="@string/Capture" />
   </LinearLayout>