Aligning textviews on the left and right edges in Android layout
Use a RelativeLayout
with layout_alignParentLeft
and layout_alignParentRight
:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/mytextview1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/mytextview2"/>
</RelativeLayout>
Also, you should probably be using dip
(or dp
) rather than sp
in your layout. sp
reflect text settings as well as screen density so they're usually only for sizing text items.
Align textviews in left and right side of a linear layout?
Try to change your layout like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="194dp"
android:layout_marginBottom="8dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@+id/textAppearanceHeadline6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@drawable/faye" />
<TextView
android:id="@+id/textAppearanceHeadline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Faye"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<LinearLayout
android:id="@+id/textAppearanceBody1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/textAppearanceHeadline6"
app:layout_constraintTop_toBottomOf="@+id/textAppearanceHeadline6">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|start"
android:text="Age : 7"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobbies: Sunbathing"
android:gravity="center"
android:textColor="#5A5656" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
How to aligning textviews on the left and right side of a linearlayout
Please check the parent layout and try to set orientation to horizontal. I tried different cases but found that its working in all cases...
How to align right a TextView in RelativeLayout which layout_width is wrap_content and it's parent layout has wrap_content to
At the end I obtained a solution using TableLayout with two rows.
The first row contain the message and the second row contain the hour.
The message is aligned left and the hour right.
Below is the code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/contentWithBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/risponditore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10sp"
android:textStyle="bold"
android:text=""
android:visibility="gone"
android:textColor="@color/bluScuro"/>
<TableLayout
android:id="@+id/tabellaMessaggioOra"
android:layout_below="@+id/risponditore"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/rigaMessaggio"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:maxWidth="245dp"
android:textSize="12sp"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:id="@+id/rigaOra"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/grigioScuro"
android:layout_gravity="right"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
How to adjust text alignment to both corners of a textview
You can add one relative layout with horizontal orientation and add two text views inside it and then configure their orientation.
For example;
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_height="wrap_content"
android:text="Name:"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:text="Tiwari Ji"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
Aligning one TextView to start of parent, other one to the end of parent should give you the desired result.
You can see what the above code renders on screen:
Properly Aligned text
The same feat can also be achieved using LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_margin="16dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name:" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="right"
android:text="Tiwari Ji"
android:layout_height="wrap_content" />
</LinearLayout>
Here I've given equal weight to the TextViews inside the LinearLayout, by giving them android:layout_weight="1"
and android:layout_width="0dp"
.
And then by giving android:gravity="right"
ensured that the text inside the layout is aligned towards the end of the right-most edge of the view.
Happy to help, you can ask if you have any queries.
Position items left and center in Android Toolbar?
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="Text Here"
android:textSize="18sp"
android:textStyle="bold"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
Two textviews one on left and one on the right side
Try to wrap those TextViews in a LinearLayout.
Like this:
<ImageView
android:id="@+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:text=""
android:textSize="14sp"
android:textStyle="bold" />
<View
android:layout_width="wrap_content"
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/ColPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/ColWeight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Aligning to left of right in linearlayout
Use this..
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="@+id/txtType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:text="Type of Event: "
style="@style/black_text_bold"
android:visibility="gone"/>
<TextView android:id="@+id/outputType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Loading..."
android:layout_weight="1"
style="@style/black_text"
android:visibility="gone"/>
</LinearLayout>
Related Topics
How to Control Navigationview Margin Between Menu Items
How to Make a Link Open an Instagram Page in an App Installed on Android
Touchableopacity Not Working Inside an Absolute Positioned View
Possible Unhandled Promise Rejection (Id:0) Error: Network Error in React Native
How to Show a Video Thumbnail from a Video Path
React-Native: Module Appregistry Is Not a Registered Callable Module
Retrieve Stored Image from Firebase Storage
Force Video to Open in Youtube App on Android
Background Image Jumps When Address Bar Hides Ios/Android/Mobile Chrome
Allow User to Select Camera or Gallery For Image
Cart Item Count Increment/Decrement & Add to Cart
Android Studio - Xml Editor Autocomplete Not Working With Support Libraries
App Crashes When Running Maps Activity
How to Enable Location Access Programmatically in Android
Remove Old and Add New Git in Android Studio
How to Open Particular Screen on Clicking on Push Notification for Flutter
Architecture Components: Observer Keep Observing Even After Removing It on Ondestroy