249

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

Android Action Bar

0

17 Answers 17

553

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.

Or, in the Android manifest XML file:

<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); 

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.

Sign up to request clarification or add additional context in comments.

7 Comments

If you are using the support library to add the actionbar, use getSupportActionBar instead of getActionBar.
getActionBar().setHomeButtonEnabled(true) uses API level 14 or up, if you just want back button functionality in older as well as newer APIs you just need getActionBar().setDisplayHomeAsUpEnabled(true)
getSupportActionBar().setDisplayShowTitleEnabled( true ); before setTitle("123");
If you use the code getActionBar().setIcon(R.drawable.my_icon); can you make it clickable?
@JoseManuelAbarcaRodríguez Thank you, that's what was missing for me. In Kotlin: supportActionBar?.setDisplayShowTitleEnabled(true) then supportActionBar?.title = "your title". And it has to be done in onResume, not in onCreate, when creating the activity with startActivity.
|
19

To make a single icon be usable by all your action bars you can do this in your Android Manifest.

<application android:logo="@drawable/Image"> ... </application> 

2 Comments

thanks- I wanted a different icon for outside the app (as seen in "All apps") and another icon at the left of the action bar. And this worked. Manifest file still has android:icon="@drawable/ic_launcher"
This is the right way as it pre-loads the icon, programmatically settings icon is slow!
13

You just need to add these 3 lines of code. Replace the icon with your own icon. If you want to generate icons use this

getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow); getActionBar().setHomeButtonEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true); 

Comments

12

In Android 5.0 material design guidelines discourage the use of icon in actionBar

to enable it add the following code

getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setLogo(R.mipmap.ic_launcher); getSupportActionBar().setDisplayUseLogoEnabled(true); 

credit goes to author of this article

Comments

10

If you want to change the Action bar title just give the following 1 line code in the onCreate() of your Activity

getActionBar().setTitle("Test"); 

1 Comment

How can I animate this title change?
8
ActionBar actionBar = getSupportActionBar(); actionBar.setTitle(getString(R.string.titolo)); actionBar.setIcon(R.mipmap.ic_launcher); actionBar.setDisplayShowHomeEnabled(true); 

Comments

7

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:

android:icon="@drawable/ic_launcher" 

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:

android:label="@string/app_name" 

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.

1 Comment

What size (dp) should this android:icon in activity tag be?
5

For that, you can do it in 2 ways: XML or Java. See here: How to change the text on the action bar

So:

XML:

<activity android:name=".Hello_World" android:label="This is the Hello World Application"> </activity> 

Java:

public class TitleBar extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); if ( customTitleSupported ) { getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); } final TextView myTitleText = (TextView) findViewById(R.id.myTitle); if ( myTitleText != null ) { myTitleText.setText("NEW TITLE"); // user can also set color using "Color" and then "Color value constant" // myTitleText.setBackgroundColor(Color.GREEN); } } } 

1 Comment

To do it from code, you should call setTitle(CharSequence text) or setTitle(int titleRes). Don't use requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) unless you are planning to replace the entire title bar with your own custom View.
3

For set Title :

getActionBar().setTitle("Title"); 

For set Icon :

getActionBar().setIcon(R.drawable.YOUR_ICON_NAME); 

Comments

3

Add the below code inside an onCreate function in your activity.

setTitle("NewName"); 

Comments

2

I used the following call inside onNavigationItemSelected:

HomeActivity.this.setTitle(item.getTitle()); 

Comments

2

This work for me:

getActionBar().setHomeButtonEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24); 

Comments

1

Go to manifest in which specific activity you want to change Action bar Title name and write android:label="Title name"

Comments

0

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's application or activity element, if there is one. The typical practice is to create an application icon (in all of the various densities you'll need) named ic_launcher.png, and place it in your drawable-* directories.

Comments

0

I got non-static method setTitle(CharSequence) cannot be referenced from a static context error because I used setTitle() in static PlaceholderFragment class. I solved it by using getActivity().getActionBar().setTitle("new title");

Comments

0

You can also do as follow :

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) setSupportActionBar(toolbar) setTitle("Activity 2") } 

Comments

-1

Go to AndroidManifest.xml file. Find the <application> tag There you can see a attribute

android:label="@string/app_name" 

Now go to res > values > strings.xml

Change the

<string name="app_name">MainActivity</string> 

to

<string name="app_name">Your Desired Name</string> 

Example

AndroidManifest.xml

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SubmitForm"> </activity> </application> 

strings.xml

<resources> <string name="app_name">Your Desired Name</string> <string name="action_settings">Settings</string> </resources> 

1 Comment

it change the whole add name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.