Convert char value to string in Java Android

How to change single character data type into string data and show on android layout screen using TextView.

character data type supports only single alphabetic character where string is multiple group of characters into a single data type. So in this tutorial we are converting character value into string value. So here is the complete step by step tutorial for Convert char value to string in Java Android.

android-project-download-code-button

How to Convert char value to string in Java Android.

Code for MainActivity.java file.

 package com.convertcharvaluetostring_android_examples.com;

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

public class MainActivity extends Activity {

 char CharData = 'A' ;
 
 String value1, value2, value3, value4, value5;
 
 TextView textview1, textview2, textview3, textview4, textview5, textview6;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview1 = (TextView)findViewById(R.id.textView1);
 textview2 = (TextView)findViewById(R.id.textView2);
 textview3 = (TextView)findViewById(R.id.textView3);
 textview4 = (TextView)findViewById(R.id.textView4);
 textview5 = (TextView)findViewById(R.id.textView5);
 textview6 = (TextView)findViewById(R.id.textView6);
 
 // Showing char value on screen.
 
 textview1.setText(" Char Value = " + CharData);
 
 //Method 1 to convert char into string.
 
 value1 = " " + CharData ;
 
 textview2.setText("Convert Using Method 1 = " + value1);
 
 //Method 2 to convert char into string.
 
 value2 = String.valueOf(new char[]{CharData});
 
 textview3.setText("Convert Using Method 2 = " + value2);
 
 //Method 3 to convert char into string.
 
 value3 = String.valueOf(CharData);
 
 textview4.setText("Convert Using Method 3 = " + value3);
 
 //Method 4 to convert char into string.
 
 value4 = new Character(CharData).toString();
 
 textview5.setText("Convert Using Method 4 = " + value4);
 
 //Method 5 to convert char into string.
 
 value5 = Character.toString(CharData);
 
 textview6.setText("Convert Using Method 5 = " + value5);
 
}
}

Code for activity_main.xml layout file.

 <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="com.convertcharvaluetostring_android_examples.com.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:text="Char"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:text="Method1"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView2"
 android:layout_centerHorizontal="true"
 android:text="Method-2"
 android:layout_marginTop="20dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView3"
 android:layout_centerHorizontal="true"
 android:text="Method-3"
 android:layout_marginTop="20dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView4"
 android:layout_centerHorizontal="true"
 android:text="Method-4"
 android:layout_marginTop="20dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView6"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView5"
 android:layout_centerHorizontal="true"
 android:text="Method-5"
 android:layout_marginTop="20dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot :

Convert char value to string in Java Android

Click here to download Convert char value to string in Java Android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *