How to instantiate layout for custom preference, using android:layout attribute

hardsky picture hardsky · Oct 20, 2011 · Viewed 7.1k times · Source

I can set appropriate layout for preference through android:layout attribute. For an example

<Preference
  android:key="friction" 
  android:title="@string/friction" 
  android:layout="@layout/friction_fragment" 
  android:shouldDisableView="true" 
  android:defaultValue="30" 
  android:enabled="true"
  android:selectable="true" 
  android:summary="Bite friction">
</Preference>

where layout is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="@string/friction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"></TextView>
    <SeekBar android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/sbFriction"></SeekBar>
    <TextView android:text="@string/friction_little" android:id="@+id/txtSummary" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <Button android:text="Button" android:id="@+id/btnFriction" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

I can get views in OnCreate in PreferenceActivity

    Preference fric = (Preference)this.findPreference("friction");
    View v = fric.getView(null, null);
    SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
    sbFriction.setOnSeekBarChangeListener(this);
    Button btnFric = (Button) v.findViewById(R.id.btnFriction);
    btnFric.setOnClickListener(m_onClick);

but these events listeners, that I have set, are not fired. How I can catch these events, for example - click from button. Edit. No, It did not fire any exception. Here is more detailed code

public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnSeekBarChangeListener
{

    private TextView m_txtSummary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        ListPreference difficulty = (ListPreference)this.findPreference("difficulty");
        difficulty.setSummary(difficulty.getEntry());
        difficulty.setOnPreferenceChangeListener(this);

        Preference fric = (Preference)this.findPreference("friction");
        View v = fric.getView(null, null);
        SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
        sbFriction.setOnSeekBarChangeListener(this);
        Button btnFric = (Button) v.findViewById(R.id.btnFriction);
        btnFric.setOnClickListener(m_onClick);

        m_txtSummary = (TextView)v.findViewById(R.id.txtSummary);

        fric.setSummary(fric.toString());
        fric.setOnPreferenceChangeListener(this);

        CheckBoxPreference music = (CheckBoxPreference)this.findPreference("music");
        music.setOnPreferenceChangeListener(this);
    }

    private OnClickListener m_onClick = new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            v.getId();
        }

    };

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if(newValue instanceof Boolean)
            return true;

        preference.setSummary(newValue.toString());
        return true;
    }

    @Override
    public void onProgressChanged(SeekBar v, int nProgress, boolean arg2) {
        // TODO Auto-generated method stub
        m_txtSummary.append(" " + nProgress);
        m_txtSummary.invalidate();
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        //notifyChanged();
    }
}

Answer

Paul Burke picture Paul Burke · Oct 22, 2011

I'm not sure you are able to use a custom layout in conjunction with a PreferenceActivity in the way that you describe above.

I believe you should either:

Use a PreferenceScreen via addPreferencesFromResource() and implement classes like CheckBoxPreference, DialogPreference, and MultiSelectListPreference for the SharedPreferences items. (example)

or

Create a custom Activity (not PreferenceActivity) with custom layout (using setContentView()), and manually hook into the SharedPreferences using PreferenceManager.getDefaultSharedPreferences() editing them in the event listeners (View.onClickListener(), etc) using SharedPreferences.Editor .

Hope that makes sense.