Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Horizontal TextView examples in LinearLayout and RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:id="@+id/tv_amenities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher_background"
android:padding="3dp"
android:text="Resturant"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:background="@drawable/ic_launcher_background"
android:padding="3dp"
android:text="LCD Tv"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:background="@drawable/ic_launcher_background"
android:padding="3dp"
android:text="Parking"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/tv_plus_amenities"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="15dp"
android:background="@mipmap/ic_launcher"
android:text="+15"
android:textColor="@android:color/background_dark" />
</LinearLayout>
</RelativeLayout>
<?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">
<RelativeLayout
android:id="@+id/tv_amenities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/first_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher_background"
android:padding="3dp"
android:text="Resturant"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/second_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:background="@drawable/ic_launcher_background"
android:padding="3dp"
android:layout_toEndOf="@+id/first_textview"
android:text="LCD Tv"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/third_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:background="@drawable/ic_launcher_background"
android:padding="3dp"
android:layout_toEndOf="@+id/second_textview"
android:text="Parking"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/white />
<TextView
android:id="@+id/fourth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginBottom="20dp"
android:background="@mipmap/ic_launcher"
android:text="+15"
android:layout_toEndOf="@+id/third_text"
android:layout_centerInParent="true"
android:textColor="@android:color/background_dark" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
@LaszloLajosT

This comment has been minimized.

Copy link
Owner Author

@LaszloLajosT LaszloLajosT commented May 1, 2020

These XML files are a quick sample of how to adjust TextView to a horizontal layout.
Question from StackOverFlow.

sample_view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.