SignIt! Part III (Alert Dialog and HashMap )


SIGN IT PART III

Hey everyone! Welcome to the final part of the Sign It series. In this section we will add the functionality to let the user select the stroke width and color of the paint object. Also the method of saving different styled paths to the bitmap is also demonstrated.

Before moving any further, it's highly recommended to go through the previous pars of the series for the basic code. Let's go now.

1. Add the function to change the Stroke Width

  • Add this function in our CanvasView.java class.
  • In this function we create an AlertDialog using AlertDialog.Builder.
  • The setPositiveButton() is used to add the "OK" button to the dialog. In the same way a negative or "Cancel" button can be added using setNegativeButton().
  • setSingleChoiceItems() is used to add the CharSequence declared before to the addded to the Alert Dialog.
  • Finally we show the Alert Dialog using the show() member function.


2. Add the function to change the Paint Color

  • Same as what we have done with stroke width , we are going to do with paint color.
  • We create the Alert Dialog in same way as before. 
  • In setSingleChoiceItems() we just use a switch-case construct to set the paint color accordingly.


3. Change the onTouchEvent() method to store path and paint in Hash Map

  • We use a private Map pathMap to store each path with its own paint settings.
  • The values are put on the Hash Map on ACTION_UP, using the pathMap.put(key,value)


4. Change the onDraw() accordingly 

  • In the onDraw() method just add for loop to loop through all the key-value pairs in our Hash Map.
  • Draw all the paths using their own specific paints, both on the canvas and on the singleusecanvas (to store it on the bitmap as in Part II).


5. Change the set_erase_on() method to clear all paths in Map

  • Add the method to clear the hash map so as to clear all the paths stored.




Sample Screenshots

Stroke width selection
Color selection




 Download Source Code

download



SHARE

Shobhit Chittora

Hi there, I am Shobhit Chittora. I am a college student and like to develop apps for Android platform.I started this blog to share my experiences with Android development and may be help you guys on the “roadtodroid”.

  • Image
  • Image