Convert String ArrayList to string array in Java Android

How to change and show string array list into simple string array using android using loop.

In this tutorial we are converting string array list into normal string array using toArray() method. So here is the complete step by step tutorial for Convert String ArrayList to string array in Java Android.

android-project-download-code-button

How to Convert String ArrayList to string array in Java Android.

Code for MainActivity.java file.

 package com.convertstringarraylisttostringarray_android_examples.com;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 //Initializing string array list;
 List<String> StringArrayList = new ArrayList<>();
 
 //Initializing string array;
 String[] StringArray;
 
 //Initializing TextView
 TextView textview1,textview2;
 
 int i,j = 0;
 
 @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);
 
 //Adding values to String array list.
 StringArrayList.add("ONE");
 StringArrayList.add("TWO");
 StringArrayList.add("THREE");
 StringArrayList.add("FOUR");
 StringArrayList.add("FIVE");
 StringArrayList.add("SIX");
 
 //Printing string array list on screen using loop.
 
 textview1.setText("String Array list : ");
 
 while ( i<StringArrayList.size() )
 {
 
 textview1.setText(textview1.getText() + StringArrayList.get(i) + ", ");
 
 i++;
 
 }
 
 //Converting string array list into string array.
 StringArray = StringArrayList.toArray(new String[StringArrayList.size()]);
 
 textview2.setText("Convert into String Array : ");
 
 
 //Displaying converted string array on screen using textview.
 while ( j<StringArray.length )
 {
 
 textview2.setText(textview2.getText() + StringArray[j] + ", ");
 
 j++;
 
 }
 
 }

}

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.convertstringarraylisttostringarray_android_examples.com.MainActivity" >

 <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="65dp"
 android:text="String Array"
 android:gravity="center"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:gravity="center"
 android:text="String ArrayList"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Convert String ArrayList to string array in Java Android

Click here to download Convert String ArrayList to string array in Java Android project with source code.

Leave a Reply

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