The Android Textview is one of the most used widget in any apps. This post shows you some tips to manipulate text and background in android’s Textview . First, we will go through the following topics: changing text color, changing text size, set typeface for whole paragraph, set typeface for specific charater, put word at bottom of textview and put image at background of Textview .
changing text color in Android TextView
obj=”<color name or #rrggbb>” where rr , gg and bb are hexadecimal values of red, green and blue respectively. All colors should be inside the quotation marks. Otherwise it will lead to compiler error.
To create a textview in xml layout file is like this,
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” which means, it will take the whole space of its parent. In Java code, you created an object for TextView . After that set its width and height by setWidth() and setHeight().
For example,
TextView textview=(TextView)findViewById(R.id.text);
textview.setWidth(200); //width of the Textview is 200 dp
textview.setHeight(100); //height of the Textview is 100 dp
TextView is a subclass of ViewGroup, so you can set layout parameters such as margin , padding , gravity or scrollbar style on it. You can find several methods in the TextView class reference that are used to modify these properties for this widget. Just try them out and see what happens!
public class MainActivity extends Activity {
TextView textview=(TextView)findViewById(R.id.text);
textview.setTypeface(Typeface.MONOSPACE); //sets typeface to Monospace for all letter in this TextView
Intrinsic Motivation: Changing Typefaces and Fonts on Android
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
Android Fonts Tutorial with Example on Android Studio 2.3
url(“fontname”) for this format, android system loads the font from the specified URL. It should be put in quotes to avoid any ambiguity during runtime. The url can either be on the device or on the web.
android:text=”Welcome to my world!”
In this tutorial I will show you some tips to manipulate text and background in android’s Textview . First, we will go through the following topics: changing text color, changing text size, set typeface for whole paragraph, set typefont for specific charater, put word at bottom of textview and put image at background of Textview .
If you want to add some style to your Android app written in Java, you may have thought about adding some custom fonts. In this tutorial we will see how to do it easily with the support library.
Android provides a simple way to use Fonts in our Application. Using the Fonts in our Application, we can customize font properties such as typeface, size and color.
This tutorial presents a step-by-step explanation of how to use custom fonts in your Android application development process with examples and screenshots. Let’s get started!
Changing Text Size
android:text=”Welcome to my world!”
We have talked about changing text size with android:typeface . You can also change the text size by specifying a dimension resource in the xml file. Here are some sample values for typefaceSize attribute,
<resources> <dimen name=”typeface_size”>14sp</dimen> <!– Font size –>
<dimen name=”typeface_size”>20dp</dimen> <!– Text size –>
android:text=”Welcome to my world!”
Run your app and you will see that the text view changes its font with dimension specified in typefaceSize.
font_name : it actually referred to the font styles that are available in your current device. To access all fonts first you need to define font family in your xml file for example,
android:text=”Welcome to my world!”
font_name=”font name” specifies the custom font which not installed on phone (e.g Roboto). The name of your custom font file must be in quotes.
android:text=”Welcome to my world!”
Just define the name of your custom font file in this attribute. Android will try to load the font from the given url location. Please note that it can’t always find the font you are trying to use, so make sure you put it somewhere where it can be found. If you want to load a font from your phone you should put the full path in quotes.
android:typeface=”serif”
conclusion:
So, in this tutorial we talked about how to change text color, change text size and set typeface for the whole paragraph. We also talked about android:typeface=”font name” attribute which specifies the font that not installed on phone (e.g Roboto). Finally Android provides a simple way to use Fonts in our Application.