Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linearlayout programmatically - How to set up a divider?

I am creating TextViews in LinearLayout programmatically and I would like to separate them with a divider (just a simple line). I have googled endlessly, what I have found is that I can use .setDividerDrawable, but I don't want to use external images for this. Any tips?


1 Answers

How to Add Divider to an Android Layout Programmatically

Create a View 1 or 2 pixels tall and width match_parent and set the background color to whatever color you want the divider to be.

Separate the divider from the items above and below with margin settings.

Example:

ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp = 
    new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.WHITE);
like image 89
David Manpearl Avatar answered Jan 27 '26 12:01

David Manpearl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!