Tuesday, January 7, 2014

System Overlay Demo


Create new project or download project source code from git  https://github.com/cm-nagariya/demo-chirag/tree/master/SystemOverlay

1. Add following permission on AndroidManifest.xml file


<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2. Create RadialMenu View

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

public class RadialMenuWidget extends View {

    //Defines the interface
    public interface RadialMenuEntry {
          public String getName();
          public String getLabel();
          public int getIcon();
          public List<RadialMenuEntry> getChildren();
          public void menuActiviated();
    }  

   
    private List<RadialMenuEntry> menuEntries = new ArrayList<RadialMenuEntry>();
    private RadialMenuEntry centerCircle = null;
   
    private float screen_density = getContext().getResources().getDisplayMetrics().density;
   
    private int defaultColor = Color.rgb(34, 96, 120);     //default color of wedge pieces
    private int defaultAlpha = 180;                          //transparency of the colors, 255=Opague, 0=Transparent
    private int wedge2Color = Color.rgb(50, 50, 50);     //default color of wedge pieces
    private int wedge2Alpha = 210;
    private int outlineColor = Color.rgb(150, 150, 150);      //color of outline
    private int outlineAlpha = 255;                            //transparency of outline
    private int selectedColor = Color.rgb(70, 130, 180);      //color to fill when something is selected
    private int selectedAlpha = 210;                        //transparency of fill when something is selected

    private int disabledColor = Color.rgb(34, 96, 120);      //color to fill when something is selected
    private int disabledAlpha = 100;                        //transparency of fill when something is selected
   
    private int pictureAlpha = 255;                            //transparency of images

    private int textColor = Color.rgb(255, 255, 255);      //color to fill when something is selected
    private int textAlpha = 255;                        //transparency of fill when something is selected

    private int headerTextColor = Color.rgb(255, 255, 255);      //color of header text
    private int headerTextAlpha = 255;                            //transparency of header text
    private int headerBackgroundColor =  Color.rgb(0, 0, 0);    //color of header background
    private int headerBackgroundAlpha =  180;                    //transparency of header background
   
    private int wedgeQty = 1;                //Number of wedges
    private Wedge[] Wedges = new Wedge[wedgeQty];
    private Wedge selected = null;            //Keeps track of which wedge is selected
    private Wedge enabled = null;            //Keeps track of which wedge is enabled for outer ring
    private Rect[] iconRect = new Rect[wedgeQty];


    private int wedgeQty2 = 1;                //Number of wedges
    private Wedge[] Wedges2 = new Wedge[wedgeQty2];
    private Wedge selected2 = null;            //Keeps track of which wedge is selected
    private Rect[] iconRect2 = new Rect[wedgeQty2];
    private RadialMenuEntry wedge2Data = null;        //Keeps track off which menuItem data is being used for the outer ring
   
    private int MinSize = scalePX(35);                //Radius of inner ring size
    private int MaxSize = scalePX(90);                //Radius of outer ring size
    private int r2MinSize = MaxSize+scalePX(5);        //Radius of inner second ring size
    private int r2MaxSize = r2MinSize+scalePX(45);    //Radius of outer second ring size
    private int MinIconSize = scalePX(15);                    //Min Size of Image in Wedge
    private int MaxIconSize = scalePX(35);            //Max Size of Image in Wedge
    //private int BitmapSize = scalePX(40);            //Size of Image in Wedge
    private int cRadius = MinSize-scalePX(7);          //Inner Circle Radius
    private int textSize = scalePX(15);                //TextSize
    private int animateTextSize = textSize;
   
    private int xPosition = scalePX(120);            //Center X location of Radial Menu
    private int yPosition = scalePX(120);            //Center Y location of Radial Menu

    private int xSource = 0;            //Source X of clicked location
    private int ySource = 0;            //Center Y of clicked location
    private boolean showSource = false;    //Display icon where at source location
   
    private boolean inWedge = false;        //Identifies touch event was in first wedge
    private boolean inWedge2 = false;        //Identifies touch event was in second wedge
    private boolean inCircle = false;        //Identifies touch event was in middle circle

    private boolean Wedge2Shown = false;        //Identifies 2nd wedge is drawn
    private boolean HeaderBoxBounded = false;    //Identifies if header box is drawn
   
    private String headerString = null;
    private int headerTextSize = textSize;                //TextSize
    private int headerBuffer = scalePX(8);
    private Rect textRect = new Rect();
    private RectF textBoxRect = new RectF();
    private int headerTextLeft;
    private int headerTextBottom;
   
    private RotateAnimation rotate;
    private AlphaAnimation blend;
    private ScaleAnimation scale;
    private TranslateAnimation move;
    private AnimationSet spriteAnimation;
    private long animationSpeed = 400L;
   

    private static final int ANIMATE_IN = 1;
    private static final int ANIMATE_OUT = 2;
   
    private int animateSections = 4;
    private int r2VariableSize;
    private boolean animateOuterIn = false;
    private boolean animateOuterOut = false;
   
   
   
