How to show all string, integer array list elements on android activity screen.
In this tutorial we are printing array list elements( Values ) on screen one by one with looping control statements and we are using TextView to display list elements. So here is the complete step by step tutorial for Display Print all elements of ArrayList in Java Android using Loop.
How to Display Print all elements of ArrayList in Java Android using Loop.
Code for MainActivity.java file.
package com.printallelementsofarraylist_android_examples.com; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { TextView integerTextView,stringTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); integerTextView = (TextView)findViewById(R.id.textView1); stringTextView = (TextView)findViewById(R.id.textView2); //Initializing integer array list; List<Integer> integerData = new ArrayList<Integer>(); //Initializing string array list; List<String> stringData = new ArrayList<String>(); //Dynamically adding values to integer array list. integerData.add(1); integerData.add(2); integerData.add(3); integerData.add(4); integerData.add(5); integerData.add(6); integerData.add(7); //Dynamically adding values to string array list. stringData.add("ONE"); stringData.add("TWO"); stringData.add("THREE"); stringData.add("Four"); stringData.add("Five"); stringData.add("Six"); stringData.add("Seven"); //Printing integer array list values on screen. for(int i=0; i < integerData.size(); i++){ integerTextView.setText(integerTextView.getText() + " " + integerData.get(i) + " , "); } //Printing string array list values on screen. for(int i=0; i < stringData.size(); i++){ stringTextView.setText(stringTextView.getText() + stringData.get(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.printallelementsofarraylist_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="168dp" android:text="" 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_below="@+id/textView1" android:layout_marginTop="27dp" android:layout_centerHorizontal="true" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center" /> </RelativeLayout>
Screenshot: