Popup menu checkbox won't - java

I have a popup menu with two checkboxs but when I press either of them the menu just disappears and nothing changes in their state. I've looked around and found things that work for other people but they don't work for me
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.isChecked())
{
item.setChecked(false);
}
else
{
item.setChecked(true);
}
switch(item.getItemId())
{
case R.id.lockscreen:
if(item.isChecked()) item.setChecked(!item.isChecked());
break;
case R.id.notif:
if(item.isChecked()) item.setChecked(!item.isChecked());
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
This the XML for the menu
<group android:checkableBehavior="all">
<item android:id="#+id/lockscreen"
android:title="Lockscreen"
android:checked="false"
android:checkable="true"/>
<item android:id="#+id/notif"
android:title="Notification"
android:checked="false"
android:checkable="true"/>
</group>

if(item.isChecked()) {
item.setChecked(false);
}
else {
item.setChecked(true);
}
These lines seem to instantly uncheck the item if it was checked beforeā€¦
What are you trying to do in this if-else construct?
Just comment it out and let the other if-statements do all the work (those in the cases).

Related

Bottomnavigationbar cant setcheckable to false but true works fine

I have two items, if i alternate between them it will uncheck the other item. But if i click once on a item i setChecked(true); but when i click again i want it to setChecked(false); but that doesnt uncheck the item. ive tried setCheckable(false); and that unchecks the item but then i cant check it again even if i write setCheckable(true) before i setChecked(true).
So the code works for one alteration then since its always checked after that the if statement doesnt work anymore. I can add more code if needed, but feels like im missing some small part.
This is the code
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.page_1:
item.setChecked(true);
break;
case R.id.page_2:
if(item.isChecked()) {
item.setChecked(false);
brushComponent_layout.setVisibility(View.INVISIBLE);
}
else {
item.setChecked(true);
brushComponent_layout.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return false;
}
});
I did a workaround that gave me the same functionality im looking for, still very strange.
instead of checking the if its checkable or checked and unchecking and that not working. I looked at the visability of the layout and just left the item checked all the time. Visually its a bit ugly but it works which is more important.
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.page_1:
item.setChecked(true);
break;
case R.id.page_2:
if(brushComponent_layout.getVisibility() == View.VISIBLE) {
brushComponent_layout.setVisibility(View.INVISIBLE);
}
else {
item.setChecked(true);
brushComponent_layout.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return true;
}
});

Implement onClick on submenu to start new activity

I'm building an app which has menu item in toolbar, i have created submenu for one menu i.e whose id is 'cloud'. I would like to implement onclick on each submenu when click will open different activity.
Here is the menu.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">
<item
android:id="#+id/action_refresh"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_loop_black_24dp"></item>
<item
android:id="#+id/notes"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_event_note_black_24dp"></item>
<item
android:id="#+id/cloud"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_cloud_upload_blackt_24dp">
<menu>
<group
android:checkableBehavior="single">
<item android:id="#+id/imageee"
android:title="Image Upload"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/pdfff"
android:title="Pdf Upload"
android:orderInCategory="100"
app:showAsAction="never"/>
</group>
</menu>
</item>
</menu>
Here is the Mainactivity file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
mymenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_refresh:
Intent intent = new Intent(MainActivity.this, UpdateService.class);
startService(intent);
}
return true;
case R.id.notes:{
Intent activity_weather = new Intent(this,Physics.class);
startActivity(activity_weather);
}
return true;
case R.id.cloud:{
}
return true;
}
return super.onOptionsItemSelected(item);
}
I looked for answer and tried on my own but I have no idea how to achieve this.
Any help is appreciated.
Thank you in advance.
I have a solution for you:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
// TODO:
toast("refresh");
return true;
case R.id.notes:
// TODO:
toast("notes");
return true;
case R.id.imageee:
// TODO: Start your image upload
toast("imageee");
return true;
case R.id.pdfff:
// TODO: Start your PDF upload
toast("pdfff");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void toast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
I write toast method to show a toast when users click on an item on menu or sub menu. Please remove //TODO: line and add your code for each case there.
I don't think you want to have
android:checkableBehavior="single"
set on your submenu. This behaviour is only useful if you just want to capture the choice (or choices) in the menu item, but not if you actually want to react (ie: do something) when the user selects a submenu item.
Based on your question, it sounds like you want to launch another Activity when the user selects one of these submenu items. To do that just add those menu items to the switch statement in onOptionsItemSelected():
case R.id.imageee:
Intent imageIntent = new Intent(this, Upload.class);
startActivity(imageIntent);
return true;
case R.id.pdfff:
Intent pdfIntent = new Intent(this, Pdf.class);
startActivity(pdfIntent);
return true;
Another interesting solution could be:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.action_refresh:
intent.setClass(this, UpdateService.class);
break;
case R.id.notes:
intent.setClass(this, Physics.class);
break;
case R.id.cloud:
intent = null;
break
case R.id.imageee:
// TODO set intent class
break;
case R.id.pdfff:
// TODO set intent class
break;
}
// If the selected item is the one that opens the submenu does not try to start the
// activity avoiding throwing null pointer exception and consequently opens the submenu
if (!(intent == null))
startActivity(intent);
return super.onOptionsItemSelected(item);
}
That way you avoid having to have multiple times, in all cases the same call to the method.
startActivity()
Saving lines of code. It is even more advantageous if you decide to add more items to the menu.

