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
Related
I am setting my toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back);
toolbar.setTitle(R.string.app_name);
How can I retrieve the home button (android.R.id.home) as a MenuItem?
Here's what I tried:
MenuItem back = menu.findItem(android.R.id.home);
But that makes a NullPointerException for some reason. So how can I retrieve it as MenuItem without getting a NullPointerException?
This SO answer, suggests the following code but it doesn't seem to work and I can't find anything else.
getWindow().getDecorView().findViewById(android.R.id.home);
In any case it is hacky, because you shouldn't need that reference, and you shouldn't be traversing the view hierarchy to get it in any case. Since the Android SDK purposefully hides this from you, it is not something you should attempt to get, as even if the above code did work, it may not work in a future version (which actually seems like a possible explanation for it not working for me as I tested in SDK 23 Marshmallow).
So, if you want to set the icon, you can do it by setting the android:homeAsUpIndicator in your theme, as suggested here.
If you want to detect a click event on the home button, the correct way is by overriding the onOptionsItemSelected method of Activity, for example
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
// do something
return true;
}
return false;
}
Any other use would be outside the intended purpose and so not supported, as is agreed by others such as in this post.
I didn't find out to get it as a MenuItem, but, to disable the home button pogrammatically:
getSupportActionBar().setDisplayHomeAsUpEnabled(true/false)
This is what I needed. But I still didn't find out how to get it as a MenuItem.
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.
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.
I'm having an issue connected with actions oferflow. On mdpi device with Android 2.3 on board, when I put two actions on the action bar and then add a ShareActionProvider it overflows to be under hardware menu button instead the overflow icon.
What is happening is partially acceptable, but the ShareActionProvider does not work at all under those circumstances. When I roll over the menu panel and click nothing happens.
Oh, I'm using AB Sherlock 4.2.0.
Do you know any workaround?
Thanks!
Current workround for me is to handle generic onOptionsItemSelected for provider's ID and do as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
[...]
case R.id.menu_item_share:
startActivity(Intent.createChooser(mShareIntent, getString(R.string.share_title)));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But it'd be nice to see this fixed. :)
I have been used this example, in this example you have to add /libs/android-support-v4.jar library file and and put a break point on public boolean onMenuItemSelected(int featureId, MenuItem item) method in /src/android/support/v4/app/Watson.java
line no. 115
debug source code when you use app for lower version api where "Menu" button exist.
Hope you will be able to find problem.
When first experimenting with the SlidingMenu library by jfeinstein10, in the example project, clicking the icon button in the action bar would cause the sliding menu to open and then close when clicked again. After implementing ActionBarSherlock and getting it to run (not throwing any errors), the icon no longer causes the menu to appear. So far I have changed the SlidingMenu library to extend SherlockActivity instead of extending android Activity as suggested in the SlidingMenu read me. I have also changed the following lines in BaseActivity:
Original:
// customize the ActionBar
if (Build.VERSION.SDK_INT >= 11) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Changed to:
// customize the ActionBar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
It seems as if the button press is being registered in LogCat, but it's not doing anything.
I've been trying to figure this out for a while now and just wanted to see if anyone has experienced this issue or is familiar enough with both/either libraries to quickly help pinpoint where I'm going wrong or what I forgot to do.
Thanks!
this is your problem
import android.view.MenuItem
you must use Shearlock Menu not android Menu.
remove android MenuItem import and use Shearlock one`s
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Menu;
I hit this problem as well, and was already importing the actionbarsherlock menu and menuitem libraries.
What did the trick for me was adding the following to the onOptionsItemSelected function so the relevant toggle function was called when the home button action was triggered...
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId())
{
case android.R.id.home:
getSlidingMenu().toggle();
return true;
...
}
return super.onOptionsItemSelected(item);
}