worked
worked

Reputation: 5880

Android + setDividerDrawable on a LinearLayout?

I'm interested in adding dividers to a LinearLayout's children dynamically. I see in the docs that LinearLayout contains a CONST "SHOW_DIVIDER_MIDDLE" along with get and set divider methods. Can someone show me how I implement it? Thanks!

"This does not work"

layout xml:

<LinearLayout android:id="@+id/bar"
        android:orientation="horizontal" 
        android:layout_height="40dip" android:layout_width="fill_parent"
        android:background="@drawable/ab_background_gradient" android:gravity="right|center_vertical">

        <!-- sort button -->
        <Button android:id="@+id/sortBtn" android:background="@drawable/defaultt"
                android:layout_width="30dip" android:layout_height="30dip" android:onClick="sortThis" />

        <!-- add button -->
        <Button android:id="@+id/addBtn" android:background="@drawable/defaultt"
                android:layout_width="30dip" android:layout_height="30dip" android:onClick="addThis" />
    </LinearLayout>

main:

...
private void setupViews() {
        //bar
        mBar = (LinearLayout) findViewById(R.id.bar);
        mBar.setDividerDrawable(R.drawable.divider);
}

Upvotes: 6

Views: 9618

Answers (1)

spurkis
spurkis

Reputation: 86

You need to convert the Resource id you get back from R.drawable.divider into a Drawable object, ala:

import android.content.res.Resources;
...

public void onCreate(Bundle savedInstanceState) {
    ...

    Resources res = this.getResources();

    LinearLayout layout = new LinearLayout(this);
    layout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_BEGINNING | LinearLayout.SHOW_DIVIDER_END);
    layout.setDividerDrawable(res.getDrawable(R.drawable.divider));

    ...
 }
...

This assumes you've got a file named 'divider.jpg' (or similar) in your resources directory.

Upvotes: 7

Related Questions