I implemented a search bar in my toolbar and it currently looks like this:
But I want to change the background to white with a shadow so that it looks more like this (taken from Material Design guidelines):
How can I do this?
Here is my implementation of the SearchView:
menu_main.xml:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_search_white_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
</menu>
MainActivity.java:
public abstract class MainActivity extends AppCompatActivity {
protected Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchActivity.class)));
searchView.setQueryHint("Search");
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
setMenuItemVisibility(menu, searchItem, false);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
return true;
}
});
return true;
}
private void setMenuItemVisibility(Menu menu, MenuItem searchItem, boolean isVisible) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
Log.d(TAG, item.toString());
if (item != searchItem) {
item.setVisible(isVisible);
}
}
}
}
How can I style the SearchView to change the background color?
Related
i m using searchview for search functionality my problem is that when i click on search icon keyboard gets opened but searchview does not get focus below is my code
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/send_invitation"
android:icon="#drawable/send_icon_invite_participant"
android:orderInCategory="93"
android:title="#string/invitation"
app:showAsAction="always">
</item>
<item
android:id="#+id/search"
android:orderInCategory="100"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/search_icn"
android:title="#string/search"
app:showAsAction="always"/>
</menu>
below is code from method
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.invite_participant, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager = (SearchManager) activity.getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.colorAccent));
searchEditText.setHintTextColor(getResources().getColor(R.color.colorAccent));
searchItem.expandActionView();
ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIcon.setImageResource(R.drawable.search_icn);
ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCloseIcon.setImageResource(R.drawable.reject_icon_notification);
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(activity.getComponentName()));
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
if (adapter != null) {
adapter.fliter(newText);
}
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
}
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
i m not able to find why it does not get focused please help me in finding where i have done mistake any help is appreciated.
You can add these lines for focus with searchview.
searchView.setFocusable(true);
searchView.requestFocus();
searchView.requestFocusFromTouch();
If it'll not work then First expand search view then request for focus.
You can achieve it either by adding below code in your Activity's onCreate method.
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or by adding this line in your AndroidManifest file
android:windowSoftInputMode="stateHidden"
Edit:
Try something like this
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
I have searchView on actionbar and I here is codes for it
public class container extends AppCompatActivity {
private ListView drawerList;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle toggle;
private TextView textContainer, titleContainer;
private ImageView imageContainer;
private drawerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
toolbar = (Toolbar) findViewById(R.id.mainToolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xffffffff);
titleContainer = (TextView) findViewById(R.id.containerTitle);
textContainer = (TextView) findViewById(R.id.textContainer);
imageContainer = (ImageView) findViewById(R.id.imageContainer);
titleContainer.setText(R.string.UtrujjTitle);
textContainer.setText(R.string.UtrujjContent);
imageContainer.setImageResource(R.drawable.utrujj);
final Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
drawerLayout = (DrawerLayout) findViewById(R.id.mainDrawer);
drawerList = (ListView) findViewById(R.id.drawerList);
final SearchView searchView = (SearchView)findViewById(R.id.searchable);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
Spannable spannable = new SpannableString(newText);
spannable.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 100, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textContainer.setText(spannable);
}
return false;
}
});
and in the end I added these overrides
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.searchable:
return false;
default:
break;
}
return super.onOptionsItemSelected(item);
}
search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/setting"
android:icon="#drawable/ic_setting"
app:showAsAction="ifRoom"
android:title="#string/setting"/>
<item android:id="#+id/searchable"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/search"/>
</menu>
when activity start I get this error in logcat
Unable to start activity ComponentInfo{com.example.tina.tibbenabvi/com.example.tina.tibbenabvi.container}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference
====================================================
This view It's a menu item so you should initialize onCreateOptionsMenu().
Example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
Spannable spannable = new SpannableString(newText);
spannable.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 100, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textContainer.setText(spannable);
}
return false;
}
});
return true;
}
EDIT
Manifest file:
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Create res/xml/searchable.xml file:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="search..." />
Create a Searchable Activity
public class SearchResultsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
...
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
...
}
And add it in the manifest file:
<activity android:name=".SearchResultsActivity" ... >
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
</activity>
Info from:
http://developer.android.com/intl/es/training/search/setup.html
I am trying to add the search view to the app bar in Android. Followed by this tutorial:http://developer.android.com/training/search/setup.html#add-sv
but when i run the program, it is only a icon could be clicked, the search view did not appear, but the tutorial says it should be appear. Can anyone help me?
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/abc_search_hint"
android:icon="#drawable/ic_search_white_48dp"
app:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
MainActivity
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return true;
}
here is the screenshot
XML :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/ic_search_white"
android:textColor="#color/white"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
final SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String queryText) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
if (mSearchTerm == null && newFilter == null) {
return true;
}
if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
return true;
}
mSearchTerm = newFilter;
mSearchQueryChanged = true;
searchText(newText); //handle this
return true;
}
});
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
return true;
}
};
MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
return super.onCreateOptionsMenu(menu);
}
Does anyone know how to hide the back button in AppCompat v21 searchview? (outlined by green line)
I've searched a lot but couldn't find anything useful.
menu_main.xml:
<item android:id="#+id/search"
android:title="#string/search_title"
app:showAsAction="always|collapseActionView"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
android:orderInCategory="300"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item android:id="#+id/action_home"
android:title="Home"
android:icon="#drawable/v_home"
app:showAsAction="always"
android:orderInCategory="180"/>
<item android:id="#+id/action_favorites"
android:title="Favorites"
android:icon="#drawable/v_favorites"
app:showAsAction="always" />
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
firstMenu = menu;
searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setActivated(true);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menuItemsVisibility(false);
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
menuItemsVisibility(true);
return false;
}
});
return true;
}
#Override
public void onBackPressed() {
menuItemsVisibility(true);
super.onBackPressed();
}
// setting visibility of menu items on search
private void menuItemsVisibility(boolean visibility) {
MenuItem homeItem = firstMenu.findItem(R.id.action_home);
MenuItem favoriteItem = firstMenu.findItem(R.id.action_favorites);
MenuItem otItem = firstMenu.findItem(R.id.action_ot);
MenuItem ntItem = firstMenu.findItem(R.id.action_nt);
homeItem.setVisible(visibility);
favoriteItem.setVisible(visibility);
otItem.setVisible(visibility);
ntItem.setVisible(visibility);
}
Note: the behavior showAsAction:Always and using methods menuItemsVisibility() to adjust the visibility of toolbar items is intentional.
Another Note: MainActivity extends ActionBarActivity and it also implements implements ObservableScrollViewCallbacks from ObservableScrollView Library.
changed app:showAsAction="always|collapseActionView" to app:showAsAction="always"
Use the method:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
in order to remove the home button from the action bar.
It's not perfectly safe method because back button (navigation up) doesn't have id. But if you are using AppCompat with Toolbar, you can use this code to find it. It should be the first in the layout.
int count = this.getToolbar().getChildCount();
for(int i = 0; i < count; ++i) {
View v = this.getToolbar().getChildAt(i);
if(v instanceof ImageButton) {
return (ImageButton)v;
}
}
Call this method inside onPrepareOptionsMenu if you want to change drawable of that button.
I have an activity that shows search in title bar perfectly, but right now i want to get the text entered in search in action bar to a string. Please help !
Her is my menu/xml
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
/>
</menu>
Her is the code in onCreate
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Have you used searchView? You can use following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
if (null != searchView) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
// This is your adapter that will be filtered
return true;
}
public boolean onQueryTextSubmit(String query) {
// **Here you can get the value "query" which is entered in the search box.**
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
If you are using a Toolbar you can do this:
MenuItem menuItem = getToolbar().getMenu().findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Log.i("well", " this worked");
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.i("well", " this worked");
return false;
}
});