10

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> 
3
  • Which way do you want to do this. On run time or in XML? Commented Mar 20, 2013 at 12:59
  • 3
    See this - stackoverflow.com/a/2179039/361230 and this - blog.stylingandroid.com/archives/177 Commented Mar 20, 2013 at 13:00
  • @Fahad i have no idea which way s better, so u specify which way is better to do Commented Mar 20, 2013 at 13:04

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't that we need to use SP for font sizes? I think it should be 40sp not 40dp
Use sp if the user can rescale the text without breaking the UI. If rescaling the text would break the UI, use dp. stackoverflow.com/a/11638914/2713250
8

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)

2 Comments

erm, question was about changing font :-/ Shade's answer looks good!
There is a typo in "fromHtml" method. It says formHtml instead.
2

Use the HTML class from this link http://code.google.com/p/android-richtexteditor/source/browse/trunk/src/net/sgoliver/Html.java?r=4 . We would be able to set font size from this class. Normal android.text.Html actually ignores font size. Tried and tested and it worked for me.

TextView tv = (TextView)findViewById(R.id.textview); String s = "<p>Some Text here<br><b>hi </b><font size =\"20\"><b><i>\"italics</i></b></font><b><i> </i></b><b><i><u>underline</u></i></b></p>"; tv.setText(Html.fromHtml(s)); 

Comments

0

You should add line in Text View Tag In XML:

android:textSize="32sp" 

Comments

-1

You can use html for this like this:

mytextView.setText(Html.fromHtml("<p> <font size="20" color="#0066FF" style="font-style:italic">Push this button</font> to start a new game.</p>")) 

remove color and style attributes if you don't need them.

3 Comments

this is not working!!
@MohamadGhaithAlzin 9 years old answer... I checked it still works.
No, it generates an error from the redundant double quotation. However, I've tested it with mytextView.setText(Html.fromHtml("<p> <font size=\"40\" color=#0066FF style=\"font-style:italic\">Push this button</font> to start a new game.</p>")); It changes only the color but not for the size and italic!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.