Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
Text Style of a Substring - Android Studio - Java
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity"> <TextView android:padding="10dp" android:id="@+id/textView" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_centerInParent="true" /> </RelativeLayout>
MainActivity.java
package com.usmtip.myapplication; import android.graphics.Typeface; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.UnderlineSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //TextView TextView mTextView = findViewById(R.id.textView); //The text to set in TextView String mText = "Our text can be BOLD and ITALIC and BOLD-ITALIC and STRIKE-THROUGH and UNDERLINE."; //Creating spannable string from normal string, we will use it to apply StyleSpan to substrings SpannableString mSpannableString = new SpannableString(mText); //styles to apply on substrings StyleSpan mBold = new StyleSpan(Typeface.BOLD); //bold style StyleSpan mItalic = new StyleSpan(Typeface.ITALIC); //italic style StyleSpan mBoldItalic = new StyleSpan(Typeface.BOLD_ITALIC); //bold italic style StrikethroughSpan mStrikeThrough = new StrikethroughSpan(); //strike through style UnderlineSpan mUnderlineSpan = new UnderlineSpan(); //underline style //applying styles to substrings mSpannableString.setSpan(mBold, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mSpannableString.setSpan(mItalic, 25, 31, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mSpannableString.setSpan(mBoldItalic, 36, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mSpannableString.setSpan(mStrikeThrough, 52, 66, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mSpannableString.setSpan(mUnderlineSpan, 71, 80, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //setting text to text view mTextView.setText(mSpannableString); } }
0 Comments: