0

Please can anyone help me, well i'm working on application that: when someone is calling it shows you a layout containing some Infos, inside that layout there's a close a button that when i click it, it should hide the layout or destroy the app, my probleme is the layout shows but the button is unclickable, it's just like an image of the button. this is my code:

@Override 
public void onCreate() { 
    super.onCreate(); 
    toastLayout = (ViewGroup)layoutInflater.inflate(R.layout.toast, null);
    defaultPopupHorizontalGravity = getResources().getInteger(R.integer.default_popup_horizontal_gravity);
    defaultPopupVerticalGravity = getResources().getInteger(R.integer.default_popup_vertical_gravity);
    //defaultPopupMap = Boolean.parseBoolean(getResources().getString(R.string.default_popup_map));
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE//FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,//FLAG_NOT_TOUCHABLE
            PixelFormat.TRANSLUCENT
    );
    Button X = (Button) toastLayout.getChildAt(0);
    toastLayout.addView(X);
    X.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("Clicked----><<<<<<<");
        }
    });

    X.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            System.out.println("Touched =----- > ");
            return false;
        }
    });
    windowManager.addView(toastLayout, params);
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • After layout inflater its just like view in android and no need to use layout params check answer here:http://stackoverflow.com/a/16079425 – Dhaval Parmar Mar 31 '15 at 10:01

1 Answers1

0

You are using the FLAG_NOT_TOUCH_MODAL and FLAG_NOT_FOCUSABLE which disables touches for your layout as it is created with these parameters

You also do not need to add the button X again as it is already a child of your toast layout already (or otherwise getChildAt would not work as expected and may produce a casting exception if the child at that position is not a button) so remove the line :

toastLayout.addView(X);
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • With What should i replace FLAG_NOT_TOUCH_MODAL and FLAG_NOT_FOCUSABLE ? – Abdellah Felahi Mar 31 '15 at 10:52
  • If there isn't a specific behavior you require just leave 0 instead. As mentiond in the comments your layout definition should include the needed attributes in the xml so there is no need to set it again – giorashc Mar 31 '15 at 10:57