1

When building an application, sometimes we need to create a custom window title to suit our needs and make our application differs from others. I've problem on create icon window title on preference activity by this code:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        addPreferencesFromResource(R.layout.setting);
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.setting);
    }

Can anybody help me?

Dewa Yudie
  • 117
  • 1
  • 2
  • 13
  • See [Customized Title Bar in Android](http://stackoverflow.com/questions/11713632/customized-title-bar-in-android/11713708#11713708) for this identical question that was asked previously. – t0mm13b Aug 10 '12 at 00:52

4 Answers4

2
public class Preferences extends PreferenceActivity {  
protected ImageView img;
   @Override
   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);        
    setContentView(R.layout.main_tab);
    title = (TextView) findViewById(R.id.img);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
    Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
    int width=30;
    int height=20;
    Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
    this.img.setImageBitmap(resizedbitmap);

  }
}
///////In title_bar.xml///////

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
     >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="3dp">
    </ImageView>



</RelativeLayout>
Furqi
  • 2,403
  • 1
  • 26
  • 32
0

Try:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.drawable.icon);
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

try this

  1. Create custom layout for window title in “layout” folder.

window_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:background="#323331">

    <ImageView
        android:id="@+id/header"
        android:src="@drawable/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
  1. Create custom style in “values” folder.

custom_style.xml

<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">35dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>
  1. Apply custom style in manifest file as theme.

AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/CustomTheme">

4.Apply custom window title in main activity class

CustomWindowTitle

    public class CustomWindowTitle extends PreferenceActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,  R.layout.Your_Custom_Title);
super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.YourPrf);
        }
    }
AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31
0

You can change icon and title of your preference activity in your Manifest file:

    <activity
        android:name="PATH TO YOUR ACTIVITY"
        android:label="@string/title_from_resources" 
        android:icon="@drawable/icon_from_resources" >
    </activity>
ThagoX
  • 1