Examples of layout styles for custom dialog in Android development

Source: Internet
Author: User
Tags gettext string format stub

On the internet to find a lot of questions about dialog custom style, there are a lot of people write more complex, need to change style or something, or custom dialog make very complex, I finally found a way to achieve.





Here is my dialog layout XML file:





[Mw_shl_code=java,true]<?xml version= "1.0" encoding= "Utf-8"?>


<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


Android:background= "@android: Color/transparent"


android:layout_margin= "50DP" >





<relativelayout


Android:id= "@+id/rl_dialog_content"


Android:layout_width= "Fill_parent"


android:layout_height= "200DP"


android:background= "@drawable/ALERTDIALOG_BG" >


<textview


Android:id= "@+id/dialog_text"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:text= "Call Customer service now: ARJINMC"


android:layout_margintop= "50DP"


android:layout_marginleft= "30DP"


android:layout_marginright= "30DP"


Android:maxlines= "5"


android:gravity= "Center"


/>





<linearlayout


Android:id= "@+id/ll_buttons"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:orientation= "Horizontal"


Android:layout_alignparentbottom= "true"


android:layout_margin= "30DP" >








<button


Android:id= "@+id/dialog_cancel"


android:text= "@string/alert_cancel"


android:background= "@drawable/btn_long_white"


style= "@style/dialog_button"


android:layout_weight= "1"


Android:textcolor= "@color/tabs_font"


/>


<button


Android:id= "@+id/dialog_ok"


android:text= "@string/alert_ok"


android:background= "@drawable/btn_long_red"


style= "@style/dialog_button"


android:layout_weight= "1"


android:layout_marginleft= "5DP"/>





</LinearLayout>


</RelativeLayout>


<imagebutton


Android:id= "@+id/dialog_close"


Android:layout_width= "Wrap_content"


android:layout_height= "Wrap_content"


android:background= "@drawable/alertdialog_close"


Android:layout_alignparentright= "true"


Android:layout_alignparenttop= "true"


android:layout_marginleft= "20DP"


/>








</RelativeLayout>


[/mw_shl_code]








This is what you need to write in your code:


[Convert mw_shl_code=java,true]//layout file to view object


Layoutinflater inflaterdl = Layoutinflater.from (this);


Relativelayout layout = (relativelayout) inflaterdl.inflate (r.layout.layout_dialog, NULL);





dialog box


Final Dialog Dialog = new Alertdialog.builder (settingactivity.this). Create ();


Dialog.show ();


Dialog.getwindow (). Setcontentview (layout);








Cancel button


Button Btncancel = (button) Layout.findviewbyid (R.id.dialog_cancel);


Btncancel.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


Toast.maketext (Getapplicationcontext (), "Cancel", Toast.length_short). Show ();


}


});








OK button


Button Btnok = (button) Layout.findviewbyid (R.ID.DIALOG_OK);


Btnok.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


Toast.maketext (Getapplicationcontext (), "OK", Toast.length_short). Show ();


}


});








Close button


ImageButton btnclose = (ImageButton) Layout.findviewbyid (r.id.dialog_close);


Btnclose.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


Dialog.dismiss ();


}


}); [/mw_shl_code]








Very easy!. Do it yourself.











Customizing the style of dialog and comparing dates





<style name= "Mydialogtheme" parent= "Android:style/theme.dialog" >


<item name= "Android:windownotitle" >true</item>


</style>








1, Layout Main.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" >





<textview


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:text= "@string/hello"/>


<button


Android:id= "@+id/btn"


android:layout_height= "Wrap_content"


Android:layout_width= "Fill_parent"


android:text= "Press the button to try"/>





</LinearLayout>





2, Ly_dialogcontent





<?xml version= "1.0" encoding= "Utf-8"?>


<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"


Android:layout_width= "220DP"


android:layout_height= "Wrap_content"


android:orientation= "Vertical"


android:gravity= "Center"


android:layout_gravity= "Center"


android:background= "#88E0EEEE" >


<linearlayout


android:layout_height= "Wrap_content"


Android:layout_width= "Fill_parent"


android:orientation= "Vertical" >


<textview


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


Android:textcolor= "@android: Color/white"


Android:textsize= "20SP"


android:text= "Query"


android:gravity= "Center"


android:layout_gravity= "Center"


android:padding= "10DP"/>


<linearlayout


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


Android:padding= "10DP"


android:gravity= "Center" >


<textview





android:layout_height= "Wrap_content"


Android:layout_width= "Wrap_content"


Android:text= "Start time:"/>


<edittext


Android:id= "@+id/begintime"


android:layout_height= "Wrap_content"


Android:layout_width= "Wrap_content"


Android:singleline= "true"/>


</LinearLayout>


<linearlayout


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


Android:padding= "10DP"


android:gravity= "Center" >


<textview





android:layout_height= "Wrap_content"


Android:layout_width= "Wrap_content"


Android:text= "Start time:"/>


<edittext


Android:id= "@+id/endtime"


android:layout_height= "Wrap_content"


Android:layout_width= "Wrap_content"


Android:singleline= "true"/>


</LinearLayout>


<linearlayout


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:gravity= "Center" >


<button


Android:id= "@+id/search"


android:layout_height= "Wrap_content"


Android:layout_width= "Wrap_content"


Android:layout_weight= "1.0"


android:text= "Query"/>


<button


Android:id= "@+id/cancel"


android:layout_height= "Wrap_content"


Android:layout_width= "Wrap_content"


Android:layout_weight= "1.0"


android:text= "Cancel"/>





</LinearLayout>





</LinearLayout>











</RelativeLayout>





3, Mainactivity





Package com.ct.dialog;





Import java.text.ParseException;


Import Java.text.SimpleDateFormat;


