Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android set divider padding for preference screen

I have PreferenceScreen contain many CheckBox , i customize it by refer it to custom layout as bellow :

<?xml version="1.0" encoding="utf-8" ?> 
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <CheckBoxPreference 
   android:summary="checkbox one" 
   android:key="key_one" 
   android:layout="@layout/mylayout"      
    /> 
  <CheckBoxPreference 
   android:summary="checkbox two"
   android:key="key_two" 
   android:layout="@layout/mylayout"      
    /> 
</PreferenceScreen>

mylayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:minHeight="?android:attr/listPreferredItemHeight"  
  android:gravity="center_vertical" 
  android:paddingRight="?android:attr/scrollbarSize">
<LinearLayout 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent"  
  android:gravity="center" 
  android:minWidth="@dimen/preference_icon_minWidth" 
  android:orientation="horizontal">
<ImageView 
  android:id="@+android:id/icon" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_gravity="center" /> 
</LinearLayout>
<RelativeLayout 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_marginLeft="16dip"
  android:layout_marginRight="8dip" 
  android:layout_marginTop="6dip" 
  android:layout_marginBottom="6dip" 
  android:layout_weight="1">
<TextView 
  android:id="@+android:id/title" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textAppearance="?android:attr/textAppearanceMedium" 
  android:fadingEdge="horizontal" /> 
<TextView 
  android:id="@+android:id/summary" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_below="@android:id/title" 
  android:layout_alignLeft="@android:id/title"
  android:textAppearance="?android:attr/textAppearanceSmall" 
  android:textColor="?android:attr/textColorSecondary" 
  android:maxLines="4" /> 
</RelativeLayout>
<LinearLayout 
  android:id="@+android:id/widget_frame" 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent" 
  android:minWidth="@dimen/preference_widget_width" 
  android:gravity="center"
  android:orientation="vertical" /> 
</LinearLayout>

Every thing run fine also i customize divider between checkbox in prefs activity as :

 ListView list = (ListView) findViewById(android.R.id.list);
    list.setDivider(new ColorDrawable(Color.RED)); // or some other color int
    list.setDividerHeight((5)); 
    list.setVerticalScrollBarEnabled(false);

when i want to set padding to divider only as :

  list.setPadding(0, 0, 0, 0);

it set padding to all view ,

HOW can i set padding to divider only without affect padding of all view .

any help will be appreciated, thanks

like image 710
androidqq6 Avatar asked Aug 01 '13 01:08

androidqq6


1 Answers

To allow preference divider padding

first : add this line to your preference activity which lead to transparency of android default divider

list.setDivider(new ColorDrawable(0x00000000));

second : create folder named drawable in res , then create on it divider.xml which will be as below :

<?xml version="1.0" encoding="utf-8" ?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="#B22222" /> 
 </shape>

third :

add View to your mylayout.xml so it will be as below :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:minWidth="@dimen/preference_icon_minWidth"
    android:orientation="horizontal">
    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dip"
    android:layout_marginRight="8dip"
    android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip"
    android:layout_weight="1">

    <TextView android:id="@+android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:textAppearance="?android:attr/textAppearanceMedium"           
        android:fadingEdge="horizontal" />

    <TextView android:id="@+android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/title"
        android:layout_alignLeft="@android:id/title"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="?android:attr/textColorSecondary"
        android:maxLines="4" />

    <View 
       android:id="@+id/divider" 
       android:background="@drawable/divider" 
       android:layout_width="match_parent" 
       android:layout_marginLeft="30dp" 
       android:layout_marginRight="30dp" 
       android:layout_height="5dp" 
       android:layout_below="@+android:id/summary"/> 

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:minWidth="@dimen/preference_widget_width"
    android:gravity="center"
    android:orientation="vertical" />

the point here you add View bellow your texts and refer that view to divider shape in drawable res so finally you will get an custom divider which it can be customized as you need.

hope that help you .

the output will be as below:

enter image description here

like image 188
Android Stack Avatar answered Nov 15 '22 19:11

Android Stack