how to add dynamically auto growing listview in android using android Studio

0 2,419

In this tut, i will show you how you can add dynamically auto growing listview in the android app in which user give input data and shows in a listview.

Main Activity

package exceptionbound.com.howtoaddautogrowingdynamiclistviewinandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private ListView listView;
    private ArrayList<String> stringArrayList;
    private EditText editText;
    private  ArrayAdapter<String> stringArrayAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=(Button) findViewById(R.id.button);
        listView=(ListView) findViewById(R.id.listView);
        editText=(EditText) findViewById(R.id.editText);

        stringArrayList= new ArrayList<String>();
        for(int i =0; i<2; i++){
            stringArrayList.add("Row"+i);
        }
        stringArrayAdapter =new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,stringArrayList);
        listView.setAdapter(stringArrayAdapter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stringArrayList.add(editText.getText().toString());
                stringArrayAdapter.notifyDataSetChanged();
            }
        });



    }
}

Layout

in the layout, we have one Text files,button and listview attributes.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="exceptionbound.com.howtoaddautogrowingdynamiclistviewinandroid.MainActivity">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:id="@+id/editText" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:id="@+id/button" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/listView"
        android:layout_marginTop="150dp"
        />
</RelativeLayout>

Download Source code

Comments

comments

Leave A Reply

Your email address will not be published.