Wednesday 31 December 2014

Creating Custom Dialog from Layout File


In this post, I will show how to create a custom dialog using xml layout file.
First you have to create a xml layout file. After creating layout file than you have to open activity in which you want to display dialog. Here, layout file contains a button "OK" as an example.

In the activity, declare the Dialog and than create a method as below:

private Dialog myDialog;

private void showDialog() { 


myDialog = new Dialog(this,R.style.wideDialogStyle);
myDialog.setCancelable(false);
myDialog.setCanceledOnTouchOutside(false);
myDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.setContentView(R.layout.log_in);
myDialog.show(); 
 
// Set up the Button
okBtn = (Button) myDialog.findViewById(R.id.ok_btn); okBtn.setOnClickListener(okBtnListener); 
} 

//This is the onClickListener() for OK button of the Dialog
private Button.OnClickListener okBtnListener = new Button.OnClickListener() { 
@Override 
public void onClick(View v) { 
myDialog.dismiss();


Thank you for visiting my blog.

No comments:

Post a Comment