You can create Android Dialog with Below code .
public class DatePickerDialogActivity extends Activity {
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
return super.onMenuItemSelected(featureId, item);
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
return super.onMenuOpened(featureId, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu);
}
public static final int DATE_PICKER_DIALOG=1;
public static final int MY_NEW_DIALOG=2;
public Calendar mCalendar;
int mYear;
int mMonth;
int mDay;
String mStringdate="04/04/2012";
String mStrdate1="2012-04-04";
public DatePickerDialog mDatePickerDialog;
public Dialog mDialog;
public Button mButton;
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case DATE_PICKER_DIALOG:
mDatePickerDialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
view.setOnClickListener(mDateSetListener);
mYear=year + 1;
mMonth=monthOfYear;
mDay=dayOfMonth;
Log.v("Log_tag", "Year is " + mYear +"month is "+ monthOfYear +"Day is "+ dayOfMonth);
}
}, mYear, mMonth, mDay);
mDatePickerDialog.setTitle(R.string.date_set_message);
return mDatePickerDialog;
case MY_NEW_DIALOG:
mSimpleDialog=new Dialog(this,R.style.CustomDialogTheme);
mSimpleDialog.setContentView(R.layout.dialog_layout);
TextView mTextView=(TextView)findViewById(R.id.msgtxt);
Button mBtnYes=(Button)mSimpleDialog.findViewById(R.id.btn_yes);
Button mBtnNo=(Button)mSimpleDialog.findViewById(R.id.btn_no);
mBtnYes.setOnClickListener(DialogButtonClickListener);
mBtnNo.setOnClickListener(DialogButtonClickListener);
mSimpleDialog.setCancelable(true);
mSimpleDialog.show();
return mSimpleDialog;
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
}
OnClickListener mDateSetListener =new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Log_tag", "Set Date is done");
}
};
OnClickListener mButtonClickListener =new OnClickListener() {
@Override
public void onClick(View v) {
if(mDatePickerOn){
showDialog(DATE_PICKER_DIALOG);
}else{
showDialog(MY_NEW_DIALOG);
}
Log.v("Log_tag", "Set Date is done");
}
};
Dialog mSimpleDialog;
ToggleButton mToggleButton;
boolean mDatePickerOn=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
// requestWindowFeature(Window.)
super.onCreate(savedInstanceState);
setContentView(R.layout.my_dialog);
mCalendar=Calendar.getInstance();
mYear=mCalendar.get(Calendar.YEAR);
mMonth=mCalendar.get(Calendar.MONTH);
mDay=mCalendar.get(Calendar.DAY_OF_MONTH);
mButton=(Button)findViewById(R.id.btn_show_calander);
mButton.setOnClickListener(mButtonClickListener);
mToggleButton=(ToggleButton)findViewById(R.id.toogleButton);
mToggleButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
SimpleDateFormat mSimpleDateFormat=new SimpleDateFormat("EEE, MMM d, ''yy");
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
try {
//dateobj1= mSimpleDateFormat.parse(mStrdate1);
Date dateObj = curFormater.parse(mStringdate);
SimpleDateFormat postFormater = new SimpleDateFormat("EEE,MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
// date2=mSimpleDateFormat.format(dateobj1);
//dateobj=mSimpleDateFormat.parse(mStringdate);
//String dateis=mSimpleDateFormat.format(dateobj);
Log.v("Log_tag", "Here we are with Date Function"+ newDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
OnCheckedChangeListener mOnCheckedChangeListener=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
mDatePickerOn=true;
}else{
mDatePickerOn=false;
}
}
};
View.OnClickListener DialogButtonClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_yes:
finish();
break;
case R.id.btn_no:
mSimpleDialog.dismiss();
break;
}
}
};
}
Here is xml Layout for Custom Dialog .
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:text="@string/hello"
android:id="@+id/msgtxt"/>
<LinearLayout
android:id="@+id/btn_linerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_horizontal"
android:layout_marginTop="50dp">
<Button
android:id="@+id/btn_yes"
android:layout_width="125dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:text="@string/btn_yes"
android:layout_marginRight="5dp" />
<Button
android:id="@+id/btn_no"
android:layout_width="125dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:text="@string/btn_no"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
Another xml layout that used in Example is below
my_dialog.xml
<?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" >
<Button
android:id="@+id/btn_show_calander"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mButtonClickListener"
android:text="@string/btn_text" />
<ToggleButton
android:id="@+id/toogleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:textOn="@string/text_toggle_on"
android:textOff="@string/text_toggle_off"
android:gravity="center"/>
</LinearLayout>
public class DatePickerDialogActivity extends Activity {
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
return super.onMenuItemSelected(featureId, item);
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
return super.onMenuOpened(featureId, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu);
}
public static final int DATE_PICKER_DIALOG=1;
public static final int MY_NEW_DIALOG=2;
public Calendar mCalendar;
int mYear;
int mMonth;
int mDay;
String mStringdate="04/04/2012";
String mStrdate1="2012-04-04";
public DatePickerDialog mDatePickerDialog;
public Dialog mDialog;
public Button mButton;
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case DATE_PICKER_DIALOG:
mDatePickerDialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
view.setOnClickListener(mDateSetListener);
mYear=year + 1;
mMonth=monthOfYear;
mDay=dayOfMonth;
Log.v("Log_tag", "Year is " + mYear +"month is "+ monthOfYear +"Day is "+ dayOfMonth);
}
}, mYear, mMonth, mDay);
mDatePickerDialog.setTitle(R.string.date_set_message);
return mDatePickerDialog;
case MY_NEW_DIALOG:
mSimpleDialog=new Dialog(this,R.style.CustomDialogTheme);
mSimpleDialog.setContentView(R.layout.dialog_layout);
TextView mTextView=(TextView)findViewById(R.id.msgtxt);
Button mBtnYes=(Button)mSimpleDialog.findViewById(R.id.btn_yes);
Button mBtnNo=(Button)mSimpleDialog.findViewById(R.id.btn_no);
mBtnYes.setOnClickListener(DialogButtonClickListener);
mBtnNo.setOnClickListener(DialogButtonClickListener);
mSimpleDialog.setCancelable(true);
mSimpleDialog.show();
return mSimpleDialog;
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
}
OnClickListener mDateSetListener =new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Log_tag", "Set Date is done");
}
};
OnClickListener mButtonClickListener =new OnClickListener() {
@Override
public void onClick(View v) {
if(mDatePickerOn){
showDialog(DATE_PICKER_DIALOG);
}else{
showDialog(MY_NEW_DIALOG);
}
Log.v("Log_tag", "Set Date is done");
}
};
Dialog mSimpleDialog;
ToggleButton mToggleButton;
boolean mDatePickerOn=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
// requestWindowFeature(Window.)
super.onCreate(savedInstanceState);
setContentView(R.layout.my_dialog);
mCalendar=Calendar.getInstance();
mYear=mCalendar.get(Calendar.YEAR);
mMonth=mCalendar.get(Calendar.MONTH);
mDay=mCalendar.get(Calendar.DAY_OF_MONTH);
mButton=(Button)findViewById(R.id.btn_show_calander);
mButton.setOnClickListener(mButtonClickListener);
mToggleButton=(ToggleButton)findViewById(R.id.toogleButton);
mToggleButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
SimpleDateFormat mSimpleDateFormat=new SimpleDateFormat("EEE, MMM d, ''yy");
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
try {
//dateobj1= mSimpleDateFormat.parse(mStrdate1);
Date dateObj = curFormater.parse(mStringdate);
SimpleDateFormat postFormater = new SimpleDateFormat("EEE,MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
// date2=mSimpleDateFormat.format(dateobj1);
//dateobj=mSimpleDateFormat.parse(mStringdate);
//String dateis=mSimpleDateFormat.format(dateobj);
Log.v("Log_tag", "Here we are with Date Function"+ newDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
OnCheckedChangeListener mOnCheckedChangeListener=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
mDatePickerOn=true;
}else{
mDatePickerOn=false;
}
}
};
View.OnClickListener DialogButtonClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_yes:
finish();
break;
case R.id.btn_no:
mSimpleDialog.dismiss();
break;
}
}
};
}
Here is xml Layout for Custom Dialog .
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:text="@string/hello"
android:id="@+id/msgtxt"/>
<LinearLayout
android:id="@+id/btn_linerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_horizontal"
android:layout_marginTop="50dp">
<Button
android:id="@+id/btn_yes"
android:layout_width="125dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:text="@string/btn_yes"
android:layout_marginRight="5dp" />
<Button
android:id="@+id/btn_no"
android:layout_width="125dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:text="@string/btn_no"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
Another xml layout that used in Example is below
my_dialog.xml
<?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" >
<Button
android:id="@+id/btn_show_calander"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mButtonClickListener"
android:text="@string/btn_text" />
<ToggleButton
android:id="@+id/toogleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:textOn="@string/text_toggle_on"
android:textOff="@string/text_toggle_off"
android:gravity="center"/>
</LinearLayout>