Search code examples
androidandroid-canvaspainttouchscreencolor-picker

How to choose different colors from ColorPicker and apply it to a text or different shapes in the same screen in Android?


enter image description here

If user selects any color from colorpicker then should be able to apply it to a text or different shapes in a same activity or screen in Android.

In my screen, shapes and color picker is there. By using onTouch event, I could be able to draw different shapes . Also I can write anything on a screen. In my app, color picker works fine.

At first- if I draw triangle and write some text like "meena", then applied blue color from color picker, it works fine

At second- In the same screen (below the text "meena") I clicked rose color from color picker and chose different shapes like rectangle and I wrote a word apple. I have already opted for rose color this time.

So from first and second step, the whole different shapes and words are applied in a rose color .

My question is, how can user choose a different colors from color picker and can write a text or draw different shapes in different color in the touch screen using touch event in a same activity or screen? So, in the entire screen different colors could be find.

But, my problem is, in the entire screen only one color has applied.

I want to apply colors from the color picker to the text in the same screen or activity like this. See above image for reference.


Solution

  • You need you use paint and change the paint color as picked by color picker and invalidate the code like this

    public class DrawPaint extends View{
    Paint textPaint = new Paint();
    Paint circlePaint = new Paint();
    
    public DrawPaint(Context context){
        super(context);
        //set default colors for them 
        textPaint.setColor(Color.RED);
        circlePaint.setColor(Color.GREEN);
    }
    
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawText("mu text " , 0 , 0 , textPaint);
        canvas.drawCircle(10 , 10 , 20 , circlePaint);
    }
    
    public void changeTextColor(int color){
        textPaint.setColor(color);
        invalidate();
    }
    
    public void changeCircleColor(int color){
        circlePaint.setColor(color);
        invalidate();
    }
    }
    

    get the reference of this view and when you get color from color picker call changeColor method

    or Here is working solution Finger-Paint