How to set icon to title bar for each Activity in Tablelayout in android

Posted by Venu Gopal on Stack Overflow See other posts from Stack Overflow or by Venu Gopal
Published on 2010-12-26T12:36:56Z Indexed on 2010/12/26 12:54 UTC
Read the original article Hit count: 315

Filed under:

In my tablayout example, i have created 3 tabs, as usually i set 3 activities for each tab. I can set image to title bar of activity, which adds the intent to each tab. Due to this, the image in the title bar is visible in all 3 tabs. My requirement is to set a different image to title bar for each activity. I followed this to set image to title bar. But when i am going to do the same thing to each activity, getting android.util.AndroidRuntimeException: You cannot combine custom titles with other title features this error and application is terminated.

manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.aptitsolution.tablayout"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">
    <activity android:name=".TabLayoutDemo"
              android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="AlbumsActivity"></activity>
    <activity android:name="ArtistsActivity"></activity>
    <activity android:name="SongsActivity"></activity>

TabLayoutDemo.java

public class TabLayoutDemo extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);
    ....
    ....

ArtistsActivity.java

public class ArtistsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//here i am getting the error 
    setContentView(R.layout.artists);
    setFeatureDrawableResource(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
}

}

my_title.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<ImageView android:id="@+id/titleImage" android:src="@drawable/nowplaying"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView android:id="@+id/titleText" android:layout_toRightOf="id/titleImage"android:layout_width="wrap_content"
    android:text="New Title" android:layout_height="wrap_content"/>

thanks
venu

© Stack Overflow or respective owner

Related posts about android