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);
}
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.
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.
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.
The default is horizontal. A Layout that arranges its children in a single column or a single row... The default orientation is horizontal.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With