Error starting method from action bar button

I am trying to create a button on the action bar that will activate a method that is in the same activity.
Below is my 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=".AddContactActivityOCR">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/undo"
android:orderInCategory="100"
android:title="UNDO"
app:showAsAction="always"/>
</menu>
Below are my relevant codes in the activity:
#Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_contact_undo, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.undo:
onRewrite();
return true;
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onRewrite(View v){
listName.setText(recognizedText);
listOwner.setText(recognizedText);
companyName.setText(recognizedText);
companyAddress.setText(recognizedText);
companyNumber.setText(recognizedText);
mCurrentLang = mFromLang;
validator(recognizedText);
}
I am trying to start "onRewrite()" when I click the "UNDO" button on my action bar.
However it shows the error below:
I tried using "android:onClick="onRewrite" on a normal button and it is able to start this method. Why is it not starting "onRewrite" in this case?
why its show error?
you have create method with parameter View like onRewrite(View v) and you are trying to call it without parameter that's why its show you that message.
remove parameter from method. like below
public void onRewrite(){ .... // your code}
I tried using "android:onClick="onRewrite" on a normal button and it
is able to start this method.
why its working?
want to use method to work in both case. create two method with same name & use same code inside that.
public void onRewrite(){ .... // your code}
public void onRewrite(View v){ .... // your code}
another solution is create button click in activity and remove android:onClick="onRewrite from layout.xml, call onRewrite() from click event as well as from onOptionsItemSelected
Change your Activity code
Error Because you calling method On onRewrite() without Parameter
#Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_contact_undo, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.undo:
onRewrite();
return true;
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onRewrite(){
listName.setText(recognizedText);
listOwner.setText(recognizedText);
companyName.setText(recognizedText);
companyAddress.setText(recognizedText);
companyNumber.setText(recognizedText);
mCurrentLang = mFromLang;
validator(recognizedText);
}
Hope this will helps you

Adding second button in Action Bar

I'm trying to add a second button to my action bar. I have one drawable that opens a new activity. I want to add a second on in onOptionsItemSelected. Here is my code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.new_account:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
this.startActivity(intent);
break;
switch (item.getItemId()) {
case R.id.action_settings:
Intent myIntent = new Intent(getActivity(), SettingsActivity.class);
this.startActivity(myIntent);
break;
}
return super.onOptionsItemSelected(item);
}
}
I get an error asking in the last closing bracket saying, "Missing return statement." I am new to android development so I'm guessing it is something simple. Thanks.
Edit: Here is my main.xml in my menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myapplication2.app.MainActivity" >
<item android:id="#+id/new_account"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="always" />
I haven't changed any lines of the settingsActivity.class. I just created a new activity with the "settings" mode.
If you want add menuItem, you must add item in xml.
Project-res-menu has a xml files.
There is already has a 1 item.
Try add new Item and set attribute, I think you show a two menu.
You are getting "Missing return statement" because you don't have a return statement in switch-case.
For ex:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.settings:
setSettings();
return true;
case R.id.second:
setColor();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How do I make menu item clickable and display a toast after clicked android?

Hello I having a problem trying to get eclipse to recognized my menu icon. I want to have the icon appear in the action bar and when clicked, I want to display a toast.The problem is that my save icon is not being recognized in eclipse. Here is my code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profilemenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case android.R.id.home:
finish();
case android.R.id.save:
save cannot be resolved or is not a field
}
return false;
}
The menu :
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save" android:showAsAction="ifRoom"
android:title="#string/save_str" android:icon="#drawable/content_save" />
</menu>
You're using the incorrect identifier within the switch statement, you should use just R.id.Save, like so:
switch(item.getItemId()) {
case R.id.home:
finish();
case R.id.save:
Toast.makeText(getContext(), "Toast message", Toast.LENGTH_SHORT).show();
}
Using the android identifier means you are trying to find a resource built into the android sdk which doesn't exist.

Categories