Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set custom title bar in PreferenceAcivity

Tags:

android

I am using PreferenceActivity, how can I set a custom title bar? Not only text but background color, size - the whole layout.

like image 252
fhucho Avatar asked Feb 07 '11 13:02

fhucho


Video Answer


2 Answers

PreferenceActivity extends ListActivity, and when you inflate the preferences from xml with addPreferencesFromResource(), it puts the stuff into the standard ListView that ListActivity uses.

So basically, you can use setContentView() to specify a layout, but you need to have a ListView in it with the id "@+android:id/list".

So using kleini's example code:

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.login_settings);
    setContentView(R.layout.login_settings_layout);
}

You would need a ListView in login_settings_layout.xml that looks something like:

<ListView 
    android:id="@+android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    /> 
like image 174
Ben Avatar answered Sep 19 '22 01:09

Ben


This is the only thing that worked for me. Rest of the above stuff did not fetch desired results on my 4.3 Nexus Tablet.

I could not really recreate a proper actionbar like widget, but was able to put a large 'Settings' title on top of the PreferenceActivity, with below steps.

I got the hint from another stackoverflow answer and am just making it more elaborate.

  1. In PreferenceActivity class (remove the existing titlebar)

    public class Settings extends PreferenceActivity· {
      ...
      @Override
      public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE); // This goes first
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    
  2. In res/xml/settings.xml, I would like to draw attention to the first PreferenceCategory

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
       <PreferenceCategory
          android:layout="@layout/settings_titlebar" />
    
       <PreferenceCategory android:title="Notifications">
          <Preference .....
    
  3. In res/layout/settings_titlebar.xml

Navals-MacBook-Pro:ver_ui_0.2 Naval$ vi res/layout/settings_titlebar.xml

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:background="@drawable/header_background"
     android:enabled="false">

     <TextView android:src="@drawable/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:padding="4dip"
        android:text="Settings"
        android:textSize="32sp"
        />

   </RelativeLayout>
like image 26
navalsaini Avatar answered Sep 21 '22 01:09

navalsaini