Part 34 : Spannable String And Global Style

 themes.xml : 

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.SpannableLearning" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>


<style name="new_text_view" parent="@android:style/Widget.TextView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">20dp</item>
</style>


</resources>


MainActivity.java : 

package first.learn.spannablelearning;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView txtForeground, txtBackground, txtSize, txtStyle, txtAppearance ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtForeground = (TextView) findViewById(R.id.txtForeground);
txtBackground = (TextView)findViewById(R.id.txtBackground);
txtSize = (TextView)findViewById(R.id.txtSize);
txtStyle = (TextView)findViewById(R.id.txtStyle);
txtAppearance = (TextView)findViewById(R.id.txtAppearance);

String one = "This is ForeGround Span";
Spannable spOne = new SpannableString(one);
spOne.setSpan(new ForegroundColorSpan(Color.RED),8,17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );

txtForeground.setText(spOne);


String two = "This is Background Color Span And Insert";

SpannableStringBuilder spTwo = new SpannableStringBuilder(two);

spTwo.setSpan(new BackgroundColorSpan(Color.BLUE), 8, 24, Spanned.SPAN_EXCLUSIVE_INCLUSIVE );
spTwo.setSpan(new ForegroundColorSpan(Color.WHITE), 8, 24, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spTwo.insert(25, "Blue");

txtBackground.setText(spTwo);


String three = "This is Size Span";
Spannable spThree = new SpannableString(three);

spThree.setSpan(new RelativeSizeSpan(1.5f), 8, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spThree.setSpan(new ForegroundColorSpan(Color.RED),8,12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );


txtSize.setText(spThree);



String four = "This is Style Span";
Spannable spFour = new SpannableString(four);
spFour.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 8, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

txtStyle.setText(spFour);


String five = "This is Text Appearance";
Spannable spFive = new SpannableString(five);
ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
TextAppearanceSpan span = new TextAppearanceSpan("serif", Typeface.BOLD, 70, blueColor, null );

spFive.setSpan(span, 8, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txtAppearance.setText(spFive);



}
}


activity_main.xml : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">

<TextView
style="@style/new_text_view"
android:text="ForeGround Span"
android:id="@+id/txtForeground"
/>


<TextView
style="@style/new_text_view"
android:text="BackGround Span"
android:id="@+id/txtBackground"
/>



<TextView
style="@style/new_text_view"
android:text="Size Span"
android:id="@+id/txtSize"
/>


<TextView
style="@style/new_text_view"
android:text="Style Span"
android:id="@+id/txtStyle"
/>



<TextView
style="@style/new_text_view"
android:text="Text Appearance Span"
android:id="@+id/txtAppearance"
/>

</LinearLayout>








No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts