Follow this link https://stackoverflow.com/questions/21585326/implementing-searchview-in-action-bar
Another link https://stackoverflow.com/questions/34825104/add-search-icon-on-action-bar-android
Action Bar Tutorial link https://www.vogella.com/tutorials/AndroidActionBar/article.html
Task 1: only SearchView in action bar
---------------------------------------------------------
1) create res/menu/menu_search.xml
Another link https://stackoverflow.com/questions/34825104/add-search-icon-on-action-bar-android
Action Bar Tutorial link https://www.vogella.com/tutorials/AndroidActionBar/article.html
Task 1: only SearchView in action bar
---------------------------------------------------------
1) create res/menu/menu_search.xml
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"> <item
android:id="@+id/action_search"
android:title="Search"
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.SearchView" /> </menu>
2) MainActivity.java
private Menu menu;
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); this.menu = menu; SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView(); search.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Overridepublic boolean onQueryTextSubmit(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show();return true; } @Overridepublic boolean onQueryTextChange(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show(); return true; } }); return true; }Task 2: SearchView and Add Photo icon in action bar
---------------------------------------------------------------------------
1) create res/menu/menu_search.xml<menu xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@+id/action_search"android:title="search"app:showAsAction="ifRoom"app:actionViewClass="android.widget.SearchView" /> <itemandroid:id="@+id/action_add_photo"android:title="add photo"android:icon="@drawable/ic_action_add_photo"app:showAsAction="always"> </item></menu>
2) MainActivity.java
private Menu menu;public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); this.menu = menu; SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView(); search.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Overridepublic boolean onQueryTextSubmit(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show();return true; } @Overridepublic boolean onQueryTextChange(String query) { //Toast.makeText(this, query, Toast.LENGTH_SHORT).show(); return true; } }); return true; }@Override
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: //Toast.makeText(this, "search selected", Toast.LENGTH_SHORT).show(); break; case R.id.action_add_photo: //Toast.makeText(this, "add photo selected", Toast.LENGTH_SHORT).show(); break; default: break; } return true; }
No comments:
Post a Comment