    public RadialMenuWidget(Context context) {
        super(context);

        // Gets screen specs and defaults to center of screen
        this.xPosition = (getResources().getDisplayMetrics().widthPixels)/2;
        this.yPosition = (getResources().getDisplayMetrics().heightPixels)/2;
      
        determineWedges();
        onOpenAnimation();
    }

   
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        int state = e.getAction();
        int eventX = (int) e.getX();
        int eventY = (int) e.getY();
        if (state == MotionEvent.ACTION_DOWN) {
            //selected = null;
            //selected2 = null;
            inWedge = false;
            inWedge2 = false;
            inCircle = false;


            //Checks if a pie slice is selected in first Wedge
            for (int i = 0; i < Wedges.length; i++) {
                Wedge f = Wedges[i];
                double slice = (2*Math.PI) / wedgeQty;
                double start = (2*Math.PI)*(0.75) - (slice/2);        //this is done so top slice is the centered on top of the circle

                inWedge = pntInWedge(eventX, eventY,
                        xPosition, yPosition,
                        MinSize, MaxSize,
                        (i* slice)+start, slice);                  
              
                if (inWedge == true) {
                    selected = f;
                    break;
                }
            }

          
            //Checks if a pie slice is selected in second Wedge
            if (Wedge2Shown == true) {
                for (int i = 0; i < Wedges2.length; i++) {
                    Wedge f = Wedges2[i];
                    double slice = (2*Math.PI) / wedgeQty2;
                    double start = (2*Math.PI)*(0.75) - (slice/2);        //this is done so top slice is the centered on top of the circle
   
                    inWedge2 = pntInWedge(eventX, eventY,
                            xPosition, yPosition,
                            r2MinSize, r2MaxSize,
                            (i* slice)+start, slice);                  
                  
                    if (inWedge2 == true) {
                        selected2 = f;
                        break;
                    }
                }

            }
          
            //Checks if center circle is selected
            inCircle = pntInCircle(eventX, eventY, xPosition,yPosition,cRadius);

        } else if (state == MotionEvent.ACTION_UP) {
            //execute commands...
            //put in stuff here to "return" the button that was pressed.
            if (inCircle == true) {
                if (Wedge2Shown == true) {
                    enabled = null;
                    animateOuterIn = true;  //sets Wedge2Shown = false;
                }
                selected = null;
                Toast.makeText(getContext(), centerCircle.getName() + " pressed.", Toast.LENGTH_SHORT).show();
                centerCircle.menuActiviated();

            } else if (selected != null){
                for (int i = 0; i < Wedges.length; i++) {
                    Wedge f = Wedges[i];
                    if (f == selected) {
                      
                        //Checks if a inner ring is enabled if so closes the outer ring an
                        if (enabled != null) {
                            Toast.makeText(getContext(), "Closing outer ring", Toast.LENGTH_SHORT).show();
                            enabled = null;
                            animateOuterIn = true;  //sets Wedge2Shown = false;
                          
                        //If outer ring is not enabled, then executes event
                        } else {
                            Toast.makeText(getContext(), menuEntries.get(i).getName() + " pressed.", Toast.LENGTH_SHORT).show();
                            menuEntries.get(i).menuActiviated();
                          
                            //Figures out how many outer rings
                            if (menuEntries.get(i).getChildren() != null) {
                                determineOuterWedges(menuEntries.get(i));
                                enabled = f;
                                animateOuterOut = true;  //sets Wedge2Shown = true;
                              
                            } else {
                                Wedge2Shown = false;
                            }
                          
                        }
                        selected = null;
                    }                  
                }
            } else if (selected2 != null){
                for (int i = 0; i < Wedges2.length; i++) {
                    Wedge f = Wedges2[i];
                    if (f == selected2) {
                    Toast.makeText(getContext(), wedge2Data.getChildren().get(i).getName() + " pressed.", Toast.LENGTH_SHORT).show();
                    animateOuterIn = true;  //sets Wedge2Shown = false;
                    enabled = null;
                    selected = null;
                    }                  
                }
            } else {
                //This is when something outside the circle or any of the rings is selected
                Toast.makeText(getContext(), "Area outside rings pressed.", Toast.LENGTH_SHORT).show();

                //selected = null;
                //enabled = null;
            }
            //selected = null;
            selected2 = null;
            inCircle = false;
        }
        invalidate();
        return true;
    }


    @Override
    protected void onDraw(Canvas c) {

      
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(3);

        // draws a dot at the source of the press
        if (showSource == true ) {
            paint.setColor(outlineColor);
            paint.setAlpha(outlineAlpha);
            paint.setStyle(Paint.Style.STROKE);
            c.drawCircle(xSource, ySource, cRadius/10, paint);
   
            paint.setColor(selectedColor);
            paint.setAlpha(selectedAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawCircle(xSource, ySource, cRadius/10, paint);
        }
      
     
         for (int i = 0; i < Wedges.length; i++) {
            Wedge f = Wedges[i];
            paint.setColor(outlineColor);
            paint.setAlpha(outlineAlpha);
            paint.setStyle(Paint.Style.STROKE);
            c.drawPath(f, paint);
            if (f == enabled && Wedge2Shown == true) {
                paint.setColor(wedge2Color);
                paint.setAlpha(wedge2Alpha);
                paint.setStyle(Paint.Style.FILL);
                c.drawPath(f, paint);
            } else if (f != enabled && Wedge2Shown == true) {
                paint.setColor(disabledColor);
                paint.setAlpha(disabledAlpha);
                paint.setStyle(Paint.Style.FILL);
                c.drawPath(f, paint);          
            } else if (f == enabled && Wedge2Shown == false) {
                paint.setColor(wedge2Color);
                paint.setAlpha(wedge2Alpha);
                paint.setStyle(Paint.Style.FILL);
                c.drawPath(f, paint);
            } else if (f == selected) {
                paint.setColor(wedge2Color);
                paint.setAlpha(wedge2Alpha);
                paint.setStyle(Paint.Style.FILL);
                c.drawPath(f, paint);  
            } else {
                paint.setColor(defaultColor);
                paint.setAlpha(defaultAlpha);
                paint.setStyle(Paint.Style.FILL);
                c.drawPath(f, paint);
            }

            Rect rf = iconRect[i];

            if ((menuEntries.get(i).getIcon() != 0) && (menuEntries.get(i).getLabel() != null)) {
              
                //This will look for a "new line" and split into multiple lines                  
                String menuItemName = menuEntries.get(i).getLabel();
                String[] stringArray = menuItemName.split("\n");

                paint.setColor(textColor);
                if (f != enabled && Wedge2Shown == true) {
                    paint.setAlpha(disabledAlpha);
                } else {
                    paint.setAlpha(textAlpha);
                }
                paint.setStyle(Paint.Style.FILL);
                paint.setTextSize(textSize);
              
                Rect rect = new Rect();
                float textHeight = 0;
                for (int j = 0; j < stringArray.length; j++)
                    {
                    paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                    textHeight = textHeight+(rect.height()+3);
                    }

                Rect rf2 = new Rect();
                rf2.set(rf.left, rf.top-((int)textHeight/2), rf.right, rf.bottom-((int)textHeight/2));

                float textBottom = rf2.bottom;
                for (int j = 0; j < stringArray.length; j++)
                    {
                    paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                    float textLeft = rf.centerX() - rect.width()/2;
                    textBottom = textBottom + (rect.height()+3);
                    c.drawText(stringArray[j], textLeft-rect.left, textBottom-rect.bottom, paint);
                    }
                          
                //Puts in the Icon
                Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon());              
                drawable.setBounds(rf2);
                if (f != enabled && Wedge2Shown == true) {
                    drawable.setAlpha(disabledAlpha);
                } else {
                    drawable.setAlpha(pictureAlpha);
                }
                drawable.draw(c);                  

        //Icon Only
            } else if (menuEntries.get(i).getIcon() != 0) {
                //Puts in the Icon
                Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon());              
                drawable.setBounds(rf);
                if (f != enabled && Wedge2Shown == true) {
                    drawable.setAlpha(disabledAlpha);
                } else {
                    drawable.setAlpha(pictureAlpha);
                }
                drawable.draw(c);              
              
              
        //Text Only                  
            } else {
                //Puts in the Text if no Icon
                paint.setColor(textColor);
                if (f != enabled && Wedge2Shown == true) {
                    paint.setAlpha(disabledAlpha);
                } else {
                    paint.setAlpha(textAlpha);
                }
                paint.setStyle(Paint.Style.FILL);
                paint.setTextSize(textSize);
              
                //This will look for a "new line" and split into multiple lines
                String menuItemName = menuEntries.get(i).getLabel();
                String[] stringArray = menuItemName.split("\n");

                //gets total height
                Rect rect = new Rect();
                float textHeight = 0;
                for (int j = 0; j < stringArray.length; j++)
                    {
                    paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                    textHeight = textHeight+(rect.height()+3);
                    }

                float textBottom = rf.centerY()-(textHeight/2);
                for (int j = 0; j < stringArray.length; j++)
                    {
                    paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                    float textLeft = rf.centerX() - rect.width()/2;
                    textBottom = textBottom + (rect.height()+3);
                    c.drawText(stringArray[j], textLeft-rect.left, textBottom-rect.bottom, paint);
                    }                              
            }          
          
        }

      
        //Animate the outer ring in/out
        if (animateOuterIn == true) {
            animateOuterWedges(ANIMATE_IN);
        }
        else if (animateOuterOut == true) {
            animateOuterWedges(ANIMATE_OUT);
        }          
      
        if (Wedge2Shown == true) {
          
            for (int i = 0; i < Wedges2.length; i++) {
                Wedge f = Wedges2[i];
                paint.setColor(outlineColor);
                paint.setAlpha(outlineAlpha);
                paint.setStyle(Paint.Style.STROKE);
                c.drawPath(f, paint);
                if (f == selected2) {
                    paint.setColor(selectedColor);
                    paint.setAlpha(selectedAlpha);
                    paint.setStyle(Paint.Style.FILL);
                    c.drawPath(f, paint);
                } else {
                    paint.setColor(wedge2Color);
                    paint.setAlpha(wedge2Alpha);
                    paint.setStyle(Paint.Style.FILL);
                    c.drawPath(f, paint);
                }
   
                Rect rf = iconRect2[i];
                if ((wedge2Data.getChildren().get(i).getIcon() != 0) && (wedge2Data.getChildren().get(i).getLabel() != null)) {
                  
                    //This will look for a "new line" and split into multiple lines                  
                    String menuItemName = wedge2Data.getChildren().get(i).getLabel();
                    String[] stringArray = menuItemName.split("\n");

                    paint.setColor(textColor);
                    paint.setAlpha(textAlpha);
                    paint.setStyle(Paint.Style.FILL);
                    paint.setTextSize(animateTextSize);
                  
                    Rect rect = new Rect();
                    float textHeight = 0;
                    for (int j = 0; j < stringArray.length; j++)
                        {
                        paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                        textHeight = textHeight+(rect.height()+3);
                        }

                    Rect rf2 = new Rect();
                    rf2.set(rf.left, rf.top-((int)textHeight/2), rf.right, rf.bottom-((int)textHeight/2));
                  
                    float textBottom = rf2.bottom;
                    for (int j = 0; j < stringArray.length; j++)
                        {
                        paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                        float textLeft = rf.centerX() - rect.width()/2;
                        textBottom = textBottom + (rect.height()+3);
                        c.drawText(stringArray[j], textLeft-rect.left, textBottom-rect.bottom, paint);
                        }
                  
                  
                    //Puts in the Icon
                    Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon());
                    drawable.setBounds(rf2);
                    drawable.setAlpha(pictureAlpha);
                    drawable.draw(c);                  

            //Icon Only
                } else if (wedge2Data.getChildren().get(i).getIcon() != 0) {
                    //Puts in the Icon
                    Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon());
                    drawable.setBounds(rf);
                    drawable.setAlpha(pictureAlpha);
                    drawable.draw(c);

            //Text Only                  
                } else {
                    //Puts in the Text if no Icon
                    paint.setColor(textColor);
                    paint.setAlpha(textAlpha);
                    paint.setStyle(Paint.Style.FILL);
                    paint.setTextSize(animateTextSize);
                  
                    //This will look for a "new line" and split into multiple lines
                    String menuItemName = wedge2Data.getChildren().get(i).getLabel();
                    String[] stringArray = menuItemName.split("\n");
   
                    //gets total height
                    Rect rect = new Rect();
                    float textHeight = 0;
                    for (int j = 0; j < stringArray.length; j++)
                        {
                        paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                        textHeight = textHeight+(rect.height()+3);
                        }

                    float textBottom = rf.centerY()-(textHeight/2);
                    for (int j = 0; j < stringArray.length; j++)
                        {
                        paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                        float textLeft = rf.centerX() - rect.width()/2;
                        textBottom = textBottom + (rect.height()+3);
                        c.drawText(stringArray[j], textLeft-rect.left, textBottom-rect.bottom, paint);
                        }  
                }
            }
        }
      
        //Draws the Middle Circle
        paint.setColor(outlineColor);
        paint.setAlpha(outlineAlpha);
        paint.setStyle(Paint.Style.STROKE);
        c.drawCircle(xPosition, yPosition, cRadius, paint);
        if (inCircle == true) {
            paint.setColor(selectedColor);
            paint.setAlpha(selectedAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawCircle(xPosition, yPosition, cRadius, paint);
            onCloseAnimation();
        } else {
            paint.setColor(defaultColor);
            paint.setAlpha(defaultAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawCircle(xPosition, yPosition, cRadius, paint);
        }      
      
      
        // Draw the circle picture
        if ((centerCircle.getIcon() != 0) && (centerCircle.getLabel() != null)) {

            //This will look for a "new line" and split into multiple lines                  
            String menuItemName = centerCircle.getLabel();
            String[] stringArray = menuItemName.split("\n");

            paint.setColor(textColor);
            paint.setAlpha(textAlpha);
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(textSize);

            Rect rectText = new Rect();
            Rect rectIcon = new Rect();
            Drawable drawable = getResources().getDrawable(centerCircle.getIcon());

            int h = getIconSize(drawable.getIntrinsicHeight(),MinIconSize,MaxIconSize);
            int w = getIconSize(drawable.getIntrinsicWidth(),MinIconSize,MaxIconSize);          
            rectIcon.set(xPosition-w/2, yPosition-h/2, xPosition+w/2, yPosition+h/2);          
          

            float textHeight = 0;
            for (int j = 0; j < stringArray.length; j++)
                {
                paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rectText);
                textHeight = textHeight+(rectText.height()+3);
                }

            rectIcon.set(rectIcon.left, rectIcon.top-((int)textHeight/2), rectIcon.right, rectIcon.bottom-((int)textHeight/2));

            float textBottom = rectIcon.bottom;
            for (int j = 0; j < stringArray.length; j++)
                {
                paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rectText);
                float textLeft = xPosition - rectText.width()/2;
                textBottom = textBottom + (rectText.height()+3);
                c.drawText(stringArray[j], textLeft-rectText.left, textBottom-rectText.bottom, paint);
                }
          
          
            //Puts in the Icon
            drawable.setBounds(rectIcon);
            drawable.setAlpha(pictureAlpha);
            drawable.draw(c);                  

        //Icon Only
        } else if (centerCircle.getIcon() != 0) {
          
            Rect rect = new Rect();
          
            Drawable drawable = getResources().getDrawable(centerCircle.getIcon());

            int h = getIconSize(drawable.getIntrinsicHeight(),MinIconSize,MaxIconSize);
            int w = getIconSize(drawable.getIntrinsicWidth(),MinIconSize,MaxIconSize);          
            rect.set(xPosition-w/2, yPosition-h/2, xPosition+w/2, yPosition+h/2);
          
            drawable.setBounds(rect);
            drawable.setAlpha(pictureAlpha);
            drawable.draw(c);

        //Text Only              
        } else {
            //Puts in the Text if no Icon
            paint.setColor(textColor);
            paint.setAlpha(textAlpha);
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(textSize);
          
            //This will look for a "new line" and split into multiple lines
            String menuItemName = centerCircle.getLabel();
            String[] stringArray = menuItemName.split("\n");

            //gets total height
            Rect rect = new Rect();
            float textHeight = 0;
            for (int j = 0; j < stringArray.length; j++)
                {
                paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                textHeight = textHeight+(rect.height()+3);
                }
          
            float textBottom = yPosition-(textHeight/2);
            for (int j = 0; j < stringArray.length; j++)
                {
                paint.getTextBounds(stringArray[j],0,stringArray[j].length(),rect);
                float textLeft = xPosition - rect.width()/2;
                textBottom = textBottom + (rect.height()+3);
                c.drawText(stringArray[j], textLeft-rect.left, textBottom-rect.bottom, paint);
                }              
                          
          
        }

        // Draws Text in TextBox
        if (headerString != null) {

            paint.setTextSize(headerTextSize);
            paint.getTextBounds(headerString,0,headerString.length(),this.textRect);
            if (HeaderBoxBounded == false) {
                determineHeaderBox();
                HeaderBoxBounded = true;
            }
          
            paint.setColor(outlineColor);
            paint.setAlpha(outlineAlpha);
            paint.setStyle(Paint.Style.STROKE);
            c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint);      
            paint.setColor(headerBackgroundColor);
            paint.setAlpha(headerBackgroundAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint);
          
            paint.setColor(headerTextColor);
            paint.setAlpha(headerTextAlpha);
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(headerTextSize);
            c.drawText(headerString, headerTextLeft, headerTextBottom, paint);
        }

    }

   
    private int getIconSize(int iconSize, int minSize, int maxSize) {
      
        if (iconSize > minSize) {
            if (iconSize > maxSize) {
                return maxSize;
            } else {    //iconSize < maxSize
                return iconSize;
            }
        } else {  //iconSize < minSize
            return minSize;
        }

    }
   
   
   
    private void onOpenAnimation() {

        rotate = new RotateAnimation(0, 360, xPosition, yPosition);
        //rotate.setRepeatMode(Animation.REVERSE);
        //rotate.setRepeatCount(Animation.INFINITE);
        scale = new ScaleAnimation(0, 1, 0, 1, xPosition, yPosition);
        //scale.setRepeatMode(Animation.REVERSE);
        //scale.setRepeatCount(Animation.INFINITE);
        scale.setInterpolator(new DecelerateInterpolator());
        move = new TranslateAnimation(xSource-xPosition, 0, ySource-yPosition, 0);

        spriteAnimation = new AnimationSet(true);
        //spriteAnimation.addAnimation(rotate);
        spriteAnimation.addAnimation(scale);
        spriteAnimation.addAnimation(move);
        spriteAnimation.setDuration(animationSpeed);

        startAnimation(spriteAnimation);

    }
    private void onCloseAnimation() {
      
        rotate = new RotateAnimation(360, 0, xPosition, yPosition);
        scale = new ScaleAnimation(1, 0, 1, 0, xPosition, yPosition);
        scale.setInterpolator(new AccelerateInterpolator());
        move = new TranslateAnimation(0, xSource-xPosition, 0, ySource-yPosition);

        spriteAnimation = new AnimationSet(true);
        //spriteAnimation.addAnimation(rotate);
        spriteAnimation.addAnimation(scale);
        spriteAnimation.addAnimation(move);
        spriteAnimation.setDuration(animationSpeed);

        startAnimation(spriteAnimation);

    }   
   
    private boolean pntInCircle(double px, double py, double x1, double y1, double radius) {
        double diffX = x1 - px;
        double diffY = y1 - py;
        double dist = diffX*diffX + diffY*diffY;
        if (dist < radius*radius) {
            return true;
            }
        else {
            return false;
            }
        }       
   
   
    private boolean pntInWedge(double px, double py,
            float xRadiusCenter, float yRadiusCenter,
            int innerRadius, int outerRadius,
            double startAngle, double sweepAngle) {
        double diffX = px-xRadiusCenter;
        double diffY = py-yRadiusCenter;
      
        double angle = Math.atan2(diffY,diffX);
        if (angle < 0)
          angle += (2*Math.PI);

        if (startAngle >= (2*Math.PI)) {
            startAngle = startAngle-(2*Math.PI);
        }
      
        //checks if point falls between the start and end of the wedge
        if ((angle >= startAngle && angle <= startAngle + sweepAngle) ||
                (angle+(2*Math.PI) >= startAngle && (angle+(2*Math.PI)) <= startAngle + sweepAngle)) {
          
            // checks if point falls inside the radius of the wedge
            double dist = diffX*diffX + diffY*diffY;
            if (dist < outerRadius*outerRadius && dist > innerRadius*innerRadius) {
                return true;
                }
            }
          return false;
        }

   
    public boolean addMenuEntry( RadialMenuEntry entry )
    {
       menuEntries.add( entry );
       determineWedges();
       return true;
    }

    public boolean setCenterCircle( RadialMenuEntry entry )
    {
       centerCircle = entry;
       return true;
    }
   
   
    public void setInnerRingRadius( int InnerRadius, int OuterRadius )
    {
       this.MinSize = scalePX(InnerRadius);
       this.MaxSize = scalePX(OuterRadius);
       determineWedges();
    }   
   
    public void setOuterRingRadius( int InnerRadius, int OuterRadius )
    {
       this.r2MinSize = scalePX(InnerRadius);
       this.r2MaxSize = scalePX(OuterRadius);    
       determineWedges();
    }   

    public void setCenterCircleRadius( int centerRadius )
    {
       this.cRadius = scalePX(centerRadius);   
       determineWedges();
    }    
   
    public void setTextSize( int TextSize )
    {
       this.textSize = scalePX(TextSize);
       this.animateTextSize = this.textSize;
    }    
   
    public void setIconSize( int minIconSize, int maxIconSize )
    {
       this.MinIconSize = scalePX(minIconSize);
       this.MaxIconSize = scalePX(maxIconSize);
       determineWedges();
    }   
   

    public void setCenterLocation( int x, int y )
    {
       this.xPosition = x;
       this.yPosition = y;
       determineWedges();
       onOpenAnimation();
    }       

    public void setSourceLocation( int x, int y )
    {
       this.xSource = x;
       this.ySource = y;
       onOpenAnimation();
    }     

    public void setShowSourceLocation( boolean showSourceLocation )
    {
       this.showSource = showSourceLocation;
       onOpenAnimation();
    }
   
    public void setAnimationSpeed( long millis )
    {
       this.animationSpeed = millis;
       onOpenAnimation();
    }  
   
    public void setInnerRingColor( int color, int alpha )
    {
       this.defaultColor = color;
       this.defaultAlpha = alpha;
    }    
    public void setOuterRingColor( int color, int alpha )
    {
       this.wedge2Color = color;
       this.wedge2Alpha = alpha;
    }      
    public void setOutlineColor( int color, int alpha )
    {
       this.outlineColor = color;
       this.outlineAlpha = alpha;
    }   
    public void setSelectedColor( int color, int alpha )
    {
       this.selectedColor = color;
       this.selectedAlpha = alpha;
    }      
   
    public void setDisabledColor( int color, int alpha )
    {
       this.disabledColor = color;
       this.disabledAlpha = alpha;
    }      
   
    public void setTextColor( int color, int alpha )
    {
       this.textColor = color;
       this.textAlpha = alpha;
    }     

    public void setHeader( String header, int TextSize )
    {
        this.headerString = header;
        this.headerTextSize = scalePX(TextSize);
        HeaderBoxBounded = false;
    }   
    public void setHeaderColors( int TextColor, int TextAlpha, int BgColor, int BgAlpha )
    {
        this.headerTextColor = TextColor;
        this.headerTextAlpha = TextAlpha;
        this.headerBackgroundColor =  BgColor;
        this.headerBackgroundAlpha =  BgAlpha;

    }     

   
    private int scalePX( int dp_size )
    {
       int px_size = (int) (dp_size * screen_density + 0.5f);
       return px_size;
    }
   
   
   private void animateOuterWedges( int animation_direction) {

           boolean animationComplete = false;
         
     
        //Wedge 2
        float slice2 = 360 / wedgeQty2;
        float start_slice2 = 270 - (slice2/2);
        //calculates where to put the images
        double rSlice2 = (2*Math.PI) / wedgeQty2;
        double rStart2 = (2*Math.PI)*(0.75) - (rSlice2/2);      
      
        this.Wedges2 = new Wedge[wedgeQty2];
        this.iconRect2 = new Rect[wedgeQty2];
      
        Wedge2Shown = true;
      
        int wedgeSizeChange = (r2MaxSize-r2MinSize)/animateSections;
      
        if (animation_direction==ANIMATE_OUT) {
            if ( r2MinSize+r2VariableSize+wedgeSizeChange < r2MaxSize) {
                r2VariableSize += wedgeSizeChange;
            } else {
                animateOuterOut = false;
                r2VariableSize = r2MaxSize - r2MinSize;
                animationComplete = true;
            }
          
            //animates text size change
            this.animateTextSize = (textSize/animateSections) * (r2VariableSize/wedgeSizeChange);
          
            //calculates new wedge sizes
            for (int i = 0; i < Wedges2.length; i++) {
                this.Wedges2[i] = new Wedge(xPosition, yPosition, r2MinSize, r2MinSize+r2VariableSize, (i
                        * slice2)+start_slice2, slice2);
                float xCenter = (float)(Math.cos(((rSlice2*i)+(rSlice2*0.5))+rStart2) * (r2MinSize+r2VariableSize+r2MinSize)/2)+xPosition;
                float yCenter = (float)(Math.sin(((rSlice2*i)+(rSlice2*0.5))+rStart2) * (r2MinSize+r2VariableSize+r2MinSize)/2)+yPosition;

                int h = MaxIconSize;
                int w = MaxIconSize;
                if ( wedge2Data.getChildren().get(i).getIcon() != 0 ) {
                    Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon());
                    h = getIconSize(drawable.getIntrinsicHeight(),MinIconSize,MaxIconSize);
                    w = getIconSize(drawable.getIntrinsicWidth(),MinIconSize,MaxIconSize);      
                }

                if (r2VariableSize < h) {
                    h = r2VariableSize;
                }              
                if (r2VariableSize < w) {
                    w = r2VariableSize;
                }                  
              
                this.iconRect2[i] = new Rect((int) xCenter-w/2, (int) yCenter-h/2, (int) xCenter+w/2, (int) yCenter+h/2);
              

                int widthOffset = MaxSize;
                if (widthOffset < this.textRect.width()/2) {
                    widthOffset = this.textRect.width()/2+scalePX(3);
                }
                this.textBoxRect.set((xPosition - (widthOffset)),
                        (int) (yPosition - (r2MinSize+r2VariableSize) - headerBuffer-this.textRect.height()-scalePX(3)),
                        (xPosition + (widthOffset)),
                        (yPosition - (r2MinSize+r2VariableSize) - headerBuffer+scalePX(3)));
                this.headerTextBottom = yPosition - (r2MinSize+r2VariableSize) - headerBuffer-this.textRect.bottom;      
              
            }

        }
        else if (animation_direction==ANIMATE_IN) {
            if ( r2MinSize < r2MaxSize-r2VariableSize-wedgeSizeChange) {
                r2VariableSize += wedgeSizeChange;
            } else {
                animateOuterIn = false;
                r2VariableSize = r2MaxSize;
                animationComplete = true;
            }
          
            //animates text size change
            this.animateTextSize = textSize - ((textSize/animateSections) * (r2VariableSize/wedgeSizeChange));

          
            for (int i = 0; i < Wedges2.length; i++) {
                this.Wedges2[i] = new Wedge(xPosition, yPosition, r2MinSize, r2MaxSize-r2VariableSize, (i
                        * slice2)+start_slice2, slice2);
              
                float xCenter = (float)(Math.cos(((rSlice2*i)+(rSlice2*0.5))+rStart2) * (r2MaxSize-r2VariableSize+r2MinSize)/2)+xPosition;
                float yCenter = (float)(Math.sin(((rSlice2*i)+(rSlice2*0.5))+rStart2) * (r2MaxSize-r2VariableSize+r2MinSize)/2)+yPosition;

                int h = MaxIconSize;
                int w = MaxIconSize;
                if ( wedge2Data.getChildren().get(i).getIcon() != 0 ) {
                    Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon());
                    h = getIconSize(drawable.getIntrinsicHeight(),MinIconSize,MaxIconSize);
                    w = getIconSize(drawable.getIntrinsicWidth(),MinIconSize,MaxIconSize);      
                }

                if (r2MaxSize-r2MinSize-r2VariableSize < h) {
                    h = r2MaxSize-r2MinSize-r2VariableSize;
                }              
                if (r2MaxSize-r2MinSize-r2VariableSize < w) {
                    w = r2MaxSize-r2MinSize-r2VariableSize;
                }                  
              
                this.iconRect2[i] = new Rect((int) xCenter-w/2, (int) yCenter-h/2, (int) xCenter+w/2, (int) yCenter+h/2);
              
      
                //computes header text box
                int heightOffset = r2MaxSize-r2VariableSize;  
                int widthOffset = MaxSize;
                if (MaxSize > r2MaxSize-r2VariableSize) {heightOffset = MaxSize;}
                if (widthOffset < this.textRect.width()/2) {
                    widthOffset = this.textRect.width()/2+scalePX(3);
                }
                this.textBoxRect.set((xPosition - (widthOffset)),
                        (int) (yPosition - (heightOffset) - headerBuffer-this.textRect.height()-scalePX(3)),
                        (xPosition + (widthOffset)),
                        (yPosition - (heightOffset) - headerBuffer+scalePX(3)));
                this.headerTextBottom = yPosition - (heightOffset) - headerBuffer-this.textRect.bottom;      

            }
        }
       
        if (animationComplete == true) {
            r2VariableSize = 0;
            this.animateTextSize = textSize;
            if (animation_direction==ANIMATE_IN) {
                Wedge2Shown = false;
            }
        }
      
        invalidate();  //re-draws the picture
  }       
  
   private void determineWedges() {

        int entriesQty = menuEntries.size();
        if ( entriesQty > 0) {
            wedgeQty = entriesQty;
          
            float degSlice = 360 / wedgeQty;
            float start_degSlice = 270 - (degSlice/2);
            //calculates where to put the images
            double rSlice = (2*Math.PI) / wedgeQty;
            double rStart = (2*Math.PI)*(0.75) - (rSlice/2);      
          
            this.Wedges = new Wedge[wedgeQty];
            this.iconRect = new Rect[wedgeQty];
                  
            for (int i = 0; i < Wedges.length; i++) {
                this.Wedges[i] = new Wedge(xPosition, yPosition, MinSize, MaxSize, (i
                        * degSlice)+start_degSlice, degSlice);
                float xCenter = (float)(Math.cos(((rSlice*i)+(rSlice*0.5))+rStart) * (MaxSize+MinSize)/2)+xPosition;
                float yCenter = (float)(Math.sin(((rSlice*i)+(rSlice*0.5))+rStart) * (MaxSize+MinSize)/2)+yPosition;
              
                int h = MaxIconSize;
                int w = MaxIconSize;
                if ( menuEntries.get(i).getIcon() != 0 ) {
                    Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon());
                    h = getIconSize(drawable.getIntrinsicHeight(),MinIconSize,MaxIconSize);
                    w = getIconSize(drawable.getIntrinsicWidth(),MinIconSize,MaxIconSize);
                }
              
                this.iconRect[i] = new Rect( (int) xCenter-w/2, (int) yCenter-h/2, (int) xCenter+w/2, (int) yCenter+h/2);
            }
          
            invalidate();  //re-draws the picture
        }
   }       
  
   private void determineOuterWedges(RadialMenuEntry entry) {

        int entriesQty = entry.getChildren().size();
        wedgeQty2 = entriesQty;
     
        //Wedge 2
        float degSlice2 = 360 / wedgeQty2;
        float start_degSlice2 = 270 - (degSlice2/2);
       //calculates where to put the images
        double rSlice2 = (2*Math.PI) / wedgeQty2;
        double rStart2 = (2*Math.PI)*(0.75) - (rSlice2/2);      
      
        this.Wedges2 = new Wedge[wedgeQty2];
        this.iconRect2 = new Rect[wedgeQty2];
              
        for (int i = 0; i < Wedges2.length; i++) {
            this.Wedges2[i] = new Wedge(xPosition, yPosition, r2MinSize, r2MaxSize, (i
                    * degSlice2)+start_degSlice2, degSlice2);
            float xCenter = (float)(Math.cos(((rSlice2*i)+(rSlice2*0.5))+rStart2) * (r2MaxSize+r2MinSize)/2)+xPosition;
            float yCenter = (float)(Math.sin(((rSlice2*i)+(rSlice2*0.5))+rStart2) * (r2MaxSize+r2MinSize)/2)+yPosition;

            int h = MaxIconSize;
            int w = MaxIconSize;
            if ( entry.getChildren().get(i).getIcon() != 0 ) {
                Drawable drawable = getResources().getDrawable(entry.getChildren().get(i).getIcon());
                h = getIconSize(drawable.getIntrinsicHeight(),MinIconSize,MaxIconSize);
                w = getIconSize(drawable.getIntrinsicWidth(),MinIconSize,MaxIconSize);
            }          
            this.iconRect2[i] = new Rect((int) xCenter-w/2, (int) yCenter-h/2, (int) xCenter+w/2, (int) yCenter+h/2);
        }
        this.wedge2Data = entry;
        invalidate();  //re-draws the picture
  }       
  
   private void determineHeaderBox() {

        this.headerTextLeft = xPosition - this.textRect.width()/2;
        this.headerTextBottom = yPosition - (MaxSize) - headerBuffer-this.textRect.bottom;
        int offset = MaxSize;
        if (offset < this.textRect.width()/2) {
            offset = this.textRect.width()/2+scalePX(3);
        }
      
        this.textBoxRect.set((xPosition - (offset)),
                (int) (yPosition - (MaxSize) - headerBuffer-this.textRect.height()-scalePX(3)),
                (xPosition + (offset)),
                (yPosition - (MaxSize) - headerBuffer+scalePX(3)));
   
    }
  
    public class Wedge extends Path {
        private int x, y;
        private int InnerSize, OuterSize;
        private float StartArc;
        private float ArcWidth;
      
        private Wedge(int x, int y, int InnerSize, int OuterSize, float StartArc, float ArcWidth) {
            super();
          
            if (StartArc >= 360) {
                StartArc = StartArc-360;
            }
          
            this.x = x; this.y = y;
            this.InnerSize = InnerSize;
            this.OuterSize = OuterSize;
            this.StartArc = StartArc;
            this.ArcWidth = ArcWidth;
            this.buildPath();
        }
      
        private void buildPath() {

            final RectF rect = new RectF();
            final RectF rect2 = new RectF();
          
            //Rectangles values
            rect.set(this.x-this.InnerSize, this.y-this.InnerSize, this.x+this.InnerSize, this.y+this.InnerSize);
            rect2.set(this.x-this.OuterSize, this.y-this.OuterSize, this.x+this.OuterSize, this.y+this.OuterSize);
                 
            this.reset();
            //this.moveTo(100, 100);
            this.arcTo(rect2, StartArc, ArcWidth);
            this.arcTo(rect, StartArc+ArcWidth, -ArcWidth);
                  
            this.close();


        }
    }

}

