Monday, August 10, 2015

How to change ListView divider color in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.util.TypedValue;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        Button button = findViewById(R.id.button);
        ListView listView = findViewById(R.id.listView);


        // Initializing a new String Array
        String[] fruits = new String[] {
                "Chokeberry",
                "Chinese Quince",
                "Clementine",
                "Coco Plum",
                "Cola nut"
        };

        // Create a List from String Array elements
        List<String> fruits_list = new ArrayList<String>
                (Arrays.asList(fruits));


        // Create an ArrayAdapter from List
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (
                        this,
                        android.R.layout.simple_list_item_1,
                        fruits_list
                );

        // DataBind ListView with items from ArrayAdapter
        listView.setAdapter(arrayAdapter);


        // Set the button click listener
        button.setOnClickListener(view -> {
            // Set ListView divider color
            listView.setDivider(
                    new ColorDrawable(
                            // 12% transparency
                            Color.parseColor("#1FED2939")
                    )
            );

            // set ListView divider height
            listView.setDividerHeight(dpToPx(1));

            // Disable the button itself
            view.setEnabled(false);
        });
    }


    // Convert dp to equivalent pixels
    private int dpToPx(int dp) {
        Resources resources = getResources();
        return Math.round(
                TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP,
                        dp,
                        resources.getDisplayMetrics()
                )
        );
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change ListView Divider Color"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:divider="#1F95A5FF"
        android:dividerHeight="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>