How to create a divider between views?

1

I'm creating an app that uses ListView to show items and wanted to know the best and most personalized way to create splitters between them, I also wanted to know how to use another divider between two icons, which are the options that stay in one Toolbar at the bottom of the screen.

I want the divider between items of ListView type Whatsapp:

ButIalsowanttouseadividerbetweeniconsinaToolbarasinthephoto,rightafterthe"SAVE" (it will not be acting as ActionBar , it will be at the bottom, I took the image because it was the best I found) :

I have tried to use a LinearLayout to serve as a divisor in any situation, but it was not so cool when android:divider of ListView , is this the only possible way?

In short: Do you have a way to make a divider whenever you need it as a standalone view, which can be fully customizable? This would make it easier to manipulate the size in ListView and also to use between Toolbar options.

    
asked by anonymous 13.11.2017 / 20:04

1 answer

2

android:divider is a drawable. That way you can look what you want.

In this case, to get it to have borders, use a InsetDrawable :

divider.xml

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="45dp"
    android:insetRight="45dp" >

    <shape>
        <solid android:color="#FF0000"  />
    </shape>

</inset>

The attributes

android:insetLeft="45dp"
android:insetRight="45dp"

control the left margin and right margin.

Use this:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/divider"
    android:dividerHeight="2dp"/>

the attribute

android:dividerHeight="2dp"

defines the thickness of divider .

To use separates any view use it in an ImageView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Texto"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/divider"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Texto"/>
</LinearLayout>
    
13.11.2017 / 20:20