Monday 2 January 2012

StrikeTextView in Android

Here we can see how to strike a TextView programmatic way.  
In Android using StrikethroughSpan to draw an underline
for the text on TextView.
 
Through the SpannableString we can StrikethroughSpan the text,
it can be static or dynamic text no problem,  
first we have to set the string to the SpannableString and then
we need to setSpan, and then we need to set that variable
to the TextView.
 
Example:
 UI Layout (activity_strike_text.xml)
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

 

    <TextView
        android:id="@+id/striketxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="25px" />

</LinearLayout>

Android Activity class(StrikeText.Class)
 
package com.androidsurya.striketext;

import android.os.Bundle;
import android.app.Activity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.Menu;
import android.widget.TextView;

public class StrikeText extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_strike_text);

       
        SpannableString Strickstr = new SpannableString("This is Stricke text");
        Strickstr.setSpan(new StrikethroughSpan(), 0, Strickstr.length(), Spanned.SPAN_PARAGRAPH);
        TextView StrickstrTv = (TextView) findViewById(R.id.striketxt);
        StrickstrTv.setText(Strickstr);
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_underline_text, menu);
        return true;
    }

}


Register Activity in Android Manifestfile
 <activity
            android:name="com.androidsurya.striketext.StrikeText"
            android:label="@string/app_name" >

Output Screenshot:
 





No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...