3. Create New Service class SystemOverlayService.class

Write following code on that code

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

public class SystemOverlayService extends Service implements OnTouchListener, OnClickListener {
    private View topLeftView;

    private Button overlayedButton;
    private float offsetX;
    private float offsetY;
    private int originalXPos;
    private int originalYPos;
    private boolean moving;
    private WindowManager wm;

    private RadialMenuWidget PieMenu;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

        PieMenu = new RadialMenuWidget(getBaseContext());

        int xLayoutSize = wm.getDefaultDisplay().getWidth();
        int yLayoutSize = wm.getDefaultDisplay().getHeight();

        PieMenu.setAnimationSpeed(0L);
        PieMenu.setSourceLocation(xLayoutSize, yLayoutSize);
        PieMenu.setIconSize(15, 30);
        PieMenu.setTextSize(13);

        PieMenu.setCenterCircle(new Close());
        PieMenu.addMenuEntry(new Menu1());
        PieMenu.addMenuEntry(new NewTestMenu());
        PieMenu.addMenuEntry(new CircleOptions());
        PieMenu.addMenuEntry(new Menu2());
        PieMenu.addMenuEntry(new Menu3());

        overlayedButton = new Button(this);
        overlayedButton.setText("Overlay button");
        overlayedButton.setOnTouchListener(this);
        overlayedButton.setBackgroundColor(0x55fe4444);
        overlayedButton.setOnClickListener(this);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = 0;
        params.y = 0;
        wm.addView(overlayedButton, params);

