Reputation: 2709
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
Upvotes: 249
Views: 416956
Reputation: 199
You can also do as follow :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setSupportActionBar(toolbar)
setTitle("Activity 2")
}
Upvotes: 0
Reputation: 390
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>
Upvotes: -1
Reputation: 441
This work for me:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
Upvotes: 2
Reputation: 457
Add the below code inside an onCreate function in your activity.
setTitle("NewName");
Upvotes: 3
Reputation: 11
Go to manifest in which specific activity you want to change Action bar Title name and write android:label="Title name"
Upvotes: 1
Reputation: 5768
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);
Upvotes: 8
Reputation: 129
For set Title :
getActionBar().setTitle("Title");
For set Icon :
getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);
Upvotes: 3
Reputation: 119
I used the following call inside onNavigationItemSelected
:
HomeActivity.this.setTitle(item.getTitle());
Upvotes: 2
Reputation: 1731
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
Upvotes: 12
Reputation: 2819
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);
Upvotes: 13
Reputation: 22306
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.
Upvotes: 553
Reputation: 465
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");
Upvotes: 10
Reputation: 2847
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>
Upvotes: 19
Reputation: 1715
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");
Upvotes: 0
Reputation: 2411
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.
Upvotes: 0
Reputation: 377
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.
Upvotes: 7
Reputation: 20223
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);
}
}
}
Upvotes: 5