7

enter image description here

As you can see in the image, it is taking some additional space in left . Later I have found that this space is assigned for icon. How can I remove this space?

I have tried preference.setIcon(null);

Also I have tried the solution given here . But no luck .

I am using compile 'com.android.support:preference-v7:25.1.1'

Edit

Here is my style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlayCustom</item>
</style>

<style name="PreferenceThemeOverlayCustom" parent="PreferenceThemeOverlay">
    <item name="preferenceFragmentListStyle">@style/PreferenceFragmentListCustom</item>
</style>

<style name="PreferenceFragmentListCustom" parent="PreferenceFragmentList">

    <item name="android:paddingEnd">0dp</item>
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
    <item name="paddingStart">0dp</item>
</style>
Community
  • 1
  • 1
mSapps
  • 601
  • 9
  • 24
  • why down vote ? Whats the reason? – mSapps Feb 22 '17 at 10:33
  • try a new sample project in Android studio with template "Settings Activity" and see if it works there first what you want to achieve – Ultimo_m Dec 13 '17 at 20:06
  • If anyone is still facing this problem I recommend downgrading your preference library to implementation 'com.android.support:preference-v7:27.1.1'. This fixed it for me. I guess some kind of problem in the version between 27.1.1 and 28.0.0 occured – Stoyan Ivanov Dec 02 '18 at 20:35

3 Answers3

2

Constructing the new preference like this:

new Preference(getPreferenceScreen().getContext());

should solve the issue.

As mentioned by guillaume-tgl the explanation can be found here.

Edit: Using the new support library (28.0.0) you should also call:

preference.setIconSpaceReserved(false);

  • 1
    Actually, this is the correct answer. See also https://stackoverflow.com/questions/32452412/new-preference-support-library-incorrect-theme-at-runtime – guillaume-tgl Jul 16 '18 at 09:53
  • 2
    You're right. This is the solution: "preference.setIconSpaceReserved(false);" – T-igra Sep 29 '18 at 21:34
1

Just add app:iconSpaceReserved="false" line to your XML file and now there won't be any space from start.

0

I think its due to the PreferenceThemeOverlay that you are using for that activity. Your selected style taking default padding from left or right.

use this code

<style name="" parent="PreferenceThemeOverlay">
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
</style>
Zakir hussain
  • 392
  • 1
  • 10