        topLeftView = new View(this);
        WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
        topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
        topLeftParams.x = 0;
        topLeftParams.y = 0;
        topLeftParams.width = 0;
        topLeftParams.height = 0;
        wm.addView(topLeftView, topLeftParams);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (overlayedButton != null) {
            wm.removeView(overlayedButton);
            wm.removeView(topLeftView);
            overlayedButton = null;
            topLeftView = null;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            float x = event.getRawX();
            float y = event.getRawY();

            moving = false;

            int[] location = new int[2];
            overlayedButton.getLocationOnScreen(location);

            originalXPos = location[0];
            originalYPos = location[1];

            offsetX = originalXPos - x;
            offsetY = originalYPos - y;

        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            int[] topLeftLocationOnScreen = new int[2];
            topLeftView.getLocationOnScreen(topLeftLocationOnScreen);

            System.out.println("topLeftY=" + topLeftLocationOnScreen[1]);
            System.out.println("originalY=" + originalYPos);
            float x = event.getRawX();
            float y = event.getRawY();

            WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();

            int newX = (int) (offsetX + x);
            int newY = (int) (offsetY + y);

            if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
                return false;
            }

            params.x = newX - (topLeftLocationOnScreen[0]);
            params.y = newY - (topLeftLocationOnScreen[1]);

