0

I am stuck on a part in my app. My app's minimum sdk version is v2.3. I am working on action bar. When I click on search icon on the action bar, it expands, but not fully. Two icons on the right side are still visible. And when I click on the home up button,it closes the SearchView, but doesn't call onOptionsItemSelected method.

Note : Home up button calls the onOptionsItemSelected method when clicked at first(when searchview is not expanded), but does not call onOptionsItemSelected when searchview is expanded.

I am using support library as I am working on very old version. Please help me.

My code is as follows:

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item
    android:id="@+id/search_action"
    app:showAsAction="always|collapseActionView"
    android:title="@string/search_action"
    android:icon="@drawable/search_icon"
    app:actionViewClass="android.support.v7.widget.SearchView"
    />

<item
    android:id="@+id/shopping_cart"
    app:actionLayout="@layout/badge_layout"
    android:title="@string/shopping_cart"
    app:showAsAction="always"
    >
</item>

<item
    android:id="@+id/user_menu"
    android:icon="@drawable/overflow_icon"
    app:showAsAction="always"
    android:title="user">
</item>
</menu>

Styles.xml

<resources  xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <!--<item name="android:background"  tools:ignore="NewApi">@color/action_bar_color</item>-->
    <item name="background">@color/action_bar_color</item>
    <item name="logo">@drawable/nt_logo</item>
    <item name="displayOptions">useLogo|showHome</item>
</style>

MainActivity.java

package com.example.stickyheader.stickyheader;

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem searchItem = menu.findItem(R.id.search_action);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint("Search here");
    searchView.setIconifiedByDefault(true);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();



    return super.onOptionsItemSelected(item);
}
}
Newinjava
  • 972
  • 1
  • 12
  • 19

1 Answers1

0

I have sorted this issue in another way. I created a custom SearchView and attached two listeners i.e expandListener and collapseListener. On expand, I made all the menu items invisible and vice versa.

Newinjava
  • 972
  • 1
  • 12
  • 19
  • I believe you don't need to hide icon on the right side. You have to hide home (or menu) button on the left. Check this https://stackoverflow.com/questions/56410048/searchview-in-expanded-mode-doesnt-hide-all-action-bar-icons-starting-from-mars – user924 Jun 01 '19 at 20:50
  • Starting from Android 23 and higher it won't hide icons on the right – user924 Jun 01 '19 at 20:51
  • `setOnActionExpandListener` didn't work for me... How did you get it working? I had to use `setOnSearchClickListener` and `setOnCloseListener` as alternative: https://stackoverflow.com/a/56410389/7767664 – user924 Jun 01 '19 at 21:12
  • found out, you set `collapseActionView` which makes it always expanded and I don't want it – user924 Jun 01 '19 at 21:29