Wednesday, April 30, 2014

Android ListView Optimization

The Android ListView, On of the complex composite widget in Android.So we have to deal with ListView wisely. To populate data onListView we have to use CursorAdapter or ArrayAdapter.Here in this article we use ArrayAdapter.

Inside ArrayAdapter there is method call getView(int position, View convertView, ViewGroup parent).Inside this method data is bound to list item. Typical ArrayAdapter code is added here.

What happen actually.

ListView is designed for scalability and performance. In practice, this essentially means 

1.It  tries to do view inflation few as possible.

2.Draw list items only that are visible in screen.

Normally ListView use view recycle system with RecyclerListener and ScapViews. What basically there are doing is,When ever the list item going off from screen,It is added to Scrap heap then reused it for the list item that going on screen.


So then what are the issues with above ArrayAdapter.

  1. View Inflation is done always.
  2. findViewById is always calling.

View Recycling (Eliminating 1st issue)

When the view recycling start to happen the converterView is not null.So then we can normally use it updating data rather than create it again.

Here we check for null and if it is null, then we inflate it again. Here we reducing view inflation. View inflation is more resource intensive task in Android.

So following source code showing example of optimized ArrayAdapter.

ViewHolder Pattern (Eliminating 2nd issue)

So Above adapter also have performance issue.It always find views using their Id. Finding view by id is also resource intensive task, because findViewById methodrecursively go through view hierarchy to find child view.

To eliminate this problem we use ViewHolder class to hold references for already founded view after inflation is done.Then ViewHolder add as a tag of View. This would guaranteed to findViewById only if inflation happens.  

So following source code shows more improved ArrayAdapter.

So you can feel deference when this is implemented.

Here I have attached source code for live example of those three Adapters.You can install it and try to feel deference.

There is nice Video from Google engineers about ListView Google I/O 2010 - The world of ListView

Thursday, January 9, 2014

Dynamically create menu items in android

Today I am going to show you how to create menu items dynamically. Some time we need to show menu items depending on different context (I mean data). Inflating menu using xml is very easy. but this also easy if you understand this well. So lets start with with android project.

First create new Android application project.This is example used API 11 and grater version if you need to support backward compatibility you have to use ActionBarSherlock or ActionBarCompat lib.

I added few icons to drawable folder and implement Activity like following.

Here you can see in onCreateOptionsMenu() .I added normal xml menu inflation and menu items by Java code.Here we can set every field that set from xml using java code.

defend on you context you can set different menu items.In my case mMenuSet=1 and mMenuSet=2 after changing context you can call invalidateOptionsMenu() to redraw menu item.

if you are using ActionBarSherlock you have to use Activity.supportInvalidateOptionsMenu() and ActionBarCompat invalidateOptionsMenu (Activity activity) method. you can download my example clicking following github  icon.Then run it and see.


Friday, July 26, 2013

How to access secure (SSL) webservice in Android

Most recently I have faced with error when  accessing https service in android.The problem was “ No peer certificate”.So I can’t access it normal way.I searched on internet and had some workaround for fix that problem.

First we need to override SSLSocketFactory and create new one.

after that we have to create modified HttpClient insteed of DefaultHttpClient in apache using created SSLSocketFactory

Then we can use this HttpClient to deal with Https enable service.


Note : Before you decide to use this method please verify this implementation is  ok with you and implement it with your own risk.

Thursday, February 28, 2013

How to make a copy of a file in Android

Hi guys.Today I faced a problem that I have a file and need to copy it to another folder. So I searched in internet and came up with solution.I had spent lot of time on that. So if that solution helpful for you,My effort is worth. following code snippet show you  how to do that.If you have better way let me know please.

Friday, January 18, 2013

How to use IntentService in Android

Hi Guys Today I’m going to discussed about IntentService.The Intentservice is a Service which can start or trigger by Intent.This Service run on it’s own thread so it will not effects on main UI thread.So we can use this service to do time consuming data processing.
When this Service receive intent which include some data for doing job.This job put in queue and execute them in sequence and asynchronously.After the job done,send data to where they necessary and stop itself.

Wednesday, December 12, 2012

Android Notification with NotificationCompat.Builder Example

notification Hi all,After long time I’m going to show how to use NotificationCompat.Builder to build Notification with backward compatibility.I used this builder for create notification.So i’m going to show how to create Notification with some data,Show it and show more content when press the notification it self.

First download my example source code from GitHub.Then Import it to eclipse and run it.

Here i have two activity,one is for creating Notification and other one is for show details.I’m not going to explain this in very details,because comment describes all the things.

device-2012-12-12-172746 device-2012-12-12-172818 device-2012-12-12-172829

Tuesday, September 25, 2012

How to work with android ExpandableListView

Hi all today I am going to show, How to work with android expandable list view.This is very much like list view but here you can group you list items.So you can expand/collapse those group and see what ever you want.So implementing this is bit difficult than normal list view.