Combine Concat Merge two string arrays in Java Android

How to dynamically concatenate and display two string array in android programmatically.

In this tutorial we are combining two different string array into single array. There are no certain method available for android developers to combine two or multiple string array, so in this tutorial we are first creating an string array list then adding string array into string array list after that converting array list into string array. So here is the complete step by step tutorial for Combine Concat Merge two string arrays in Java Android.

android-project-download-code-button

How to Combine Concat Merge two string arrays in Java Android.

Code for MainActivity.java file.

 package com.mergetwostringarrays_android_examples.com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

public class MainActivity extends Activity {

 TextView arrayText1,arrayText2,arrayText3;
 
 // Initializing array 1
 String[] array1 = new String[]{"one", "two", "three","four","five"};

 // Initializing array 2
 String[] array2 = new String[]{"six", "seven"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 arrayText1 = (TextView)findViewById(R.id.textView1);
 arrayText2 = (TextView)findViewById(R.id.textView2);
 arrayText3 = (TextView)findViewById(R.id.textView3);
 
 //Printing array 1 on screen.
 for(int i=0;i<array1.length;i++){
 
 arrayText1.setText(arrayText1.getText() + array1[i] + ", ");
 }
 
 //Printing array 2 on screen.
 for(int i=0;i<array2.length;i++){
 
 arrayText2.setText(arrayText2.getText() + array2[i] + ", ");
 }
 
 //Creating array list string.
 
 List<String> array3 = new ArrayList<>();
 
 //adding array 1 to array 3.
 
 Collections.addAll(array3,array1);
 
 //adding array 2 to array 3.
 
 Collections.addAll(array3,array2);
 
 //converting array list to string array;
 
 String[] finalArray = array3.toArray(new String[array3.size()]);
 
 //Printing final array on screen.
 
 for(int i=0; i < finalArray.length; i++){
 
 arrayText3.setText(arrayText3.getText() + finalArray[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.mergetwostringarrays_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:layout_marginTop="153dp"
 android:text="Array 1 :"
 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_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Array 2 : "
 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="36dp"
 android:text="Combine Array : "
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Combine Concat Merge two string arrays in Java Android

Click here to download Combine Concat Merge two string arrays in Java Android project with source code.

Leave a Reply

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