Apologize for the confusing wordings.
Here's a LinearLayout:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Note:" />
<EditText
android:hint="Add Note"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
This is the actual output:
And this is the expected output:
How to set the EditText aligned on the right of the LinearLayout while still horizontally aligned with the TextView? And I also want the layout_width of the EditText to be wrap_content, so it doesn't occupy the rest of the LinearLayout
CodePudding user response:
So you just want give space between edittext and textview, if yes, give another linearlayout between edditext and textview like this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Note:" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1"/>
<EditText
android:hint="Add Note"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Let me know if i wrong assume your question
CodePudding user response:
You can add any sort of view in between them with a layout_weight
of 1 and a width of 0dp
, then set the layout_weight
to 0 on the other views so the middle one stretches to fill the remaining space, like 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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Note:"
android:layout_weight="0"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="@android:color/transparent"
/>
<EditText
android:hint="Add Note"
android:gravity="right"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Giving you this layout
Alternately, you can use a ConstraintLayout to avoid having to add the extra view, like this:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Note:"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/editText"
app:layout_constraintBottom_toBottomOf="@id/editText"
app:layout_constraintEnd_toStartOf="@id/editText"/>
<EditText
android:id="@ id/editText"
android:hint="Add Note"
android:gravity="right"
app:layout_constraintStart_toEndOf="@id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is especially helpful if you have a lot of layouts like this on a screen, you can do this with a single ConstraintLayout instead of multiple nested LinearLayouts.
CodePudding user response:
An easier way would be to use ConstraintLayout for this. If you haven't used ConstraintLayout before, do give it a try. Read this to know more -