5

I'm trying to set a divider to be used in a list for my app. I have made the XML code for the "dicedivider" as shown below

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
   android:width="1px"
   android:color="@color/divider_Color"
   />

</shape>

Then I am trying to set it as the divider drawable for my LinearLayout as shown below

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    diceCount = 0;
    diceList = (LinearLayout) this.findViewById(R.id.main_Dice_List);

    diceList.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
    diceList.setDividerDrawable(this.getResources().getDrawable(R.drawable.dicedivider));
    diceList.setDividerPadding(5);
    addDice();
}

Regardless of this though the app shows no dividers. I've even tried embedding it directly into the XML and not had any luck.

I'm very new at Android coding. Any idea where I'm going wrong?

Jamstruth
  • 51
  • 1
  • 2

4 Answers4

1

Create a file mydivider.xml inside res/drawable and put the following shape:

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

add the shape as divider for your layout

<LinearLayout android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:divider="@drawable/mydivider"
    android:showDividers="middle"
    android:dividerPadding="22dp">    
</LinearLayout>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
seetharam
  • 47
  • 4
0

You can use in the xml

<View 
        android:layout_width="fill_parent"
        android:layout_height="1dp"       android:Background="@android:color/darker_gray"/>

To set a divider just after your layout

Stefano Munarini
  • 2,711
  • 2
  • 22
  • 26
  • This was the solution I came at after much messing around (though I made the view in Java code because I need to add them dynamically). It's a shame the actual android code doesn't work though. It would have been more elegant. – Jamstruth Aug 19 '13 at 13:01
  • Why don't add dynamically this view by code? You create a linear layout and then just after that linear you add this view. Isn't this a solution for you? – Stefano Munarini Aug 19 '13 at 14:16
0

Try using shape ="rectangle" instead of line.

<shape
    android:shape="rectangle">

    <size android:height="1px" />
    <solid android:color="@color/white" />

</shape>
abpatil
  • 916
  • 16
  • 20
Selcan
  • 41
  • 1
  • 4
-3

You must set the divider to the listView, not to the LinearLayout

Javier Enríquez
  • 630
  • 1
  • 9
  • 25
  • 1
    There is no ListView. Only a LinearLayout. As far as I could gather when I made this code a ListView wouldn't do what I wanted to do here. This LinearLayout contains a series of other LinearLayouts which contain all the useful widgets etc. I need for the code. When I looked up info on ListView it could only contain one other View with no children. The divider methods exist for the LinearLayout. If they don't work why are they there? – Jamstruth Aug 18 '13 at 22:21