Cdacians

Cdacians
Cdacians

Tuesday, 7 August 2012

Create custom dialog with EditText


Create custom dialog with EditText

Create custom dialog with EditText

Create a XML in /res/layout/ folder to define the layout of the custom dialog.
eg. customlayout.xml
?
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
<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="10dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<TextView
android:id="@+id/dialogtextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/dialogedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/dialogupdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update"
/>
<Button
android:id="@+id/dialogdismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss"
/>
</LinearLayout>


Java code, eg. AndroidCustomDialog.java
?
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
package com.AndroidCustomDialog;
 
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class AndroidCustomDialog extends Activity {
 
static final int CUSTOM_DIALOG_ID = 0;
 
TextView customDialog_TextView;
EditText customDialog_EditText;
Button customDialog_Update, customDialog_Dismiss;
;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       Button buttonStartDialog = (Button)findViewById(R.id.startdialog);
       buttonStartDialog.setOnClickListener(new Button.OnClickListener(){
 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   showDialog(CUSTOM_DIALOG_ID);
  }
       });
   }
   
   private Button.OnClickListener customDialog_UpdateOnClickListener
   = new Button.OnClickListener(){
 
 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  customDialog_TextView.setText(customDialog_EditText.getText().toString());
 }
    
   };
   
   private Button.OnClickListener customDialog_DismissOnClickListener
   = new Button.OnClickListener(){
 
 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  dismissDialog(CUSTOM_DIALOG_ID);
 }
    
   };
   
@Override
protected Dialog onCreateDialog(int id) {
 // TODO Auto-generated method stub
 Dialog dialog = null;;
    switch(id) {
    case CUSTOM_DIALOG_ID:
     dialog = new Dialog(AndroidCustomDialog.this);
 
     dialog.setContentView(R.layout.customlayout);
     dialog.setTitle("Custom Dialog");
     
     customDialog_EditText = (EditText)dialog.findViewById(R.id.dialogedittext);
     customDialog_TextView = (TextView)dialog.findViewById(R.id.dialogtextview);
     customDialog_Update = (Button)dialog.findViewById(R.id.dialogupdate);
     customDialog_Dismiss = (Button)dialog.findViewById(R.id.dialogdismiss);
     
     customDialog_Update.setOnClickListener(customDialog_UpdateOnClickListener);
     customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener);
     
        break;
    }
    return dialog;
}


akm
www.cdacians.com

1 comment: