I am trying to add the checkbox to menu of my Android app. But I don't know why item with android:checkable="true" attribute has this square of checkbox, but behave like others items. Do I have to write something in java code? I can read the value but tapping does not changing it's value...
My menu\main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:title="#string/settings"/>
<item
android:id="#+id/menu_tests"
android:title="#string/i_want_to_test"/>
<item
android:id="#+id/menu_translating"
android:enabled="false"
android:title="#string/i_want_to_translating"/>
<item
android:id="#+id/menu_funmade"
android:checkable="true"
android:title="Enable funmade"/>
</menu>
Look:
The last option behve like other with no specified behaviour. I can tap and manu closes and state does not change. Hope you can help.
Andret
When the user selects an item from the options menu (including action
items in the action bar), the system calls your activity's
onOptionsItemSelected() method.
The following code goes inside the onOptionsItemSelected(MenuItem item) method in your Activity.
Code:
switch (item.getItemId()) {
case R.id.menu_settings:
// Specify actions for the menu click
return true;
case R.id.menu_tests:
// Specify actions for the menu click
return true;
case R.id.menu_translating:
// Specify actions for the menu click
return true;
case R.id.menu_funmade:
// Specify actions for the menu click
if (item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
return true;
}
return super.onOptionsItemSelected(item);
You can look at the official docs about checkable menus for more details.
Related
I am new to developing Android apps.
I would like to add a settings option menu. The requirement for the app is that when you click settings in the option menu the screen will show a sentence Add beep after ___ counts.
How can I do that, please?
Its on the OnCreateOptions and OnOptionsItemSelected, Plus you need to manipulate the menu folder / menu xml also. Try to read about them and instantiating them is as easy as a switch case, as well as for the functions.
You will need a separate activity/fragment to handle Add beep after ___ counts
Here is the flow, read a little bit more on these. You can find lot of examples on each of these:
Create Main Activity with a menu.xml that has a settings action
Create another activity or a dialog fragment with a layout of
<textview> <edittext> <textview>. This is to show the Add beep
after ___ counts. In MainActivity, on menu action "settings", show this fragment.
You can save the count persistently using preferences
Hope this helps.
mainmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:title="#string/settings_label"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
Toast.makeText(context, "Add beep after ___ counts.", duration).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
For more information read this Menu
I am a noob in android development and I follow the tutoral of the android website.
1. In the part of "Starting Another Activity", I just copied the code and tried to run it, but I found after the activity is changed (changed to new page), the title of action bar will change to the name of the class of that activity.
2. When it talks about the respond of the action button, the code is written as:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, in the default code:
`public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}`
It only return true which contains no method to respond(no openSettings()), but a "setting" word still pop out when I press it.
3. How do I remove the action bar and make it full screen?
Don't fully understand your question (you really didn't ask one specifically) but I think this is what you're asking
How to change the title of a new activity?
Here: How do I change the android actionbar title and icon
How does settings open up?
Android does this automatically, if the onCreateOptionsMenu(Menu menu) is called.
How do i make a full screen activity?
Here: Fullscreen Activity in Android?
In the future, be sure to look through Google and StackOverflow for you answers, most likely someone has already asked a similar question
The Name of the activity is, in your case, is declared in AndroidManifest.xml. Check the android:label attribute of your activity. You can manipulate it through java code if you want. See this SO question.
Check the menu file under res/menu/your_menu_file.xml. I think it contains something like
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
The displayed icon is NOT setting icon, but overflow icon. If the overflow icon is clicked, it lists all menu items. In your case only one (i.e Setting)
To hide the action bar, include these in your onCreate() method
//getWindow().requestWindowFeature(Window.FEATURE_NO_TITLE);
getActionBar().hide();
You can also do it via xml. Look at this SO question.
I've Googled and come up with no code on how to manipulate what the settings button does, or what is brought up when the settings touch button on the phone is pressed.
Here is a screenshot of what I mean:
At the bottom, you can see that it says settings, but when I click on it, nothing happens. I want to change what happens when it is clicked.
Also, I want to be able to change what is brought up (the settings option in the screenshot is what's brought up) when the settings button on the actual phone is pressed. eg. on the S3, it's the touch button at the bottom left of your device.
Can anyone link me to a resource, or write some sample code for how to do both, or even one of the things I want to do?
You need to override your Activity's onOptionsItemSelected(...) method.
Do it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) { // or whatever your id is, see the .xml file
// do something
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
Check the id of the options item (e.g. settings) and do specific stuff inside the if.
In order to add more items or manipulate the Id, take a look at your projects "menu" folder and the .xml file inside it.
Here the .xml file of my main menu:
And how it looks inside:
(you can manipulate this file and add more actions to it that will show up in the menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_one" android:title="Action 1"></item>
<item android:id="#+id/action_two" android:title="Action 2"></item>
<item android:id="#+id/action_three" android:title="Action 3"></item>
</menu>
In this case my menu would have 3 Actions, and you should check for them inside your onOptionsItemSelected(...) method like that:
if (item.getItemId() == R.id.action_one) //...
... or use switch-case.
For a more detailed explanation, you can also have a look at this tutorial:
http://mobile.tutsplus.com/tutorials/android/android-sdk-implement-an-options-menu/
I am new to this android.In my application when user clicks Menu button ,i want to show 1 option at the bottom of the screen(background should remain as the current screen).Simply i have to say means if user clicks menu button as like pop up window 1 option should come at the
bottom of the screen.And user clicks on that some action should happen..I can t able put some screenshots for this.
----------------------
------------>Consider this is screen
---------------------
------------>here the option should come.
----------------------
Please help me.Thanks in advance.
Menu is generally used to give extra functionality to an application.
To achieve your goal you have to implement menu, which will open when you click menu button of device, like below.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.optionmenu, menu);
return super.onCreateOptionsMenu(menu);
}
R.menu#
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Color" android:title="Color">
<menu>
<item android:id="#+id/RedColor" android:title="Red"/>
<item android:id="#+id/GreenColor" android:title="Green"/>
</menu>
</item>
</menu>
and if you want to do any action on click of option menu, you have to override onOptionsItemSelected to do action on click of menu option, like below.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.RedColor)
{
Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();
}
else if(item.getItemId()==R.id.GreenColor)
{
Toast.makeText(MenuOptionDemoActivity.this, "Green Color Selected", 1000).show();
}
return super.onOptionsItemSelected(item);
}
}
A clear and simple tutorial to implement menu in your app.
That can be done, but the solution is too long for me to post here, so instead follow the link to a tutorial for creating a menu
http://www.androidhive.info/2011/09/how-to-create-android-menus/
Just as a brief overview of the tutorial - You start by overriding the onCreateOptionsMenu method. In this you inflate your menu. Then you override the onItemSelected method, in which you pu a switch to determine which option phase been selected, though in your case it will only be one item. Finally you have to create an XML file which contains all the options for your menu.
I have created menu button which have two functions bookmark and home button.
This is working well in all the android version without android 3.0
do here any ways ? so my menu button will be display also in android 3.0 with all the version.
my code:-
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
db.updateContact(new Contact(itemN,imageStatus));
return true;
case R.id.home_page:
Intent i = new Intent(imageTouchs.this, Comics.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
and my androidmanifest.xml :-
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_bookmark"
android:title="Bookmark"
android:showAsAction="ifRoom|withText" />
<item android:id="#+id/home_page"
android:title="Home"
android:showAsAction="ifRoom|withText" />
</menu>
any menu is not display in action bar
The "overflow' button will only show up when there isn't a physical menu button on the device. This is the standard behavior for the ActionBar.
You can actually set the value of your AVD to specifically not have a menu button when you are configuring the image.
Here is a SO question addressing this same thing: Action bar overflow not displayed
If you are talking about the OverFlowMenu icon, to the best of my knowledge, it cannot be achieved (forced) using the standard Android Support Library.
If you must have an OverFlowMenu (forced), you will need to use the ActionBarSherlock library. Go through a couple of my answers where I have a few fairly detailed suggestions on how to achieve that:
https://stackoverflow.com/a/13307583/450534
https://stackoverflow.com/a/13180285/450534
NOTE: As already mentioned in both my answers linked above, this is not recommended to ensure users get a seamless UI on their devices. And also, if you must absolutely force the OverFlowMenu, you will need to use an older version of ABS, which is again, not recommended.