Implement custom title
![]() |
App with custom title |
Create /res/layout/custom_title.xml, to define the layout of your custom title bar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" > < ImageView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:src = "@drawable/ic_launcher" /> < TextView android:id = "@+id/titletext" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textColor = "@android:color/white" android:textStyle = "italic|bold" < ImageView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:src = "@drawable/ic_launcher" /> </ LinearLayout > |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.example.androidcustomtitle; import android.os.Bundle; import android.app.Activity; import android.view.Window; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); if (customTitleSupported){ getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); Toast.makeText(getApplicationContext(), "Custom Title Applied" , Toast.LENGTH_LONG).show(); //Set title text programmatically TextView titleText = (TextView)findViewById(R.id.titletext); titleText.setText( "Android-Coding" ); } else { Toast.makeText(getApplicationContext(), "FEATURE_CUSTOM_TITLE not supported!" , Toast.LENGTH_LONG).show(); } } } |
Also modify AndroidManifest.xml to use style without title features on android:theme; otherwise "android.util.AndroidRuntimeException: You cannot combine custom titles with other title features" will be thrown. "@android:style/Theme.Light" and "@android:style/Theme.Black" have been tested work ok.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <? xml version = "1.0" encoding = "utf-8" ?> package = "com.example.androidcustomtitle" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "8" android:targetSdkVersion = "17" /> < application android:allowBackup = "true" android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" android:theme = "@android:style/Theme.Light" > < activity android:name = "com.example.androidcustomtitle.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 >
|
Android Coding: Implement custom title