Tuesday 15 April 2014

FingerTouch Drawing Signature with different colors in Android

Hello Friends,

Today i am going to show you the tutorial of drawing or creating your own signature or simply draw/paint a line using finger with different colors and save it as image. You will be able to draw,clear,save your signature in this tutorial. You will be able to draw a picture as well.


Here i am going to show you the steps to create finger touch view.

Step 1: Create Custom View

The first thing we need to do is to create CustomView by creating a class and extends View in it.

 import android.content.Context;  
 import android.util.AttributeSet;  
 import android.view.View;  
 public class Signature extends View {  
     public Signature(Context context, AttributeSet attrs)  
       {   
          super(context, attrs);    
       }   
    }  

This view you can add in your Activity or you can create separate class called Signature . You can add it inside of a layout and, if you wish to expand on this example, you could add some buttons for colors, etc…
 <com.example.signaturedemo.DrawingView  
     android:id="@+id/drawing"  
     android:layout_width="fill_parent"  
     android:layout_height="0dp"  
     android:layout_marginBottom="3dp"  
     android:layout_marginLeft="5dp"  
     android:layout_marginRight="5dp"  
     android:layout_marginTop="3dp"  
     android:layout_weight="1"  
     android:background="#FFFFFFFF" />  

Now, if we run this, our application will run the activity and create the layout with our custom view.

But this is not enough we need to write the code for drawing line on it. So, lets see the code.


Step 2: Using the onTouch event to draw on a canvas

We need to:
  • Set a paint style
  • Listen for a touch event
  • Create a new starting point when we touch the screen
  • Draw a path when we move our finger
  • Redraw the view when the onTouchEvent fires.
