I'm playing around with the sherlockactionbar library, there is a problem which is the up button won't work on API 10. The up icon can be shown on the actionbar, but it won't respond to my pressing. On higher API such as 16, it functions as expected without problems.
I tried several solutions I came across, but none of them work. Below is one of them.
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpTo(this, new Intent(this, test.class));
return true;
Make sure you have these set:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And that your switch is in this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
/* Do Something //*/
return true;
default:
return false;
}
}
Related
I'm working on an android app and recently, I discovered that the default back button onBackPressed() produces a back behaviour, while the getSupportActionBar().setDisplayHomeAsUpEnabled(true) produces an up behaviour. And the two have significant differences.
I was wondering if I can simulate the up behaviour when I press the hardware back button, so that it navigates up instead of back.
Thanks.
You can do this.
#Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
}
You can do this
Overide onOptionsItems Selected
Create a switch case of android.R.id.home
Call method onBackPressed();
here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}
I'm using the navdrawer layout from android to show a filter on the right side of my screen in a sliding menu.
Now I wanted to remove the left icon in the toolbar. I did this as follows:
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.syncState();
The only problem I'm now facing is that the icon is an arrow and it doesn't do anything and I want it still to have the up behaviour.
I tried something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_filter:
Timber.d("Open or close filter");
mNavigationDrawerFragment.openDrawer();
return true;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
But this doens't work because onOptionsItemSelected is not called for that arrow?
Anyone an idea?
try :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
I would like to have an ActionBar in Android similar to evernote where in the top right there is a sign that if touched triggers an action. How do I put this in writing clickable? And as I move to the right?
You need to override OnOptionsItemSelected() method in your activity. You can also check the for offical doc for full tutorial
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuItem1:
action1();
case R.id.menuItem2:
action2();
default:
return super.onOptionsItemSelected(item);
}
Good luck
// this is code on my other activities
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
//NavUtil show error
//suggests creating another class
return true;
}
return super.onOptionsItemSelected(item);
}
}
Make sure you have latest Android.support.v4.jar added to your lib files.
Also there are a few known issues with this files. See the link below:
Download Android Support v4 Jar Revision 9 (!)
When i used Actionbarsherlock
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I notice that android.R.id.home is from API 11.
How can we make sure android.R.id.home is right on API 8?
It is a static final constant which means its value is copied into the compiled code instead of being just a reference. This is why it is able to be used successfully on every API level.