Search This Blog

Friday, 25 January 2013

Tips and tricks while creating your own android application.

Android development is becoming prodigy business day by day. As its market grows, its attract more developer. One easy thing is, if you know Java, you can easily shift to developing android application. It add more value to android as well as it added a lot of  crap in android play store. Every one want one's application to make iron wave on Play store. Before jumping into sea, we should do some home work which one necessity and universal truth to create one milestone in your career.
I am observing some regular mistake that make a good application to be in category of crap. From the last 15 days i am observing trend of Play store applications. On basis of all my observation and experience, I suggest you some tips to improve your application quality

Never allow your application to be unresponsive

User can tolerate anything but can not tolerate if becomes confuse of application behavior. Extra few seconds can harm your application reputation, if it does not show any required message to user. So to avoid this, always show proper progress message while you are doing background task. Use background service, Thread, Asynchronous task where performing time consuming task and show loading. E.g. Reading data from Data base or Downloading data from URL. Even though in latest version of android you can not call URL in UI thread. But being proactive is always good.


Use multi-pane Layout with Fragment

Every one knows how immense the screen space for mobile is?  Use complete screen space as you can. Wasting space is a bad development habit. Use different layout for different orientation, device. Use fragment to show list and detail on same page in landscape mode, as we have enough space.

Use combination of Relative Layout and Density Pixel(dp). 

dp is device independent pixel,which provide good design on phone with different densities. Relative Layout align child on relativity, so chances of hampering layout reduces.

Never keep unnecessary Permission 

Recently, I read review of my application. User raise one issue, why should one's allow to make phone call. I was surprised that my application never need this permission but i forgot to remove it from manifest and it cost me worse review. So never play with user, Take preventing measure and remove all permission that does not required. This is not common issue but sometimes we commit this by mistake. So double check your manifest permission.

Always shows proper Alert

Showing proper alert, is big boost to make user happy. I download one application which always show "Your connection is not available" either i have connection or my server does not respond with in time (while downloading application data).This make awkward as i was confused. So always keep related message in alert.

Avoid to use high quality Image if you do not really want these 

Using high quality image, limit available memory for your application. When we load image into memory, it take lots of memory and application heap size shrink drastically. And this becomes the main cause of crashing application. This is really bad experience. If your application really required image of high quality then you can scale them according to device heap size and density. Using image of 3000*4000 on 720*1280 resolution does not mean anything.

Release hardware if you are using

If you are using hardware like camera, Bluetooth then please release them properly after use is over. Most immense issue is of GPS if you are using GPS then remove update after use is over. Else it will drain user battery

Never forgot to close connection 

Either you open data base or reading data from online URL, make sure you close all connection pointing to source. Close Input stream and data base connection when you read data. This will help you to avoid conflict and save your memory also.

And last but not least, if you have commit any mistake. Then do not need worry, upload a updated version of your application so that user will download your update version. I consider application as child of developer, so do not leave any stone to be upturned to make it successful

Saturday, 12 January 2013

Android Map Application : Drawing straight path between two Geo point on android Map

Drawing overlay on map in android, we have discusses in previous article. I suggest you to go through that article for easy way to get current concept.  Projection class is responsible for converting device coordinate to Geo point and vice-versa.  Once we convert these point to each other, our task of drawing path between two Geo point becomes very easy.

So lets start consider that you have already make one map simple application in android. And look at screen of simple map application result using your Map API Key


Now divide further task in steps----

Step 1) Create two point to draw on map using OverlayItem. After creating two Geo point, we will create one class ItemizedOverlayItem  to populate these point on MapView

      ItemizedOverlayItem itemMized;  
      Vector<OverlayItem> allOverLArrayList = new Vector<OverlayItem>();  
      private void drawTwoPoint() {  
           itemMized = new ItemizedOverlayItem(getResources().getDrawable(  
                     R.drawable.ic_launcher));  
           GeoPoint geopoint = new GeoPoint(230286706, 72509145);  
           mapView.getController().setCenter(geopoint);  
           OverlayItem item = new OverlayItem(geopoint, "", "");  
           allOverLArrayList.add(item);  
           geopoint = new GeoPoint(222286706, 71509145);  
           item = new OverlayItem(geopoint, "", "");  
           allOverLArrayList.add(item);  
           itemMized.addOverLayItme(allOverLArrayList);  
           mapView.getOverlays().add(itemMized);  
           mapView.getController().setCenter(geopoint);  
      }  
   

Step 2) So lets populate our two point on map. In next step, we will draw a most awaited path.


 package com.ahmad.displaylineonmap;  
 import java.util.Vector;  
 import android.graphics.drawable.Drawable;  
 import com.google.android.maps.ItemizedOverlay;  
 import com.google.android.maps.OverlayItem;  
 public class ItemizedOverlayItem extends ItemizedOverlay<OverlayItem> {  
      public ItemizedOverlayItem(Drawable arg0) {  
           super(boundCenter(arg0));  
      }  
      @Override  
      protected OverlayItem createItem(int arg0) {  
           return mlocItems.get(arg0);  
      }  
      public void addOverLayItme(Vector<OverlayItem> mlocItems) {  
           this.mlocItems = mlocItems;  
           populate();  
      }  
      Vector<OverlayItem> mlocItems;  
      @Override  
      public int size() {  
           return mlocItems.size();  
      }  
 }  


