Declare Initialize string array in Java Android

How to Create string[] array in android activity and show print array values on screen using for loop via TextView.

In this tutorial we are simply creating the string array to store multiple string values. So here is the complete step by step tutorial for Declare Initialize string array in Java Android.

android-project-download-code-button

How to Declare Initialize string array in Java Android.

Code for MainActivity.java file.

 package com.declarestringarray_android_examples.com;

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" 
 };
 
 TextView textview;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 textview.setText(" String Array Values = ");
 
 for(int i=0;i<stringArray.length;i++){
 
 textview.setText(textview.getText() + stringArray[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.declarestringarray_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" />

</RelativeLayout>

Screenshot :

Declare Initialize string array in Java Android

Click here to download Declare Initialize string array in Java Android project with source code.

Leave a Reply

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