Change the height of preference items

I have a subclass of PreferenceFragment. I want each of its elements (Preferences and SwitchPreferences) to have a height of 120dp. How to do it?

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's what it looks like now:

enter image description here

Therefore, I want all four list items to have a height of 120dp. As you can see, I'm not the one who creates the ListView, it is created internally. I tried to get it using

 findViewById(android.R.id.list) 

but iterating over its elements gives Preference objects that do not allow you to set the height.

+3
source share
3 answers

Here's how I managed to do this:

I needed to subclass Preference, but MattMatt's answer didn't work.

 public class HigherPreference extends Preference { public HigherPreference(Context context) { super(context); } public HigherPreference(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected View onCreateView(ViewGroup parent) { Resources res=getContext().getResources(); View v=super.onCreateView(parent); v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, res.getDimensionPixelSize(R.dimen.rowheight))); return v; } } 

The XML format looks like it provided.

+1
source

Just do this:

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

other style attributes that can be overridden can be found here location of preference items

+4
source

Try creating your own preference class (you may have to do this for each type of preference you want to use / want the 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!

+1
source

Source: https://habr.com/ru/post/1389709/


All Articles