Posts Tagged ‘Android’

Android – Application Theme Based on Platform Version

Tuesday, April 16th, 2013

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

Link2SD and Read-only file system Error

Monday, February 11th, 2013

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.

Android: CyanogenMod Recovery Partition Gets Overwritten During Next Boot

Wednesday, February 6th, 2013

I have been using the CyanogenMod Recovery partition to install custom ROMs, but with my stock Android 2.3.6 ROM, I was having an issue where the Recovery partition is getting restored with stock one with very next boot. This issue is also mentioned in notes for CM Recovery installation (http://wiki.cyanogenmod.org/w/Install_CM_for_passion):

Note: Some ROMs overwrite recovery at boot time so if you do not plan to immediately boot into recovery to install CyanogenMod, please be aware that this may overwrite your custom recovery with the stock one.

Though it was not a show-stopper as it gets fixed once I install the custom ROM, but what if you plan to use this recovery, but keep using the stock ROM. Fortunately, the solution for this is simple too. As mentioned above in the warning note, the Stock 2.3.6 ROM tries to restore the Recovery during the boot. The recovery backup is stored at the following path in System:
/system/recovery-from-boot.p

So, all you have to do to get rid of this automatic recovery overwrite is to delete this recovery backup file, and Android system will no longer overwrite. your custom recovery image. You can delete this file easily using ES File Explorer. If you have never used ES File Explorer, here is quick tutorial to do this:

1. Open ES File Explorer File Manager.
2. Select the menu button on your device and click on Settings.
3. Check the Up to Root option, then also check the Root Explorer option. When the prompt appears for root privileges, press the 4. Allow/Grant button.
5. Check the Mount File System option as well.
6. Go to the Root folder of your Android file-system i.e. “/” path.
7. Go to the “system” folder and long-tap on the recovery-from-boot.p file, and then delete it.

I hope you find this of help.

Android: Installing System Images using flashboot

Friday, February 1st, 2013

In my previous article, Installing Jelly Bean on Nexus One, I discussed how I was able to install Jelly Bean on my NexusOne using couple of tools and helper applications.

However, after a couple of days of exploring it, I decided to try to downgrade it back to Gingerbread (mainly because of the Camera issues). This step was even more simple than installing the Jelly Bean on my nexus one. The good thing was that I took the full backup of my Gingerbread before upgrading to the Jelly Bean (using ROM Manager). This backup folder had flash-able images of the system, boot and recovery partitions. So all I had to do to revert back the original version was to run couple of commands in the bootloader mode.

Here are the exact commands (for your info, and my future reference):

1. First you need to boot into bootloader mode. You can do this from key combination of your phone (for Nexus-One, it’s Power+Trackball down), or you can reboot into bootloader mode using the following adb command:

1
adb reboot-bootloader

2. Wait for your cell to reboot in bootloader mode, and once you see it, test it by running following command:

1
fastboot devices

If the above command lists your device, then you are good to go forward. Otherwise wait for few more second and/or check USB cable connection.

3. Once the fastboot mode is detecting the device, you can flash your Image and System flashable images using following commands:

1
2
fastboot flash boot boot.img
fastboot flash system system.img

4. The next step is to simply clear your cache and user-data to avoid any problem with previous ROM data.

1
2
fastboot erase userdata
fastboot erase cache

5. And at last, simply reboot your phone:

1
fastboot reboot

If everything went well, you should be able to see your previous version without any problems.

Installing Jelly Bean on Nexus One

Sunday, January 27th, 2013

I have my Nexus One for more than a year. Although its stock OS (Gingerbread) is quite outdated, I still overall love it. But I must admit that it’s quite outdated OS now. So, I decided to upgrade this to Android Jelly Bean with hope to learn a lot during this process.

First of all, please note that this is not a guide to how to install Jelly Bean on Nexus One. If you are looking for one, search on the web, and you will find dozens (if not hundreds). FYI, I read many, but referenced the following two mostly:
http://marian.schedenig.name/2012/07/22/installing-android-4-ics-on-the-google-nexus-one/
http://c2med.blogspot.com/2012/07/how-to-install-ics-nexus-one.html

Here are the mains steps I used:

1. Unlocking your Phone
It’s basically unlocking your phone bootloader. this is required because if your bootloader is locked, you can’t replace your recovery image, you can’t flash a custom ROM, and you can’t edit your system files. So we have to do this before the fun start. Here is a quick guide on how to do this (just follow the “Unlocking the device” section):
http://wiki.cyanogenmod.org/w/Install_CM_for_passion

Note: Unlocking the bootloader on a Nexus device will automatically wipe all device data.

2. Rooting your Phone
Once you have unlocked the Phone, the next step is Root it. Rooting is the process of allowing you to run your device with root-level permissions i.e. allow Superuser permissions. This gives you as a user to perform all the permissions including deleting the files required to run the system. While you normally wouldn’t do this, you can still do this. The main reason for Rooting is to give you additional control while in the Android to setup the Custom ROMs.

For the rooting, you can use either install the CyanogenMod custom ROM for your device (as in the above link), or you can try some application which exploits the Android OS vulnerability to root it. I tried the OneClickSuper application, and it worked for me. So you can try it as a simple alternative too. This application interface is quite straightforward, but if you have any confusion, there are plenty of tutorials available.

3. Install Custom Recovery
Recovery refers to the dedicated, bootable partition that has the recovery console installed. This is used to detect and load the Android OS from the main partition. Plus this also has supports some basic commands/options to restore your system in case of any problem. The stock recovery is pretty basic, and don’t have much options. Specially the stock recovery doesn’t allow you to do the installation of the custom ROMs and do backup/restore.

For installing custom recovery, there are two possible ways. If you like Geeky style, then you can do this manually by following this tutorial (follow the “Installing recovery using fastboot” section):
http://wiki.cyanogenmod.org/w/Install_CM_for_passion

There is also a simple way, that I followed, and that’s to install the CyanogenMod Recovery using ROM Manager. Here is very simple tutorial on this:
http://tech2.in.com/how-to/smartphones/how-to-root-and-install-custom-recovery-on-any-android-phone/280442

4. Partition you SD Card
The next step is to reparation your SD Card so that the applications can be installed on your SD Card. This way you can leave more RAM for the system applications. I think this is required (but not very sure) to install large size ROMs like Jelly Bean. Anyway, as this was simple process, I decided to do this anyway.

For this, again there are two ways. First is geeky way and it’s to use the 4EXTRecovery to install custom recovery and use it to partition the SD-Card. Here is a good tutorial on this:
http://www.roms-au.com/howtos/ext3/

Another, simple one is to create this partition using ROM Manager. Again, as I have the ROM Manager installed, so I use this option. Here is a tutorial for this:
http://theultralinx.com/2011/10/how-to-partition-your-sd-card-with-rom-manager.html

Warning: After doing the above step, I got the famous Android boot failure (Android with Exclamation sign). To fix this, I booted in to Recovery mode, and Cleared the User-Data. This fixed my problem.

5. Partition your Internal Memory (HBoot)
To Install the Jelly Beans or ICS, you need to first resize the internal memory (called HBoot). This is because the new ROM size for the JB or ICS is so big that it will not fit on the default partition created for the Gingerbread on Nexus One. Different ROMs require the different HBoot size. I was planning to use CM10.1 VJ Jelly Bean 4.2.1 and this requires 250 HBoot. So next step is to re-size the partition. This can be done by BlackRose.

Warning: In case after the above step you can’t load into recovery. You can fix this by flashing your recovery image again from the fastboot mode.

6. Install your Custom ROM
Once all the above is done, you are ready to install your custom ROM. For this download it, and place it on your SD-Card. If you forgot to do this (like I did) before formatting and erasing your system. You can mount the SD Card from the Recovery too (if you are using CyanogenMod recovery. Once you are in the recovery mode, do “wipe data/factory reset” and also wipe all paritions except the sd-card. Then select update from SD-Card, select the new ROM ZIP file. This should start the installer. Cross your fingers while it finish the installation and do the booting.

7. Results
If everything went well, then you will see your new Android OS. I’m using Jelly Bean and it’s really great. There are still few (but minor) display issues, but overall it’s fully functional. The best thing is that as soon as I signed in with Google Account, all my contacts and applications were restored. So, the system is back in running state in an hour or so.

8. Bonus
I like the new Jelly Beans OS and overall functions. However, I didn’t find the booting animation that cute (sorry CyanogenMod designer). Fortunately, there is simple way to change that too. Here is the one I tried:
http://forum.xda-developers.com/showpost.php?p=28025789&postcount=1

The setup is simple. Just download the flashable version of the animation. Place it on SD-Card. Boot to Recovery mode, and simply flash the new animation. Enjoy the new elegant animation.

Conclusion
If you try this, please do a very thorough reading of all the above articles (and any other articles you can find on web) before trying any of this. The key is to make sure you know what you are doing. If you are not sure, read more or post questions to experts (I’m not expert). Good luck!