0

I would like to ask if I can make an icon in the title of a dialog box clickable. I have use the following lines to set the title "New Contact" in the left side of the dialog box and an icon in the right side. So, can this icon(x5) become clickable in some way? I would like it to function like a close button.

         dialog.requestWindowFeature(Window.FEATURE_RIGHT_ICON);
         dialog.setTitle("New Contact");
         dialog.setContentView(R.layout.pop_up_new_contact);//loads the layout we have already create pop_up_new_contact.xml
         dialog.setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON, R.drawable.x5);//adds an icon in the left of the Title.
         dialog.setCancelable(true);
Ajeett
  • 814
  • 2
  • 7
  • 18
anna
  • 1
  • 1
  • 1

3 Answers3

2

I Found the solution! This is what I've done, in case someone will need it.

@Override
protected Dialog onCreateDialog(int id)  {//creates the dialog

final Dialog dialog = new Dialog(this);

dialog.requestWindowFeature( Window.FEATURE_CUSTOM_TITLE );//adds in the dialog box a frame for the custom title

@Override
protected Dialog onCreateDialog(int id)  {//creates the dialog
    final Dialog dialog = new Dialog(this);

    dialog.requestWindowFeature( Window.FEATURE_CUSTOM_TITLE );//adds in the dialog box a frame for the custom title

    //dialog.setTitle("New Contact");//sets the title of the dialog box

    dialog.setContentView(R.layout.pop_up_new_contact);//loads the layout we have already create pop_up_new_contact.xml
    dialog.getWindow().setFeatureInt( Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_dialog_box);//sets our custom title(custom_title_dialog_box.xml) as the current title of the dialog box. The custom_title_dialog_box.xml contains a TextView with id: title_new_contact and an ImageView with id: x1_button.

    dialog.setCancelable(true);//close the dialog box when the back key is pushed.

    ImageView x1_button = (ImageView)dialog.findViewById(R.id.x1_button);

    x1_button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View View3) {
         dialog.dismiss();
        }
    });

    return dialog;
}

Maybe it's not the best solution but at least it works!

koopajah
  • 23,792
  • 9
  • 78
  • 104
anna
  • 745
  • 2
  • 9
  • 30
  • I'm using `AlertDialog` inside `DialogFragment`, and compiler throw me `android.util.AndroidRuntimeException: You cannot combine custom titles with other title features` – murt Aug 25 '17 at 11:49
  • Actually you may use now `new AlertDialog.Builder(...).setCustomTitle(R.layout.custom_title)` https://stackoverflow.com/a/9430855/2163045 – murt Aug 25 '17 at 12:23
0

Its best that you create a custom dialog. But its bad UX design to use it as a close button as Android users are used to the back back doing the same action.

Ravi Vyas
  • 12,212
  • 6
  • 31
  • 47
0

It does occur to me that this is not exactly what you asked for, however if your looking for an alliterative to the back button. This will work.

dialog.setButton(getText(R.string.cancel), new DialogInterface.OnClickListener()
{
   public void onClick(DialogInterface dialog, int whichButton) { }
});
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
Ashterothi
  • 3,282
  • 1
  • 21
  • 35