1

I am using the SettingsActivity generated from AndroidStudio (New, Activity, Settings Activity) that relies on AppCompatDelegate. I have succeeded to add a toolbar and to set a custom divider to the listview used for displayer preference headers as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout root =
            (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.toolbar, root, false);
    root.addView(toolbar, 0);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    ListView listView = getListView();
    listView.setDivider(ContextCompat.getDrawable(this, R.drawable.settings_divider));
    listView.setDividerHeight(1);

    setupActionBar();
}

However, I cannot figure out how to increase the padding assocatied to each listview item neither how to increase spacing between the icon and the preference header title. Is it possible using styles or by passing a custom layout?

enter image description here

Laurent
  • 14,122
  • 13
  • 57
  • 89

2 Answers2

1

You can create a custom layout that overrides PreferenceCategory. For example like this:

public class CustomPreferenceCategory extends PreferenceCategory {

    public CustomPreferenceCategory(Context context) {
         super(context);
    }

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

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

    @Override
    protected View onCreateView(ViewGroup parent) {
        View view = super.onCreateView(parent);

        return view;
    }

    @Override
    public View getView(View convertView, ViewGroup parent) {
        final View view = super.getView(convertView, parent);
        view.setPadding(view.getPaddingLeft(), view.getPaddingTop()*2, view.getPaddingRight(), 10);

        TextView textView = (TextView) view.findViewById(android.R.id.title);
        textView.setGravity(Gravity.BOTTOM);

        return view;
    }
}

By overriding these methods (and some more if you want you can set a padding, margin, backgroundcolor, etc...

Then use CustomPreferenceCategory instead of PreferenceCategory

Of course you can do the same with a Preference (I was not sure if you want custom headers or custom list items - the category will be the header and the preference is a normal list item)

Katharina
  • 1,612
  • 15
  • 27
0

The adapter of your list should inflate you own custom view inside getView method. In your view set a padding to the content. Much like this

Or You could use this solution: https://stackoverflow.com/a/5309871/6507689

Community
  • 1
  • 1
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90