Tuesday 4 December 2012

BackPress click event with Confirmation dialogbox

Put this method in your activity.


@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Handle the back button
        if (keyCode == KeyEvent.KEYCODE_BACK) {
     
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popuplayoutReject = inflater.inflate(R.layout.popup_reject_request, (ViewGroup) findViewById(R.id.popup_element));
        final PopupWindow popUpReject = new PopupWindow(popuplayoutReject, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT, false);
        ImageButton btnPopupReject_Ok, btnPopUpReject_Cancel;
        TextView txtRequestRejectMessage = (TextView) popuplayoutReject.findViewById(R.id.errorMessage);
        txtRequestRejectMessage.setText(getResources().getString(R.string.AddEntryNotSaved));
        btnPopupReject_Ok = (ImageButton) popuplayoutReject.findViewById(R.id.btnPopup_Yes);
        btnPopupReject_Ok.setImageResource(R.drawable.yes_button);
        
        btnPopupReject_Ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });

        btnPopUpReject_Cancel = (ImageButton) popuplayoutReject.findViewById(R.id.btnPopUp_No);
        btnPopUpReject_Cancel.setImageResource(R.drawable.no_button);
        btnPopUpReject_Cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popUpReject.dismiss();
            }
        });
        popUpReject.showAtLocation(popuplayoutReject, Gravity.CENTER, 0, 0);
        return true;
        }
        return super.onKeyDown(keyCode, event);
    }

No comments: