I'm having trouble with using menu item on action bar using slidingmenu library with actionbarsherlock.
Anyone can help?
Here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
this is the menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/print"
android:title="#string/printItem"
android:icon="#drawable/ic_print"
android:showAsAction="always" />
<item
android:id="#+id/share"
android:title="#string/shareItem"
android:icon="#drawable/ic_action_share"
android:showAsAction="always"
android:actionProviderClass="android.widget.ShareActionProvider" />
My logcat says that I encountered runtime error at
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Don't really understand your question.
This code works in my app.
in MainActivity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
break;
case R.id.print:
//your code
break;
case R.id.share:
//your code
break;
default:
return false;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
in Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_fragment,menu);
super.onCreateOptionsMenu(menu, inflater);
}
Related
When I click the button in the title bar it doesn't do any thing.
I use item in menu and I linked it to main activity class but it doesn't work.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return true;
}
#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);
// Inflate switch
notification_badge = (TextView) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.notification_badge);
ImageButton notification_open = (ImageButton) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.Notificatio_open);
ImageButton addFollwes = (ImageButton) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.my_action_search);
notification_badge.setVisibility(View.GONE);
addFollwes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchActivity.class));
}
});
notification_open.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, com.account.Notification.class));
// notification_badge.setVisibility(View.GONE);
}
});
Switch mSwitchNightMode = (Switch) menu.findItem(R.id.item_switch)
.getActionView().findViewById(R.id.Switchedbtn);
// Get state from preferences
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_action:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
case R.id.my_action_search:
startActivity(new Intent(MainActivity.this, SearchActivity.class));
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
It's hard to tell without looking at the XML menu layout code. I am guessing that something went wrong such that findViewById for the menu did not register. You don't have to explicitly identify the menu items, but actually the entire menu. Then we can query which item was clicked via a switch statement.
Below I provide a simple way of handling menu
Layout
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_01"
android:title="1"/>
<item
android:id="#+id/menu_item_02"
android:title="2"/>
<item
android:id="#+id/menu_item_03"
android:title="3"/>
</menu>
Java
import androidx.appcompat.widget.PopupMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
private ImageView menuButton;
private PopupMenu mainMenu;
private MenuInflater menuInflater;
menuButton = findViewById(R.id.menu_button);
menuButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
mainMenu = new PopupMenu(MainActivity.this, v);
menuInflater = mainMenu.getMenuInflater();
mainMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId())
{
case R.id.menu_item_01:
// perform action
return true;
case R.id.menu_item_02:
// perform action
return true;
case R.id.menu_item_03:
// perform action
return true;
default:
return false;
}
}
});
menuInflater.inflate(R.menu.main_menu, mainMenu.getMenu());
mainMenu.show();
}
});
I need to have oncloselistener for my searchview in a menu, but oncloselistener is not working. I am not sure how to set actionexpand to my searchview.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQueryHint("جستجو...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
I had the same issue last week! To my surprise, you don't set the listener to the SearchView, you actually set it to the menu item that expands it.
This is exactly what I had to do in onCreateOptionsMenu:
item = menu.findItem(R.id.friend_fragment_search_icon);
searchView = (SearchView) MenuItemCompat.getActionView(item);
// When using the support library, the setOnActionExpandListener() method is
// static and accepts the MenuItem object as an argument
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//the searchview has been closed
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
mBtnFindFriends.hide();
return true; // Return true to expand action view
}
});
The friend_fragment_search_icon is the menu icon that opens the SearchView.
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dingding="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/friend_fragment_search_icon"
android:title="#string/search_title"
android:icon="#drawable/ic_search_white_48dp"
dingding:showAsAction="always|collapseActionView"
dingding:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
How can I do something like this ?
When the ActionBar searchview is closed, the button is enabled.
When the ActionBar searchview is opened, the button is disabled.
Are there listeners that I can use ? Or am I forced to use something like a while loop or something else ?
For the case when SearchView is opened, I know I can use menuItem.setOnMenuItemClickListener, but for the closing I don't know...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.search);
//...
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
findViewById(R.id.button).setEnabled(false);
return false;
}
});
return true;
}
To know when the SearchView is opening or closing you can use OnActionExpandListener:
when is closing onMenuItemActionCollapse will be called.
when is opening onMenuItemActionExpand will be called.
For example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.search);
//...
MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
findViewById(R.id.button).setEnabled(false);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
findViewById(R.id.button).setEnabled(true);
return true;
}
});
return true;
}
I have a menu in my ActionBar, which is checkable (in XML), but when I try, in java, to check it on press on this item, the item stay unchecked (but the other things related to this action is done)
My XML menu :
<?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">
[...]
<item
android:id="#+id/menu_switch_full_original"
android:title="#string/menu_switch_full_original"
android:checkable="true"/>
</menu>
And the java code :
public boolean onOptionsItemSelected(MenuItem item) {
if (mEntriesIds != null) {
Activity activity = getActivity();
switch (item.getItemId()) {
[...]
case R.id.menu_switch_full_original: {
item.setChecked(true);
[...]
}
activity.invalidateOptionsMenu();
}
return true;
}
What am I missing ?
Try this...
private boolean isChecked = false;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.menu_switch_full_original);
checkable.setChecked(isChecked);
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.a, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_switch_full_original:
isChecked = !item.isChecked();
item.setChecked(isChecked);
// your other functionality
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
I am trying to populate search and listview from text file and am having issues with this code can someone please help me This is the error message in the log cat
java.lang.NullPointerException at searchviewsqlite.MyActivity.onCreateOptionsMenu"
This is my code:
public class MyActivity extends ActionBarActivity {
Menu m;
final Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
DatabaseTable db=new DatabaseTable(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));//Error here
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
}
This is my menu code below
<item
android:id="#+id/search"
android:title="Search"
android:icon="#drawable/search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"/>