Option menu in Android - android

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

Related

Checkboxed option in Android menu does not work

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.

onClick start new activity android

I have a little problem with action bar item.
I want to create clicable item in action bar which opens new java activity.
ab_button_desktop is the custom button which I created.
I tried a lot java codes from tutorials and I havent still any resoults.
Could anyone recommend me java code which opens new activity.
This is my xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:webviewapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/ab_button_desktop"
android:title="#string/ab_button_desktop"
android:icon="#drawable/ic_menu_desktop"
android:orderInCategory="1"
android:onClick="desktop"
webviewapp:showAsAction="always" />
<item
android:id="#+id/ab_button_share"
android:title="#string/ab_button_share"
android:icon="#drawable/ic_menu_share"
android:orderInCategory="1"
webviewapp:showAsAction="ifRoom" />
</menu>
By adding android:onClick="desktop" you are telling Android the name of a function within your Activity to call when your button is clicked. This function has to follow few rules though. It must have a return type of void, take only one argument which is a View. To open a new Activity you would have to add the following code to your Activity where the button currently is:
public void desktop(View v){
//handle the click here
}
Lets say you create one called InformationActivity. To launch it from example button click, you would do this:
public void desktop(View v){
startActivity(new Intent(this, InformationActivity.class);
}
For more details, read about Activities and UI in the Android dev guide.
Original Answer: Android Button to open xml layout
You can handle clicks on menu items by overriding OnOpionsItemSelectd:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getId()){
case R.id.ab_button_desktop:
//Do something
break;
case R.id.ab_button_share:
//Do something else
break;
}
return super.onOptionsItemSelected(item);
}
For a tutorial on how to launch activities, click here.

Showing option at the bottom of the screen

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.

Menu button is not show in 3.0 OS

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.

How to create the Action Overflow part of the ActionBar?

I have been looking online for a while, and there are very little tutorials on how to do this. Even the google docs have very vague info on how to put this stuff up there! I am sure its subtle, but I can't understand the terminology because I am still fairly new to Android. I would like to be able to get the Action Overflow icon to the right side of the ActionBar. Putting the view control was pretty understandable using the docs and example code when creating a project, but it does not have one for the Action Overflow. Thanks in advance!
Edit: I should probably elaborate. I would like the menus to default being under the action overflow. I found an eye opener answer to a similar question, but it only tells you how to put the menus at the top. How can I force them to go under the list? Is it even possible to force that? Thanks!
If you use API-11, it's not problem. If lower I am inviting you to this topic :
The best way to create drop down menu in android 2.x like in ICS
In the case when API-11 and higher you must :
Create menu xml like this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item_refresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"
android:showAsAction="ifRoom|withText" />
<item
android:id="#+id/item_save"
android:icon="#drawable/ic_menu_save"
android:title="Save"
android:showAsAction="ifRoom|withText" />
</menu>
And create code like this :
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// gets the activity's default ActionBar
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu); //inflate our menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.item_refresh:
//click on refresh item
break;
case R.id.item_save:
//click on save item
break;
}
return true;
}
Good Luck!

Categories

Resources