Insert divider line programmatically

1

How can I place a div line programmatically using java? The XML code is:

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ddd" />
    
asked by anonymous 31.05.2017 / 12:42

1 answer

2

For example, to insert into a LinearLayout , you first need to instantiate it:

LinearLayout linear = (LinearLayout)findViewById(R.id.linearLayout);  

Then just create a View programmatically in this way, using the setLayoutParams method to set the width settings View :

View divider = new View(this);
divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
divider.setBackgroundColor(Color.rgb(51, 51, 51));
linear.addView(divider);

You can also set a color using a static color from the Color class . See:

linear.setBackgroundColor(Color.RED);
    
31.05.2017 / 14:59