0

I'm trying to provide a quick choice of specific colours in my Android app, and I'd like to be able to set the list options in a simple dialog to be the specific colours. Seems like it should be easy but I haven't been able to find a direct answer.

How can I (programmatically at run time) set the background colour of each option accordingly?

HomerPlata
  • 1,687
  • 5
  • 22
  • 39

2 Answers2

0

I think this might be useful:

private void showAlertDialogForColors() {
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View alertLayout = inflater.inflate(R.layout.choose_color_theme, null);
        //alert.setTitle("Theme Color");
        // this is set the view from XML inside AlertDialog
        alert.setView(alertLayout);
        final Integer tempValue = SessionManager.getInstance(ColorSettingsActivity.this).getPrefThemeCode();

        TextView textViewBlue = (TextView) alertLayout.findViewById(R.id.themeColorBlue);
        TextView textViewOrange = (TextView) alertLayout.findViewById(R.id.themeColorOrange);
        TextView textViewPink = (TextView) alertLayout.findViewById(R.id.themeColorPink);
        TextView textViewPurple = (TextView) alertLayout.findViewById(R.id.themeColorPurple);
        TextView textViewCyan = (TextView) alertLayout.findViewById(R.id.themeColorCyan);
        TextView textViewDarkGreen = (TextView) alertLayout.findViewById(R.id.themeColorDarkGreen);
        selectedThemeColor = (TextView) alertLayout.findViewById(R.id.textViewSelectedTheme);
        selectedThemeColor.setTypeface(font);

        textViewBlue.setOnClickListener(this);
        textViewOrange.setOnClickListener(this);
        textViewPink.setOnClickListener(this);
        textViewPurple.setOnClickListener(this);
        textViewCyan.setOnClickListener(this);
        textViewDarkGreen.setOnClickListener(this);


        // disallow cancel of AlertDialog on click of back button and outside touch
        alert.setCancelable(false);
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SessionManager.getInstance(MessageSettingsActivity.this).setPrefThemeCode(tempValue);
                Toast.makeText(getBaseContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
            }
        });
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                recreate();
            }
        });
        AlertDialog dialog = alert.create();
        dialog.show();
    }

If you have a set of predefined colors use this or you want to pass colors dynamically stored in Shared Preferences or DB and populate on a list

Then implement switch to do your operation whenever he clicks on particular item in my case there are 5 constant.

ik024
  • 3,566
  • 7
  • 38
  • 61
Ramesh Kanuganti
  • 275
  • 1
  • 11
0

Thanks for the responses, guys. I've figured it out...

private void showColourChooserDialog(){
    final ColourItem[] items = {
            new ColourItem(Color.RED),
            new ColourItem(Color.GREEN),
            new ColourItem(Color.BLUE),
    };      

    ListAdapter adapter = new ArrayAdapter<ColourItem>(
            this,
            android.R.layout.select_dialog_item,
            android.R.id.text1,
            items){
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView)v.findViewById(android.R.id.text1);

            int colour = items[position].getColour();
            tv.setBackgroundColor(colour);

            return v;
        }
    };

    new AlertDialog.Builder(this)
    .setTitle("Choose Colour")
    .setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // ... respond to choice here
        }
    }).show();
}

The ColourItem class:

public class ColourItem {
    private String displayString;
    private int colour;

    public ColourItem (int colour) {
        this(colour, "");
    }
    public ColourItem (int colour, String displayString) {
        this.colour = colour;
        this.displayString = displayString;
    }

    @Override
    public String toString() {
        return displayString;
    }
    public void setDisplayString(String s){
        this.displayString = s;
    }

    public int getColour(){
        return colour;
    }
    public void setColour(int i){
        this.colour = i;
    }
}

The answer was inspired by: This solution regarding custom icons for dialog items

Community
  • 1
  • 1
HomerPlata
  • 1,687
  • 5
  • 22
  • 39