Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the font size of an textview element in Android?

I am trying to display 2 items in a TextView. Is their any way to change the font of the single item in a TextView?

Here is the XML which I am using

<?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:gravity="center_horizontal"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:id="@+id/Rowtext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:text="Listiems"
        android:background="@drawable/customshape"
         />

</LinearLayout>
like image 683
Beginner Avatar asked Mar 20 '13 12:03

Beginner


2 Answers

Use android:textSize.

<TextView
    android:id="@+id/time"
    android:gravity="right"
    android:padding="5dp"
    android:textSize="40dp"
    android:textColor="#88ffff00"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Time: 60"/>

Use sp if the user can rescale the text without breaking the UI. If rescaling the text would break the UI, use dp.

like image 181
Ags1 Avatar answered Oct 24 '22 07:10

Ags1


One way is to use TextView.setText() method and feed it with HTML, like this:

import android.text.Html;

String n = "<b>bold</b> <small>small</small>";
TextView tv = (TextView) findViewById(...)
tv.setText(Html.fromHtml(n));

I often use it for some minor markup (like make part bolder or smaller)

like image 39
pelotasplus Avatar answered Oct 24 '22 08:10

pelotasplus