Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to detect click on custom SearchView element

I m creating a custom SearchView, but so far I didn't find a (click)event or listener, if the user clicks on the searchView widget. So SearchView is close, user clicks on the search Icon (which listener?!), SearchView is opened. See the picture below. I need that event, because I want to change the UI after user opens the searchView

enter image description here

EDIT: What I tried so far is

  • setOnClickListener to SearchView Element -> but it's only triggered if the SearchView is open(2nd picture)

  • searched SearchView Api, but there isn't a listener for that action (or am I blind?)

  • tried a workaround with onWindowFocusChanged of the searchView, but due the fact that the searchView is hidden in some kind of youtube/facebook sidebar, this doesn't work

Any ideas? I attached my SearchView Code as well, but don't think the code is wrong. just no listener yet. btw: any workarounds are welcome. But it would be nice, if the SearchView is closed at the beginning! (like in the pictures above)

public class AmplifySearchView extends SearchView implements
        OnQueryTextListener, OnFocusChangeListener,
        OnCloseListener {

    private static final String DEBUG_TAG = "AmplifySeachView";

    private Context context;
    private DataBaseController datasource;
    private StationListView searchResultListView;
    private TextView noResultTextView;

    private Boolean searchStarted = false;

    public AmplifySearchView(Context context) {
        super(context);
        this.context = context;
    }

    public AmplifySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void initSearchView(StationListView view, TextView noResultTextView) {
        this.searchResultListView = view;
        this.searchResultListView.setActivityId(Constants.UI_SEARCH_RESULT_LIST);
        this.noResultTextView = noResultTextView;


        this.setOnQueryTextListener(this);
        this.setOnFocusChangeListener(this);
        this.setOnCloseListener(this);
        this.setOnCloseListener(this);

        setIconifiedByDefault(true);

        datasource = DataBaseController.getInstance(context);

        int id = this.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) findViewById(id);
        textView.setTextColor(Color.WHITE);


        resetSearchInputTextField();
    }

    private void resetSearchInputTextField() {
        setQueryHint(getResources().getString(R.string.search_hint));
    }

    @Override
    public boolean onClose() {
        Log.d(DEBUG_TAG, "onClose()");
        searchResultListView.setVisibility(View.GONE);
        noResultTextView.setVisibility(View.GONE);

        searchStarted = false;

        Intent intent = new Intent(Constants.SEARCH_ENDED);
        intent.addCategory(Constants.UI_AMPLIFY_CATEGORY);
        context.sendBroadcast(intent);

        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        Log.d(DEBUG_TAG,"onFocusChange()" + hasFocus);

    }


    @Override
    public boolean onQueryTextChange(String newText) {

        //do sth...
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + query);
        return true;
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction,
            Rect previouslyFocusedRect) {
        Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + gainFocus);
        // TODO Auto-generated method stub
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    @Override
    public void onActionViewCollapsed() {
        Log.d(DEBUG_TAG, "onActionViewCollapsed");
        super.onActionViewCollapsed();
    }

    @Override
    public void onActionViewExpanded() {
        Log.d(DEBUG_TAG, "onActionViewExpanded");
        super.onActionViewExpanded();
    }
}

btw: on Focus change don't work here, because searchview is invisible at the beginning and insert in the ui later...

like image 586
longi Avatar asked Jan 30 '13 11:01

longi


4 Answers

How about this?

public void setOnSearchClickListener (View.OnClickListener listener)

Description from android developer website says.. Sets a listener to inform when the search button is pressed. This is only relevant when the text field is not visible by default. Calling setIconified(false) can also cause this listener to be informed.

http://developer.android.com/reference/android/widget/SearchView.html#setOnSearchClickListener(android.view.View.OnClickListener)

like image 184
chuthan20 Avatar answered Nov 18 '22 20:11

chuthan20


for text field visible use:

SearchView searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your code here
        }
    });

for text field invisible use:

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            //your code here
            return false;
        }
    });
like image 45
Davi Menezes Avatar answered Nov 18 '22 21:11

Davi Menezes


 public void setOnSearchClickListener (View.OnClickListener listener)

solves the issue. actually i thought i ve been using this listener before, but the OnSearchClickListener isn't the OnClickListener in my SearchView. You have to declare that listener from the outside like

 AmplifySearchView sv = (AmplifySearchView) findViewById(...);

 sv.setOnSearchClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("CLICK","CLICK");

        }
 });
like image 8
longi Avatar answered Nov 18 '22 21:11

longi


Try this in Kotlin:

    searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }

In Java :

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }
});
like image 7
ethiopians Avatar answered Nov 18 '22 22:11

ethiopians