Android – Application Theme Based on Platform Version

April 16th, 2013 Bookmark and Share

When testing my Jack Sparrow Compass application on the Jelly Bean 4.2, I realized that all its Users Interface (or overall theme) was still of the Gingerbread (on which I had developed this application). Even though that interface doesn’t look that bad on Jelly Bean, it’s not consistent with other application interface, and I would like to keep it consistent with new platforms, but would still like to keep the compatibility with old Android versions.

After few tries and tricks, I was successful, and I’m going to share that here for easy future reference. To best understand the problem, let’s begin from start. Here is how my application was appearing on Jelly Bean 4.2:

But if I look at other native application of the Jelly Bean, they have quite different theme and styles as shown below:

So, I wanted to look my application dialog similar to above one on Jelly Bean or higher version, but still appear the same on the old version. The trick lies in the styles XML files in corresponding Android version folder. So, I create my custom theme styles (in res/values/styles.xml):

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MainTheme" parent="@android:style/Theme.Light.NoTitleBar"></style>
</resources>

Here I’m defining the MainTheme for my application and inheriting it from the Android built-in “Theme.Light.NoTitleBar”. This is the style I plan to apply to my main Activity in the Application manifest file by adding following attribute:android:theme=”@style/MainTheme”

Note: I’m using the “Theme.Light.NoTitleBar” as the parent style because I don’t want a default TitleBar in my application. If you want one, please choose the “Theme.Light”.

This is not all you want, but the magic comes by adding the correct style based on higher version styles. For this you create res/values-v11/styles.xml (for Android 3.0+) and res/values-v14/styles.xml (for Android 4.1) files under corresponding values-XX folders.

So here is the XML file I created under res/values-v11/styles.xml:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MainTheme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
</resources>

And here is the XML file I created under res/values-v14/styles.xml:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MainTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar"></style>
</resources>

As you can see I’m using the different parent style per Android version, and this does the magic. Now if you run your applicaiton on Android 2.2 or 2.3 it will use the default theme “Theme.Light.NoTitleBar”. If you run it on Android 3.0+ version, it will use Theme.Holo.Light.NoActionBar style, and similarly for Android 4.1, it will use Theme.DeviceDefault.Light.NoActionBar.

Note: To use the higher version styles, you must set your Android target version to the higher version i.e. “4.1″. Otherwise you will get the resource/style not found error.

Now if I run the application on 4.1, it will use the native theme on all the UI components as shown below:

It was a bit tricky to grasp in start, but once you get used to this theme concept model based on version folder, you will see how powerful this is when designing applications for future versions but still maintaining backward comparability with older versions. If you want to read more, I highly recommend you to check official “Themes and Styles” guidelines on Android website:
http://developer.android.com/guide/topics/ui/themes.html

IIS 7 and URL Rewerite

March 10th, 2013 Bookmark and Share

I have been using IIS 6 (and some older versions) for a long time for many of the clients website. One of the biggest feature I miss is the URL Rewrite component. Apache support this module for so long, and it’s must have for friend URLs.

Fortunately, due to powerfull ISAPI support, there are already many powerful 3rd Party applications available which provides really good URL Rewrite support. I would like to mention two of these:
www.isapirewrite.com
www.urlrewriter.net

However, I recently needed to use the similar function on one of client website which is running on IIS 7, and before trying any of these 3rd Party tools, I decided to search and see if IIS 7.0 support and Viola! It does. The URL Rewrite module in IIS 7 is not only as powerful as you would expect it to be (supporting most of the Apache counterpart URL rewrite module), but its integration in the IIS Manager is a big plus. You can easily add and test rules them using the visual interface. Here is a very good beginner tutorial:
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

For some of the geeky fans, who still like the type this instead of wasting time in UI dialog, these rewrite rules are stored in your website web.config file, so you can view, add or edit these directly there too. Here is a sample of how these rules appear in the web.config file:

1
2
3
4
5
6
7
8
<rewrite>
	<rules>
		<rule name="Rewrite to article.aspx">
			<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
			<action type="Rewrite" url="article.aspx?id={R:1}&amp;title={R:2}" />
		</rule>
	</rules>
</rewrite>

You still have to type more than you have to do for something similar in Apache and other 3rd Party URL rewrite tools, but this is because of the XML syntax, and there price you have to pay for better (debatable) data format.

Jelly Bean 4.2.2 On Nexus One

March 6th, 2013 Bookmark and Share

I have been using the Jelly Bean 4.2.2 – v4.7 for few days now, and I must say that it’s working great so far. I covered all the installation steps and tricks in the previous article:
Installing Jelly Beans on Nexus One

However, with time, I learned something more about this ROM, and applied few more tricks to get a very smooth running Jelly Bean on my Nexus One (haven’t had a single crash since last 4 days). I thought to share these with others using the same (or similar ROMs) and see if this helps you guys too. So, here these are:

1: Custom Camera Application
The Camera in this ROM works, but it’s just functional i.e. the picture quality is very bad. I know the N1 camera quality is great, because I have used this a lot in Gingerbread and loved the quality. Fortunately, there are some workarounds available. The simplest is to use 3rd Party camera application from the Google Play store. The one I’m using and highly recommend are:
Camera ZOOM FX
HDR Camera

You will get way good quality pictures with these applications than the stock camera application.

2: Use SD Card Booster
I recommend installing some SD Card Booster form the Google App Store. There are many, but I tried the following and it works well for me:

