Advertisement

header ads

The Use of HTML Tags in Android Studio



In the last Android tutorial I’ve shown you how to InvokeDialPad with Phone Number in Android Studio. If you haven’t read it yet, you can read it from the above link. 

So today, I’m going to show you how to use HTML tags in android app. 

To begin coding you can first create a new project and write the code below in your new projects MainActivity.java class. 

This below example demonstrate how to use HTML tags withing String values and make tags effective by using HTML. From HTML methods . This example uses Bold, Italic, h2 , br and underline HTML tags. 

Also this example uses Html.fromHtml(strStyle) methods. That method use to interpret simple 
string which contains html tags as webpages. 

Here is the code,
 
@Override
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView(R.layout.
activity_main);

    LinearLayout layout =
new LinearLayout(this);
   
    String strStyle =
"<br><h2><i>This is Italic text,</i><br><b>This is Bold text</b><br><br>W: <u>This is Underlined text.</u>!<br>";
   
    TextView txtStyle =
new TextView(this);
    txtStyle.setText(Html.fromHtml(strStyle));
   
   
//Text Color
   
txtStyle.setTextColor(Color.RED);
   
   
//Font Size
   
txtStyle.setTextSize(16);
   
   
//Align text into Center
   
txtStyle.setGravity(Gravity.CENTER);
   
    layout.addView(txtStyle);
    setContentView(layout);
}

When you run this code in an AVD or real android device, the output will be like this,


If you have any questions regarding this tutorial or if you have any suggestions feel free to comment them in the comment section. 

So this is the end of this tutorial, hope you learn something from this tutorial. 

Happy Coding!

Post a Comment

0 Comments