Is Android Apps Development Better Than iPhone Development?

App Development

While there are more Android handsets than iPhone, over 30% of internet traffic from mobile devices comes from iPhone, compared with just under 26% from Android devices. The remaining 44% comes from RIM and Symbian devices and other iOS devices like the iPad.

When all iOS devices are considered, 73% of ad revenue generated by them comes from apps rather than mobile websites. The typical mobile user spends around 100 minutes per day using apps, and nearly 18 million apps are downloaded yearly. Today, around 20% of all searches are mobile, and of those, 40% are local searches.

Android apps development is big. Really big. According to research firm Gartner, 79% of all smartphones sold between April and June this year were running Android: 177.9m handsets compared to Apple’s 31.9m iPhone. Another research firm, IDC, estimates that 62.6% of tablets that shipped to retailers between April and June were running Android: 28.2m devices versus 14.6m iPads.

Meanwhile, Google says that more than 1.5m new Android devices are being activated every day, it’s nearing 1bn activated in total so far, and that by the end of this year that total will include more than 70m Android tablets.

Yet a lot of apps still come out for Apple’s iOS first or even exclusively. Right now, if you own an iOS device, you can play Plants vs. Zombies 2, Clash of Clans and Worms 3, but Android owners can’t. Why are android apps developments behind?

Instagram launched on Android 18 months after iOS. Nike’s Nike+ Fuel Band still hasn’t made the leap. Mailbox and Tweetbot are still no-shows, and while much-praised children’s app-maker Toca Boca has 18 apps available on iOS, only one of them is also on Android.

It’s one of the blogosphere’s favorite tech topics. Is the iPhone this, can android apps development do that? Every new nugget of competitive information is fodder for an avalanche of coverage. Oftentimes, a story will declare that android apps development is beating iOS development or that iOS is beating Android.

Really, though, it’s silly to obsess over any one data point. If what you’re after is a clear idea of how the world’s two dominant mobile operating systems are doing — rather than an excuse to make bold proclamations and/or cheer for your favorite — you want to consider lots of data points.

Is android apps development better than iPhone? Let us know what you think. The battle rages on!

[Source]

10 Tips for Efficient Android Apps Development

Android App Development Tips

The best recipe for becoming a complete flop in Google Play is to release an app that is battery and memory hungry with a slow interface. Most likely, these qualities will ensure negative reviews from users and result in a bad reputation, even if your app has great design and a unique idea. Every drawback in product efficiency, battery and memory consumption can really affect your app’s success. That’s why it is critical to develop well-optimized, smooth running apps that never make Android system guard against them. We will not speak about efficient coding, since it goes without saying that the code you write should stand any performance test. But even brilliant code takes time to run. In today’s post, we’ll talk about how to minimize this time and make Android apps that users love.

Efficient threading


 

Tip 1: How to off-load operations onto threads in background

Since by default all operations in an app run on the main thread (UI thread) in the foreground, the app responsiveness might be affected, which will imminently result in hangs, freezes and even system errors. To reinforce responsiveness, you should shift long-running tasks (e.g. network or database operations, complex calculations) from an app’s main thread to a separate background thread. The most effective way to accomplish this task is at a class level. You can use AsyncTask class or IntentService class to organize background work. Once you have implemented an IntentService, it starts when needed and handles requests (Intents) using a worker thread. When using IntentService, you should consider the following limitations:

  • This class does not put results to UI, so to show results to users use Activity.
  • Only one request is processed at a time.
  • Any request processing can not be interrupted.

Tip 2: How to avoid ANR and stay responsive

The same approach of off-loading long-running operations from the UI thread will save your users from the “Application Not Responding” (ANR) dialog. What you need to do is to create a background worker thread by extending AsyncTask and implementing doInBackground() method. Another option is to create a Thread or HandlerThread class of your own. Keep in mind that unless you specify “background” priority for the thread, it will slow down the app since the default thread priority is the same as of the UI thread.

Tip 3: How to initiate queries on separate threads

Displaying data is not immediate, although you can fasten it by using CursorLoader objects, which allows not to distract Activity from interacting with a user while query is processing in the background. Armed with this object your app would initiate a separate background thread for each ContentProvider query and return results to Activity from which the query was called only when the query is finished.