            wm.updateViewLayout(overlayedButton, params);
            moving = true;
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (moving) {
                return true;
            }
        }

        return false;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();

        wm.removeView(overlayedButton);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.CENTER;

        wm.addView(PieMenu, params);

    }

    public static class Menu1 implements RadialMenuEntry {
        public String getName() {
            return "Menu1 - No Children";
        }

        public String getLabel() {
            return "Menu1\nTest";
        }

        public int getIcon() {
            return 0;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("Menu #1 Activated - No Children");
        }
    }

    public static class Menu2 implements RadialMenuEntry {
        public String getName() {
            return "Menu2 - Children";
        }

        public String getLabel() {
            return "Menu2";
        }

        public int getIcon() {
            return R.drawable.icon;
        }

        private List<RadialMenuEntry> children = new ArrayList<RadialMenuEntry>(Arrays.asList(new StringOnly(), new IconOnly(), new StringAndIcon()));

        public List<RadialMenuEntry> getChildren() {
            return children;
        }

        public void menuActiviated() {
            System.out.println("Menu #2 Activated - Children");
        }
    }

    public static class Menu3 implements RadialMenuEntry {
        public String getName() {
            return "Menu3 - No Children";
        }

        public String getLabel() {
            return null;
        }

        public int getIcon() {
            return R.drawable.icon;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("Menu #3 Activated - No Children");
        }
    }

    public static class IconOnly implements RadialMenuEntry {
        public String getName() {
            return "IconOnly";
        }

        public String getLabel() {
            return null;
        }

        public int getIcon() {
            return R.drawable.icon;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("IconOnly Menu Activated");
        }
    }

    public static class StringAndIcon implements RadialMenuEntry {
        public String getName() {
            return "StringAndIcon";
        }

        public String getLabel() {
            return "String";
        }

        public int getIcon() {
            return R.drawable.icon;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("StringAndIcon Menu Activated");
        }
    }

    public static class StringOnly implements RadialMenuEntry {
        public String getName() {
            return "StringOnly";
        }

        public String getLabel() {
            return "String\nOnly";
        }

        public int getIcon() {
            return 0;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("StringOnly Menu Activated");
        }
    }

    public static class NewTestMenu implements RadialMenuEntry {
        public String getName() {
            return "NewTestMenu";
        }

        public String getLabel() {
            return "New\nTest\nMenu";
        }

        public int getIcon() {
            return 0;
        }

        private List<RadialMenuEntry> children = new ArrayList<RadialMenuEntry>(Arrays.asList(new StringOnly(), new IconOnly()));

        public List<RadialMenuEntry> getChildren() {
            return children;
        }

        public void menuActiviated() {
            System.out.println("New Test Menu Activated");
        }
    }

    public static class CircleOptions implements RadialMenuEntry {
        public String getName() {
            return "CircleOptions";
        }

        public String getLabel() {
            return "Circle\nSymbols";
        }

        public int getIcon() {
            return 0;
        }

        private List<RadialMenuEntry> children = new ArrayList<RadialMenuEntry>(Arrays.asList(new RedCircle(), new YellowCircle(), new GreenCircle(),
                new BlueCircle()));

        public List<RadialMenuEntry> getChildren() {
            return children;
        }

        public void menuActiviated() {
            System.out.println("Circle Options Activated");
        }
    }

    public static class RedCircle implements RadialMenuEntry {
        public String getName() {
            return "RedCircle";
        }

        public String getLabel() {
            return "Red";
        }

        public int getIcon() {
            return R.drawable.red_circle;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("Red Circle Activated");
        }
    }

    public static class YellowCircle implements RadialMenuEntry {
        public String getName() {
            return "YellowCircle";
        }

        public String getLabel() {
            return "Yellow";
        }

        public int getIcon() {
            return R.drawable.yellow_circle;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("Yellow Circle Activated");
        }
    }

    public static class GreenCircle implements RadialMenuEntry {
        public String getName() {
            return "GreenCircle";
        }

        public String getLabel() {
            return "Green";
        }

        public int getIcon() {
            return R.drawable.green_circle;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("Green Circle Activated");
        }
    }

    public static class BlueCircle implements RadialMenuEntry {
        public String getName() {
            return "BlueCircle";
        }

        public String getLabel() {
            return "Blue";
        }

        public int getIcon() {
            return R.drawable.blue_circle;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {
            System.out.println("Blue Circle Activated");
        }
    }

    public class Close implements RadialMenuEntry {

        public String getName() {
            return "Close";
        }

        public String getLabel() {
            return null;
        }

        public int getIcon() {
            return android.R.drawable.ic_menu_close_clear_cancel;
        }

        public List<RadialMenuEntry> getChildren() {
            return null;
        }

        public void menuActiviated() {

            System.out.println("Close Menu Activated");
            //Need to figure out how to to the layout.removeView(PieMenu)
            //ll.removeView(PieMenu);
//            ((WindowManager) PieMenu.getParent()).removeView(PieMenu);
            wm.removeView(PieMenu);
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.LEFT | Gravity.TOP;
            params.x = 0;
            params.y = 0;
            wm.addView(overlayedButton, params);
        }
    }
}

