Skip to main content

Make your TextView Colorful in Android(Using SpannableStringBuilder)

Add Colors to your TextView

There is a small trick behind this. You have to Keep the text value of textView Empty.
Because the string will be written in java part this time.

Here Goes the entire code for this:

MainActivity.java


package com.androiddevelopmentguru.spannable;

import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;


import android.widget.TextView;
import android.widget.TextView.BufferType;

public class MainActivity extends Activity {

TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    text = (TextView)findViewById(R.id.textView1);
    SpannableStringBuilder spanBuilder =new SpannableStringBuilder();
    // Taking a string as a span
    String welcome = "Welcome To ";
    
    
    //SpannableString class is called and welcome is passed as an object
    SpannableString firstSpan = new SpannableString(welcome);
    
    //calling setSpan method to set desired color in our string
    firstSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, welcome.length(), 0);
    
    spanBuilder.append(firstSpan);
    
    //Repeat the whole process with another string

    String developer="Android Development Guru";
    
    SpannableString secondSpan = new SpannableString(developer);
    
    secondSpan.setSpan(new ForegroundColorSpan(Color.GREEN), 0, developer.length(), 0);
    
    spanBuilder.append(secondSpan);
    text.setText(spanBuilder,BufferType.SPANNABLE);
    }

  
}



activitymain.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:textSize="16sp"
        android:text="" />

</LinearLayout>


Output:





Comments

Popular posts from this blog

Android: Login Screen using Fragments

In this tutorial, our focus is on making a User/Member login activity using fragments. With the help of Fragments we will use the same activity to show User login area and also Members Login area. Prerequisite for this tutorial: You should be know how to make an Activity And most importantly you should have prior Knowledge of Fragments. For practising basic Fragment implementation refer to Android Simple Fragment Example      We have used only three activities for this: MainActivity(which represents the Login Screen) Fragments for Members area Fragment for New Users

Simple Login/Register Example using SQLite database

MainActivity.java(login Screen) package com.AndroidDevelopmentGuru.database_new; import java.util.List; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity {                                 EditText user, pass;                 Button login, not_reg;                 DatabaseHandler db;           ...

Android: How to Generate Key Hash for Facebook Integration

Hello everyone, As this is a quite confusing for everyone to generate the key hash in your PC, so i decided to write about it. I have also tried many ways but every time i failed to generate the key hash. After trying for an hour i successfully generated the key. Just follow the below given Steps and you are done with this. Step I: Download Openssl from   here Step II: Download openssl-0.9.8e_X64.zip for 64 bit PC and openssl-0.9.8e_WIN32.zip for 32 bit PC Step III: once your download the zip folder. Go to C drive(where window is installed) and make a folder named openssl Step IV: extract all zip file in this folder. Step V: Now go to Java folder and copy path of jre. For example: C:\Program Files\Java\jre1.8.0_101\bin