1

I have AutocompleteTextView with cross symbol right side of AutocompleteTextView. When i touch cross symbol clear AutocompleteTextView. this is the code

public class MyFragment extends Fragment {
    private AutocompleteTextView autocompleteTextViewSearch;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_myhealth, container, false);
        autocompleteTextViewSearch = (AutoCompleteTextView) rootView.findViewById(R.id.autocompleteTextViewSearch);

        autocompleteTextViewSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getX() >= (autocompleteTextViewSearch.getRight() - autocompleteTextViewSearch.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        autocompleteTextViewSearch.setText("");
                        return true;
                    }
                }
                return false;
            }
        });
    }
}

this setOnTouchListener is working fine in Activity, But it is not working in Fragment. i want suggestions...

0 Answers0