Вопрос

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...

Это было полезно?

Решение

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)

Другие советы

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;
        }
    });
 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");

        }
 });

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
        }
    }
});

This was written with API 15. It has not been tested. Details at bottom.

public class MySearchView extends SearchView {
    private boolean mHasFocus;
    private OnOpenListener mOnOpenListener;

    public MySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnOpenListener(OnOpenListener onOpenListener) {
        mOnOpenListener = onOpenListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean onTouchEvent = super.onTouchEvent(event);

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!mHasFocus) {
                mHasFocus = true;

                if (mOnOpenListener != null) {
                    mOnOpenListener.onOpen();
                }
            }

            break;
        }

        return onTouchEvent;
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

        if (!gainFocus) {
            mHasFocus = false;
        }
    }

    public interface OnOpenListener {
        public void onOpen();
    }
}

You give it an OnOpenListener and when the View is clicked it will fire the event. When it loses focus it will reset the boolean value. I think it will also fire the OnClose aswell that is already built in. What can be a problem is the focus bit; I have had problems before using a SearchView and it will not give up it's focus. Maybe you can outside of the class assign when the View shall have focus or not. If you want more features you have to build your own.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top