Thursday, 10 September 2015

Add Currency Symbol in Android Textview


       Here I am going to explain how to add a currency symbol in Android textview
                 


   Step 1)strings.xml


  <?xml version="1.0" encoding="utf-8"?>
  <resources>

    <string name="app_name">Currency</string>
    <string name="us">\u0024</string>
    <string name="Rs">\u20B9</string>

 </resources>


 Step 2)activity_main.xml

  <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" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:textColor="#e6443c"
        android:textSize="wrap_content
        android:layout_height="100dp"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:textColor="#e6443c"
        android:layout_height="wrap_content"
        android:textSize="25dp
        android:layout_below="@+id/textview"
        android:layout_centerInParent="true" />

</RelativeLayout>

 Step 3)MainActivity.java


 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.TextView;

  public class MainActivity extends Activity {

TextView t, t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.textview);
t1 = (TextView) findViewById(R.id.textview1);

String rupee = getResources().getString(R.string.Rs);
String dollar = getResources().getString(R.string.us);
t.setText(rupee +   "   05   " + "AndroidDhina");
t1.setText(dollar + "   19   " + "AndroidDhina");
}

}

Happy Coding:)