7

I have a custom class that extends Preference that I'm using in conjunction with a PreferenceActivity.

When I try to adjust the height in the layout my Preference is using (with a static layout_height or with wrap_content) it is always displayed in a uniform height cell in the Preference Activity - the same size that all of the "normal" preferences default to.

Is there a way present a given preference with a different layout_height.

I've looked at the API demos related to preferences and I'm not seeing anything that matches what I'm trying to do.

Nick
  • 8,483
  • 10
  • 46
  • 65
  • Looking on an Api level 9 device the preferences are not the same height, but sizes based on the content. For api 7 devices I have had to make changed to cope with sumaries > 2 lines. is this your issue? – Ifor Jan 12 '12 at 22:04

2 Answers2

7

You can override getView(View, ViewGroup) in your Preference. Then send new LayoutParams to the getView(). I tried it with a customized CheckBoxPreference. Works great.

import android.content.Context;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;


public class CustomCheckBoxPreference extends CheckBoxPreference {

public CustomCheckBoxPreference(final Context context, final AttributeSet attrs,
        final int defStyle) {
    super(context, attrs, defStyle);
}

public CustomCheckBoxPreference(final Context context, final AttributeSet attrs) {
    super(context, attrs);
}

public CustomCheckBoxPreference(final Context context) {
    super(context);
}

@Override
public View getView(final View convertView, final ViewGroup parent) {
    final View v = super.getView(convertView, parent);
    final int height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
    final int width = 300;
    final LayoutParams params = new LayoutParams(height, width);
    v.setLayoutParams(params );
    return v;
}

}

Just be careful to use the correct LayoutParams for the View or you might get a class cast exception.

NAUSHAD
  • 174
  • 1
  • 15
theJosh
  • 2,894
  • 1
  • 28
  • 50
-1

This should help:

<!-- Application theme -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- Min item height -->
    <item name="android:listPreferredItemHeight">10dp</item>
</style>

another styling attributes that can be overridden can be found here preference item layout

Original answer https://stackoverflow.com/a/27027982/975886

Community
  • 1
  • 1
Nikolay Nikiforchuk
  • 1,998
  • 24
  • 20
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Mathias Nov 19 '14 at 22:30
  • @Mathias it's just to avoid duplication – Nikolay Nikiforchuk Nov 19 '14 at 22:44