Android, checkable menu item - java

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);
}

Related

Button in Action Menu in Title bar doesn't Work

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();
}
});

searchview oncloselistener not working

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 to go another screen via menu

I created a menu (Fack Call, Fake SMS, Statistic & About).
I want this text with an icon.
I tried, but it didn't work.
I also want to move to another screen when I click Fack Call, Fake SMS, Statistic text.
How can I do that?
Here is my code:
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressWarnings("deprecation")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.call:
Toast.makeText(MainActivity.this, "call", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(this, FakeCall.class);
startActivity(intent);
return true;
case R.id.sms:
Toast.makeText(MainActivity.this, "sms", Toast.LENGTH_SHORT).show();
return true;
case R.id.statistic:
Toast.makeText(MainActivity.this, "statistic", Toast.LENGTH_SHORT)
.show();
return true;
case R.id.about:
Toast.makeText(MainActivity.this, "about", Toast.LENGTH_SHORT)
.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
FackCall.java
public class FakeCall extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fakecall);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/call"
android:icon="#drawable/fakecall"
android:title="#string/call"/>
<item
android:id="#+id/sms"
android:icon="#drawable/fakesms"
android:title="#string/sms"/>
<item
android:id="#+id/statistic"
android:icon="#drawable/statistic"
android:title="#string/statistic"/>
<item
android:id="#+id/about"
android:icon="#drawable/about"
android:title="#string/about"/>
</menu>
#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);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}'

How to add a drop-down menu on any given actionbar icon

I have been trying to create a simple drop down menu when I click my action bar icon and have certain selections link to particular activities.
Something like this:
I've tried to follow this but no luck.
Android ActionBar ActionProvider submenu
How to add submenu items to ActionBar action in code?
Currently I have this as my Java code, but the drop down menu appears under my appname instead of the overflow icon:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
SpinnerAdapter adapter =
ArrayAdapter.createFromResource(this, R.array.actions,
android.R.layout.simple_spinner_dropdown_item);
// Callback
OnNavigationListener callback = new OnNavigationListener() {
String[] items = getResources().getStringArray(R.array.actions); // List items from res
#Override
public boolean onNavigationItemSelected(int position, long id) {
// Do stuff when navigation item is selected
Log.d("NavigationItemSelected", items[position]); // Debug
return true;
}
};
You can use Preference Activity for this. So Just Create a xml folder in res folder. and create a settings.xml
<?xml version="1.0" encoding="utf-8"?>
<com.xxx.MyPreferenceCategory
android:title="#string/about_application" >
<Preference
android:key="shareApp"
android:summary="#string/share_app_summary"
android:title="#string/share_app" />
<Preference
android:key="rateApp"
android:summary="#string/rate_app_summary"
android:title="#string/rate_app" />
<Preference
android:key="version"
android:summary="#string/version_app_summary"
android:title="#string/version_app" />
</com.xxx.MyPreferenceCategory>
<com.xxx.MyPreferenceCategory
android:title="#string/about_developer" >
<Preference
android:key="contactUs"
android:summary="#string/contact_us_summary"
android:title="#string/contact_app" />
<Preference
android:key="aboutUs"
android:title="#string/about_app" />
<Preference
android:key="terms"
android:title="#string/about_app_terms" />
<Preference
android:key="policy"
android:title="#string/about_app_policy" />
</com.xxx.MyPreferenceCategory>
After that create an MyPreferenceCategory.java.
public class MyPreferenceCategory extends PreferenceCategory{
public MyPreferenceCategory(Context context) {
super(context);
}
private TextView categoryTitle;
public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected View onCreateView(ViewGroup parent) {
categoryTitle = (TextView) super.onCreateView(parent);
return categoryTitle;
}
}
After that create a MenuOptions.java file
public class MenuOptions extends SherlockPreferenceActivity {
Handler disconnectHandler;
Runnable disconnectCallback;
Preference shareApp, rateApp, contactUs, aboutUs, termsCondition,
privacyPolicy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
findViewById(android.R.id.list).setBackgroundColor(Color.WHITE);
disconnectHandler = new Handler() {
public void handleMessage(Message msg) {
}
};
disconnectCallback = new Runnable() {
#Override
public void run() {
}
};
shareApp = (Preference) findPreference("shareApp");
shareApp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
//do your stuff
return false;
}
});
rateApp = (Preference) findPreference("rateApp");
rateApp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
//do your stuff
return false;
}
});
contactUs = (Preference) findPreference("contactUs");
contactUs.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
//do your stuff
return false;
}
});
aboutUs = (Preference) findPreference("aboutUs");
aboutUs.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
});
termsCondition = (Preference) findPreference("terms");
termsCondition
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
});
privacyPolicy = (Preference) findPreference("policy");
privacyPolicy
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
});
}
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
After that where you are where you are using action bar setting button where put this code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Intent intent = new Intent(getBaseContext(), MenuOptions.class);
startActivity(intent);
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}

Cannot create actionbar menu item using actionbarsherlock

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);
}

Categories