How do I change the android actionbar title and icon
AndroidAndroid LayoutAndroid ActionbarAndroid Problem Overview
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 Solutions
Solution 1 - Android
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.
Solution 2 - Android
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>
Solution 3 - Android
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);
Solution 4 - Android
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
Solution 5 - Android
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");
Solution 6 - Android
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.
Solution 7 - Android
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);
Solution 8 - Android
For that, you can do it in 2 ways: XML or Java. See here: https://stackoverflow.com/questions/3438276/change-title-bar-text-in-android
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);
}
}
}
Solution 9 - Android
For set Title :
getActionBar().setTitle("Title");
For set Icon :
getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);
Solution 10 - Android
Add the below code inside an onCreate function in your activity.
setTitle("NewName");
Solution 11 - Android
I used the following call inside onNavigationItemSelected
:
HomeActivity.this.setTitle(item.getTitle());
Solution 12 - Android
Go to manifest in which specific activity you want to change Action bar Title name and write android:label="Title name"
Solution 13 - Android
This work for me:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
Solution 14 - Android
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.
Solution 15 - Android
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");
Solution 16 - Android
You can also do as follow :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setSupportActionBar(toolbar)
setTitle("Activity 2")
}
Solution 17 - Android
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>