Jay
Jay

Reputation: 5074

Allow TextView to be outside of Screen

I want my TextView to be allowed out of the screen horizontally (And also disable horizontal scroll bars), such as this:

enter image description here

However, the XML I have right now causes it to automatically fit inside the parent, such as this:

enter image description here

Code:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello1"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab2"
        android:layout_toRightOf="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello2"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab3"
        android:layout_toRightOf="@+id/tab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello3"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

</RelativeLayout>

How do I change my code to get the layout displayed in the First Image? Simply just cut off the textview if it goes out of the parent.

EDIT: Tried LinearLayout. It didn't work either

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="horizontal">

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello1"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab2"
        android:layout_toRightOf="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello2"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab3"
        android:layout_toRightOf="@+id/tab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello3"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

</LinearLayout>

Upvotes: 4

Views: 1158

Answers (2)

Sachin Chandil
Sachin Chandil

Reputation: 17809

Simply use LinearLayout instead of RelativeLayout and set orientation to horizontal.

EDITED

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello1"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:maxLines="1"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello2"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:maxLines="1"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello3"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:maxLines="1"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>
</LinearLayout>

Upvotes: 0

Sanjeev
Sanjeev

Reputation: 4375

Try using This attribute on your 3 TextView

android:maxLines="1"

After setting maxLines="1" I got this result

enter image description here

Upvotes: 1

Related Questions