How to Change the Android Actionbar Title and Icon

How do I change the android actionbar title and icon

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.

How to change the text on the action bar


Update: Latest ActionBar (Title) pattern:

FYI, ActionBar was introduced in API Level 11. ActionBar is a window feature at the top of the Activity that may display the activity title, navigation modes, and other interactive items like search.

I exactly remember about customizing title bar and making it consistent through the application. So I can make a comparison with the earlier days and can list some of the advantages of using ActionBar:

  1. It offers your users a familiar interface across applications that the system gracefully adapts for different screen configurations.
  2. Developers don't need to write much code for displaying the Activity Title, icons and navigation modes because ActionBar is already ready with top level abstraction.

For example:

Sample Image

Sample Image

=> Normal way,

getActionBar().setTitle("Hello world App");   
getSupportActionBar().setTitle("Hello world App"); // provide compatibility to all the versions

=> Customizing Action Bar,

For example:

@Override
public void setActionBar(String heading) {
// TODO Auto-generated method stub

com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
actionBar.setTitle(heading);
actionBar.show();

}

Styling the Action Bar:

The ActionBar provides you with basic and familiar looks, navigation modes and other quick actions to perform. But that doesn't mean it looks the same in every app. You can customize it as per your UI and design requirements. You just have to define and write styles and themes.

Read more at: Styling the Action Bar

And if you want to generate styles for ActionBar then this Style Generator tool can help you out.

=================================================================================

Old: Earlier days:

=> Normal way,

you can Change the Title of each screen (i.e. Activity) by setting their Android:label

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

=> Custom - Title - bar


But if you want to Customize title-bar in your own way, i.e. Want to put Image icon and custom-text, then the following code works for me:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="fill_parent"
android:orientation="horizontal">

<ImageView android:id="@+id/ImageView01"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:background="@drawable/icon1"/>

<TextView

android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
/>
</LinearLayout>

TitleBar.java

public class TitleBar extends Activity {

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

strings.xml

The strings.xml file is defined under the values folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Set_Text_TitleBar!</string>
<string name="app_name">Set_Text_TitleBar</string>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
</resources>

How to change action bar Title

1. The easiest way is:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.strind.app_name);

2. You can also use a custom Toolbar layout

Change your existing code us

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);

To like this:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_main;

Follow please these my tutorial to see how to implement it.

Here's my solution - create a TextDrawable logo using custom toolbar

  1. Create a custom layout with name action_bar.xml
  2. Put into it this code


    <ImageView
    android:id="@+id/image_view"
    android:layout_width="32dp"
    android:layout_height="32dp"
    tools:src="@mipmap/ic_launcher"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:text="@string/app_name"> //THIS GUY
    android:textColor="#ffffff"
    android:textSize="24sp"
    android:textAlignment="gravity"
    android:gravity="center_vertical"/>

  3. Add to your onCreate method in MainActivity class this code:

    //SET A DRAWABLE TO IMAGEVIEW
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.actionbar_main);

    TextDrawable drawable = TextDrawable.builder()
    .buildRound("A", getResources().getColor(R.color.colorAccent));
    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    imageView.setImageDrawable(drawable);
  4. After changes it should look like:


NOTICE: To show appName i used TextView with android:text="@string/app_name" value.

Hope it help

How to manage ActionBar title and icon when working with Fragments hosted by one Activity?

I found the answer here: https://stackoverflow.com/a/20314570/5222156

In onCreate on hosting Activity, after setting up Toolbar and Drawer I initialized listener for changes in backstack

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupToolbar(R.drawable.ic_menu);
setupNavigationDrawer();

getSupportFragmentManager().addOnBackStackChangedListener(this);
}

The rest of the code looks almost the same as in the original answer:

@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}

public void shouldDisplayHomeUp() {
//Enable Up button only if there are entries in the back stack
FragmentManager fragmentManager = getSupportFragmentManager();
int count = fragmentManager.getBackStackEntryCount();

Fragment fr = fragmentManager.findFragmentById(R.id.flContent);
String tag = fr.getTag();

boolean canGoBack = count > 0;
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(canGoBack ? R.drawable.ic_arrow_back : R.drawable.ic_menu);
actionBar.setTitle(tag);
}
}

@Override
public boolean onSupportNavigateUp() {
boolean canGoBack = getSupportFragmentManager().getBackStackEntryCount() > 0;

if (canGoBack) {
navigationController.navigateBack();
} else {
mDrawerLayout.openDrawer(Gravity.START);
}
return true;
}

@Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
closeDrawer();
} else {
super.onBackPressed();
}
}

Add Icon to the left of the title in the action bar in android

Recommended way

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_settings_24dp);// set drawable icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Handle icon click event

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "click..!!", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);

}
}

How can I change the title of a action bar in Android?

It is very easy.
In every .class you want a different title in your actionbar, you only have yo setTitle.
Like here:

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Your custom title");}

Change ActionBar Title style

for changing the title color use html as below:

actionbar.setTitle(
Html.fromHtml("<font color='#FFFFFF'>"
+ YOUR_TITLE
+ "</font>"));


Related Topics



Leave a reply



Submit