4. Create Activity to start Service


class ShowView extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent svc = new Intent(this, SystemOverlayService.class);
        startService(svc);
        finish();
    }
}

5. Registered Service on AndroidManifest.xml file

<service
            android:name=".SystemOverlayService"
            android:exported="true" />


6. Use Following drawables on res/drawable folder

--- blue_circle.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#FF000000" />
            <size android:width="16dp" android:height="16dp" />
        </shape>
    </item>
    <item android:top="2dp" android:left="2dp" android:right="2dp" android:bottom="2dp">
        <shape android:shape="oval">
            <solid android:color="#FFFFFFFF" />
        </shape>
    </item>
    <item android:top="4dp" android:left="4dp" android:right="4dp" android:bottom="4dp">
        <shape android:shape="oval">
                <gradient android:startColor="#FF4040FF" android:endColor="#FF0000A0"
            android:angle="270"/>
        </shape>
    </item>    
</layer-list>


--- green_circle.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#FF000000" />
            <size android:width="16dp" android:height="16dp" />
        </shape>
    </item>
    <item android:top="2dp" android:left="2dp" android:right="2dp" android:bottom="2dp">
        <shape android:shape="oval">
            <solid android:color="#FFFFFFFF" />
        </shape>
    </item>
    <item android:top="4dp" android:left="4dp" android:right="4dp" android:bottom="4dp">
        <shape android:shape="oval">
                <gradient android:startColor="#FF40FF40" android:endColor="#FF00A000"
            android:angle="270"/>
        </shape>
    </item>  
