Sunday, October 16, 2016

android - ListView divider color and height

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#a8bfa4"
    >
    <!--
        android:divider
            Drawable or color to draw between list items.

            May be a reference to another resource, in the form "@[+][package:]type:name" or
            to a theme attribute in the form "?[package:][type:]name".

            May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

            This corresponds to the global attribute resource symbol divider.
    -->
    <!--
        android:dividerHeight
            Height of the divider. Will use the intrinsic height of the divider if this is not specified.

            Must be a dimension value, which is a floating point number appended with a unit
            such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels),
            sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

            This may also be a reference to a resource (in the form "@[package:]type:name") or
            theme attribute (in the form "?[package:][type:]name") containing a value of this type.

            This corresponds to the global attribute resource symbol dividerHeight.
    -->
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:divider="#ec1886"
        android:dividerHeight="2dp"
        />
</android.support.design.widget.CoordinatorLayout>

2. ListView divider color and height programmatically

MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private ListView mListView;
    private ArrayAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mListView = (ListView) findViewById(R.id.list_view);

        // Initialize a new list with values
        List<String> flowers = new ArrayList<>();
        flowers.add("Aquilegia");
        flowers.add("Astilbe");
        flowers.add("Astrantia");
        flowers.add("Aubreita deltoidea");
        flowers.add("Bee Balm Flower");

        // Initialize an array adapter
        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,flowers);

        /*
            setDivider
                void setDivider (Drawable divider)

                Sets the drawable that will be drawn between each item in the list.

                Note: If the drawable does not have an intrinsic height, you
                should also call setDividerHeight(int).

                Parameters
                    divider Drawable: the drawable to use
        */

        // Set a new divider color for list view
        mListView.setDivider(new ColorDrawable(Color.parseColor("#FF3049ED")));

        /*
            setDividerHeight
                void setDividerHeight (int height)

                Sets the height of the divider that will be drawn between each item in the list.
                Calling this will override the intrinsic height as set by setDivider(Drawable)

                Parameters
                    height int: The new height of the divider in pixels.
        */

        // Set height for list view divider
        mListView.setDividerHeight(5);

        // Data bind the list view with array adapter items
        mListView.setAdapter(mAdapter);
    }
}