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.

Monday, June 25, 2012

How to use pluralization in android

Today I'm going to show you how to use android plurals in your application.first we talk about why we android plurals. let’s say you want to show message depending on some count.if your count is one you want to show “you select 1 item” if count is grater than one it want to show “you select 2 items”.so how we manage this situation properly in android.I create sample solution for that.
it shows result depending on your selected item following screens
screen1 screen2
to do that