Step 3) Now this tutorial creates a layer on map to draw a path between two Geo points.


 package com.ahmad.displaylineonmap;  
 import android.graphics.Canvas;  
 import android.graphics.Color;  
 import android.graphics.Paint;  
 import android.graphics.Point;  
 import com.google.android.maps.GeoPoint;  
 import com.google.android.maps.MapActivity;  
 import com.google.android.maps.MapView;  
 import com.google.android.maps.Overlay;  
 import com.google.android.maps.Projection;  
 public class OverLayClass extends Overlay {  
      Projection projection;  
      MapActivity mapActivity;  
      MapView mapView;  
      public OverLayClass(Projection projection, MapActivity act, MapView map) {  
           this.projection = projection;  
           mPaint = new Paint();  
           mPaint.setAntiAlias(true);  
           mPaint.setDither(true);  
           mPaint.setColor(Color.BLACK);  
           mPaint.setStyle(Paint.Style.STROKE);  
           mPaint.setStrokeJoin(Paint.Join.ROUND);  
           mPaint.setStrokeCap(Paint.Cap.ROUND);  
           mPaint.setStrokeWidth(12);  
           this.mapActivity = act;  
           mapView = map;  
      }  
      Paint mPaint;  
      public void draw(Canvas canvas, MapView mapv, boolean shadow) {  
           super.draw(canvas, mapv, shadow);  
           GeoPoint geopoint = new GeoPoint(230286706, 72509145);  
           Point pointOne = projection.toPixels(geopoint, null);  
           geopoint = new GeoPoint(222286706, 71509145);  
           Point pointtwo = projection.toPixels(geopoint, null);  
           canvas.drawLine(pointOne.x, pointOne.y, pointtwo.x, pointtwo.y, mPaint);  
      }  
 }  



Some points and lines need to be discusses here. One important thing is Projection which allows us to interchange device coordinate to Geo point. So look at this
        
                GeoPoint geopoint = new GeoPoint(230286706, 72509145);
Point pointOne = projection.toPixels(geopoint, null);
geopoint = new GeoPoint(222286706, 71509145);
Point pointtwo = projection.toPixels(geopoint, null);

Attaching Layer name Overlay class to map to create any figure
                       
                OverLayClass mOverLayClass = new OverLayClass(projection, this, mapView);
mapView.getOverlays().add(mOverLayClass);

So now its time for complete source code
                  

Download Complete Source Code 

Monday, 7 January 2013

Android Map Application : Making shape on android MapView using overlay and Touch event

Generally we know how to make sample application of map in android. Its quite easy. But we struck when we goes for advance like showing ping either single or multiple on map, drawing figure on map , drawing path between  two point. Consider all point, there is only one solution for all, and that is Overlay. Overlay consider to be layer on a MapView in android. Using and modifying Overlay almost match with View and SurfaceView.
This tutorial will cover  about drawing shape on android map.  This tutorial will go now steps wise

Step 1) Create one simple Map application

Step 2) Create one OverLayClass which extends Overlay. Overlay create one layer above map. We add this layer to MapView. This layer provide us to draw figure using canvas, path and paint

Step 3) Add OverLayClass to map. Now this layer will attach to our MapView to allow us to draw something. Overlay method onDraw calls every time we touch Overlay. So it enable our change to Overlays layer

Explanation About OverLayClass


Our task start when we touch the Overlay. We start counting point and draw on canvas using Path. For giving path a real look in constructor of OverLayClass we customize it's Paint class mPaint. We track three touch event ACTION_DOWN, ACTION_UP and ACTION_MOVE. And draw between ACTION_DOWN and ACTION_DOWN. Our path goes on increase between ACTION_UP and ACTION_DOWN.  Have a look at code

      private float oldX, oldY;  
      private float firstoldX, firstoldY;  
      private boolean isFirstTime = false;  
      @Override  
      public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {  
           switch (arg0.getAction()) {  
           case MotionEvent.ACTION_MOVE:  
                touch_move(arg0.getX(), arg0.getY());  
                break;  
           case MotionEvent.ACTION_DOWN:  
                mapView.getOverlays().clear();  
                mapView.getOverlays().add(this);  
                touch_start(arg0.getX(), arg0.getY());  
                break;  
           case MotionEvent.ACTION_UP:  
                mPath.lineTo(oldX, oldY);  
                float mDistance = getDistanceBetwenFirstAndLastPoint();  
                Log.d("Distance F and L", String.valueOf(mDistance));  
                if (mDistance < 100)  
                     mPath.close();  
                isFirstTime = false;  
                break;  
           }  
           return true;  
      }  
      private void touch_start(float x, float y) {  
           mPath.reset();  
           mPath.moveTo(x, y);  
           oldX = x;  
           oldY = y;  
           if (!isFirstTime) {  
                firstoldX = oldX;  
                firstoldY = oldY;  
                isFirstTime = true;  
           }  
      }  
      private void touch_move(float x, float y) {  
           float dx = Math.abs(x - oldX);  
           float dy = Math.abs(y - oldY);  
           if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {  
                mPath.quadTo(oldX, oldY, (x + oldX) / 2, (y + oldY) / 2);  
                oldX = x;  
                oldY = y;  
           }  
      }  

Adding Overlay to android map

      private void setOverLay() {  
           OverLayClass mOverLayClass = new OverLayClass(projection, this, mapView);  
           mapView.getOverlays().add(mOverLayClass);  
      }  


You can close the path if you want. In OverLayClass you will get complete idea by seeing source code of application. Source code can be downloaded using link at the end of article.


Download Source

Android News and source code