</layer-list>

--red_circle.xml


<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#FF000000" />
            <size android:width="16dp" android:height="16dp" />
        </shape>
    </item>
    <item android:top="2dp" android:left="2dp" android:right="2dp" android:bottom="2dp">
        <shape android:shape="oval">
            <solid android:color="#FFFFFFFF" />
        </shape>
    </item>
    <item android:top="4dp" android:left="4dp" android:right="4dp" android:bottom="4dp">
        <shape android:shape="oval">
                <gradient android:startColor="#FFFF4040" android:endColor="#FFA00000"
            android:angle="270"/>
        </shape>
    </item>    
</layer-list>

--yellow_circle.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#FF000000" />
            <size android:width="16dp" android:height="16dp" />
        </shape>
    </item>
    <item android:top="2dp" android:left="2dp" android:right="2dp" android:bottom="2dp">
        <shape android:shape="oval">
            <solid android:color="#FFFFFFFF" />
        </shape>
    </item>
    <item android:top="4dp" android:left="4dp" android:right="4dp" android:bottom="4dp">
        <shape android:shape="oval">
                <gradient android:startColor="#FFFFFF40" android:endColor="#FFA0A000"
            android:angle="270"/>
        </shape>
    </item>  
</layer-list>















Friday, June 29, 2012

Create Database

These are many way to create database in android application.
When your application install only that time database is created and it deleted when you remove your application from device.

For this you need know about SQLiteOpenHelper.

Here is small demo of how to create database and how to use it.

1.  Create class that extends SQLiteOpenHelper class in that create sub-class of table data that you need in onCreate() method you write code for table creation.

