Android dialog ListView

If you want to display a dialog listview when a user clicks a button in your Android application, this tip might help you. In this tip, I am going to show you a simple solution to display the dialog listview.

In this solution, you will create an instance of ListView. Then create an instance of AlertDialog.Builder class. You will use the setView method of the AlertDialog.Builder to set the listview instance to the alert dialog.

Now to have a workable application that implements this solution. First, you need to create a new Android project. Then in the activity_main.xml file, add a Button as shown below. This button will be pushed to display the dialog listview.


<LinearLayout 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:padding="10dp"  
   tools:context=".MainActivity"
   android:orientation="vertical"
   >
  
   <Button
android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:onClick="showDialogListView"
      android:text="Show Dialog"
     />
  
</LinearLayout>


After that, create a layout file called list_item.xml in the res/layout directory. It represents a single list item design template. For this simple listview, its item or row contains only text. So in the list_item.xml file, only a TextView is specified.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
   android:id="@+id/txtitem"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="10dp"
/>
</LinearLayout>


In the onCreate() method of the MainActivity class, you write code to create an instance of ListView, add data to the ListView using ArrayAdapter class, and respond to item click event by registering the ListView with OnItemClickListener interface. Then you create showDialogListView method to display an alert dialog listview when the button is clicked. 

  
		import android.app.AlertDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
  ListView listView=null;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create an instance of a ListView
    listView=new ListView(this);
   // Add data to the ListView
    String[] items={"Facebook","Google+","Twitter","Digg"};
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtitem,items);
    listView.setAdapter(adapter);
    // Perform action when an item is clicked
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      ViewGroup vg=(ViewGroup)view;
     TextView txt=(TextView)vg.findViewById(R.id.txtitem);
     Toast.makeText(MainActivity.this,txt.getText().toString(),Toast.LENGTH_LONG).show();
       }
   });

}
   // This method is called to display a dialog listview when you click the button
    public void showDialogListView(View view){
      AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
      builder.setCancelable(true);
      builder.setPositiveButton("OK",null);
      builder.setView(listView);
      AlertDialog dialog=builder.create();
      dialog.show();

   }
}



Android-dialog-ListView

Posted by: Dara | post date: 06-16-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.