0

The following Screenshot represents my Applications Layout

LAYOUT WITH BUTTTONS

Is there a way for the complete Layout View (Parent) to disappear completely if I click anywhere on the screen excluding the Buttons by Code.

6
  • create custom Alert dialog with these buttons .......
    – sushildlh
    Sep 10, 2016 at 7:44
  • do you want to disappear the entire activity or just buttons
    – Ajay Sivan
    Sep 10, 2016 at 7:46
  • As sushildlh said try using custom alert dialog with this layout. developer.android.com/guide/topics/ui/dialogs.html.
    – Ajay Sivan
    Sep 10, 2016 at 7:48
  • @sushildlh how it can be done
    – Sandhu
    Sep 10, 2016 at 8:02
  • @Ajay entire layout to be disappear when clicked some where else than this layout, similar as in case of alert dialog
    – Sandhu
    Sep 10, 2016 at 8:06

2 Answers 2

1

To achieve what you have described, you need to use Dialog in android.

    final Dialog dialog = new Dialog(context_of_ur_class);
    //if not title required
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //add your layout in this
    dialog.setContentView(R.layout.your_layout);
    dialog.setCanceledOnTouchOutside(true);
    dialog.show();

    //example for button 1
    Button one = (Button) dialog.findViewById(R.id.one);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do what ever
        }
    });
5
  • android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    – Sandhu
    Sep 10, 2016 at 8:29
  • final Dialog dialog = new Dialog(getApplicationContext()); //if not title required dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //add your layout in this dialog.setContentView(R.layout.activity_select_app); dialog.setCanceledOnTouchOutside(true); dialog.show();
    – Sandhu
    Sep 10, 2016 at 8:30
  • use your_class_name.this, not getApplicationContext();
    – user6798817
    Sep 10, 2016 at 8:32
  • it is inside a service class not activity class
    – Sandhu
    Sep 10, 2016 at 8:40
  • Let us continue this discussion in chat.
    – user6798817
    Sep 10, 2016 at 8:45
0

First declare a global private AlertDialog mAlertDialog;

private void buttonsDialog() {
        AlertDialog.Builder mAlertBuilder = new AlertDialog.Builder(getActivity());
        mAlertBuilder.setCancelable(true);//you can change this
        mAlertDialog = mAlertBuilder.create();
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogLayout = inflater.inflate(R.layout.inform_dialog, null);
        mAlertDialog.setView(dialogLayout);

        Button button1 = (Button) dialogLayout.findViewById(R.id.alert_positive);
        Button button2 = (Button) dialogLayout.findViewById(R.id.alert_negative);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do your stuff
            }
        })
        mAlertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

Call buttonsDialog() in onCreate. And mAlertDialog.show to display the dialog.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.