public class Database extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "database.db";
    private static final int DATABASE_VERSION = 1;
    SQLiteDatabase sqlitedatabase = null;

    public Database(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    public static final class EMP {
        public final static String TABLE_NAME = "employee";

        public final static String _ID = "_id";
        public final static String NAME = "emp_name";

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        sqlitedatabase = db;

        sqlitedatabase.execSQL("create table " + EMP.TABLE_NAME + "(" + EMP._ID
                + " Integer primary key autoincrement," + EMP.NAME + " text);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}



2. Create class that contains all the function that you will use in your application.

public class DatabaseFunction {
    SQLiteDatabase db = null;
    Database database;

    DatabaseFunction(Context context) {
        database = new Database(context);
        db = database.getWritableDatabase();

    }

    public long insertData(String name)
    {
        ContentValues cv = new ContentValues();
        cv.put(EMP.NAME    , name);
        return db.insert(EMP.TABLE_NAME, null, cv);
    }
   
    public int deleteData(String name)
    {
        return db.delete(EMP.TABLE_NAME, EMP.NAME+"=?", new String[]{name});
    }
   
    public int updateDate(String oldName, String newName)
    {
        ContentValues cv =new ContentValues();
        cv.put(EMP.NAME, newName);
        return db.update(EMP.TABLE_NAME, cv, EMP.NAME+"=?", new String[]{oldName});
    }
    public Cursor selectFromEmp() {
        return db.query(EMP.TABLE_NAME, null, null, null, null, null, null);
    }

    // close SQLiteDatabase object it must
    public void close() {
        db.close();
    }

}

3. How to use above database in your application
    Create object of DatabaseFunction class that contains your function that you want to use in your application

using object of DatabaseFunction call appropriate method.


public class EmpActivity extends Activity{

@Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emp);
        DatabaseFunction df=new DatabaseFunction(getApplicationContext()) ;
       
        String name = "abc";
        df.insertData(name);
        Cursor c=df. selectFromEmp();
        // perform any operation on cursor
        if( c!=null && c.getCount()>0)
        {
            c.moveToFirst();
            String nm = c.getString(c.getColumnIndex(EMP.NAME));
            Log.i("Name", nm);
        }
//         then close cursor object first
       c.close();
      
       String newName = "xyz";
      
       // for updating table
       df.updateDate("abc", newName);
       c=df. selectFromEmp();
       // display updated values from cursor
       if( c!=null && c.getCount()>0)
       {
           c.moveToFirst();
           String nm = c.getString(c.getColumnIndex(EMP.NAME));
           Log.i("Name", nm);
       }
      
       // delete value from table
       df.deleteData(newName);
      
//     then close DatabaseFunction object
       df.close();
    }

}

Thank you for referring my blog.
Please write comment for any suggestion and improvement for writing blog.






Saturday, June 2, 2012

Save Activity state example in Android

In some application it is needed to store activity state/data. If this data not store before activity finishes it loss the data. So before finishing activity you need to store data somewhere from where you can get it back when activity restarted.

There are many way to store activity state but here I am uses of SharePreferences. It uses because it easy to manage to user.

Here is complete example of save state of activity.

----------------------------------------------------------------------------------------------

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class SavingActivityStateActivity extends Activity {
   
   
    @Override
    protected void onDestroy() {
        saveState();
        super.onDestroy();
    }

    EditText etName;
    EditText etAddress;
    EditText etEmail;
    EditText etPno;
   
    RadioGroup rgGender;
    CheckBox chbsingle;
    Spinner spEdu;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        loadUI();    // it load previously saved data if any data stored in preference.
     
    }
  
// load data from saved preference.
    private void loadUI() {
       
        SharedPreferences savedPref=getPreferences(MODE_PRIVATE);
       
        setEditText(R.id.etName,  savedPref.getString("Name", ""));
        setEditText(R.id.etAddress,  savedPref.getString("Address", ""));
        setEditText(R.id.etEmail,  savedPref.getString("Email", ""));
        setEditText(R.id.etPno,  savedPref.getString("Pno", ""));
       
        String gender=savedPref.getString("Gender", "Male");
       

        rgGender=(RadioGroup) findViewById(R.id.rgGender);
       
        if(gender.equals("Male"))
        {
            RadioButton rbMale=(RadioButton) findViewById(R.id.rbMale);
            rbMale.setChecked(true);
        }
        else if(gender.equals("Female"))
        {
            RadioButton rbFemale=(RadioButton) findViewById(R.id.rbFemale);
            rbFemale.setChecked(true);
        }
       
        boolean single=savedPref.getBoolean("Single", true);
        chbsingle=(CheckBox) findViewById(R.id.chbSingle);
       
        chbsingle.setChecked(single);
       
        spEdu=(Spinner) findViewById(R.id.spEducation);
        int edu=savedPref.getInt("Edu", 0);
        spEdu.setSelection(edu);
       
    }

    @Override
    protected void onPause() {
   
        super.onPause();
        // before finishing activity data of each field is store to preference using this method.
        saveState();
    }

    // save data to preference
    private void saveState() {
        SharedPreferences savePref=getPreferences(MODE_PRIVATE);
        // create preference

        SharedPreferences.Editor editor=savePref.edit();
        // create editor of preference to edit


        // following code save each value of view to share preference       
        editor.putString("Name",getEditText(R.id.etName));
        editor.putString("Address",getEditText(R.id.etAddress));
        editor.putString("Email",getEditText(R.id.etEmail));
        editor.putString("Pno",getEditText(R.id.etPno));
       
        rgGender=(RadioGroup) findViewById(R.id.rgGender);
        int selected=rgGender.getCheckedRadioButtonId();
       
        RadioButton rbSelected=(RadioButton) findViewById(selected);
        String gender=rbSelected.getText().toString();
   

        editor.putString("Gender", gender);
       
        chbsingle=(CheckBox) findViewById(R.id.chbSingle);
       
        boolean single=chbsingle.isChecked();
       
        editor.putBoolean("Single", single);
       
        spEdu=(Spinner) findViewById(R.id.spEducation);
       
        int edu=spEdu.getSelectedItemPosition();
       
        editor.putInt("Edu", edu);
        editor.commit();
    }

    private String getEditText(int id)
    {
        String text="";
       
        EditText et=(EditText) findViewById(id);
        text=et.getText().toString();
        return text;
    }
   
   
    private void setEditText(int id, String text)
    {   
        EditText et=(EditText) findViewById(id);
        et.setText(text);
    }
}

Friday, June 1, 2012

Video Recording example in Android

Here is complete example of how to record video in android device.
(Note: test project in real device not in emulator)

1.  Video View Activity
   
     
import java.io.File;
import java.io.IOException;
import java.util.Date;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Menu;
import android.view.SurfaceView;
import android.widget.Toast;

public class VideoViewActivity extends Activity implements Callback {

    @Override
    protected void onDestroy() {
        stopRecording();
        super.onDestroy();
    }

    private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;
    public MediaRecorder mrec = new MediaRecorder();   
    private Camera mCamera;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mCamera = Camera.open();
       
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
               
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {

        menu.add(0, 0, 0, "Start");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getTitle().equals("Start"))
        {
            try {
               
                startRecording();
                item.setTitle("Stop");

            } catch (Exception e) {

                String message = e.getMessage();
                Log.i(null, "Problem " + message);
                mrec.release();
            }

        }
        else if(item.getTitle().equals("Stop"))
        {
            mrec.stop();
            mrec.release();
            mrec = null;
            item.setTitle("Start");
        }

        return super.onOptionsItemSelected(item);
    }

    protected void startRecording() throws IOException
    {
        if(mCamera==null)
            mCamera = Camera.open();
       
         String filename;
         String path;
       
         path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();
        
         Date date=new Date();
         filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";
        
         //create empty file it must use
         File file=new File(path,filename);
        
        mrec = new MediaRecorder();

        mCamera.lock();
        mCamera.unlock();

        // Please maintain sequence of following code.

        // If you change sequence it will not work.
        mrec.setCamera(mCamera);   
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);    
        mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mrec.setPreviewDisplay(surfaceHolder.getSurface());
        mrec.setOutputFile(path+filename);
        mrec.prepare();
        mrec.start();

       
    }

    protected void stopRecording() {

        if(mrec!=null)
        {
            mrec.stop();
            mrec.release();
            mCamera.release();
            mCamera.lock();
        }
    }

    private void releaseMediaRecorder() {

        if (mrec != null) {
            mrec.reset(); // clear recorder configuration
            mrec.release(); // release the recorder object
        }
    }

    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.release(); // release the camera for other applications
            mCamera = null;
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {     

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {      

        if (mCamera != null) {
            Parameters params = mCamera.getParameters();
            mCamera.setParameters(params);
            Log.i("Surface", "Created");
        }
        else {
            Toast.makeText(getApplicationContext(), "Camera not available!",
                    Toast.LENGTH_LONG).show();

            finish();
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();      

    }
}




2. main.xml file


      
         <SurfaceView android:id="@+id/surface_camera"     xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:layout_weight="1"
        >
    </SurfaceView>






3.  Get following permission in AndroidManifest.xml

  
      <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>