checkbox menu item doesn't work - java

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.!!!

Related

Can't get Android Menu to Show

I am trying to get a 3 dot menu to show in my app but can't get it to work.
I have created a file called mymenu.xml under res/menu and the contents are:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/level"
android:title="#string/Level"
app:showAsAction="never">
</item>
</menu>
This is what is in my MainActivity.java file:
#Override
public boolean onCreateOptionsMenu(Menu my_menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, my_menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.level:
levelMax = 41;
levelTextView.setText("Level:\nEasy");
mCountDownTimer.cancel();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What am I doing wrong? Any help would be appreciated. Thanks.
Change app:showAsAction="never" to app:showAsAction="always"

Target action bar item from navigation drawer

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

Android Clicks On Popup Menu Items Are Not Shown On The Popup Menu

I click an Icon on the action bar to display a popup menu, then I click a popup menu item and a toast message in my Java code shows that it changes state from false to true. The problem is that when the popup menu is opened again no items are clicked. And clicking a popup menu item always shows that it changes state from false to true.
Does anyone have a code solution for this problem?
Thank you for your help.
Java code:
#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_action, 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();
if (id == R.id.action_popup) {
View menuItemView = findViewById(R.id.action_popup);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.opt1_name) {
if (item.isChecked()){
item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{
item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
else if (id == R.id.opt2_date) {
if (item.isChecked()){item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
return true;
}
});
popupMenu.show();
}
return super.onOptionsItemSelected(item);
}
menu_action.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=".PlayerActivity">
<item android:id="#+id/action_popup" android:title="Sort Popup" android:icon="#drawable/ic_sort"
android:showAsAction="always" />
<item android:id="#+id/action_settings" android:title="Settings"
app:showAsAction="never" />
<item android:id="#+id/action_help" android:title="Help"
app:showAsAction="never" />
</menu>
popup_menu.xml
<?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/opt1_name"
android:title="Name" />
<item
android:id="#+id/opt2_date"
android:title="Date" />
</group>
</menu>
Check the error if you examine then you must have one listview in your mainlist.xml file with id as #android:id/list
<ListView
android:id="#android:id/list"
android:layout_height="wrap_content"
android:layout_height="fill_parent"/>

Set menu item text

I am having trouble setting the menu item text. I want when I start the activity to see the price of all my items in the basket. But whenever I start the shopping list activity I get below error:
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference
This is the code:
public class Shopping_list_activity extends AppCompatActivity{
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shopping_list_activity);
updateMenuTitles();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_shopping_list, menu);
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_shopping_list_saveList) {
Toast.makeText(Shopping_list_activity.this, " Clicked", Toast.LENGTH_SHORT).show();
}
// else if (id == R.id.menu_shopping_list_total){
// Toast.makeText(Shopping_list_activity.this, getPriceOfDisplayedItems() + " total", Toast.LENGTH_SHORT).show();
// }
return super.onOptionsItemSelected(item);
}
private void updateMenuTitles() {
MenuItem menuItem = menu.findItem(R.id.menu_shopping_list_total);
if (getPriceOfDisplayedItems() > 0) {
menuItem.setTitle(String.valueOf(getPriceOfDisplayedItems()));
} else {
menuItem.setTitle("Total: 0.00");
}
}
The getPriceOfDisplayedItems() displays the correct price.
And the XML file :
<?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_shopping_list_total"
android:title="#string/total"
app:showAsAction="always" />
</menu>
Can anyone help me with this problem?
I think onCreate method is called before onCreateOptions menu. That's why this.menu is null. Try moving invocation of updateMenuTitles to onCreateOptionsMenu.
You should add menu_shopping_list_saveList item in your 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"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/menu_shopping_list_total"
android:title="#string/total"
app:showAsAction="always" />
<item
android:id="#+id/menu_shopping_list_saveList"
android:title="#string/total"
app:showAsAction="always" />
</menu>

Change Menuitems when Sliding Tab

I followed this tutorial to build up a Sliding Tab application :
http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
Now my Question:
How can I change my Menu icons in my actionbar when I change to another Tab ?For example in Tab1 you can see a Search Icon and when you change to Tab2 this gets invisible but there comes a Add icon.
My code till now :
#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.add("menu_search").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("menu_contact").setIcon(R.drawable.ic_add_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
You can define menu in Fragment instead of Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_1:
// handling menu item click
break;
}
return super.onOptionsItemSelected(item);
}
Of course each Fragment will have its own menu with corresponding icons.
This is an example (menu_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item_1"
android:showAsAction="ifRoom"
android:title="#string/ic_menu_label"
android:icon="#drawable/ic_menu_icon"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>

Categories

Resources