I want the android searchview in toolbar open without click on search icon. Here is the XML code
<item
android:id="@+id/action_search"
android:title="@string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
And Java code in MainActivity.
@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 menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setQueryHint("Type something...");
int searchPlateId = searchView.getContext()
.getResources()
.getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate != null) {
searchPlate.setBackgroundColor(Color.DKGRAY);
int searchTextId = searchPlate.getContext()
.getResources()
.getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText != null) {
searchText.setTextColor(Color.WHITE);
searchText.setHintTextColor(Color.WHITE);
}
}
return true;
}
This works and the output screen below ...
So, after I click Search Icon it output screen below ...
What I want is directly the second screen!
Try to add this line to your code after initializing the SearchView
:
searchView.setFocusable(true);
Update:
searchView.setIconified(false) worked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With