How to convert ArrayList to String using GSON?

AndroidMobile DevelopmentApps

GSON is java library, It is used to convert OBJECT to JSON and JSON to Object. Internally it going to work based on serialization and de- serialization.

This example demonstrate about how to convert ArrayList to string using GSON library.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code in build.gradle.

apply plugin: ''
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner ""
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation ''
   implementation ''
   implementation ''
   testImplementation 'junit:junit:4.12'
   androidTestImplementation ''
   androidTestImplementation ''

In the above code we have added GSON latest library.

Step 3 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
< xmlns:android = ""
   xmlns:app = ""
   xmlns:tools = ""
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
      android:id = "@+id/result"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Result Data"
      app:layout_constraintBottom_toBottomOf = "parent"
      app:layout_constraintLeft_toLeftOf = "parent"
      app:layout_constraintRight_toRightOf = "parent"
      app:layout_constraintTop_toTopOf = "parent" />

In the above code we have added textview, This text view going to show result data.

Step 4 − Add the following code to src/

package com.example.andy.myapplication;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
   protected void onCreate(Bundle savedInstanceState) {
      TextView result = findViewById(;
      ArrayList<String> list = new ArrayList<String>();
      list.add("C programing Language");
      list.add("C plus plus");
      Gson gson = new Gson();
      String arrayData = gson.toJson(list);

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above output, it is showing arraylist as String data.

Published on 11-Jan-2019 12:14:18