Set String Array List data into TextView android

Add array list data into TextView to display string values line by line like listview.

List view is a custom type of data elements showing platform but now android developer can use textView widget as list view to show array list elements data on it. So here is the complete step by step tutorial for Set String Array List data into TextView android.

android-project-download-code-button

How to Set String Array List data into TextView android.

Code for MainActivity.java file.

 package com.displayarraylisttextview;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends Activity {

 ArrayList<String> StringArray = new ArrayList<String>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ArrayValueAddFunction();
 
 LinearLayout LinearLayoutView = new LinearLayout(this);
 TextView DisplayStringArray = new TextView(this);
 DisplayStringArray.setTextSize(25);
 LinearLayoutView.addView(DisplayStringArray);
 for (int i=0; i<StringArray.size();i++){
 DisplayStringArray.append(StringArray.get(i));
 DisplayStringArray.append("\n");
 }
 setContentView(LinearLayoutView);
 }
 
 private void ArrayValueAddFunction() {
 StringArray.add("ONE");
 StringArray.add("TWO");
 StringArray.add("THREE");
 StringArray.add("FOUR");
 StringArray.add("FIVE");
 StringArray.add("SIX");
 
 }
}

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.displayarraylisttextview.MainActivity" >

</RelativeLayout>

Screenshot:

Set String Array List data into TextView android

Click Here to Download Set String Array List data into TextView android project.

Leave a Reply

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