Hi Below is the code I am using to create option menu in my FragmentActivity :-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Menu options to set and cancel the alarm.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
}
return false;
}
Will anybody tell me why it's not working? It is not affecting app but nothing is happening when I click the option menu button from device. Please Help to resolve this.
Thanks in advance!
Below is my main.xml :-
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/start_action"
android:showAsAction="ifRoom|withText"
android:title="#string/start_text" />
<item android:id="#+id/cancel_action"
android:showAsAction="ifRoom|withText"
android:title="#string/cancel_text" />
</menu>
Return item within Switch case like. ITs Work For me.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Although this question is old but to close it here is what i believe OP is missing in the code
In the
onCreateOptionsMenu
return it with the super as super.onCreateOptionsMenu(menu);
and in the
onOptionsItemSelected
return it with the super as super.onOptionsItemSelected(item);
all the return type is boolean so you will know it worked correctly when it returns true. Its like simillar to super.onCreate(savedInstancestate).
Change
return false;
to
return super.onOptionsItemSelected(item);
as
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Edit:
Also you have to add the following to your Fragment
setHasOptionsMenu(true);
Related
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#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 == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...
I currently have two navigation drawers/menus:
However I need a little bit of help setting up an OnClick listener for the right Action Bar menu.
Currently - I have an onclick listener for the left Action Bar Menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
However I'm not sure how I might be able to handle an onClick event for the right Action Bar menu.
Any suggestions are greatly appreciated.
Full XML and Java Source:
http://pastebin.com/ygyyjtLZ
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case R.id.action_more: // change the action_more with your own id in main.xml menu
// do your stuff here (your right click method)
return true;
// rest of your menu actions here...
}
}
return super.onOptionsItemSelected(item);
}
I want to add a new button to the ActionBar. When I click it, it does a specific Action. So I don't want a button that, after being pressed, opens a sub-menu (like the classic 3-dot menu).
I created a new button, this:
<item android:id="#+id/action_refresh"
android:icon="#drawable/refresh"
android:title="#string/refresh_string"
android:showAsAction="always"/>
and it's shown on the ActionBar, but if I click it, naturally, it doesn't do anything.
How can I manage to get an Action just pressing it?
Thanks!
You Need to override onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_refresh:
//do your stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Same as Homo sapiens's answer, but with an if-structure.
Add this method to your Activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.action_refresh)
{
// do your stuff
return true;
}
else if (item.getItemId() == R.id.otherItem)
{
// do other stuff
return true;
}
// ...
else
{
return super.onOptionsItemSelected(item);
}
}
I want to add action to add action to main.xml button.
here is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/SendMes"
android:showAsAction="ifRoom|withText"
android:textColor="#ff0000"
android:textColorHighlight="#ff0000"
android:textColorHint="#ff0000"
android:textColorLink="#ff0000"
android:enabled="True"
android:title="Pargi"/>
Just how to start something like onclicklistner?
You need to override onOptionsItemSelected method in your activity in order to trap the selected item's click event:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//get the item id, match with required one and do your stuff
}
Make sure you inject your menu xml file too:
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getMenuInflater().inflate(R.menu.your_whatever_menu_file, menu);
return true;
}
For more info, read this.
Use the onOptionsItemSelected like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.SendMes:
//Do whatever you want
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
have u tried adding>> android:onClick="YOUR_METHOD_IN_ACTIVITY" ?
I have read the instructions at the android developers page's in order to get the Checkable menu items:
http://developer.android.com/guide/topics/ui/menus.html
this is my xmlmenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="all">
<item android:id="#+id/regu"
android:title="#string/Regulatory" />
<item android:id="#+id/warn"
android:title="#string/Warning" />
<item android:id="#+id/temp"
android:title="#string/Temporary" />
<item android:id="#+id/bicy"
android:title="#string/Bicycle" />
</group>
</menu>
And here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.regu:
if (item.isChecked())
{
item.setChecked(false);
currAvailableOptions++;
}
else if(0 != currAvailableOptions)
{
item.setChecked(true);
currAvailableOptions--;
}
return true;
case R.id.warn:
if (item.isChecked())
{
item.setChecked(false);
currAvailableOptions++;
}
else if(0 != currAvailableOptions)
{
item.setChecked(true);
currAvailableOptions--;
}
return true;
case R.id.temp:
if (item.isChecked())
{
item.setChecked(false);
currAvailableOptions++;
}
else if(0 != currAvailableOptions)
{
item.setChecked(true);
currAvailableOptions--;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
The problem is when I clicked one item, the menu item disappeared. It wouldn't have to stay visible in order to check other menu items?
Any idea?
Greetings
Checkable items appear only in submenus or context menus.
And with submenu they (Google) means:
Submenu A floating list of menu items that appears when the user
touches a menu item that contains a nested menu.
Since your menu items are not submenu items, it will not work
I know this is not a direct answer to your question but please consider the following code instead of your switch, it might help you find the problem.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.regu:
case R.id.warn:
case R.id.temp:
if (item.isChecked())
currAvailableOptions++;
else if(currAvailableOptions != 0)
currAvailableOptions--;
item.setChecked(!item.isChecked());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What is currAvailableOptions? I looked at the article you linked to and there wasn't anything about that in there. It would seem that all you need to do is check:
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
or at least that's what the tutorial says. Perhaps you should give it another read? Hope this helps.
You should probably add break; statements after each case:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
item.setChecked(!item.isChecked());
break;
case R.id.item2:
item.setChecked(!item.isChecked());
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}