ColorPreference class for Android

There are many Android color pickers used for preferences, but they all seem to be unnecessary complex for an ordinary user. If all you need is to let the user choose a single color from a set, you can use this simple class which is conveniently integrated into the Preferences scheme.

      


Put it in a class, and use it from your preferences.xml. Apache license.

/*
 * Copyright (C) 2011-2012 George Yunaev
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 */
public class ColorPreference extends DialogPreference implements DialogInterface.OnClickListener
{
	// Keeps the colors in the nice integer format
	private int[] 	m_colors;

	// Font adaptor responsible for redrawing the item TextView with the appropriate background color
	public class ColorAdapter extends BaseAdapter
	{
		@Override
		public int getCount()
		{
			return m_colors.length;
		}

		@Override
		public Object getItem(int position)
		{
			return m_colors[ position ];
		}

		@Override
		public long getItemId(int position)
		{
			// We use the position as ID
			return position;
		}

		@Override
		public View getView( int position, View convertView, ViewGroup parent )
		{
            View view = convertView;

            // This function may be called in two cases: a new view needs to be created, 
            // or an existing view needs to be reused
            if ( view == null )
            {
            	// Since we're using the system list for the layout, use the system inflater
            	final LayoutInflater inflater = (LayoutInflater) 
                                  getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

            	// And inflate the view android.R.layout.select_dialog_singlechoice
            	// Why? See com.android.internal.app.AlertController method createListView()
            	view = inflater.inflate( android.R.layout.select_dialog_singlechoice, parent, false);
            }

            if ( view != null )
            {
            	// Find the text view from our interface
            	CheckedTextView tv = (CheckedTextView) view.findViewById( android.R.id.text1 );

            	// Color the text view background using our color
            	tv.setBackgroundColor( m_colors[position] );
            }

            return view;
		}
	}

	public ColorPreference( Context context, AttributeSet attrs )
	{
		super(context, attrs);

		// !!! TODO !!!
		// Here you should fill up the m_colors array with the colors you want to use. You can either 
		// have a static initializer, or read the array from resources.
	}

	@Override
	protected void onPrepareDialogBuilder( Builder builder )
	{
        super.onPrepareDialogBuilder(builder);

        int checked_item = 0;
        int selectedValue = getSharedPreferences().getInt( getKey(), 0 );

		// Find out the checked item index
        for ( int idx = 0; idx < m_colors.length; idx++ )
        {
			if ( m_colors[idx] == selectedValue )
			{
				checked_item = idx;
				break;
			}
        }

		// Create out adapter
    	// If you're building for API 11 and up, you can pass builder.getContext 
       // instead of current context
		ColorAdapter adapter = new ColorAdapter();
        builder.setSingleChoiceItems( adapter, checked_item, this );

        // The typical interaction for list-based dialogs is to have 
        // click-on-an-item dismiss the dialog
        builder.setPositiveButton( null, null );
	}

    public void onClick(DialogInterface dialog, int which)
    {
    	if ( which >=0 && which < m_colors.length )
    	{
    		Editor editor = getSharedPreferences().edit();
    		editor.putInt( getKey(), m_colors[ which ] );
    		editor.commit();

    		dialog.dismiss();
    	}
    }
}
This entry was posted in android.

2 Responses to ColorPreference class for Android

  1. jhvdven says:

    Thanks for the code. I added this for feedback to the user:
    @Override
    protected void onBindView(View view) {
    view.setBackgroundColor(getPreferenceManager().getSharedPreferences().getInt(getKey(), 0));
    super.onBindView(view);
    }

  2. Sebastian C. says:

    How to do this in a default time picker wheel? So that I can scroll and pick the color I want?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.