Ak7J
Ak7J

Reputation: 5

Android Drawing Divider in Layout

I've been mainly trying to draw a line in between my layout but can't get it to show nor work.

I've put this in my activity_main.xml yet nothing shows. I've tried changing the color and still nothing. I've tried also placing this in the styles but I don't think it's the right place to put it and I usually get an error when compiling it.

    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

My main xml is. I haven't done any changes to the styles.xml right now.

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="Team A" />

            <TextView
                android:id="@+id/team_a_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="0" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="threePoints"
                android:text="+3 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="twoPoints"
                android:text="+2 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="FreeThrow"
                android:text="Free throw" />


        </LinearLayout>



        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="Team B" />

            <TextView
                android:id="@+id/team_b_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="0" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="threePointsB"
                android:text="+3 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="twoPointsB"
                android:text="+2 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="FreeThrowB"
                android:text="Free throw" />


        </LinearLayout>


    </LinearLayout>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Reset"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="Reset"
        />




</RelativeLayout>

Upvotes: 0

Views: 2658

Answers (3)

roshni batra
roshni batra

Reputation: 39

you can simply add a view

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

Upvotes: 1

Sanjay Ravichandran
Sanjay Ravichandran

Reputation: 44

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.

Upvotes: 0

chef417
chef417

Reputation: 544

To add a view that will serve as a divider, you can insert something like this if using LinearLayout (example for horizontal divider):

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

For more sophisticated approach, see something like this: https://cyrilmottier.com/2014/11/17/grid-spacing-on-android/

Upvotes: 0

Related Questions