HtmlTextView is an extended TextView component for Android, which can load HTML components by converting them into Android Spannables for viewing. In addition to HTML tags, the library allows to load images from the local drawable folder or from the Internet and also URL from the internet. In this article, we are going to show text, images, and URLs in Android using Html components.
Step by Step Implementation
Step 1: Create a new Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add this into the AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET"/>
Add this into build.gradle file
implementation 'org.sufficientlysecure:html-textview:4.0'
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > < TextView android:id = "@+id/text" android:layout_width = "match_parent" android:layout_height = "wrap_content" /> < org.sufficientlysecure.htmltextview.HtmlTextView android:id = "@+id/text2" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "16sp" /> < org.sufficientlysecure.htmltextview.HtmlTextView android:id = "@+id/text3" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "16sp" /> </ LinearLayout > |
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file
Spanned spanned= HtmlFormatter.formatHtml(new HtmlFormatterBuilder() .setHtml("<p><big><bold>GeeksForGeeks</bold></big><p>")); // load text using HtmlFormatterBuilder text2.setHtml("<font color='blue'><a href='https://www.google.com'</a>Go to this link</font>"); text2.setOnClickATagListener(new OnClickATagListener() { @Override public boolean onClick(View widget, String spannedText, @Nullable String href) { Toast.makeText(getApplicationContext(),href,Toast.LENGTH_LONG).show(); return false; } }); // loading image using HtmlAssetsImageGetter text1.setHtml("Load Image From Asset<br><img src='image.png'/>",new HtmlAssetsImageGetter(text1));
Below is the code for the MainActivity.java file.
Java
import android.os.Bundle; import android.text.Spanned; import android.view.View; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import org.sufficientlysecure.htmltextview.HtmlAssetsImageGetter; import org.sufficientlysecure.htmltextview.HtmlFormatter; import org.sufficientlysecure.htmltextview.HtmlFormatterBuilder; import org.sufficientlysecure.htmltextview.OnClickATagListener; public class MainActivity extends AppCompatActivity { TextView gtext; org.sufficientlysecure.htmltextview.HtmlTextView text1, text2; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); gtext = findViewById(R.id.text); text1 = findViewById(R.id.text2); text2 = findViewById(R.id.text3); Spanned spanned = HtmlFormatter.formatHtml( new HtmlFormatterBuilder().setHtml(" <p><big><bold>GeeksForGeeks</bold></big> <p>")); gtext.setText(spanned); text2.setOnClickATagListener( new OnClickATagListener() { @Override public boolean onClick(View widget, String spannedText, @Nullable String href) { Toast.makeText(getApplicationContext(), href, Toast.LENGTH_LONG).show(); return false ; } }); text1.setHtml( "Load Image From Asset<br><img src='image.png'/>" , new HtmlAssetsImageGetter(text1)); } } |
Output: