I'm trying to do some things on the ActionBar in Android.
I've already added new items in the right side of the action bar.
How can I change the left side of the action bar? I want to change the icon and the text, and I want to add a "Back Button" in the action bar for the other screens
The action bar title will, by default, use the label of the current activity, but you can also set it programmatically via
ActionBar.setTitle()
.To implement the "Back" (more precisely, "Up") button functionality you're talking about, read the "Using the App Icon for Navigation" section of the Action Bar developer guide.
Finally, to change the icon, the guide covers that as well. In short, the action bar will display the image supplied in
android:icon
in your manifest'sapplication
oractivity
element, if there is one. The typical practice is to create an application icon (in all of the various densities you'll need) namedic_launcher.png
, and place it in yourdrawable-*
directories.In Android 5.0 material design guidelines discourage the use of icon in actionBar
to enable it add the following code
credit goes to author of this article
This is very simple to accomplish
If you want to change it in code, call:
And set the values to whatever you please.
Or, in the Android manifest XML file:
To enable the back button in your app use:
The code should all be placed in your
onCreate
so that the label/icon changing is transparent to the user, but in reality it can be called anywhere during the activity's lifecycle.You can change the icon in your by adding whatever icon you want to your respective drawable folders, then changing this line in your AndroidManifest.xml file:
to match whatever the name of your icon is in there. Or put your icon as ic_launcher, if they're the same icon. As for what it says, add or change whatever strings match up to that in your res/values/strings.xml file. Then, once again in your AndroidManifest.xml file, change this line:
to whatever the string you have in their. You'll have to do this for the application as a whole, and whichever activities you want, but the lines are the same.
Hope this helps.
This work for me:
Add the below code inside an onCreate function in your activity.