Convert String Array to single String value in Java Android

How to change the whole string array into only single string variable and show on screen using TextView.

String array which contain multiple values can be easily converted into only single string value using Arrays.toString() method. This method is automatically converted string array into string variable. So here is the complete step by step tutorial for Convert String Array to single String value in Java Android.

android-project-download-code-button

How to Convert String Array to single String value in Java Android.

Code for MainActivity.java file.

 package com.convertstringarraysinglestring_android_examples.com;

import java.util.Arrays;

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

public class MainActivity extends Activity {

 TextView textview1,textview2,textview3;
 
 String DemoValue;
 
 String[] stringArray = new String[]{
 
 "One",
 "Two",
 "Three",
 "Four",
 "Five"
 };
 StringBuilder stringbuilder = new StringBuilder();
 
 @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);
 
 //Printing string array on screen.
 textview1.setText("String Array = ");
 
 for(int i=0;i<stringArray.length;i++){
 
 textview1.setText(textview1.getText() + stringArray[i]+" , ");
 }
 
 //Converting string array to string .
 
 DemoValue = Arrays.toString(stringArray);
 
 //Printing string variable on screen.
 
 textview2.setText("String = " + DemoValue);
 
 //Converting Using string builder.
 
 for(int i=0;i<stringArray.length;i++){
 
 stringbuilder.append(stringArray[i]);
 
 textview3.setText(" String = " + stringbuilder.toString());
 
 }
 
 }
}

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.convertstringarraysinglestring_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 array"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <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="38dp"
 android:text="string value"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <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="38dp"
 android:text="string value-2"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshot:

Convert String Array to single String value in Java Android

Click here to download Convert String Array to single String value in Java Android project with source code.

Leave a Reply

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