Dynamically add values in string array list in android Java

Insert elements in array using arrayName.add() function on app program running time.

Dynamically adding values in string array is very easy because android development environment provides us its already build function .add() to add values in array on program execution time. So here is the complete step by step tutorial for Dynamically add values in string array list in android Java.

android-project-download-code-button

How to Dynamically add values in string array list in android Java.

Code for MainActivity.java file.

 package com.stringarrayaddelementsdynamically_android_examples.com;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {

 ArrayList<String> SampleArrayList = new ArrayList<String>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ArrayValueAddFunction();
 
 }
 
 private void ArrayValueAddFunction() {
 SampleArrayList.add("APPLE");
 SampleArrayList.add("BOY");
 SampleArrayList.add("CAT");
 SampleArrayList.add("DOG");
 SampleArrayList.add("ELEPHANT");
 SampleArrayList.add("FISH");
 
 Toast.makeText(MainActivity.this, "Values added successfully.", Toast.LENGTH_SHORT).show();
 }
}

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

</RelativeLayout>

Screenshot:

Dynamically add values in string array list in android Java

Click Here To Download Dynamically add values in string array list in android Java project.