Reputation: 8841
I need to put a TextView
and EditText
in the same line within constraint layout and keep it align each other! below code generate a picture like below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/serverIpAddr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="192.168.1.11"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/serverPort"
style="@style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:ems="10"
android:inputType="number"
android:text="8080"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toEndOf="@+id/serverIpAddr"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now it looks ugly, the TextView and EditText log align horizontally correctly, TextView a little bit above EditText
Upvotes: 1
Views: 1309
Reputation: 40830
You can use app:layout_constraintBaseline_toBaselineOf
attribute to make the text of both horizontally aligned
<TextView
android:id="@+id/serverIpAddr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="192.168.1.11"
app:layout_constraintBaseline_toBaselineOf="@id/serverPort"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Upvotes: 2