Tip 4: What else you can do

  • Use StrictMode to detect potentially lengthy operations you do in the UI thread.
  • Use special tools, i.g. Systrace, Traceview, to find bottlenecks in your app responsiveness.
  • Show progress to users with a progress bar.
  • Display splash screens if initial setup is time-consuming.

 

Device battery life optimization


We cannot blame users for angrily uninstalling applications that abuse battery life. The main threats to battery life are:

  • Regular wake-ups for updates
  • Data transfer via EDGE and 3G
  • Textual data parsing, regex without JIT

Tip 5: How to optimize networking issues

  • Make your app skip operations if there is no connection; update only if 3G or WiFi is connected and there is no roaming.
  • Choose compact data format, e.g. binary formats that combine text and binary data into one request.
  • Use efficient parser; consider choosing stream parsers over tree parsers.
  • For faster UX lessen round-trip times to server.
  • When possible use framework GZIP libs for text data to make the best use of CPU resources.

Tip 6: How to optimize apps working in foreground

  • When designing wakelocks, set the lowest level possible.
  • To avoid battery costs caused by potential bugs you might have missed, use specific timeouts.
  • Enable android:keepScreenOn.
  • In addition to GC (garbage collection) consider recycling Java objects manually, e.g.:
    • XmlPullParserFactory and BitmapFactory
    • Matcher.reset(newString) for regex
    • StringBuilder.setLength(0)
  • Mind synchronization issues, although it can be safe when driven by UI thread.
  • Recycling strategies are used heavily in ListView.
  • Use coarse network location not GPS when possible. Just compare 1mAh for GPS (25 sec. * 140mA) and 0.1mAh for network (2 seconds * 180mA).
  • Make sure to unregister as GPS location updates can continue even after onPause(). When all applications unregister, users can enable GPS in Settings without blowing the battery budget.
  • Since the calculation of a floating point requires lots of battery power, you might consider using microdegrees for bulk geo math and caching values when performing DPI tasks with DisplayMetrics.

Tip 7: How to optimize apps working in background

  • Since each process requires 2MB and might be restarted when foreground apps need memory, make sure the services are short-lived.
  • Keep memory usage low.
  • Design app to update every 30 minutes but only if device is already awake.
  • Services that pall or sleep are bad, that is why you should use AlarmManager or <receiver> manifest elements: stopSelf() when finished. When you start service using AlarmManager, apply the *_WAKEUP flags with caution. Let Android bin your application update together with the rest through setInexactRepeating(). When using <receiver>, enable/disable its components in manifest dynamically, especially when no-ops.

Tip 8: What else you can do

  • Check current states of battery and network before launching a full update; wait for better states for bulk transfers.
  • Provide users with battery usage options, e.g. update intervals and background behavior.

 

Implementing UI that leaves minimum memory footprints


 

Tip 9: How to identify layout performance problems

When creating UI sticking solely to basic features of layout managers, you risk to create memory abusing apps with annoying delays in the UI. The first step to implementation of a smooth, memory caring UI is to search your application for potential layout performance bottlenecks with Hierarchy Viewer tool included into Android SDK: <sdk>/tools/. Another great tool for discovering performance issues is Lint. It scans application sources for possible bugs along with view hierarchy optimizations.

Tip 10: How to fix them

If layout performance results reveal certain drawbacks, you might consider to flatten the layout by converting it from LinearLayout class to RelativeLayout class, lowing level hierarchy.

To perfection and beyond


Even though each tip mentioned above might seem like a rather small improvement, you might see unexpectedly efficient results if these tips become an essential part of your daily coding. Let Google Play see more brilliant apps that work smoothly, quickly, and consume less battery power, bringing the Android world one more step closer to perfection.


Information provided by Anna Orlova [Source]

Compressing and Uncompressing Data in Android/Java Using Zlib

Compressing and Uncompressing Data in Android/Java Using Zlib

This Could Be Better

The Zlib data compression library is built into Java, and allows you to compress and decompress data. So, uh… ’nuff said?

1. If you have not already done so, download and install the Java Development Kit. Details are given in a previous tutorial. Make a note of the directory to which the files “javac.exe” and “java.exe” are installed.

2. In any convenient location, create a new directory named “CompressionTest”.

3. In the newly created CompressionTest directory, create a new text file named “CompressionTest.java”, containing the following text.

