I am currently working on an app that i implemented a toolbar and a search functionality on the toolbar with a searchview to filter list views which is working fine.
I also have a navigation drawer whereby i also want one of its items to do exactly what the search functionality is doing on the toolbar. How do implement an intent to lead me to SearchViewon toolbar when searchitem is clicked from navigation drawer.
menu_main for toolbar
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/viewMode"
android:orderInCategory="100"
app:showAsAction="never"
android:icon="#drawable/ic_view_compact_black_24dp"
android:title="#string/view" />
<item
// my search functionality
android:id="#+id/search"
android:orderInCategory="100"
app:actionViewClass= "android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_search"
android:title="#string/search" />
</menu>
activity_navdrawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_notes"
android:icon="#drawable/ic_note_add_black_24dp"
android:title="#string/first_layout" />
<item
// search functionality which should lead me to searchview on toolbar
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/third_layout" />
</group>
<item android:title="#string/app_communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_share_black_24dp"
android:title="#string/app_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_email_black_24dp"
android:title="#string/app_send" />
</menu>
</item>
</menu>
from main activity
public boolean onNavigationItemSelected(MenuItem item) {
// Handling navigation view item clicks here.
int id = item.getItemId();
if(id == R.id.nav_notes) {
Intent noteIntent = new Intent(this, MainActivity.class);
startActivity(noteIntent);
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.search);
searchMenuItem.expandActionView();
// what should i implement here?
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public boolean onCreateOptionsMenu(`final` Menu menu) {
mOptionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
ArrayAdapter<Note> arrayAdapter = (ArrayAdapter<Note>) mListNotes.getAdapter();
arrayAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
Try the following code below:
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
mOptionsMenu = menu;
...
}
...
public boolean onNavigationItemSelected(MenuItem item) {
...
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.menu_search);
searchMenuItem.expandActionView();
}
...
Add <category android:name="android.intent.category.DEFAULT" /> to your search activity in manifest. Then do this:
else if(id == R.id.nav_search) {
startActivity(new Intent(Intent.ACTION_SEARCH));
}
If this works, courtesy - Start Activity Using Custom Action
Related
Hi I wouldike to know when I click on my bottomnavigationView how to display my search toolbar ?
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_loope:
toolbar_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:id="#+id/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Bottom_nav_view.xml
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_loope"
android:icon="#drawable/ic_loope"
android:title="#string/title_loope"
/>
<item
android:id="#+id/navigation_take_photo"
android:icon="#drawable/ic_take_photo"
android:title="#string/title_take_photo" />
<item
android:id="#+id/navigation_public"
android:icon="#drawable/ic_public"
android:title="#string/title_public" />
</menu>
I can't actually use getMenuInflater().inflate(R.menu.toolbar_menu, menu) I don't know how to implement it for doing the same result when i click on my loope (R.id.navigation_loope)
EDIT : Bottom_nav_view it's for my bottom bar and toolbar_menu for my topbar. If I click on the item navigation_loop, I wouldike to show the search bar on the top.
i want to just add searchview in my app.but i dont want to search any thing just i want the query entered by the user.
so far i tried this code but when i run my app it crashes.
Update:
I tried this one but eventhough my app crashes.
main_menu.xml code
<?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="Search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SerachView"
app:showAsAction="always"
/>
MainActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
SearchView searchView=(SearchView)findViewById(R.id.search);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Toast.makeText(this,query,Toast.LENGTH_LONG).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Can Anyone help me to solve this problem please.
ThankYou.
Lets not use SearchView directly, in your menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
and add this simple code to your java :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Not implemented here
return false;
default:
break;
}
searchView.setOnQueryTextListener(queryTextListener);
return super.onOptionsItemSelected(item);
}
Main_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/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:title="Search" />
</menu>
onCreateOptionMenu method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_view_menu_item, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchViewAndroidActionBar.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
I want to added to menu item but a user can choose what he want to see or hide , but I don't know how I can do it . I did this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="NARZĘDZIA">
<menu>
<item
android:id="#+id/show_point"
android:icon="#drawable/point"
android:title="Pokaż wybraną pozycję obiektu" />
<item
android:id="#+id/sync"
android:icon="#drawable/sync"
android:title="Pojedyńcze odpytanie" />
<item
android:id="#+id/show_chooce"
android:icon="#drawable/show_chooce"
android:title="Pokaż wybrane obiekty" />
<item
android:id="#+id/my_location"
android:icon="#drawable/position2"
android:title="Moja lokalizacja" />
</menu>
</item>
<item android:title="APLIKACJA">
<menu>
<item
android:id="#+id/settigs"
android:icon="#drawable/set"
android:title="Ustawienia aplikacji" />
<item
android:id="#+id/about"
android:icon="#drawable/about"
android:title="O programie" />
</menu>
</item>
</menu>
Now I want to create a Activity in which user can choose what it will be show , but I don't have idea how I can hide this item on menu
you can change java file
//menu intialation
#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);
menu.clear();
MenuItem item =
menu.add(Menu.FIRST, R.id.dashboard_nodifycation, 3, "NODIFICATION");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.FIRST, R.id.System_log, 4, "LOGOUT");
dbHelper = new Database_for_GCM_data(Activity_Drawer_Dashboard.this);
int unread_count = Integer.parseInt(dbHelper.getUnreadCount());
// calling function for nodification inflater
item.setIcon(buildCounterDrawable(unread_count, R.drawable.white_nodification, getApplicationContext()));
return true;
}
public class MyActivity extends AppCompatActivity {
private Menu mToolbarMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//init menu here
mToolbarMenu = menu;
getMenuInflater().inflate(R.menu.toolbar, menu);
setToolbarMenuItemVisibilties();
return true;
}
private void setToolbarMenuItemVisibilties() {
final MenuItem menuItem1 = mToolbarMenu.findItem(R.id.item1);
final MenuItem menuItem2 = mToolbarMenu.findItem(R.id.item2);
final MenuItem menuItem3 = mToolbarMenu.findItem(R.id.item3);
final MenuItem menuItem4 = mToolbarMenu.findItem(R.id.item4);
//Set visibilities
menuItem1.setVisible(true);
menuItem2.setVisible(true);
menuItem3.setVisible(false);
menuItem4.setVisible(false);
}
}
you can change visibilities any time in activity, but you have to inflate it first
I have this in my menu_check.xml But when I click on the check nothing happens so the Toast never show... what's the problem? Thanks a lot
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_selecciontodo"
android:title="#string/check"
android:checkable="true"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="always" />
</menu>
and this in my java class
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_check, menu);
checkBox = (CheckBox) menu.findItem(R.id.action_selecciontodo).getActionView();
checkBox.setText("Select all");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_selecciontodo:
CheckBox checkBox= (CheckBox) item.getActionView();
if (checkBox.isChecked())
Toast.makeText(getApplicationContext(), "You selected all", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
No need to app:actionViewClass="android.widget.CheckBox" just do like this
in oncreate options menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
and in onOptionsItemSelected write this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_bookmark:
if (item.isChecked()) {
item.setChecked(false);
Toast.makeText(MainActivity.this, "Un Checked", Toast.LENGTH_SHORT).show();
} else {
item.setChecked(true);
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
and in your menu.xml in menu folder declare menu like this
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/menu_bookmark"
android:checkable="true"
android:title="Bookmark"/>
so when you check uncheck the menu item it show toast and show checkbox checked or unchecked
Try this,
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.stackoverflow.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_check"
android:title="YOUR_TITLE"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
</menu>
MainActivity.java
#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);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_check) {
item.setChecked(!item.isChecked());
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
Log.i("cheeck status" , "" + item.isChecked());
return true;
}
return super.onOptionsItemSelected(item);
}
this should work.
Happy coding.!!!
I am trying to have a search action on my action bar. I have set an icon and have showAsAction set to always but it is still always displayed in the overflow menu. How can I make it display on the action bar itself? Thanks!
Activity
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) {
// Inflate the menu; this adds items to the action bar if it is present.
setTitle("Add");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings",
Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search",
Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always"
android:title="#string/title_search"/>
</menu>
Either remove
android:actionViewClass="android.support.v7.widget.SearchView"
or delete your case statement for action_search in onOptionsItemSelected. Doing either of those will display the icon.
If you want to use SearchView the callback is handled differently than onOptionsItemSelected.
Documentation here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
Just change app:showAsAction instead of android:showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"`enter code here`
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
app:showAsAction="always"
android:title="#string/title_search"/>
</menu>