1)main.xml:
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<Button
android:id="@+id/showDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:text="Show
Dialog" />
</RelativeLayout>
2)Custom Dialog xml:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your
Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter
Password"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
/>
</LinearLayout>
</LinearLayout>
3)Activity:
package com.venkool.customdialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CustomDialogActivity extends Activity {
/** Called when the activity is first created. */
Dialog customDialog;
Button btnShowDialog, btnSave, btnCancel;
EditText edtUsername, edtPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowDialog = (Button) findViewById(R.id.showDialog);
btnShowDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
customDialog = new Dialog(CustomDialogActivity.this);
customDialog.setTitle("Custom Dialog");
customDialog.setContentView(R.layout.custom_dialog);
customDialog.show();
edtUsername = (EditText) customDialog
.findViewById(R.id.editText1);
edtPassword = (EditText) customDialog
.findViewById(R.id.editText2);
btnSave = (Button) customDialog.findViewById(R.id.button1);
btnCancel = (Button) customDialog.findViewById(R.id.button2);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Username = edtUsername.getText().toString();
String Password = edtPassword.getText().toString();
Toast.makeText(CustomDialogActivity.this,
Username + "-" + Password, 3000).show();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(CustomDialogActivity.this, "Cancel",
3000).show();
}
});
}
});
}
}
No comments:
Post a Comment