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.
Related
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).
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
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);
}
}
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.
I'm trying to set a color background to my welcome menu. I looked for a solution but I didn't find one good for me. I'm working with android 2.3 API
my welcome_menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_preferiti"
android:orderInCategory="100"
android:title="#string/menu_preferiti"
android:icon="#drawable/menu_preferiti_pressed"/>
<item
android:id="#+id/menu_sicuro_bici"
android:orderInCategory="100"
android:title="#string/menu_navigazionesicura"
android:icon="#drawable/menu_sicuro_bici_pressed"/>
<item
android:id="#+id/menu_credits"
android:orderInCategory="100"
android:title="#string/menu_credits"
android:icon="#drawable/menu_credits_pressed"/>
WelcomeActivity code to create menu
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Intent intent = null;
switch (item.getItemId())
{
case R.id.menu_preferiti:
// CAMBIARE LA QUERY!!! METTERE QUELLA DEL RECUPERO DEI PREFERITI CON JOIN!!!
daos = new DAOService(this);
ArrayList<Itinerario> listafav = daos.doRetriveAllItinerari();
intent = new Intent(WelcomeActivity.this, FavoriteActivity.class);
intent.putExtra("lista_favoriti", listafav);
startActivity(intent);
return true;
case R.id.menu_sicuro_bici:
intent = new Intent(WelcomeActivity.this, NavigazioneSicuraActivity.class);
startActivity(intent);
return true;
case R.id.menu_credits:
intent = new Intent(WelcomeActivity.this, CreditsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Maybe a solution is to create a style for menu, but I don't know how to do it.
That's not possible. However you can display your own custom menu and handle it with onKeyDown()