Back Arrow in Action bar Sherlock is not displaying - android

In my project I have used action bar sherlock library. I want to make back button in action bar I used following code
getSupportActionBar().setHomeButtonEnabled(true);
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
Every thing is working fine, but back Arrow (to the left of icon) is not displaying. I want to show Back Arrow also. Can anyone tell me what I am doing wrong.

You have to set it up this way:
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
Hope that helps.

Related

Add icon to fragment actionbar and get reference to it

I have no custom bar, I just set a delete icon to the actionbar, but now I need to set OnClickListener to this icon. how can I do that without custom bar is this possible. Also the icon not apears on the left side, can I set it on the rightside?
activity.getSupportActionBar().setIcon(R.drawable.ic_delete);
I use Navigation Drawer, when I use custom bar the toggle icon despairs.
Looks like you want to set the ActionBar's home button as your delete button. I would suggest not to do it as that is a bad design decision in my opinion. And moreover you also want to show the button on the right hand side which can be done in a much intuitive manner by using a menu.
Please have a look at the official documentaion for adding ActionBar actions here
Basically you need to add an XML menu resource and declare your actions like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_delete"
android:icon="#drawable/ic_delete"
android:title="#string/action_delete"
app:showAsAction="always"/>
</menu>
Then in your Activity override the OnOptionsItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete:
// Do your stuff here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
actionBar.setDisplayHomeAsUpEnabled(true);
Then you need to override activity method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onIconClicked();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
To create an item in the right side, you need to create custom menu, actually, it is simple.
Here is example how to do this

Android Action Bar Logo Home Button

I'm currently developing an android application and i'm looking to make the logo on the left of the action bar a menu button. I have enabled the logo using the code
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
from my research I should be able to make this a a menu button by using the code
if (id == R.id.home) {
Intent i = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(i);
}
in onOptionsItemSelected but this is not working for me.
Is there a way to change or find the correct ID for this logo as im starting to think R.id.home is incorrect
I think with enabling your home button with
actionBar.setDisplayHomeAsUpEnabled(true);
and then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//fire intent or whatever you require.. do over here
return true;
default:
return super.onOptionsItemSelected(item);
}}
I think this should work fine.

How can i set back navigation button in my action bar?

I am using actionBar.setDisplayHomeAsUpEnabled(true); for back navigation functionality but it is not working as properly as i need it and when i am using back button of my phone it works properly.
Is there any way to code on a button such that it work same as back navigation button of phone?
Along with
actionBar.setDisplayHomeAsUpEnabled(true);
or
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
you can try using this in Activity, too:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So your menu item "Home"'s click will be handled using that.
I know this has been answered, but i always do this in my method when having the same back-button-like capability in another ActionBarActivity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
and ofcourse:
actionBar.setDisplayHomeAsUpEnabled(true);
You may/may not used this, its up to you :)

Enabling the action bar's home button

Pressing on the app's icon doesn't change the activity, but I'd like it to return to the app's MainActivity.
I've made my app's icon appear on the left of the action bar by doing the following:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
I'm assuming I should define some functionality in the onOptionsItemSelected() methods of the activities concerned. I've gotten a log of the MenuItem's ID when the app's icon is pressed and onOptionsItemSelected() gets called, but obviously I can't hardcode that id into the switch statement. What should I do?
You should set android:parentActivityName for your activity in the manifest file.
android:parentActivityName="com.example.myfirstapp.MainActivity"
You also need to handle the click in onOptionsItemSelected for the id of android.R.id.home:
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);
return true;
}
return super.onOptionsItemSelected(item);
}
See more on Android developer website.
This is how I'd do it
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.HomeButtonId){
//Return Home
}
}

Actionbar menu onclick

Is there any way to use the standard menu button in the action bar to do something? In my case a sliding menu, I already have code that works in the onClick of a normal button. I just want to use the menu button in the action bar instead. Is this possible? Or will I have to customize the action bar and not use the button that is already there?
I believe you mean the ActionBar Home icon.
First you have to enable it
getActionBar().setHomeButtonEnabled(true);
Then you have to handle the event
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//do your work
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FYI if you want to follow the Navigation drawer pattern you should read this tutorial.

Categories

Resources