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);
}
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 joined Android Studio and Android App project a few week ago, and I'm trying to create a simple app with ActionBar options.
When I start Android Studio, following Android Dev. Training, I meet always this rendering error:
Android Studio doesn't found android.support.v7.app.ActionBarActivity (ecc...)
I resolved this error by setting a different theme. But whenever I try a new project i will do this again and again. First question: there is a way to fix this rendering problem? I meet this problem also in the MainActivity.java, where the extends ActionBarActivity is deleted with a line, telling me it is deprecated and advising me to use AppCompatActivity. Should i follow this tip?
Question number two: i read like 100 post on guys who can't show the actionbar in the activity, and I tried everything, but when I link actionbar menu with activity through:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I still can't view the menu I created on the actionbar.
Some images can maybe help me to explain better my problem:
http://i.stack.imgur.com/5nPFx.png
And there is the layout of my activity:
http://i58.tinypic.com/oau8ed.png
As you see, there is no icon button i added and no setting button like menu layout show.
ActionBarActivity is now deprecated use AppCompatActivity
to avoid this error:
Android Studio doesn't found android.support.v7.app.ActionBarActivity
you have to Download the latest version of the Support Library! and then you can use
import android.support.v7.app.AppCompatActivity
public class MainActivity extends AppCompatActivity{
...
...
...
I have an application that has an options menu in fragments that works perfectly in 4.0. But when i test it in 2.3 i get a weird issues. When i click on an option menu it doesn't cause any events to trigger. But if i do anything else after like click back, click a button, open another activity. The event that was supposed to happen when i click the action button triggers.
I have setHasOptionsMenu(true); in my fragments the only thing different that maybe the source of the issue is that my fragments inherit from a base fragment that has the logic for the action buttons.
---Base Class ---
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(menu != null)menu.clear();
inflater.inflate(R.menu.msg_menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//Logic Here
}
}
To make this issue more strange sometimes the button in the actionbar will behave normally but i can't figure out why.
EDIT: I found that if i press a menu button then open and shut the navigation drawer. The button then work correctly after but still haven't found a work around
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'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.