Context: Very familiar with iOS development and Java. New to Android.
Problem: I added a handler to navigate 'back' when the home button is pressed. It works fine, until I add another handler when the user scan button:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.home) //The ID here is no longer R.id.home
{
navigateBack();
return true;
}
else if (item.getItemId() == R.id.scan_button)
{
presentScanner();
return true;
}
return super.onOptionsItemSelected(item);
}
As soon as I add the code for the Scan the id is no longer R.id.home. It works if I do this:
if (item.getItemId() == 16908332)
{ . . etc. . .
What has happened? How can I correctly get the ID of the home button in this current activity?
Update: Changed from R.id.home to 'android.R.id.home' and this works. Why?
As pre #Romain Guy the man behind android this cannot be done see here. But for older devices you might want to try the keyevent method.. This post explores additional ways to achieve what you are looking for.
UPDATE
android.R.id.home is used in the actionbar to know that your actionappIcon is pressed(left side). While R.id.home is the reference to physical home button(or touch as in nexus 5). Have a look at this tutorial for further explanation and use cases.
Related
I have created a Toolbar for an activity in Android in Xamarin. I have enabled the back/home button with SupportActionBar.SetDisplayHomeAsUpEnabled(true);. I am trying to capture the event of pressing the back/home button with the following code, as instructed by this and many other stackoverflow posts:
public override bool OnOptionsItemSelected(IMenuItem item)
{
System.Diagnostics.Debug.WriteLine("OnOptionsItemSelected() called: " + item.ItemId);
switch (item.ItemId)
{
case Resource.Id.home:
System.Diagnostics.Debug.WriteLine("Home button pressed");
Finish();
return base.OnOptionsItemSelected(item);
default:
return base.OnOptionsItemSelected(item);
}
}
When I press the back button, OnOptionsItemSelected is called, but item.ItemId is not equal to Resource.Id.home. The former is 16908332 (tested on two different devices) but the latter is 2131492903. How can I capture the home/back button from the toolbar in Xamarin? One possible option is to hardcode the back button ID as 16908332, but I do not know if that number will stay the same permanently.
You are using the wrong resource, you want the one from the Android space:
Android.Resource.Id.Home
The right one to use is
case Android.Resource.Id.Home:
Finish();
break;
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'm using the ActionBarSherlock library and I'm following the exact steps as suggested here and here to enable navigation to the previous screen.
My code looks like this:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// This callback is used only when mSoloFragment == true (see
// onActivityCreated above)
switch (item.getItemId()) {
case android.R.id.home:
// App icon in Action Bar clicked; go up
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Reuse the
// existing
// instance
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
But R.id.home is not recognized and home shows up in red. :-/ If I use the native actionbar the home declaration takes me to ids.xml file. But here the declaration is not found while I use the ActionBarSherlock Activity. Am I missing something?
just replace this
android.R.id.home
to
R.id.home
and check your code... run it
because
R.layout.* are layouts you provide (in res/layout, for example).
android.R.layout.* are layouts that ship with the Android SDK.
I know this is an old question but I believe the right answer is missing.
It should be be android.R.id.home because it is a platform resource, so your code is fine.
Make sure your minSdkVersion is 11 or higher since home was introduced in 11.
I remeber running into this problem and apparently its quite frequent a quick google or search through stack overflow should've given you some insight anyways check this thread out R cannot be resolved - Android error
Im pretty sure your running into same problem
I want to use a dialog as option menu in my application, the problem is I've understood that smartphones without buttons only show the menu button if the activity have an action menu implemented.
How can I show a Dialog instead a Menu without lost the Menu button on ICS?
Thanks a lot! Regards from Spain!
You can find your answer here. In short, you just decrease the target sdk version, and then the menu button will appear on all ICS devices. And then you just use this to detect the click:
#Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
if(keycode == KeyEvent.KEYCODE_MENU){
//do you thing here
return true;
}
return super.onKeyDown(keycode,event);
}
I have an Options menu up and running in my Android application and I've overridden the onCreateOptionsMenu, onOptionsItemSelected and onPrepareOptionsMenu methods to customize the menu a little.
My question is related to keeping the Options menu open after the user clicks on a menu item. Basically, I'd like to be able to hide the menu until the user clicks on the device menu key. Once the user clicks on this key, I'd like to be able to hold the menu in place regardless of how many times the user clicks on menu items. If the user wants to hide the Options menu, they'd just need to click on the device menu key again.
Is this type of interaction supported (or even advisable). If this interaction is not supported, any alternative suggestions are more than welcome.
Cheers!
Sean
This will not be possible with onCreateOptionsMenu and the other methods. They always act that way.
But you can do it another way. But there you have to program the whole menu yourself. Basically add the Menu in your layout.xml and let it be hidden (visibility = gone). Then you overwrite the methods onKeyDown. There you check if it is the Menu key. if the menu is not yet open yes, then you show the menu. If it is open already, you hide it.
should not be too difficult. Good thing as well is, that you can make the menu look exactly the way you want and as well let it react the way you want.
For anybody like me, who found this question in google:
To keep menu open after selecting item, you need this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setChecked(!item.isChecked());
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
Important to return false in onOptionsItemSelected and methods of OnActionExpandListener
This solution from #MayurGajra. More details here: How to hold the overflow menu after I click it?