Cdacians

Cdacians
Cdacians

Tuesday 7 August 2012

Custom dialog with ListView


Custom dialog with ListView

Custom dialog with ListView


Create /res/layout/dialoglayout.xml, it define the layout of the custom dialog with ListView.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customdialog"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dp"
    android:minWidth="300dp">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
 
    <ListView
        android:id="@+id/dialoglist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
 
</LinearLayout>


Java code of the main activity.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.AndroidCustomDialog;
 
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
public class AndroidCustomDialogActivity extends Activity {
 
 Button buttonOpenDialog;
  
 String KEY_TEXTPSS = "TEXTPSS";
 static final int CUSTOM_DIALOG_ID = 0;
  
 ListView dialog_ListView;
  
 String[] listContent = {
   "January", "February", "March", "April",
   "May", "June", "July", "August", "September",
   "October", "November", "December"};
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        buttonOpenDialog = (Button)findViewById(R.id.opendialog);
        buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
 
   @Override
   public void onClick(View arg0) {
    showDialog(CUSTOM_DIALOG_ID);
   }});
   
    }
 
 @Override
 protected Dialog onCreateDialog(int id) {
 
  Dialog dialog = null;
   
  switch(id) {
     case CUSTOM_DIALOG_ID:
      dialog = new Dialog(AndroidCustomDialogActivity.this);
      dialog.setContentView(R.layout.dialoglayout);
      dialog.setTitle("Custom Dialog");
       
      dialog.setCancelable(true);
      dialog.setCanceledOnTouchOutside(true);
       
      dialog.setOnCancelListener(new OnCancelListener(){
 
    @Override
    public void onCancel(DialogInterface dialog) {
     // TODO Auto-generated method stub
     Toast.makeText(AndroidCustomDialogActivity.this,
       "OnCancelListener",
       Toast.LENGTH_LONG).show();
    }});
       
      dialog.setOnDismissListener(new OnDismissListener(){
 
    @Override
    public void onDismiss(DialogInterface dialog) {
     // TODO Auto-generated method stub
     Toast.makeText(AndroidCustomDialogActivity.this,
       "OnDismissListener",
       Toast.LENGTH_LONG).show();
    }});
 
      //Prepare ListView in dialog
      dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
      ArrayAdapter<String> adapter
       = new ArrayAdapter<String>(this,
         android.R.layout.simple_list_item_1, listContent);
      dialog_ListView.setAdapter(adapter);
      dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
 
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
     // TODO Auto-generated method stub
     Toast.makeText(AndroidCustomDialogActivity.this,
       parent.getItemAtPosition(position).toString() + " clicked",
       Toast.LENGTH_LONG).show();
     dismissDialog(CUSTOM_DIALOG_ID);
    }});
       
         break;
     }
 
  return dialog;
 }
 
 @Override
 protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
  // TODO Auto-generated method stub
  super.onPrepareDialog(id, dialog, bundle);
 
  switch(id) {
     case CUSTOM_DIALOG_ID:
      //
         break;
     }
   
 }
 
}


main.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
 
    <Button
        android:id="@+id/opendialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Open Dialog" />
 
</LinearLayout>


akm
www.cdacians.com

2 comments: