3

I have some problem when I try to display my SearchView.

What I need:

1.Make SearchView always opened without any close buttons.

2.Display another icon after Searchview

My code:

menu_fragment_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=".CardBaseFragment">

    <item android:id="@+id/action_search"
        android:title="Search"
        style="@style/SearchViewStyle"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        />
    <item android:id="@+id/сhange_country"
        android:title="@string/add_new_card"
        app:showAsAction="always"
        android:actionBarWidgetTheme="@style/myStyle"
        android:icon="@drawable/ic_change_country">

    </item>

</menu>

style:

<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
    <!-- Gets rid of the "underline" in the text -->
    <item name="queryBackground">@null</item>
    <!-- Gets rid of the search icon when the SearchView is expanded -->
    <item name="searchHintIcon">@null</item>
    <!-- The hint text that appears when the user has not typed anything -->
    <item name="queryHint">Test</item>

</style>

Now, when program starts , everything is ok, but still I have back button.

When I change SearchView app:showAsAction="ifRoom|collapseActionView" to "always" , SearchView overlap my second item, how can I remove back button correctly, without creating new layout for toolbar?

  ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); 

Didn't help.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
Ololoking
  • 1,577
  • 4
  • 22
  • 37
  • Create your own custom layout for it and don't use searchview if you don't want any functionality of it. – Vivek Mishra Feb 15 '16 at 12:43
  • @VivekMishra i need searchview funtionality, and i need it to be always opened, i just want disable searchview collapse – Ololoking Feb 15 '16 at 12:46
  • This issue is common on Android 23 and higher https://stackoverflow.com/questions/56410048/searchview-in-expanded-mode-doesnt-hide-all-action-bar-icons-starting-from-mars – user924 Jun 01 '19 at 20:38

2 Answers2

0

Try doing this, after setContentView call below method to hide back button completely

public void HideHomeButton(){
     ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(false); // disable the button
     ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); // remove the left caret
     ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false); 
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
-1

to make SearchView always opened change: app:showAsAction="ifRoom|collapseActionView" to app:showAsAction="always"

to display another icon after Searchview if you want it always visible choose app:showAsAction="always" to it

Apostolos
  • 10,033
  • 5
  • 24
  • 39
AJS
  • 57
  • 5
  • He doesn't ask about making searchview always opened (expanded). Check this question to understand his problem https://stackoverflow.com/questions/56410048/searchview-in-expanded-mode-doesnt-hide-all-action-bar-icons-starting-from-mars – user924 Jun 01 '19 at 20:37
  • The worst answer ever – Neuron Sep 02 '19 at 07:16