Search code examples
androidtimepicker

Android Picker with String


I have got a simple idea.

I would like to use a picker like that and insert Strings into it. Has anyone an idea how to realize that ?

So instead of the numbers I would like to insert names into the round time picker.

Here is a sample implementation for the usual time picker.

enter image description here


Solution

  • As it turns out from the source code, basically these numbers inside the Circular are just Texts over the Bitmaps.

    The basic principle to achieve that goal is to use Canvas class. Canvas holds the "draw" calls. So to draw something, you need 4 basic components:

    • A Bitmap to hold the pixels
    • a Canvas to host the draw calls (writing into the bitmap)
    • a drawing primitive (e.g. Rect, Path, text, Bitmap) and
    • a paint (to describe the colors and styles for the drawing).

    So we create Paint object and set them text size, color, shadow, etc using Canvas object.

    A typical example for using Canvas:

     Paint paint = new Paint(); 
     paint.setColor(Color.WHITE); 
     paint.setStyle(Style.FILL); 
     canvas.drawPaint(paint); 
    
     paint.setColor(Color.BLACK); 
     paint.setTextSize(20); 
     canvas.drawText("Text to be drawn", x-co ord, y-co ord, paint obj);
    


    But, since the input type parameter for Text setup for initializing the drawTexts() is already in String format in the link you mentioned, thing that is required to change as you can see here in the initialize() method is to change the 3 int arrays defined namely hours, hours_24 and minutes to String arrays that you want.

    This will make the Texts display "One", "Two", "Three" etc. rather than "1", "2", "3".

    Ofcourse, you will have to change the type of other getter/setters or any other methods that calls this initialize() and that relates to these int arrays everywhere.

    I hope it helps.