How to Add (Vertical) Divider to a Horizontal Linearlayout

How to add (vertical) divider to a horizontal LinearLayout?

use this for horizontal divider

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/honeycombish_blue" />

and this for vertical divider

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/honeycombish_blue" />

OR if you can use the LinearLayout divider, for horizontal divider

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:height="1dp"/>
<solid android:color="#f6f6f6"/>
</shape>

and in LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/divider"
android:orientation="vertical"
android:showDividers="middle" >

If you want to user vertical divider then in place of android:height="1dp" in shape use android:width="1dp"

Tip: Don't forget the android:showDividers item.

Divider for vertical LinearLayout?

For the vertical layout, in the drawable, I think you need to replace

  <size android:width="1dip" />

with

  <size android:height="1dip" />

Programmatically setting LinearLayout divider size

your drawable height is 0. try the following:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="10dp"
android:height="10dp" />
<!--android:height depends on the height you want for the horizontal line--->
</shape>

or add:

drawable.setIntrinsicHeight(height);

How build a vertical divider

You can use a View

Horizontal:

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/holo_blue_light" />

Vertical:

<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/holo_blue_light" />

Android Drawing Divider in Layout

In your styles xml, you could add this code for a vertical divider.

<style name="Divider">
<item name="android:layout_width">1dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">@android:color/darker_gray</item>
</style>

Then in your main activity xml, you could add

<View style="@style/Divider" />

between the two linear layouts, so the final code would be along the lines of

<Linear Layout... for Team A/>
<View style="@style/Divider" />
<Linear Layout... for Team B/>

Hope it helps.

Linearlayout programmatically - How to set up a divider?

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);

Cannot add Vertical Divider to TableLayout Android

Here is the main xml file. You can use

    android:divider="@drawable/vertical_divider"
android:showDividers="middle"

to display vertical divider in Linear layout. I have designed your layout, I don't like Table layout, You can try Linear layouts.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="@drawable/vertical_divider"
android:showDividers="middle">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="Lorem ipsum" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="Lorem ipsum" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFCCCCCC"></View>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/vertical_divider"
android:orientation="horizontal"
android:showDividers="middle">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="Lorem ipsum" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="Lorem ipsum" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFCCCCCC"></View>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/vertical_divider"
android:orientation="horizontal"
android:showDividers="middle">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="Lorem ipsum" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="Lorem ipsum" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFCCCCCC"></View>

</LinearLayout>

Here is the code of

rounded_corner.xml

  <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#bababa" />

<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />

<corners
android:bottomLeftRadius="25dip"
android:bottomRightRadius="25dip"
android:topLeftRadius="25dip"
android:topRightRadius="25dip" />
</shape>

Here is the code of

vertical_divider.xml

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="2dp"
android:insetTop="2dp">
<shape>
<size android:width="1dp" />
<solid android:color="#FFCCCCCC" />
</shape>
</inset>

Place your color codes above :)

Output

Sample Image



Related Topics



Leave a reply



Submit