Change/Convert string array to list string in Java Android

How to create list string using string array with the use of Arrays.asList() method in android programming.

In this tutorial we are converting string array elements into list string and showing them on screen with textview. So here is the complete step by step tutorial for Change/Convert string array to list string in Java Android.

android-project-download-code-button

How to Change/Convert string array to list string in Java Android.

Code for MainActivity.java file.

 package com.convertstringarraytoliststring_android_examples.com;

import java.util.Arrays;
import java.util.List;

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

public class MainActivity extends Activity {

 String[] StringArray = new String[]{
 "one",
 "two",
 "three",
 "four",
 "five",
 "six",
 "seven"
 };
 List<String> StringList;
 int i = 0;
 TextView textview;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 StringList = Arrays.asList(StringArray);
 
 while(i < StringList.size())
 { 
 textview.setText(textview.getText()+ " " + StringList.get(i) + ", ");
 
 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.convertstringarraytoliststring_android_examples.com.MainActivity" >

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

</RelativeLayout>

Screenshot:

Change/Convert string array to list string in Java Android

Click here to download Change/Convert string array to list string in Java Android project with source code.

Leave a Reply

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