Search code examples
javaandroidcolorscolor-picker

Android : ColorPicker only showing one color, and selecting wrong colors from Pallete


I am working on a project, where I would require a color-picker, with specific colors. As my requirements were simpler, I didn't wanted anything fancy, and I ended up with this from Github. The implementation is simple, although I can only see one color. I have the colors which I want specified in colors.xml, so I added them, but doesn't help, it's just purple for entire bar.

Screenshot :

enter image description here

XML :

   <uz.shift.colorpicker.LineColorPicker
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/colorPicker"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:orientation="horizontal"/>

Code :

public class EditNoteActivity extends Activity {

    LineColorPicker colorPicker;

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_note);
 colorPicker = (LineColorPicker)findViewById(R.id.colorPicker);

        colorPicker.setColors(new int[] {R.color.noteAqua,R.color.noteBlue,R.color.noteDarkBlue,R.color.noteDeepBlue,
                R.color.noteDeepRed,R.color.noteGreen,R.color.noteGrey,
                R.color.noteOrange,R.color.notePink,R.color.notePurple,R.color.noteRed,
                R.color.noteYellow});
  colorPicker.setOnColorChangedListener(new OnColorChangedListener() {
            @Override
            public void onColorChanged(int c) {
                Log.d("Selected color ",Integer.toHexString(c));
            }
        });
}
private void setColorForPicker(String noteColor){
    if (noteColor.equals("#1abc9c")) {
        colorPicker.setSelectedColor(R.color.noteAqua);

    }
    if (noteColor.equals("#3498db")) {
        colorPicker.setSelectedColor(R.color.noteBlue);
    }
    if (noteColor.equals("#34495e")) {
        colorPicker.setSelectedColor(R.color.noteDarkBlue);
    }
    if (noteColor.equals("#0071c1")) {
        colorPicker.setSelectedColor(R.color.noteDeepBlue);
    }
    if (noteColor.equals("#c00000")) {
        colorPicker.setSelectedColor(R.color.noteDeepRed);
    }
    if (noteColor.equals("#2ecc71")) {
        colorPicker.setSelectedColor(R.color.noteGreen);

    }
    if (noteColor.equals("#95a5a6")) {
        colorPicker.setSelectedColor(R.color.noteGrey);
       }
    if (noteColor.equals("#e67e22")) {
        colorPicker.setSelectedColor(R.color.noteOrange);
    }
    if (noteColor.equals("#ff56bb")) {
        colorPicker.setSelectedColor(R.color.notePink);
    }
    if (noteColor.equals("#9b59b6")) {
        colorPicker.setSelectedColor(R.color.notePurple);
    }

    if (noteColor.equals("#e74c3c")) {
        colorPicker.setSelectedColor(R.color.noteRed);
    }
    if (noteColor.equals("#f1c40f")) {
        colorPicker.setSelectedColor(R.color.noteYellow);
    }
}

color.xml file :

  <color name="noteAqua">#1abc9c</color>
    <color name="noteBlue">#3498db</color>
    <color name="noteDarkBlue">#34495e</color>
    <color name="noteDeepBlue">#0071c1</color>
    <color name="noteDeepRed">#c00000</color>
    <color name="noteGreen">#2ecc71</color>
    <color name="noteGrey">#95a5a6</color>
    <color name="noteOrange">#e67e22</color>
    <color name="notePink">#ff56bb</color>
    <color name="notePurple">#9b59b6</color>
    <color name="noteRed">#e74c3c</color>
    <color name="noteYellow">#f1c40f</color>

So, what am I doing wrong? THank you.


Solution

  • You are passing the colors identifiers to the array rather than the values themselves, you need to load the values from resources using:

    int noteAqua = getResources().getColor(R.color.noteAqua);
    colorPicker.setColors(new int[] {noteAqua});
    

    Or you can define your array using constants rather than using the color resource:

    int noteAqua = Color.parseColor("#1abc9c")
    colorPicker.setColors(new int[] {noteAqua});