https://play.google.com/store/apps/details?id=com.sdincrease.it

For my SD-Card, I got best performance with 1024 size. You can try few values on your system, and see which one works well for you. Once configured properly, you should get some good speed in running applications and playing movies from the SD-Card.

3: Install/Update Google Search
Go to Google Play store, search for “Google Search” and it will let you install or update the Google Search version. With this, you can use the voice search and input using the keyboard. It still needs the Internet (no offline version available yet) but still it’s better than nothing.

4: Integrate Link2SD
If you are using the Jelly Bean or any other custom ROM which provides the ROOT access, then I think this is the must have application for you. You can download Link2SD this from Google PlyStore. The configuration is simple, and there are many tutorials on how to setup this. So, I will not go into that detail. However, I would recommend one thing special, and that’s if you are using this ROM, partition your SD-Ext as FAT32 (instead of Ext2, Ext3 or Ext4). Though the linux paritions like EXT3 or EXT4 works, I had lot of permission issues using these. But FAT32 worked very well for me. If you are using Windows, you can use the MiniTool Partition tool to format your SD-Card. Again, there are many online tutorial on how to do that, so will not go in these details.

Note: Link2SD can’t work be juxtaposed with A2SD or similar scripts. So, if you want to use this, don’t use built-in A2SD or any other SD-Ext manager application.

5: Keep Dalvik Cache in Internal Memory
If you have low access speed SD-Card, then you may find it better to keep the Dalvik Cache in the internal memory. Doing this I have found the application to be more responsive and speedy. I’m not a guru on Android architecture, so may be there are some shortfalls of this, but so far this (keeping Dalvik Cahce in internal memory) has been working great for me. So, other thing I did different for Jelly Bean 4.2.1, is to keep the Dalvik Cache in internal memory. If you followed that ROM steps, for A2SD install, you need to do this in terminal:

1
2
3
4
5
# su 
# a2sd install
N 
N
Y

This means that you may have to keep cleaning up the Dalvik Cache from time to time. But fortunately you can do this from Recovery (I’m using CynogenMod Recovery). When you do this, system will build the cache again on next start-up. It should work well if you have comparatively low number of applications.

Note: If you want to use the A2SD, then you must have the Ext2 or Ext4 as second partition. Having the Fat32 as second partition works well with Link2SD, but doesn’t work with A2SD (and it doesn’t complain about this either). It simply will not move the cache.

ASP.Net Cookieless Session and Absolute URLs

February 18th, 2013 Bookmark and Share

For one of our ASP.Net websites, we have been using the Cookies mode to “AutoDetect” (by adding cookieless=”AutoDetect” in sessionState tag in web.config). This has worked well for years in serving the website correctly to users with all types of securities (even who don’t allow the web cookies).

When the cookies are disabled by the user security policies, then ASP.Net auto detect this and adds the session info in the URL of the each request, something like::
~/(X(1)S(cp53yq30mtagv555unhr0t45))/system/index.aspx

The cryptic path “(X(1)S(cp53yq30mtagv555unhr0t45))” in the URL is actually used to indicate the session ID. ASP.Net auto manage the addition and removal of this session info, and in the ASP.Net codebehind, you don’t need to worry about this. Even you don’t get this URL with session info with Request.URL or any other property.

The only limitation is that this breaks when you try to redirect from ASP.Net to an absolute URL e.g. “/system/index.asp”. We normally don’t use any absolute URL in our application, but there are few places where the absolute path is used (mostly to handle protocol changes), and we discovered that it was causing mysterious automatic log-out for some users (which were not allowing cookies).

The fix, fortunately, was very simple. The ASP.Net naively provides a method which add the current session ID to the URL if using the Cookieless mode. The method is HttpResponse.ApplyAppPathModifier and you can apply this by simply calling and passing required absolute URL:

1
string redirectUrl = Response.ApplyAppPathModifier("/system/index.aspx")

The beauty of this method is that it auto detects the session mode and only appends session info, if required. If want to know more about Cookieless session in general, I would suggestion this MSDN article:
http://msdn.microsoft.com/en-us/library/aa479314.aspx

Link2SD and Read-only file system Error

February 11th, 2013 Bookmark and Share

I have been using Link2SD for few days now (on my Rooted NexusOne) and I simply loves this application. It solves all the issues of low internal memory for the custom applications.

However from time to time, the SD-Ext partition used by Link2SD gets corrupt. Now I’m not very sure whom to blame for this i.e. Android Kernal, Link2SD or anything application which writes files here. But anyway, I found a simple fix for any such problems, and that’s if you get the read-only file system error when installing or updating the SD-Ext in the Link2SD, you can quickly repair this from the ADB Shell. If you have never used this before, here is quick reference and guide:

http://developer.android.com/tools/help/adb.html

Once you have this tool. But the phone into recovery mode, and then run the ADB shell from your command prompt:

adb Shell

Once you see the shell prompt, you can check and fix your sd-ext partition by typing following command:

e2fsck -fpDC0 /dev/block/mmcblk0p2

If fixing is successful, re boot back into phone and try using the Link2SD again.

Update: A quick tip on this (based on my personal experience). If you are using Link2SD and your frequently get the file system error on your SD-Ext partition, then one trick which worked well for me is to change the SD-Ext partition format from the Ext3 or Ext4 to FAT32. This should get rid of most of the file system errors happen during transfer of files.