import java.io.*; import java.util.*; import java.util.zip.*; public class CompressionTest { public static void main(String[] args) { Compressor compressor = new Compressor(); String stringToCompress = "This is a test!"; //String stringToCompress = "When in the course of human events, it becomes necessary for one people to dissolve the bands that bind them..."; System.out.println("stringToCompress is: '" + stringToCompress + "'"); byte[] bytesCompressed = compressor.compress(stringToCompress); System.out.println("bytesCompressed…

View original post 713 more words

How to Override Android’s Back Key When Soft Keyboard is Open

There are various scenarios where we need to handle the Android powered handsets’ hardware “back” key to do some important stuff. One scenario that’s very common is to use it on your custom search view where pressing back key only leads to hide the keyboard and user has to tap it again to hide the search view.

Let’s consider this:

Here we have a search view with keyboard open. If user wants to cancel searching and go back, he can press the back key and here is what we get on back key press:

You can see that the keyboard has gone but the search view is still there. User has to press the back key another time to navigate back to original activity.

Apparently, there is no method in your activity which listens to hardware key events when software keyboard is open. onKeyDown() and onKeyUp() methods work only when keyboard is hidden and this happen because Android considers on-screen keyboard as a separate activity and behaves accordingly.

So what should one do to override it?

Answer is simple and the easiest way to do is to override onKeyPreIme() method which is inherited from View class. To override it, you need to create a custom view. we’ll do it With minimum of the effort. For the example above, we have an EditText which we have using for searching.

Now create a new class which extends from EditText class. Override all the constructors to avoid any possible app crash. Now we’ll override the onKeyPreIme() method and check for Back key:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // User has pressed Back key. So hide the keyboard
        InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
        // TODO: Hide your view as you do it in your activity
    }
    return false;
}

Replace your EditText with this brand new class and that’s all. Now whenever the soft keyboard is hidden through back key, you’ll be notified and you can do the required code to get things work your way.

Bonus Trick:

Here’s a bonus trick for you. If you have implemented menus in your activity, you might face a problem like this:

If you press menu key when the keyboard is opened, you ‘ll have your menus over the keyboard. Now isn’t that funny? But don’t worry, we don’t have to worry about this. What do we want here is that menus should not become visible when keyboard is opened. Piece of cake!! Update your above code to this:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // User has pressed Back key. So hide the keyboard
        InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
        // TODO: Hide your view as you do it in your activity
    } else if (keyCode == KeyEvent.KEYCODE_MENU) {
        // Eat the event
        return true;
    }
    return false;
}

And here you go!! Menus won’t show up on pressing menu key when the keyboard is visible.
Please share the post if you liked.

Creating an Alternate-Colored List View in Android

This post will help you make a list with alternate row in different colors. This is What your list will look like:

To accomplish this, first of all create a layout file and add a listview in it. We name this file “categories.xml“:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/RelativeLayout1">
    <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listView1" />
</RelativeLayout>

Next, you need to create an activity. Let’s name this file “CategoriesActivity.java” and enter following code in oncreate() method:

setContentView(R.layout.categories);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new CategoriesListAdapter(this));

Next step it to create create a custom adapter for our list. In our code above, we’ve named it CategoriesListAdapter. Extend this class from BaseAdapter and implement the methods as usual.

Now, in CategoriesListAdapter.java, we’ll first declare and initialize a list of colors:

public class CategoriesListAdapter extends BaseAdapter {
    private final int[] bgColors = new int[] { R.color.list_bg_1, R.color.list_bg_2 };
}

You have to define these colors in an xml file e.g., colors.xml under res/color. Alternatively, you can use Android’s built-in colors like Color.YELLOW or Color.BLUE for which you don’t need to define these colors. But if you select the first method, your colors.xml will look like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="list_bg_1">#ffffff</color>
    <color name="list_bg_2">#fef2e8</color>
</resources>

Now the last step is to set these colors in getView() method. Use the following code snippet for this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_categorylist, null);
        holder = new CategoryContentsHolder();
        holder.title = (TextView) convertView.findViewById(R.id.txtCategoryName);
        convertView.setTag(holder);
    } else
        holder = (CategoryContentsHolder) convertView.getTag();
        int colorPosition = position % bgColors.length;
        convertView.setBackgroundResource(bgColors[colorPosition]);
        if (holder != null) {
            holder.name.setText(categoryStringArray[position]);
        }
        return convertView;
    }
}

class CategoryContentsHolder {
    TextView title;
}

That was all! The same way you can create a list with 3 or more color.