Reputation: 21
This is my piece of code for exit message in dialog box.I want to customize it. i.e. change it's colour,text width,font size of text etc,background etc.Please can someone tell me how to do it?
public void addListenerOnButton2()
{
exit = (Button) findViewById(R.id.button2);
exit.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setTitle("EXIT?");
alertDialogBuilder
.setMessage("Do you want to quit?")
.setCancelable(false)
.setPositiveButton("YES",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
MainActivity.this.finish();
}
})
.setNegativeButton("NO",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
Upvotes: 0
Views: 376
Reputation: 1001
private void showDialog(String title, String message){
dialog = new AlertDialog.Builder(context).create();
dialog.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.user_dialog, null);
TextView tvDialogTitle = (TextView) v.findViewById(R.id.tv_user_dialog_header);
tvDialogTitle.setText(title);
dialog.setCustomTitle(v);
// Setting Dialog Message
dialog.setMessage(message);
dialog.setButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
please refer above code for making custom dialog. Here, I have set the custom title, but with this reference, u can customize your own dialog according your custom layout.
Hope this can be useful for your problem.
Upvotes: 0
Reputation: 4562
Create your dialog layout as xml file, get a layoutinflator from your context, when you try to show the custom dialog, inflate the layout from xml and set it to your alertdialog object.
Upvotes: 2