Skip to content

Instantly share code, notes, and snippets.

@ckdevrel
Last active August 29, 2015 14:25
Show Gist options
  • Save ckdevrel/5b57c1fbdda37ff976a8 to your computer and use it in GitHub Desktop.
Save ckdevrel/5b57c1fbdda37ff976a8 to your computer and use it in GitHub Desktop.
MaterialAppCompatSearchView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/blue" />
</LinearLayout>
public class SearchActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
toolbar = (Toolbar)findViewById(R.id.toolbar);
if (toolbar != null) {
toolbar.setTitle("SearchView");
setSupportActionBar(toolbar);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
final SearchView searchView = (SearchView) MenuItemCompat
.getActionView(menu.findItem(R.id.search));
SearchManager searchManager = (SearchManager) this.getSystemService(this.SEARCH_SERVICE);
searchView.setSearchableInfo (searchManager.getSearchableInfo (this.getComponentName()));
//changing edittext color
EditText searchEdit = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
searchEdit.setTextColor(Color.BLACK);
searchEdit.setHint("Search...");
searchEdit.setHintTextColor(Color.WHITE);
searchEdit.setCursorVisible(true);
searchEdit.setFocusable(true);
searchEdit.setFocusableInTouchMode(true);
View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("#00000000"));
searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId ()) {
case R.id.search:
return true;
case android.R.id.home:
return true;
default:
return super.onOptionsItemSelected (item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment