Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferences of varying height within a PreferenceActivity

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.

like image 834
Nick Avatar asked Jan 06 '12 19:01

Nick


People also ask

What are preferences in Android?

Preferences in Android are used to keep track of application and user preferences. In any application, there are default preferences that can accessed through the PreferenceManager instance and its related method getDefaultSharedPreferences(Context)

Where can I find preferences in Android Studio?

From the menu bar, click File > Settings (on macOS, click Android Studio > Preferences).

How do I turn off preferences on Android?

Programmatically: getPreferenceScreen(). findPreference("yourpref"). setEnabled(false);

Which of these methods can be used to view the user preferences in the context of overall Android framework?

In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.


1 Answers

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.

like image 179
theJosh Avatar answered Oct 03 '22 03:10

theJosh