Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide a loading spinner when you click outside of it in Android

I am using a loading spinner animation with a set of items. If you click outside of it, then it should disappear. Does anybody know how to do this?

Spinner

I have tried this. It's working with EditText. But it's not working for Spinner

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View view = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (view instanceof EditText||view instanceof Spinner) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        if (event.getAction() == MotionEvent.ACTION_UP 
 && (x < w.getLeft() || x >= w.getRight() 
 || y < w.getTop() || y > w.getBottom()) ) { 
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }
    }
 return ret;
}

Thanks in Advance.

like image 912
Ram kiran Pachigolla Avatar asked Nov 08 '12 09:11

Ram kiran Pachigolla


1 Answers

What I personaly did was to create a custom AlertDialog with setSingleChoiceItems() to do the same thing. Then I used setCanceledOnTouchOutside().

like image 88
shkschneider Avatar answered Nov 13 '22 12:11

shkschneider