SetHomeButtonEnabled not working but setDisplayHomeAsUpEnabled is working - android

I want to add the application icon in the actionbar for all the activities in my application and on the icon click , I would like to navigate to the home page of my application.
I tried with the following code in onCreate
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
Now the application icon is coming in the actionbar , but on clicking it, onOptionsItemSelected is not getting called. But if use actionBar.setDisplayHomeAsUpEnabled(true) instead of actionBar.setHomeButtonEnabled(true) , onOptionsItemSelected is getting called with item.getItemId() . Below is the code snippet
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
The documentation says using both setDisplayHomeAsUpEnabled and setHomeButtonEnabled , onOptionsItemSelected will be called and the only difference is the up arrow. I dont need the up arrow in the actionbar, I only require the application icon. How can that be done?
My minSdkVersion 14 and targetSdkVersion 21.

From http://developer.android.com/reference/android/app/ActionBar.html#setHomeAsUpIndicator(int)
You can use:
actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher);
actionBar.setDisplayShowHomeAsUpEnabled(true);
and this should replace the back arrow with your icon

you can use this:
Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
toolbar.setNavigationIcon(R.drawable.ic_back);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
finish();
}
});

You can use :
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
fist code shows left arrow on toolbar and second code lets you to put your icon

Related

how to set title in toolbar?

Seems pretty simple question, but could't find any answer.
How to change the toolbar title.
The toolbar is created by AppCompat Activity,not a custom created one.
It has the app name as default.I would like to change its Name corresponding to the action the activity performs.
Note : I can change custom toolbar title using the below code,so please i am not looking for that.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("My Title");
You can set default Toolbar title as following:
YourActivity.this.setTitle("Your Title");
For fragment:
getActivity().setTitle("Your Title");
getSupportActionBar().setTitle("Title")
Set ActionBar title based on your need
ActionBar ab = getActionBar();
ab.setTitle("My Title");

Change the App name to some other text in the top bar

Can please someone tell me how to change the App name in the top bar of my android App that gets displayed by default to some other text? I have seen this in a few Apps I used before. I'm building my first Android App and don't have that much experience with this stuff... thank you :)
after
setContentView(R.layout.layout_name);
simply add
setTitle("title_name");
this will change the name of your current activity in top bar or toolbar and i think your activity should extend AppCompactActivity
it is easier if you convert the actionbar (which I think is the bar you mean) into a toolbar if you want to do further modifications to it (as I assume):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
to change the title then simply call actionbar.setTitle as shown here:
actionBar = getSupportActionBar();
actionBar.setTitle("Your Title");
and if you want a subtitle add:
actionBar.setSubtitle("Your Subtitle");
If you are using the new support library, you should use this:
Toolbar toolber = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(Toolbar);
getSupportActionBar().setTitle("App Title");

ActionBar Default Logo Has Wide Space

I'm working with actionBar in android and the default logo is not showing. So I have added this code to show the Logo in my onCreate method in MainActivity.
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
Why is the logo has a wide space causing the title to not be seen?
Please help. Thanks.
This is very simple to accomplish
If you want to change it in code call
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
And set the values to whatever you please.
In Manifest XML:
<activity android:name=".MyActivity"
android:icon="#drawable/my_icon"
android:label="My new title" />
To enable the back button in your app use
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);

Remove Left arrow from SupportActionbar but keep logo

I am able to do this but I now can not click the logo. It doesn't fire onOptionsItemSelected()..
actionBar= getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setTitle("");
actionBar.setLogo(R.drawable.logo);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(true);
if i set actionBar.setDisplayHomeAsUpEnabled(true); the arrow shows up. I have found a clone of this question, on SO but no one answered it correctly. Someone suggested using a transparent image for teh arrow but how do I override that?
I got it, use
setHomeAsUpIndicator()
Like this:
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.logo);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);

Removing left arrow from the actionbar in android?

I want to remove the left arrow from the action bar and only icon and title needed.
My code:
getSupportActionBar().setIcon(R.drawable.logo);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(R.string.dashboardtitle);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
But this doesn't remove the left arrow . Any help!!
According to specification android specification:
To enable the icon for up navigation (which displays the "up" indicator next to the icon), call setDisplayHomeAsUpEnabled(true) on your ActionBar:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
So you should set actionBar.setDisplayHomeAsUpEnabled(false);
UPDATE:
I test next code with ActionBar sherlock and it's work. There is no back arrow:
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(R.string.about_event_location);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
You need to add this to your actionBar theme. You can replace arrow image with your own image which can be transparent
<item name="android:homeAsUpIndicator">#drawable/action_arrow</item>
getActionBar().setHomeAsUpIndicator(R.drawable.action_arrow);
Where action_arrow is a 25x10px transparent png.
Same code for sherlock would be:
getSupportActionBar().setHomeAsUpIndicator(R.drawable.action_arrow);
#vivek: works great. I do not recommend to use #android:color/transparent as Tim Cooper said, because the Title is aligned to the left. So a empty image is better
In AndroidManifest.xml
remove the below code, which will remove back button from your ActionBar
android:parentActivityName=".Main.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Main.MainActivity" />
Just keep it like below:
<activity
android:name=".YourActivity"
android:label="#string/your_activity_label">
</activity>

Categories

Resources