So, lets write the code as below:
 public class Signature extends View {  
  private static final float STROKE_WIDTH = 5f;  
  private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;  
  private Path m_path = new Path();  
  private float m_lastTouchX;  
  private float m_lastTouchY;  
  private final RectF m_dirtyRect = new RectF();  
  public Signature(Context context, AttributeSet attrs) {  
   super(context, attrs);  
   m_paint.setAntiAlias(true);  
   new ColorPickerDialog(context, new OnColorChangedListener() {  
   public void colorChanged(int color) {  
    m_paint.setColor(color);  
    mColor = color;  
   }  
   }, 0xFFFF0000).show();  
   m_paint.setStyle(Paint.Style.STROKE);  
   m_paint.setStrokeJoin(Paint.Join.ROUND);  
   m_paint.setStrokeWidth(STROKE_WIDTH);  
  }  

Now all that is left is to make sure that we add points to the path when we move our finger over the screen. Edit the onTouchEvent like below:

  @Override  
  public boolean onTouchEvent(MotionEvent event) {  
   float m_eventX = event.getX();  
   float m_eventY = event.getY();  
   switch (event.getAction()) {  
   case MotionEvent.ACTION_DOWN:  
   m_path.moveTo(m_eventX, m_eventY);  
   m_lastTouchX = m_eventX;  
   m_lastTouchY = m_eventY;  
   return true;  
   case MotionEvent.ACTION_MOVE:  
   case MotionEvent.ACTION_UP:  
   resetDirtyRect(m_eventX, m_eventY);  
   int historySize = event.getHistorySize();  
   for (int i = 0; i < historySize; i++) {  
    float historicalX = event.getHistoricalX(i);  
    float historicalY = event.getHistoricalY(i);  
    expandDirtyRect(historicalX, historicalY);  
    m_path.lineTo(historicalX, historicalY);  
   }  
   m_path.lineTo(m_eventX, m_eventY);  
   break;  
   default:  
   debug("Ignored touch event: " + event.toString());  
   return false;  
   }  
   invalidate((int) (m_dirtyRect.left - HALF_STROKE_WIDTH),  
    (int) (m_dirtyRect.top - HALF_STROKE_WIDTH),  
    (int) (m_dirtyRect.right + HALF_STROKE_WIDTH),  
    (int) (m_dirtyRect.bottom + HALF_STROKE_WIDTH));  
   m_lastTouchX = m_eventX;  
   m_lastTouchY = m_eventY;  
   return true;  
  }  
            /**  
   * This method checks the earlier rectangle.  
   *   
   * @param historicalX  
   * @param historicalY  
   */  
  private void expandDirtyRect(float historicalX, float historicalY) {  
   if (historicalX < m_dirtyRect.left) {  
   m_dirtyRect.left = historicalX;  
   } else if (historicalX > m_dirtyRect.right) {  
   m_dirtyRect.right = historicalX;  
   }  
   if (historicalY < m_dirtyRect.top) {  
   m_dirtyRect.top = historicalY;  
   } else if (historicalY > m_dirtyRect.bottom) {  
   m_dirtyRect.bottom = historicalY;  
   }  
  }  
  /**  
   * This method resets rectangle.  
   *   
   * @param eventX  
   * @param eventY  
   */  
  private void resetDirtyRect(float eventX, float eventY) {  
   m_dirtyRect.left = Math.min(m_lastTouchX, eventX);  
   m_dirtyRect.right = Math.max(m_lastTouchX, eventX);  
   m_dirtyRect.top = Math.min(m_lastTouchY, eventY);  
   m_dirtyRect.bottom = Math.max(m_lastTouchY, eventY);  
  }  

onTouch method is called when a touch event is dispatched to a view. This allows a listeners to get chance to respond before the target view.

   public boolean onTouch(MotionEvent event)      
  • event - is object of MotionEvent containing full information about event
In the code above, is first store the X and Y coordinates of the event (the touch or the moving of your finger) in variables. We then put in a switch statement to identify the type of event that was fired. In this case we need to create a new starting point for the path if the event is of type MotionEvent.ACTION_DOWN, which means we placed our finger on the screen, and connect points on MotionEvent.ACTION_MOVE which is when we moved our finger.

Next, We save the drawing into sdcard as an image. 

Output:







Thursday 10 April 2014

Create different Selectors in Android

           

In Android GUI is always an important part of any application, because ordinary users don't know and don't care about what's behind the scene; they want something easy to work with and now a days attractive GUI is a must for most applications.

Although making an appealing and innovative interface needs something more than just programming skills and knowledge, every programmer should know how to customize different GUI components within whatever framework and environment they are working.When designing GUIs, most of the times you want to change the appearance of buttons, input Fields, menus etc.. 

Android Selectors have been provided to solve all these kind of problems, they enable us to decide what to show and how to show based on different states of each components.

For example. You can tell a button to have black background color with red text color when it is in pressed state or whatever else.

So today i am going to show you how you can create selectors for Buttons with single color,two color and three color of layers.

Step 1: Create Project and add resources.

First of all create a simple android project in your workspace. 
After creating project Prepare/select any two images for Button states and put it into res/drawable folder.

1. "btn_selector_img.png"  -Default image of Button
2. "btn_selector_hv_img.png" - Display when the Button is Pressed.


Step 2: Create xml resources for the different types of selectors in res/drawable folder


1. Simple Image selector for showing images for pressed and unpressed states 

 image_selector.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:drawable="@drawable/btn_selector_hv_img" android:state_pressed="true"/>  
   <item android:drawable="@drawable/btn_selector_img"/>  
 </selector>  
 2.Simple Color selector to show different colors on Button when pressed or unpressed.  
   color_selector.xml  
 <?xml version="1.0" encoding="utf-8"?>  
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:drawable="@color/yellow" android:state_pressed="true"/>  
   <item android:drawable="@color/pink"/>  
 </selector>  

2.Simple Color selector to show different colors on Button when pressed or unpressed.

  color_selector.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:drawable="@color/yellow" android:state_pressed="true"/>  
   <item android:drawable="@color/pink"/>  
 </selector>  

3.Two Color selector to show different colors on Button when pressed or unpressed.

two_color_selector.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_pressed="true">  
     <shape xmlns:android="http://schemas.android.com/apk/res/android">  
       <gradient android:angle="90" android:endColor="@color/yellow" android:startColor="@color/pink"   
         android:type="linear" />  
     </shape>  
     </item>  
   <item>  
     <shape xmlns:android="http://schemas.android.com/apk/res/android">  
       <gradient android:angle="90" android:endColor="@color/pink" android:startColor="@color/yellow"  
          android:type="linear" />  
     </shape>  
     </item>  
 </selector>  

4.Three Color selector to show different colors on Button when pressed or unpressed.

three_color_selector.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_pressed="true">  
     <shape xmlns:android="http://schemas.android.com/apk/res/android">  
       <gradient android:angle="90" android:centerColor="@color/white" android:endColor="@color/pink"   
         android:startColor="@color/yellow" android:type="linear" />  
     </shape>  
   </item>  
   <item>  
     <shape xmlns:android="http://schemas.android.com/apk/res/android">  
       <gradient android:angle="90" android:centerColor="@color/white" android:endColor="@color/yellow"   
         android:startColor="@color/pink" android:type="linear" />  
     </shape>  
    </item>  
 </selector>  

Step 3: Create layout xml 

 <?xml version="1.0" encoding="utf-8"?>  
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent" >  
   <LinearLayout  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:gravity="center_vertical|center"  
     android:orientation="vertical" >  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_marginTop="20dip"  
       android:text="@string/title_selector_type"  
       android:textSize="20dp"  
       android:textStyle="bold" />  
     <!-- When you use image view then the attribute "android:clickable" must be set to "true" -->  
     <Button  
       android:layout_width="250dp"  
       android:layout_height="60dp"  
       android:layout_marginTop="20dip"  
       android:background="@drawable/image_selector"  
       android:text="@string/image_selector" />  
     <Button  
       android:layout_width="250dp"  
       android:layout_height="60dp"  
       android:layout_marginTop="20dip"  
       android:background="@drawable/color_selector"  
       android:text="@string/single_color_selector" />  
     <Button  
       android:layout_width="250dp"  
       android:layout_height="60dp"  
       android:layout_marginTop="20dip"  
       android:background="@drawable/two_color_gradiant_selector"  
       android:text="@string/two_color_selector" />  
     <Button  
       android:layout_width="250dp"  
       android:layout_height="60dp"  
       android:layout_marginTop="20dip"  
       android:background="@drawable/three_color_gradiant_selector"  
       android:text="@string/three_color_selector" />  
     <Button  
       android:layout_width="250dp"  
       android:layout_height="60dp"  
       android:layout_marginTop="20dip"  
       android:textStyle="bold"  
       android:background="@drawable/color_selector"  
       android:text="@string/gradiant_font_selector"  
       android:textColor="@color/text_selector" />  
   </LinearLayout>  
 </ScrollView>  

Tuesday 8 April 2014

Tinder App like Control with Animation in Android

Hello Friends,

You must have understood from the title only what i am going to share with you now. I think you must have checked the Tinder App on play store. Tinder app is a social application which will allow you to find the people nearby and chat with them. 

This application contains the nice control of like/dislike the people by swipe right or left. That animated control is already available ready for the iOS Tinder animation, but for android its not available as far as my knowledge. 

So i have tried to create similar kind of control for android and that is what today i  am going to share with you. 

Here are the screenshots of the view which i have developed. 





You can Download Source Code also. 

I hope it will help you. Feel free to give you suggestions and improve the control also. 

Thank you.

Thursday 3 April 2014

Android Custom Dialog Example

How to create custom dialog in Android

Hello, Friends.

Today i am going to show you that how you can customize Dialog in Android using custom layout file.

      Dialog box is mainly a popup or we can say a prompt that opens in front of the current activity to perform some operation or functionality.
          You can use a dialog box to ask the user to confirm an action, notify the user of an event, or prompt the user for additional information. Because dialog boxes disrupt the flow of the UI, you should use them only as a last resort. In most cases, you should integrate confirmation, feedback, and prompts directly into your app.

Sometimes in our applications we want to alert the user for an event and/or ask user about taking a decision. For this purpose AlertDialog class can be used, where a message and one, two or three buttons are displayed in a popup window.

       When a dialog box opens the current activity by which we open the dialog box goes to the background and the dialog box comes in the foreground. After performing the operation on the dialog box we dismiss the dialog box and the background activity comes back to the foreground. 


To create custom dialog first of all create simple Android application in eclipse.

Create a project with the following details:
  • ProjectNameCustomDialog 
  • PackageName: com.demo.customdialog
  • ActivityNameCustomDialogActivity

In your CustomDialogActivity write the code as below:

 import android.app.Activity;  
 import android.app.Dialog;  
 import android.os.Bundle;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.Window;  
 import android.widget.Button;  
 import android.widget.LinearLayout;  
 import android.widget.ScrollView;  
 import android.widget.Toast;  
 import com.wli.framework.model.ClearActivityObjectListner;  
 import com.wli.framework.view.ActivityHelper;  
 public class CustomDialogActivity extends Activity implements OnClickListener  
 {  
  private static final int SIMPLE_DIALOG = 0;  
  private static final int BORDER_DIALOG = 1;  
  private static final int ROUNDE_CORNER_DIALOG = 2;  
  private static final int ROUNDE_CORNER_BORDER_DIALOG = 3;  
  private Dialog m_dialog; //Dialog instance.  
  private Button m_btnSimpleDialog, m_btnBorderDialog, m_btnRoundeCornerDialog, m_btnRoundeCornerBorderDialog;  
  private ScrollView m_svMain;  
  private LinearLayout m_llMain;  
  @Override  
  protected void onCreate(Bundle p_savedInstanceState)  
  {  
  super.onCreate(p_savedInstanceState);  
  setContentView(R.layout.maindialog_layout);  
  m_btnSimpleDialog = (Button) findViewById(R.id.dlbtnSimpleDialog);  
  m_btnBorderDialog = (Button) findViewById(R.id.dlbtnBorderDialog);  
  m_btnRoundeCornerDialog = (Button) findViewById(R.id.dlbtnRoundeCornerDialog);  
  m_btnRoundeCornerBorderDialog = (Button) findViewById(R.id.dlbtnRoundeCornerBorderDialog);  
  m_svMain = (ScrollView) findViewById(R.id.dlsvMain);  
  m_btnSimpleDialog.setOnClickListener(this);  
  m_btnBorderDialog.setOnClickListener(this);  
  m_btnRoundeCornerDialog.setOnClickListener(this);  
  m_btnRoundeCornerBorderDialog.setOnClickListener(this);  
  }  
  /**  
  * This is method to show customize dialog.  
  *   
  * @param p_index  
  *      - index of customize dialog  
  */  
  public void showCustomDialog(int p_index)  
  {  
  m_dialog = new Dialog(CustomDialogActivity.this, R.style.Dialog_No_Border);  
  m_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  LayoutInflater m_inflater = LayoutInflater.from(CustomDialogActivity.this);  
  View m_view = m_inflater.inflate(R.layout.custom_dialog, null);  
  m_llMain = (LinearLayout) m_view.findViewById(R.id.cadllMain);  
  //Change the background of the dialog according to the layout.   
  if (p_index == BORDER_DIALOG)  
  {  
   m_llMain.setBackgroundResource(R.drawable.btn_style_border);  
  }  
  else if (p_index == ROUNDE_CORNER_DIALOG)  
  {  
   m_llMain.setBackgroundResource(R.drawable.btn_style_roundcorner);  
  }  
  else if (p_index == ROUNDE_CORNER_BORDER_DIALOG)  
  {  
   m_llMain.setBackgroundResource(R.drawable.btn_style_border_roundcorner);  
  }  
  Button m_btnOk = (Button) m_view.findViewById(R.id.cadbtnOk);  
  Button m_btnCancel = (Button) m_view.findViewById(R.id.cadbtnCancel);  
  OnClickListener m_clickListener = new OnClickListener(){  
   @Override  
   public void onClick(View p_v)  
   {  
   Toast.makeText(CustomDialogActivity.this, "Pressed " + ((Button) p_v).getText(), Toast.LENGTH_SHORT).show();  
   switch (p_v.getId())  
    {  
    case R.id.cadbtnOk:  
     m_dialog.dismiss();  
    break;  
    case R.id.cadbtnCancel:  
     m_dialog.dismiss();  
     break;  
    default:  
     break;  
    }  
   }  
  };  
  m_btnOk.setOnClickListener(m_clickListener);  
  m_btnCancel.setOnClickListener(m_clickListener);  
  m_dialog.setContentView(m_view);  
  m_dialog.show();  
  }  
  /**  
  * Common click listener for the buttons.  
  */  
  @Override  
  public void onClick(View p_v)  
  {  
  switch (p_v.getId())  
   {  
   case R.id.dlbtnSimpleDialog:  
    showCustomDialog(SIMPLE_DIALOG);  
    break;  
   case R.id.dlbtnBorderDialog:  
    showCustomDialog(BORDER_DIALOG);  
    break;  
   case R.id.dlbtnRoundeCornerDialog:  
    showCustomDialog(ROUNDE_CORNER_DIALOG);  
    break;  
   case R.id.dlbtnRoundeCornerBorderDialog:  
    showCustomDialog(ROUNDE_CORNER_BORDER_DIALOG);  
    break;  
   default:  
    break;  
   }  
  }  
  public void clearView()  
  {  
  m_dialog = null;  
  m_btnSimpleDialog = null;  
  m_btnBorderDialog = null;  
  m_btnRoundeCornerDialog = null;  
  m_btnRoundeCornerBorderDialog = null;  
  m_svMain = null;  
  m_llMain = null;  
  }  
  @Override  
  protected void onDestroy()  
  {  
  super.onDestroy();  
  clearView();  
  }  
 }  

Here is the layout code. Create the layouts for the activity and dialog as below:

maindialog_layout.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/dlsvMain"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent" >  
   <LinearLayout  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent"  
     android:gravity="center_horizontal"  
     android:orientation="vertical"  
     android:paddingTop="80dp" >  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="@string/title_customdialog"  
       android:textSize="25sp"  
       android:textStyle="bold" />  
     <Button  
       android:id="@+id/dlbtnSimpleDialog"  
       style="@style/commonButtonStyle"  
       android:text="@string/dl_simple_dialog" />  
     <Button  
       android:id="@+id/dlbtnBorderDialog"  
       style="@style/commonButtonStyle"  
       android:text="@string/dl_border_dialog" />  
     <Button  
       android:id="@+id/dlbtnRoundeCornerDialog"  
       style="@style/commonButtonStyle"  
       android:text="@string/dl_roundcorner_dialog" />  
     <Button  
       android:id="@+id/dlbtnRoundeCornerBorderDialog"  
       style="@style/commonButtonStyle"  
       android:text="@string/dl_roundcorner_border_dialog" />  
   </LinearLayout>  
 </ScrollView>  

custom_dialog.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:id="@+id/cadllMain"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:background="@color/grey"  
   android:orientation="vertical"  
   tools:ignore="ButtonOrder" >  
   <FrameLayout  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content" >  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:padding="10dp"  
       android:text="@string/title_text" />  
     <ImageView  
       android:id="@+id/imageView_close"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_gravity="top|right"  
       android:clickable="true"  
       android:src="@drawable/cancel"  
       android:visibility="gone"  
       tools:ignore="ContentDescription" />  
   </FrameLayout>  
   <View  
     android:layout_width="fill_parent"  
     android:layout_height="1dp"  
     android:layout_marginLeft="3dp"  
     android:layout_marginRight="3dp"  
     android:background="@android:color/white" />  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:lines="3"  
     android:padding="10dp"  
     android:text="@string/message" />  
   <LinearLayout  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:gravity="center_horizontal"  
     android:orientation="horizontal" >  
     <Button  
       android:id="@+id/cadbtnOk"  
       android:layout_width="100dp"  
       android:layout_height="40dp"  
       android:layout_margin="10dp"  
       android:background="@android:color/darker_gray"  
       android:text="@android:string/ok" />  
     <Button  
       android:id="@+id/cadbtnCancel"  
       android:layout_width="100dp"  
       android:layout_height="40dp"  
       android:layout_margin="10dp"  
       android:background="@android:color/darker_gray"  
       android:text="@android:string/cancel" />  
   </LinearLayout>  
 </LinearLayout>  

Copy the below code into your res/style file

style.xml

 <style name="Dialog_No_Border">  
     <item name="android:windowIsFloating">true</item>  
     <item name="android:windowBackground">@color/transparent_color</item>  
   </style>  
   <style name="commonButtonStyle">  
     <item name="android:layout_width">wrap_content</item>  
     <item name="android:layout_height">wrap_content</item>  
     <item name="android:layout_gravity">center_horizontal</item>  
     <item name="android:layout_margin">10dp</item>  
   </style>  

color.xml
 <color name="transparent_color">#00000000</color>  
 <color name="light_green">#00AA00</color>  
 <color name="grey">#DCDCDC</color>  

strings.xml
  <string name="title_activity_custom_dialog">CustomDialogActivity</string>  
   <string name="title_customdialog">Various Custom Dialogs</string>  
   <string name="dl_simple_dialog">Simple Custom Dialog</string>  
   <string name="dl_border_dialog">Custom Dialog with Border</string>  
   <string name="dl_roundcorner_dialog">Custom Dialog with Round Corner</string>  
   <string name="dl_roundcorner_border_dialog">Custom Dialog with Round Corner &amp; Border</string>  
   <string name="title_text">Add Title Text</string>  
   <string name="message">Alert Message...</string>  

Here is the AndroidManifest.xml file

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.demo.customdialog"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="15" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >  
     <activity  
       android:name="com.demo.customdialog.CustomDialogActivity"  
       android:label="@string/title_activity_custom_dialog" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Output:




You can Download sourcode HERE