This program will show how to create a custom dialog which contains 2 edit text boxes to take log in and pass word credentials from user.
First Activity
package com.techpalle.b15_dialogassign;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button b;
EditText e1, e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
e1 = (EditText) findViewById(R.id.editText1);
e2 = (EditText) findViewById(R.id.editText2);
b.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(1);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog d = null;
switch(id){
case 1:
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Enter user credentials");
ab.setView(getLayoutInflater().
inflate(R.layout.dialog, null));
ab.setPositiveButton("Done", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Dialog d = (Dialog) dialog;
EditText x = (EditText) d.findViewById(R.id.editText1);
EditText y = (EditText) d.findViewById(R.id.editText2);
e1.setText(x.getText().toString());
e2.setText(y.getText().toString());
}
});
d = ab.create();
break;
}
return d;
}
}
xml layout file for First Activity
File name: activity_main.xml
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:ems="10" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="61dp"
android:layout_marginTop="15dp"
android:text="Button" />
custom xml layout file for Dialog to show log in and password.
File name: dialog.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
More programs on dialogs:
android Dialogs(alert dialog, datepicker dialog, time picker dialog, progress dialog) tutorial
Tags: android, tutorial, examples, Custom dialog, login dialog, user input dialog
No comments:
Post a Comment