Sunday, 2 March 2014

Strike-Through of Text on UI Component

Today i am going to show you how to Strike through the text attached UI Component of Android As shown below

Step-1   First write the code for XML layout
<RelativeLayout 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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="44dp"
        android:text="RadioButton" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/radioButton1"
        android:layout_alignLeft="@+id/textView"
        android:layout_marginBottom="23dp"
        android:text="Button" />

</RelativeLayout>
Step-2  write the below code in MainActivity.java
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView strike_through = (TextView)findViewById(R.id.textView);
Button btn = (Button)findViewById(R.id.button1);
RadioButton radio_btn = (RadioButton)findViewById(R.id.radioButton1);
strike_through.setText("This is strike-thru");
// Strike-through code for TextView
strike_through.setPaintFlags(strike_through.getPaintFlags()|Paint.STRIKE_THRU_TEXT_FLA);
// Strike-through code for Button
 btn.setPaintFlags(btn.getPaintFlags()|Paint.STRIKE_THRU_TEXT_FLAG);
//Strike-through code for RadioButton
  radio_btn.setPaintFlags(radio_btn.getPaintFlags()|Paint.STRIKE_THRU_TEXT_FLAG);
}

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

}
Step-3 for removing strike-through you can use below code for UIcomponent
yourUIcomponent.setPaintFlags(yourUIcomponent.getPaintFlags()|Paint.STRIKE_THRU_TEXT_FLAG);


Thats it! Run your project and see result as shown above in image.

please give your valuable feedback.
Enjoy Coding and blogging!! 

No comments:

Post a Comment