Import Java.util.Calendar;


Import Java.util.Date;





Import android.app.Activity;


Import Android.app.DatePickerDialog;


Import Android.app.DatePickerDialog.OnDateSetListener;


Import Android.app.Dialog;


Import Android.app.TimePickerDialog;


Import Android.app.TimePickerDialog.OnTimeSetListener;


Import Android.os.Bundle;


Import Android.text.InputType;


Import Android.view.LayoutInflater;


Import Android.view.View;


Import Android.view.View.OnClickListener;


Import Android.widget.Button;


Import Android.widget.DatePicker;


Import Android.widget.EditText;


Import Android.widget.TimePicker;


Import Android.widget.Toast;





public class Mainactivity extends activity {


/** called the activity is a. */


Private Button btnbn;


Private Dialog Dlg;


Private Layoutinflater Minflater;


private Calendar Calendar;





@Override


public void OnCreate (Bundle savedinstancestate) {


Super.oncreate (savedinstancestate);


Setcontentview (R.layout.main);





BTNBN = (Button) Findviewbyid (R.ID.BTN);


Init ();


Btnbn.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


TODO auto-generated Method Stub


Dlg.show ();


}


});


}





private void init () {


Calendar = Calendar.getinstance ();


Dlg = new Dialog (mainactivity.this, r.style.mydialogtheme);


Minflater = Layoutinflater.from (mainactivity.this);


Dlg.setcancelable (TRUE);


Dlg.setcanceledontouchoutside (TRUE);





View view = Minflater.inflate (r.layout.ly_dialogcontent, NULL);


Final EditText begin = (edittext) View.findviewbyid (r.id.begintime);


Final EditText end = (edittext) View.findviewbyid (r.id.endtime);


Button sbtn = (button) View.findviewbyid (R.id.search);


Button clebt = (button) View.findviewbyid (R.id.cancel);


Begin.setinputtype (Inputtype.type_null);


End.setinputtype (Inputtype.type_null);


Dlg.setcontentview (view);


Cancel


Clebt.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


TODO auto-generated Method Stub


Dlg.dismiss ();


}


});





Inquire


Sbtn.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


TODO auto-generated Method Stub


String begintime = Begin.gettext (). toString ();


String endtime = End.gettext (). toString ();


if (Begintime.equals ("") | | Endtime.equals ("")) {





Toast.maketext (Mainactivity.this, "Neither can be empty", Toast.length_long). Show ();





}else if (!islarge (BeginTime, Endtime)) {


Toast.maketext (Mainactivity.this, "End time cannot be smaller than start time", Toast.length_long). Show ();


} else {


Dlg.dismiss ();}


}


});





Start time


Begin.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


TODO auto-generated Method Stub


Final String second = "00";


Datepickerdialog DPG = new Datepickerdialog (Mainactivity.this,


New Ondatesetlistener () {





@Override


public void Ondateset (DatePicker view, int year, int monthofyear,


int DayOfMonth) {


TODO auto-generated Method Stub


Begin.settext (year+ "-" +


Format (++monthofyear) + "-" +


Format (dayofmonth));





Timepickerdialog TPG = new Timepickerdialog (Mainactivity.this,


New Ontimesetlistener () {





@Override


public void Ontimeset (


Timepicker arg0, int hourofday,


int minute) {


TODO auto-generated Method Stub


Begin.settext (Begin.gettext (). ToString ()


+ " "


+ Format (hourofday)


+ ":"


+ Format (minute)


+ ":" + second);


}},calendar.get (Calendar.hour_of_day),


Calendar.get (Calendar.minute), true);


Tpg.show ();


}


}, Calendar.get (Calendar.year),


Calendar.get (Calendar.month),


Calendar.get (Calendar.day_of_month));





Dpg.show ();


}


});


End time


End.setonclicklistener (New Onclicklistener () {





@Override


public void OnClick (View v) {


TODO auto-generated Method Stub


Final String second = "00";


Datepickerdialog DPG = new Datepickerdialog (Mainactivity.this,


New Ondatesetlistener () {





@Override


public void Ondateset (DatePicker view, int year, int monthofyear,


int DayOfMonth) {


TODO auto-generated Method Stub


End.settext (year+ "-" +


Format (++monthofyear) + "-" +


Format (dayofmonth));





Timepickerdialog TPG = new Timepickerdialog (Mainactivity.this,


New Ontimesetlistener () {





@Override


public void Ontimeset (


Timepicker arg0, int hourofday,


int minute) {


TODO auto-generated Method Stub


End.settext (Begin.gettext (). ToString ()


+ " "


+ Format (hourofday)


+ ":"


+ Format (minute)


+ ":" + second);


}},calendar.get (Calendar.hour_of_day),


Calendar.get (Calendar.minute), true);


Tpg.show ();


}


}, Calendar.get (Calendar.year),


Calendar.get (Calendar.month),


Calendar.get (Calendar.day_of_month));





Dpg.show ();


}


});








}





/**


* Compare the size of two times


* @throws ParseException


*


* */


public static Boolean IsLarge (String begintime,string endtime) {


Boolean flag = false;


try {


SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");


Date begin = Sdf.parse (BeginTime);


Date edn = Sdf.parse (Endtime);


if (Edn.gettime ()-begin.gettime () >0) {


Flag = true;


}else {


Flag = false;


}





catch (ParseException e) {


Todo:handle exception


E.printstacktrace ();


}


return flag;





}








/**


* Add 0 to two digits before one digit


*


* @param value


* @return


*/


Private String format (int value) {


String s = string.valueof (value);


if (s.length () = = 1) {


s = "0" + s;


}


return s;


}


}





(In F:\java\z custom dialog+ Click Popup time to adjust that \mydialogtheme)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.