I have a PreferenceFragment subclass. I want each one of its items (Preferences and SwitchPreferences) to have a height of 120dp. How to do that?
Here is the related code:
public class SettingsFragment extends PreferenceFragment {
public SettingsFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main);
}
}
and
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference android:key="app_main_switch"
android:title="@string/app_name"
android:defaultValue="true"/>
<Preference android:title="@string/events_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
</Preference>
<Preference android:title="@string/filters_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
</Preference>
<SwitchPreference android:dependency="app_main_switch"
android:key="learn_switch"
android:defaultValue="false"
android:title="@string/learning"/>
</PreferenceScreen>
Here is how it looks like now:
So I want all four items of the list to have a height of 120dp. As you can see I'm not the one creating the ListView, it's created internally. I tried to retrieve it with
findViewById(android.R.id.list)
but iterating over its elements gives Preference objects which do not allow me to set the height.
Just do it this way:
<!-- 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
Try creating a custom Preference class (you may have to do it for every type of Preference you are going to use/want height to be 120DP)
public class SwitchPref extends SwitchPreference {
Context context;
public Pref(Context context) {
super(context);
this.context = context;
}
@Override
protected View onCreateView(ViewGroup parent) {
LinearLayout layout = new LinearLayout(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(120));
layout.setLayoutParams(params1);
//if this returns just the linearlayout, you will have to add your own switch
// or reference to a layout.xml resource
return super.onCreateView(layout);
}
public int dpToPx(int dp) {
int valueInDp = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
.getDisplayMetrics());
return valueInDp;
}
}
Then, in your preference.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!--use your custom SwitchPrefence class-->
<my.package.name.SwitchPref android:key="app_main_switch"
android:title="@string/app_name"
android:defaultValue="true"/>
<Preference android:title="@string/events_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
</Preference>
<Preference android:title="@string/filters_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
</Preference>
<my.package.name.SwitchPref android:dependency="app_main_switch"
android:key="learn_switch"
android:defaultValue="false"
android:title="@string/learning"/>
</PreferenceScreen>
Hope this helps, happy coding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With