Tuesday, January 27, 2015

Gesture detection and scrollview issue

In one of my latest application when I implemented ViewFlipper with gesture listener, I come across a strange issue. All child of ViewFlipper are scrollViews. I implemented simple gesture listener like when swipe to left or right will take to the next scrollView. But the issue was swipe functionality is not working in scrollView. If the ViewFlipper child are any other layout like Linearlayout, swipe functionality is working fine. As usual, I debugged and found that, we have to override the dispatchTouchEvent(MotionEvent e) of activity class.

The dispatchTouchEvent() will look like this,
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return gestureDetector.onTouchEvent(ev);
}
Where gestureDetector is an instance of android.view.GestureDetector class.

Reference:
http://stackoverflow.com/questions/8330187/gesture-detection-and-scrollview-issue

Thursday, January 22, 2015

How to avoid MEMORY LEAKS in Android

Memory leaks means resources which are not available for garbage collection. GC will free resources which are not referenced. Sometimes a single reference can prevent a large set of objects from being garbage collected. For example, some static variables which are referenced in an activity may not be for GC, because as long as the reference exists, the Activity will be kept in memory, leaking all of its views.

Code snippet from Romain Guy is as,

private static Drawable sBackground;

@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);

  TextView label = new TextView(this);
  label.setText("Leaks are bad");

  if (sBackground == null) {
    sBackground = getDrawable(R.drawable.large_bitmap);
  }
  label.setBackgroundDrawable(sBackground);

  setContentView(label);
}
Here, the static drawable sBackground will leak.

The solution here is when the activity gets destroyed, release the drawable in destroy and unbind all callbacks


Here, our onDestroy() will look like this,


protected void onDestroy() { 
super.onDestroy();
sBackground.setCallbacks(null);
sBackground = null;
}

So that the drawable sBackground will be available for GC and can prevent memory leak when activity gets destroyed.

Another common cause for memory leak is non-static inner classes in an Activity. This commonly comes when creating fragments in an Activity. Either create static inner classes or create stand alone classes.

Reference:
http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html
http://stackoverflow.com/questions/6567647/avoid-memory-leaks-on-android

Tuesday, April 8, 2014

CalledFromWrongThreadException in Android

I got CalledFromWrongThreadException when I tried to update UI from another thread. I have two threads in my app, one our main UI thread and another for network operations. So when I tried to update UI from "network thread", I got the exception CalledFromWrongThreadException.

Solution:
The solution for this is update the UI in UI thread. For this, just put the update UI statements inside runOnUiThread block. First get the activity context, and call runOnUiThread(). Like,
runOnUIThread(new Runnable() {
public void run() {
...
}
});
Happy Coding

Sunday, March 9, 2014

Recap: A look back for Android, from late 2008 to early 2014

I was not active in blogging for a while, and after a long gap, posting a new one. All my previous posts are from my app development experience in Android. This one is a little different. I like to tell about brief history of Android from my experience and updates

Android changed a lot from the initial days. Late 2008 and early 2009's, where Android phones are just entering into the smartphone market and first phone was from T-Mobile called G1. During that time, testing of the apps are mainly on emulator, so when I first tested my app in an real Android phone, I was so excited. G1 is almost like what we see in an emulator. It was Android SDK v 1.0. Now, jelly bean and the latest kitkat arrived, which are improved a lot, also in the process, Android evolved as a strong mobile OS as well as matured a lot in terms of security, user experience and added more features.

In the global smartphone market, 80% share belongs to Android. In five years from 0 to 80% share is a big achievement. Of this, 80% of Android phones are Android version ICS (API level 14 and above). In this majority of the share goes to Samsung. Samsung share in Android phones is about 63%. That means Out of 80 Android phones, 50 are from Samsung.

Tablets

Now Android is capturing Tablet market also, In 2013, Android takes 62% market share of tablet as Apple's share declines.

Note:
The numbers in this post are got from various sites. I did my best to get the numbers accurate and updated.

Friday, June 7, 2013

Open Android market (Google Play Store) using Intent

To open google play store (Android market) using intent, just use the code snippet
 

String appName = "";  
Uri uri = Uri.parse("market://details?id=" + appName);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (isAvaialable(intent, mContext)) {
startActivity(intent);  
} else {
Uri uri = Uri.parse("http://play.google.com/store/apps/details?id="+appName);
intent.setData(uri);
startActivity(intent); 
}  

public static boolean isAvailable(Context ctx, Intent intent) {
   final PackageManager mgr = ctx.getPackageManager();
   List list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
   return list.size() > 0;
}
isAvailable() is an utility method that I got from http://www.grokkingandroid.com/checking-intent-availability/

Uniquely Identify a device in Android

For many of the application we may require to uniquely Identify android device. The best way to get the unique device Id is through Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); according to the "android developers blog".

Wednesday, May 22, 2013

After updating to ADT 22, no more R.java files

I recently came across this problem that after updating the ADT to 22, none of my apps are building. Android can't create the R.java file. The solution for this is, open "Android SDK Manager". Download "Android SDK build tools". Then for safety, restart your eclipse. That's it. Back to normal. All apps start building.

Androidians