Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to add a divider to a horizontal linear layout but am getting nowhere. The divider just doesn't show. I am a total newbie with Android.

This is my layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity" >          <LinearLayout          android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:id="@+id/llTopBar"         android:orientation="horizontal"         android:divider="#00ff00"         android:dividerPadding="22dip"         android:showDividers="middle">          <Button             android:layout_width="wrap_content"             android:layout_height="match_parent"             android:text="asdf" />                      <Button             android:layout_width="wrap_content"             android:layout_height="match_parent"             android:text="asdf" />          </LinearLayout>      </RelativeLayout> 
like image 663
Ahmed-Anas Avatar asked Feb 28 '13 06:02

Ahmed-Anas


People also ask

How do you create a LinearLayout vertical?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do I add a line in android?

This example demonstrates how do I draw a line in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is orientation property in LinearLayout?

The LinearLayout is the most basic layout, and it arranges its elements sequentially, either horizontally or vertically. To arrange controls within a linear layout, the following attributes are used: android:orientation—Used for arranging the controls in the container in horizontal or vertical order.


1 Answers

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.

like image 113
Kapil Vats Avatar answered Sep 24 '22 23:09

Kapil Vats