Wednesday, November 4, 2020

Android Textview multiline example

 TextView is used to display text on Android application. By default, TextView displays text on one line and if long, TextView will automatically display with more lines to display its text in the most logical way.

 Android developers can create a new line on TextView both in programming and syntax. Android developers can create multi-line TextView without dividing text into multiple lines according to android: minLines properties.

Android Textview multiline example

The following android example code shows us the TextView utility with the xml layout file and the string resource file.

Create layout.xml 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <TextView

        android:id="@+id/text_view1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#DA70D6"

        android:text="This is line 1 \nThis is line 2 \nLine number 3"

        />


    <TextView

        android:id="@+id/text_view2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#DEB887"

        android:text="This is line 1 \nThis is line 2 \nLine number 3"

        android:maxLines="2"

        />


    <TextView

        android:id="@+id/text_view3"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#8FBC8F"

        android:text="This is line 1 \nThis is line 2"

        android:minLines="3"

        />


    <TextView

        android:id="@+id/text_view4"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#5F9EA0"

        android:text="@string/Multiline_Text_By_N"

        />

</LinearLayout>

Add string in file string.xml

<string name="Multiline_Text_By_N">

        Line number 1 \nLine number 2

    </string>

TextView new line programmatically

Create layout.xml next

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">

    <TextView

        android:id="@+id/text_view1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#F5F5DC"

        android:text="Sample TextView 1"

        />

    <TextView

        android:id="@+id/text_view2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#E9967A"

        android:text="Sample TextView 2"

        />

    <TextView

        android:id="@+id/text_view3"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#DEB887"

        android:text="Sample TextView 3"

        />

    <TextView

        android:id="@+id/text_view4"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#7FFF00"

        android:text="Sample TextView 4"

        />

    <TextView

        android:id="@+id/text_view5"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:background="#B0C4DE"

        android:text="Sample TextView 5"

        />

    <Button

        android:id="@+id/push_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="left"

        android:text="Apply TextView New Line"

        android:onClick="perform_action"

        />

</LinearLayout>

Create a class progam TextviewLine.class we use System.getProperty("line.separator") on java

public class TextviewLine extends Activity {

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.text_layout);

        init();

    }

    public void init(){

        

    }

    public void btn_click_app(View v)

    {

        TextView tv1 = (TextView) findViewById(R.id.text_view1);

        //define multiline by \n line separator

        tv1.setText("Line number 1 \nLine number 2 \nLine number 3");


        TextView tv2 = (TextView) findViewById(R.id.text_view2);

        tv2.setText("Line number 1");

        //define new line by append android system line separator

        tv2.append(System.getProperty("line.separator"));

        tv2.append("Line number 2");


        TextView tv3 = (TextView) findViewById(R.id.text_view3);

        String str = "Line number 1"

                + System.getProperty("line.separator")

                + "Line number 2";

        //define new line by android system line separator

        tv3.setText(str);


        TextView tv4 = (TextView) findViewById(R.id.text_view4);

        //define new line by html <br />tag

        String str2 = "Line number 1 <br /> Line number 2";

        //need to import android.text.Html class

        tv4.setText(Html.fromHtml(str2));


        TextView tv5 = (TextView) findViewById(R.id.text_view5);

        tv5.setText(R.string.Multiline_Text_By_N);

    }

Tags : Multiline TextView in Android, How to make a TextView multiline programmatically in Android, TextView new line (multiline) in android, How to set the multiple lines in TextView Android Studio

No comments:

Post a Comment