Saturday, July 30, 2016

android - How to create ListView states

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="#627062"
    >
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/states_list_view_items"
        android:background="#f9289e"
        android:choiceMode="singleChoice"
        >
    </ListView>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
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;


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

    private CoordinatorLayout mCLayout;
    private ListView mListView;


    @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 ArrayList
        ArrayList<String> list = new ArrayList<String>();

        // Add some items to the list
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        list.add("f");
        list.add("g");
        list.add("h");
        list.add("i");
        list.add("i");
        list.add("j");
        list.add("k");
        list.add("l");
        list.add("m");
        list.add("n");
        list.add("o");
        list.add("p");
        list.add("q");

        // Initialize a new ArrayAdapter instance
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,R.layout.list_view_custom_item,list);

        // DataBind the ListView with the ArrayAdapter
        mListView.setAdapter(adapter);
    }
}
res/layout/list_view_custom_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/states_list_view_items"
    android:padding="8dp"
    android:fontFamily="sans-serif-condensed"
    android:textAppearance="?android:attr/textAppearanceLarge"
    />
res/drawable/states_list_view_items.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- normal state -->
    <item
        android:state_activated="false"
        android:state_pressed="false"
        android:drawable="@drawable/list_view_item_bg"
        />
    <!-- pressed stat -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/list_view_item_pressed_bg"
        />
    <!-- selected state -->
    <item
        android:state_activated="true"
        android:state_pressed="false"
        android:drawable="@drawable/list_view_item_selected_bg"
        />
</selector>
res/drawable/list_view_item_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <gradient
        android:startColor="#e11d8c"
        android:endColor="#f91f9a"
        android:angle="90"
        />
</shape>
res/drawable/list_view_item_pressed_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <stroke android:color="#31588e" android:width="1dp"/>
    <solid android:color="#4782d4"/>
</shape>
res/drawable/list_view_item_selected_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <stroke android:color="#1b602b" android:width="1dp"/>
    <solid android:color="#3aa641"/>
</shape>