1

I have an actionBar with a searchView icon. I click on searchView icon and softInputMode keyboard appears and my ListView appears for searching. However, when you go to close searchView, the searchView closes but I can't get the ListView to also close when searchView closes.

Here's my code for ListView in activity_maps.xml

<ListView
    android:id="@+id/list"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#ffffff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_editor_absoluteX="0dp"
    />

MapsActivity.java

    ArrayAdapter<String> adapter; 


    @Override
   public boolean onCreateOptionsMenu(final Menu menu){

    final ListView lv = (ListView) findViewById(R.id.list);

    ArrayList<String> arrayList = new ArrayList<>();                      

      arrayList.addAll(Arrays.asList(getResources().getStringArray(R.array.array_states_trial)));


    adapter = new ArrayAdapter<>(MapsActivity.this, android.R.layout.simple_list_item_1, arrayList);

    lv.setAdapter(adapter);

    lv.setVisibility(View.INVISIBLE);

So initially when MapsActivity.java loads I don't want the ListView to show right away.

    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            lv.setVisibility(View.VISIBLE);

        }
    });

I want the listView to show when searchView is clicked open and keyboard pops up.

    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {

               lv.setVisibility(View.INVISIBLE);

            return false;
        }
    });

In the above code, I want the listView to close, disappear, be invisible, but it doesn't.

btw I'm referring to the "Back" button in the actionBar that comes with closing searchView.

When searchView and keyboard closes, I want the ListView to also close but for some reason the code isn't recognized and listView never closes.

How do I close ListView?

iBEK
  • 622
  • 2
  • 8
  • 27

2 Answers2

1

What you can simply do is override this method in your activity:

@Override
public void onBackPressed() {
    if(lv.getVisibility()==View.VISIBLE){
      lv.setVisibility(View.INVISIBLE);
      return;
    }
    super.onBackPressed();
}

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • 1
    This works when the Android Back button is pressed. But I want to make it that when you click searchView and click on the back button that comes with app:showAsAction="ifRoom|collapseActionView" in menu_main.xml, then the listView closes as the searchView closes. – iBEK Dec 05 '17 at 20:07
  • 1
    Do you know a code that will get the searchView's back button to close the listView instead of just Android's Back button? Currently searchView's back button just closes the searchView but I also want it to close the listView at the same time. – iBEK Dec 06 '17 at 21:55
0

According to Android reference, try to return true if you want to override the default behavior of clearing the text field and dismissing it, false otherwise.

See this link: https://developer.android.com/reference/android/widget/SearchView.OnCloseListener.html

  searchView.setOnCloseListener(new SearchView.OnCloseListener() {
    @Override
    public boolean onClose() {

           lv.setVisibility(View.INVISIBLE);

        return true;//Should work.
    }
});
ozanonurtek
  • 306
  • 5
  • 18