A ListView is a great widget to show a list of entries to the user but many applications don’t use the ListView control fully. If the Android API for ListView is properly used then we can add items to ListView dynamically which gives the user an option to edit the ListView as shown in following example:

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="55dp" >
    </ListView>

</RelativeLayout>

Java Code

Let’s now see the java code for this:

package com.example.dynamiclistviewexample;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

	private Button bt;
	private ListView lv;
	private ArrayList<String> strArr;
	private ArrayAdapter<String> adapter;
	private EditText et;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt = (Button) findViewById(R.id.button1);
		lv = (ListView) findViewById(R.id.listView1);
		et = (EditText) findViewById(R.id.editText1);

		strArr = new ArrayList<String>();
		for (int i = 0; i < 2; i++) {
			strArr.add("Row:" + i);
		}
		adapter = new ArrayAdapter<String>(getApplicationContext(),
				android.R.layout.simple_list_item_1, strArr);
		lv.setAdapter(adapter);
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				strArr.add(et.getText().toString());
				adapter.notifyDataSetChanged();

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Screenshots for Dynamic List View

1) On launching the app, only the static items are shown. User has the option to enter some text in Edittext and click Add button:

dynamiclistview1

2) User enters text in EditText and clicks the add button

dynamiclistview2

Download code for this example

Related Posts

How to create a dynamically growing ListView in Android admin Android
A ListView is a great widget to show a list of entries to the user but many applications don't use the ListView control fully. If the Android API for ListView is properly used then we can add items to ListView dynamically which gives the user an option to edit...
A ListView is a great widget to show a list of entries to the user but many applications don't use the ListView control fully. If the Android API for ListView is properly used then we can add items to ListView dynamically which gives the user an option to edit the ListView as shown in following example: https://www.youtube.com/watch?v=tNoeFkXCZ6w <h2>Layout File</h2> 1 <h2>Java Code</h2> Let's now see the java code for this: 1 <h2>Screenshots for Dynamic List View</h2> 1) On launching the app, only the static items are shown. User has the option to enter some text in Edittext and click Add button: <img src="http://www.javaexperience.com/wp-content/uploads/2013/09/dynamiclistview1-180x300.png" alt="dynamiclistview1" width="180" height="300" class="alignnone size-medium wp-image-3600" /> 2) User enters text in EditText and clicks the add button <img src="http://www.javaexperience.com/wp-content/uploads/2013/09/dynamiclistview2-180x300.png" alt="dynamiclistview2" width="180" height="300" class="alignnone size-medium wp-image-3601" /> <h2>Download code for this example</h2> <div class="download-code"><img src="http://www.javaexperience.com/wp-content/uploads/2012/10/downloadcode-e1349278942164.jpg"><a href="http://www.javaexperience.com/android/DynamicListviewExample.zip"><br><br><strong>Creating listview items dynamically</strong></a></div>
How to show popup menu onclick of GridView items
Centre button position in layout xml
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments