10 November 2014

Change the Title and icon of a particular page in the action bar

Today we will learn how to change the title in the action bar of a single page. If you have noticed the name of the page in title bar remains as the same name as that of the app. And if you try to change it from the xml layout .Its not possible.

What we will do?
-> We will make a demo application and change the image and the name(title) of the page from the action bar.

What we need ?
  •  An Android Project
  • An image we will use to replace the previous one.
Step 1: Make an Android Project. I have made an application named TitleApp and logo of Made in Android.
title icon

Step 2: Now I want to change the name of the page to Home and also change the title image to the image shown below.
home icon

Step 3: First I will import the image into drawable-hdpi folder found in workspace->TitleApp->res folder.
drag icon

Step 4: Go into AndroidManifest.xml file from the Project Explorer.
android manifest

Look at the code:

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mia.titleapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name="com.mia.titleapp.MainActivity"
           android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Each <activity> represents a single page execution. To change the title we change the string besides the label parameter inside the activity parenthesis.
android:label="Home"

To change the icon we add a line below the bold line in activity parenthesis.

android:icon="@drawable/homeimg"
Note: homeimg is the name of the new icon imported in the previous step.

The final AndroidManifest.xml will become.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mia.titleapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

 <activity
            android:name="com.mia.titleapp.MainActivity"
            android:label="Home"
android:icon="@drawable/homeimg" >
 <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5: Run the project.

Before making changes.
before change

After making changes.
after change
 Stay Tuned with Made In Android

Previous Page Next Page Home
Top