Toolbar is showing on left of SearchView in android - android

I want to show Back button but distorted ToolBar is coming on left as in the following image.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
TextView tv = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
tv.setTextColor(getResources().getColor(android.R.color.white));
tv.setHintTextColor(getResources().getColor(android.R.color.white));
tv.setHint(getString(R.string.action_search));
ImageView imgCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
imgCloseIcon.setImageResource(R.drawable.cross_btn);
searchView.setOnQueryTextListener(this);
}
How to show Back button and hide distorted ToolBar ??

I have used the below code and its working fine for me,
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(queryTextListener);

Related

How to make Action Bar SearchView text to contain search icon

I'd like to have small search icon in text hint like that:
but I get it like so:
I am using:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.work, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}

Android toolbar SearchView is automatically opening keyboard

I am having a weird problem. In my MainActivity toolbar I have an action search and this is how I use it:
#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);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(this, SearchableActivity.class)));
searchView.setIconified(false);
return true;
}
but the problem here is that when my app starts the keyboard pops up before I even click the search button in the toolbar. I tried to force the keyboard to hide in the manifest I added this to my MainActivity:
android:windowSoftInputMode="stateAlwaysHidden"
I even tried this:
android:windowSoftInputMode="stateHidden"
but they keyboard still pops up. I don't have any EditText or anything I only have an image and a FAB in my MainActivity.
1) android:windowSoftInputMode="adjustPan|stateHidden"
2) getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
3) mSearchView.clearFocus();

how to set an expanding animation to a searchview

I want to set an expanding animation to a searchview and I try to apply the method that I found:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) item.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
final int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
searchBar.setLayoutTransition(new LayoutTransition());
return true;
}
However, the variable searchBar is always null and it always crashes at the line
searchBar.setLayoutTransition(new LayoutTransition());
I don't know what is causing the error. Can someboby help me out? Thanks!
I had the same problem. It turns out that you get null in your searchBar if you use android.support.v7.widget.SearchView
I changed it to android.widget.SearchView (recommended if supporting api > 11). Now I get the needed linear layout and animation works fine.
Edit
If you need a support version, you have to use proper id: LinearLayout searchBar = (LinearLayout) mFilterInput.findViewById(android.support.v7.appcompat.R.id.search_bar);

SearchView does not iconify

In my app I am using an android.support.v7.widget.SearchView (from support v7 library) to provide search from action bar.
I setup this view in MainActivity, with the following code:
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SupportMenuItem searchMenuItem = (SupportMenuItem) menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setQueryRefinementEnabled(true);
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// OnQueryTextListener body
...
});
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
Then in a Fragment i need to ensure that the SearchView is collapsed:
MyFragment.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.clearFocus(); // Try to prevent SearchView to steal focus. No luck!
searchView.setIconified(true);
}
#Override
public void onResume() {
super.onResume();
setHasOptionsMenu(true);
// other stuff
}
The problem is that not only the SearchView does not collapse (I tried also with MenuItemCompat.collapseActionView(searchMenuItem) with no success: it returns always false), but also the SearchView "steals" focus at every fragment transaction (e.g. replace one fragment with another), so when the "new" fragment is displayed, the SearchView has focus, so it shows its suggestions.
I also noticed that if I do not call setHasOptionsMenu then the problem of "stealing focus" does not occur.
Is there something I am doing wrong?

SearchView is occupying entire actionBar Space

In my application i am trying to add an searchView in actionbar. But while clicking on the search icon searchView is expanding to the entire actionbar while clicking the key down it is coming to normalView.
While clicking on the search icon. It is coming like this..
But I want a behavior like this..
options_menu.xml
<item
android:id="#+id/search"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="ifRoom|collapseActionView"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"/>
onCreateOptions() method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
mSearchView.setBaselineAligned(false);
return super.onCreateOptionsMenu(menu);
}
please help me..Thanks in advance...
Add this line in your onCreateOptionsMenu()
searchView.setIconifiedByDefault(false);
Any particular reason to write the property twice in menu.xml ?
I just commented the code like this...Now it is working properly...
case R.id.search:
//onSearchRequested();
return true;

Categories

Resources