Sunday, October 23, 2016

android - AlertDialog list style

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="#1e3376"
    >
    <Button
        android:id="@+id/btn_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:layout_gravity="top|center_horizontal"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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


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

    private CoordinatorLayout mCLayout;
    private Button mButton;
    private Typeface mTypeface;


    @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);
        mButton = (Button) findViewById(R.id.btn_alert);

        // Initialize a new typeface
        mTypeface = Typeface.createFromAsset(getAssets(),"fonts/CourgetteRegular.ttf");

        // Set a click listener for the button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Initializing a new alert dialog
                final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

                // Set the alert dialog title
                builder.setTitle("Choose a flower.");

                // Initialize a new list of flowers
                final List<String> flowers = new ArrayList<String>();
                flowers.add("Echinacea");
                flowers.add("English Bluebell");
                flowers.add("Erica");
                flowers.add("Eustoma");
                flowers.add("Evening Primrose");

                // Initialize a new array adapter instance
                ArrayAdapter arrayAdapter = new ArrayAdapter<String>(
                        mContext,
                        android.R.layout.simple_list_item_1,flowers
                ){
                    @Override
                    public View getView( int position,View convertView, ViewGroup parent){
                        // Cast list view each item as text view
                        TextView text_view = (TextView) super.getView(position,convertView,parent);

                        // Set item text font
                        text_view.setTypeface(mTypeface);

                        // Set text size
                        text_view.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);

                        // Get the list view odd and even position items
                        if(position%2 == 1){ // Odd items
                            // Set the list view one by one items (row) background color
                            text_view.setBackgroundColor(Color.parseColor("#FFFF33A0"));

                            // Set the list view one by one items text color
                            text_view.setTextColor(Color.parseColor("#FF831952"));
                        }else { // Even items
                            // Set the list view alternate items (row) background color
                            text_view.setBackgroundColor(Color.parseColor("#FF1B9BFD"));

                            // Set the list view alternate items text color
                            text_view.setTextColor(Color.parseColor("#FF005BA1"));
                        }

                        // Finally, return the modified items
                        return text_view;
                    }
                };

                // Set a single choice items list for alert dialog
                builder.setSingleChoiceItems(
                        arrayAdapter, // Items list
                        -1, // Index of checked item (-1 = no selection)
                        new DialogInterface.OnClickListener() // Item click listener
                        {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Get the alert dialog selected item's text
                        String selectedItem = flowers.get(i);

                        // Display the selected item's text on toast
                        Toast.makeText(mContext,"Checked : " + selectedItem,Toast.LENGTH_LONG).show();
                    }
                });

                // Set the a;ert dialog positive button
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Just dismiss the alert dialog after selection
                        // Or do something now
                    }
                });

                // Create the alert dialog
                AlertDialog dialog = builder.create();

                // Finally, display the alert dialog
                dialog.show();
            }
        });
    }
}