Tutorial details

Android Dialog – Android Custom Dialog | App Code for Sale | Preview

Android Dialog – Android Custom Dialog | Android Tutorial

How to create Android Custom Dialg

Overview PAGE TOP

In this tutorial I am going to describe how to create Android Custom Dialg.

Android Dialog

Android__Dialog_-_Custom_Dialog_for_User_Login35.png

Create Android Project AndroidDialog ; File -> New -> Android Project

Android Layout PAGE TOP

activityandroiddialog.xml

01  <RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android'
02      xmlns:tools='http://schemas.android.com/tools'
03      android:layout_width='match_parent'
04      android:layout_height='match_parent' >
05  
06      <Button
07          android:id='@+id/btn_launch'
08          android:layout_width='wrap_content'
09          android:layout_height='wrap_content'
10          android:layout_alignParentTop='true'
11          android:layout_centerHorizontal='true'
12          android:layout_marginTop='115dp'
13          android:text='Launch Dialog' />
14  
15      <TextView
16          android:id='@+id/textView1'
17          android:layout_width='wrap_content'
18          android:layout_height='wrap_content'
19          android:layout_alignParentLeft='true'
20          android:layout_alignParentTop='true'
21          android:layout_marginLeft='28dp'
22          android:layout_marginTop='54dp'
23          android:text='@string/app_desc'
24          android:textAppearance='?android:attr/textAppearanceLarge' />
25       
26  </RelativeLayout>

Dialog Layout PAGE TOP

dialog_layout.xml

01  <?xml version='1.0' encoding='utf-8'?>
02  <LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
03      android:layout_width='fill_parent'
04      android:layout_height='fill_parent'
05      android:orientation='vertical'
06      android:padding='10sp' >
07  
08      <EditText
09          android:id='@+id/txt_name'
10          android:layout_width='fill_parent'
11          android:layout_height='wrap_content'
12          android:hint='@string/dialog_uname'
13          android:singleLine='true' >
14  
15          <requestFocus />
16      </EditText>
17  
18      <EditText
19          android:id='@+id/password'
20          android:layout_width='match_parent'
21          android:layout_height='wrap_content'
22          android:ems='10'
23          android:inputType='textPassword' >
24      </EditText>
25  
26      <RelativeLayout
27          android:layout_width='match_parent'
28          android:layout_height='wrap_content' >
29  
30          <Button
31              android:id='@+id/btn_login'
32              android:layout_width='120dp'
33              android:layout_height='wrap_content'
34              android:text='@string/dialog_submit' />
35  
36          <Button
37              android:id='@+id/btn_cancel'
38              android:layout_width='120dp'
39              android:layout_height='wrap_content'
40              android:layout_alignParentTop='true'
41              android:layout_marginLeft='10dp'
42              android:layout_toRightOf='@+id/btn_login'
43              android:text='@string/dialog_cancel' />
44      </RelativeLayout>
45  
46  </LinearLayout>

AndroidDialog Activity PAGE TOP

lop.png

Override both onCreateDialog(int id) and onPrepareDialog(int id, Dialog dialog) methods and add following code which will create your custom Android Dialog.

01  import android.os.Bundle;
02  import android.view.LayoutInflater;
03  import android.view.View;
04  import android.widget.Button;
05  import android.widget.EditText;
06  import android.widget.Toast;
07  import android.app.Activity;
08  import android.app.AlertDialog;
09  import android.app.Dialog;
10  
11  public class AndroidDialog extends Activity {
12  
13   final private static int DIALOG_LOGIN = 1;
14  
15   @Override
16   public void onCreate(Bundle savedInstanceState) {
17    super.onCreate(savedInstanceState);
18    setContentView(R.layout.activity_android_dialog);
19  
20    Button launch_button = (Button) findViewById(R.id.btn_launch);
21  
22    launch_button.setOnClickListener(new View.OnClickListener() {
23  
24     @Override
25     public void onClick(View v) {
26      showDialog(DIALOG_LOGIN);
27     }
28    });
29   }
30  
31   @Override
32   protected Dialog onCreateDialog(int id) {
33  
34    AlertDialog dialogDetails = null;
35  
36    switch (id) {
37    case DIALOG_LOGIN:
38     LayoutInflater inflater = LayoutInflater.from(this);
39     View dialogview = inflater.inflate(R.layout.dialog_layout, null);
40  
41     AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
42     dialogbuilder.setTitle('Login');
43     dialogbuilder.setView(dialogview);
44     dialogDetails = dialogbuilder.create();
45  
46     break;
47    }
48  
49    return dialogDetails;
50   }
51  
52   @Override
53   protected void onPrepareDialog(int id, Dialog dialog) {
54  
55    switch (id) {
56    case DIALOG_LOGIN:
57     final AlertDialog alertDialog = (AlertDialog) dialog;
58     Button loginbutton = (Button) alertDialog
59       .findViewById(R.id.btn_login);
60     Button cancelbutton = (Button) alertDialog
61       .findViewById(R.id.btn_cancel);
62     final EditText userName = (EditText) alertDialog
63       .findViewById(R.id.txt_name);
64     final EditText password = (EditText) alertDialog
65       .findViewById(R.id.password);
66  
67     loginbutton.setOnClickListener(new View.OnClickListener() {
68  
69      @Override
70      public void onClick(View v) {
71       alertDialog.dismiss();
72       Toast.makeText(
73         AndroidDialog.this,
74         'User Name : ' + userName.getText().toString()
75           + '  Password : '
76           + password.getText().toString(),
77         Toast.LENGTH_LONG).show();
78      }
79     });
80  
81     cancelbutton.setOnClickListener(new View.OnClickListener() {
82  
83      @Override
84      public void onClick(View v) {
85       alertDialog.dismiss();
86      }
87     });
88     break;
89    }
90   }
91  }

Happy coding and don’t forget to share!

Reference PAGE TOP

http://www.javacodegeeks.com/2012/10/android-dialog-android-custom-dialog.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.