Android: Textview

Kelinci Kertas > Blog > Code > Android: Textview

Time to learn how to display Text in your app. In this post will use Textview as component to show the text. We can use it for several things but in this first post about text we will use from basic.

Start with this is simple way to use Textview.

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="kelincikertas.com" />
</LinearLayout>

android textview sample 1

You can change it size using textSize parameter. Make sure you use sp metric for font size. Based on Android documentation “It is recommend you use this unit when specifying font sizes”. Look at this page to see detail and see this code below for sample.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="kelincikertas.com" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="kelincikertas.com"
    android:textSize="15sp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="kelincikertas.com"
    android:textSize="20sp" />

android textview sample 2

Second part is about aligning text. There are three type alignment supported by android. Check this code and result of it.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="kelincikertas.com" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="kelincikertas.com"
    android:textSize="15sp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="kelincikertas.com"
    android:textSize="20sp" />

android textview sample 3

Textview also support bold and italic text. Just use textStyle parameter like this sample

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="kelincikertas.com"
    android:textStyle="normal" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="kelincikertas.com"
    android:textSize="15sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="kelincikertas.com"
    android:textSize="20sp"
    android:textStyle="italic" />

android textview sample 4

Need to limit number of line? Don’t worry, use lines parameter and set how many maximum line you want. Your text will trimmed if exceed its limit.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lines="1"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:lines="2"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />

android textview sample 5

Seeing your text trimmed but you want to tell user there is more than that. Use ellipsize to add “…” as trimmed text. There are three ways to put it.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="start"
    android:lines="1"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ellipsize="middle"
    android:lines="1"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ellipsize="end"
    android:lines="1"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />

android textview sample 6

You can combine and put all parameters into one Textview and make better way to show your content. I know your designer have more complex layout and interface, but hope this can be a start point for you.

That all for first part of basic Textview. Simple way to use on your app for beginner. Wait for next Textview post and of course still with easy way to learn. Because there are always beginner before being advance.

Leave a Reply

Your email address will not be published. Required fields are marked *