Convert string variable value to character array in Java Android

How to change string variable data type value into char array data type value and show on screen using TextView.

In this tutorial we are simply converting the string data type value into char data type. A char data type can hold only single char so we need to create char array here to hold multiple values coming from string variable. So here is the complete step by step tutorial for Convert string variable value to character array in Java Android.

android-project-download-code-button

How to Convert string variable value to character array in Java Android.

Code for MainActivity.java file.

 package com.convertstringvariablechararray_android_examples.com;

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


public class MainActivity extends Activity {

 TextView textview1,textview2,textview3;
 
 //Initializing string variable.
 String value = "STRING SAMPLE VALUE" ;
 
 //Initializing char variable.
 char[] data;
 
 @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);
 
 //Converting string to char array.
 data = value.toCharArray();
 
 //Showing string on screen using textview.
 textview1.setText("String = " + value);
 
 //Showing char array on screen with Comma.
 
 textview2.setText("Char Array = ");
 
 for(int i=0;i<data.length;i++){
 
 textview2.setText(textview2.getText() +""+ data[i] + ", ");
 }
 
 //Showing char array on screen without Comma.
 
 textview3.setText("Char Array without Comma = ");
 
 for(int i=0;i<data.length;i++){
 
 textview3.setText(textview3.getText() +""+ data[i]);
 }
 }
}

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.convertstringvariablechararray_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="String Value"
 android:gravity="center"
 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="37dp"
 android:text="char value"
 android:gravity="center"
 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:layout_marginTop="41dp"
 android:gravity="center"
 android:text="char without comma"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Convert string variable value to character array in Java Android

Click here to download Convert string variable value to character array in Java Android project with source code.

Leave a Reply

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