Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 379
worked Avatar asked Jul 25 '11 17:07

worked


People also ask

How do I fix the bottom layout on my Android?

You can set the layout_height="0dp" of your header, footer and ScrollView and define a layout_weight . Just play around with the values until you find out which works best. The resulting heights of header and footer would dynamically change with the screensize.

What are the orientation values used by the LinearLayout?

Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc. This specifies the direction of arrangement and you will use "horizontal" for a row, "vertical" for a column. The default is horizontal.

What is LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

What is the default value of the orientation attribute in LinearLayout?

The default is horizontal. A Layout that arranges its children in a single column or a single row... The default orientation is horizontal.


1 Answers

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.

like image 186
spurkis Avatar answered Oct 14 '22 14:10

spurkis