Tag Archives: Android

Using Reflection with Android

Faced an interesting problem today. In my android app I needed to show some random strings on the UI (textview).  So obvious solution was to add the strings in string.xml (for supproting i18n), and then fetch the text based on the random number generated.

textView.setText(R.id.text_1);

or if random number is 2

textView.setText(R.id.text_2);

and so on

I realized that only change happening is the last one digit, so this is a good candidate for using reflections.

Class c =  Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("tip_"+tip);
tipTextView.setText(field.get(null)));

But this actually gave an Exception for ClassCast, so I realized that field.get(null) is actually returning the resource Id (as shown in R.java class) instead of string value.

The fix found was to use getResources().getString(resource_id) to fetch the string from the resource id

Class c =  Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("tip_"+tip);
tipTextView.setText(getResources().getString((Integer)field.get(null)));

Creating a basic Android app

I am exploring Android application development for some new projects so thought about sharing my notes here. So far it is turning out to be fun and very simple :).

I am using eclipse for development so first of all we will need to install ADT. Follow the steps mentioned here

http://developer.android.com/sdk/eclipse-adt.html

Once we have ADT installed, creating first android project is pretty simple. Create a new android project (right click->new project->Android project).

In the project creation window, provide details-

Provide name say test_project, Choose target say 2.1, package- com.kamalmeet.android.test and activity- test. Skip test project creation and click Finish

Now we will run this project on emulator. Just go to your project and run as ‘android application’, this will give a “Hello World Test” message.

Now let us see what happened when we created and executed the project. When we created a a new project it created some files. Here are the important ones to get started

We will see AndroidManifest.xml in base project directory. This is the root file of our project. This contains details of the activities, services etc for our project. For now you are seeing only one activity under application node in this xml file. Also note the package declaration in manifest, this is the base package of our project.

Now we will check our package under src/ folder. Lets take a quick look into com.kamalmeet.android.test. We see the activity java file test.java. It extends the  parent android.app.Activity class, and has one method it overrides, that is onCreate. No points on guessing that this is the first method getting called when activity loads. For now it has just one line setContentView(R.layout.main);

To understand meaning of this line, lets move to gen/ folder, which has the same package and a autogenerated R.java file. This file gets automatically generated on compilation. It provides a unique Id to all our visible contents. For example, we can see an inner class as layout which has an int main value.

What does this main value corresponds to? Checkout res/ folder. This has all out UI related stuff like images, view XMLS etc. under res we have res/layout/ folder. This is where we have our UI XML, in this case main.xml. As you can see android supports XML defined UI like just Flex, XAML etc. In addition we can define

You can see one node as LinearLayout and a child node of TextView. This is responsible for what we see on UI. We can see test being set as android:text=”@string/hello”. It picks up value from /res/values/strings.xml. In this XML you can see a string node where name is set as hello with value as “Hello World, test!” Change it to any value say “My first Android app” and re-run the application. Simple. Now we can go ahead and play around with this simple UI app.