Saturday, July 26, 2014

Android Development : Changing theme of Current Activity during runtime (eg: during Button Click event)

Introduction

There may be situations where the entire look of the screen (current activity) needs to be changed as a result of some event. The event may be as simple as a button click, or may be a sensor feedback, or even as a result of streaming data from internet. 

Many articles explain how to set a theme in android manifest file, during design time. However, doing this will result in the application/activity itself starting with the theme and not as a response to an "event".

The Finished Demo





Repository Link

https://github.com/gopalnair/Android-Style-Demo

Key Points On Code

In method MainActivity.onCreate , make sure the setTheme() is called BEFORE call to parent. In this example, when the activity starts for the first time from launcher, there are no extras in the launcher intent. Hence, application start with default theme applied.

However, when user clicks on one of the "Style" buttons, we call the SAME activity (MainActivity), and package the target theme information in intent extras like so:


Notice that after calling startActivity() to start the Main Activity again, we are calling "finish()" method, so that the current instance of Main Activity will be killed, and we will be left with the just "triggered" main activity. If we do not call the "finish", by default, multiple main activities will stack one above other (Unless android manifest file is updated to force the activity to have a single instance).

Thursday, July 10, 2014

Errors running builder 'Android Resource Manager' on Project java.lang.NullPointerException

Note: The problem described here seems to be only for Mac OSX & JDK 7 is installed.

After failing to upgrade Android SDK tools to version 23 because of dependency issue (documented at https://code.google.com/p/android/issues/detail?id=72912), I was searching for a solution for the following error message.

Cannot complete the install because of a conflicting dependency.
  Software being installed: Android Development Tools 23.0.1.1256982 
(com.android.ide.eclipse.adt.feature.feature.group 23.0.1.1256982)
  Software currently installed: Android Development Tools 
22.6.3.v201404151837-1123206 

One suggestion was to download a fresh copy of eclipse directly from http://www.eclipse.org, and do a fresh install of Android SDK tools (from repository : https://dl-ssl.google.com/android/eclipse/)

After downloading eclipse, and installing Android SDK, I decided to create a basic "HelloWorld" android app, to see if everything is working fine - and came across the following error, when clicking "Finish" button on android project creation wizard:



Errors running builder 'Android Resource Manager' on Project java.lang.NullPointerException

Clicking "Ok" will still create the project, but will not have any activity code generated.

To resolve the issue, do the following:
  1. Go to your eclipse directory and find "Eclipse.app" file. Just to be on safe side, back up this file somewhere (Just the Eclipse.app file).
  2. Right-Click on "Eclipse.app" file, and select "Show Package Contents"

  3. Once inside package, go to Contents-->MacOS, and open "eclipse.ini"


  4. Just before the "-vmargs", add the following:

    -vm
    /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Commands/java

  5. Here is my "Before" and "After" eclipse.ini file
    Before
    After

  6. Run eclipse again, and you should be able to create the project without any problems.