Archive

Archive for the ‘Mobile Market’ Category

JSON Parsing in android

October 29th, 2009

Let’s look at how to parse JSON objects in android

1> First we’ll need an example :

Lets look at a standard example from the json site http://json.org/example.html

     {"menu": {
				  "id": "file",
				  "value": "File",
				  "popup": {
				    "menuitem": [
				      {"value": "New", "onclick": "CreateNewDoc()"},
				      {"value": "Open", "onclick": "OpenDoc()"},
				      {"value": "Close", "onclick": "CloseDoc()"}
				    ]
				  }
				}}

you could either save this in a file or save it in a string…..like I’ve done

2> Android already contains the required JSON libraries

Lets create a JSON Object;

private JSONObject jObject;

and lets our example be a String ,

private String jString = "{\"menu\":	{\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": 	[ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, 	{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, 	 	{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";

now we have to convert jString to the jObject ,

jObject = new JSONObject(jString); 

Now we have to start extracting the content from jObject ,

Lets extract the menu object by creating a new menu object,

JSONObject menuObject = jObject.getJSONObject("menu");

Now lets extract its atrtibutes ,

String attributeId = menuObject.getString("id");
String attributeValue = menuObject.getString("value");
JSONObject popupObject = menuObject.getJSONObject("popup");

since “popup” is not plainly a String lets extract it to an object ,

3> Now popup contains an array of “menuitem”

So, we’ll have to extract it to a JSONArray,

 JSONArray menuitemArray = popupObject.getJSONArray("menuitem"); 

Since it contains 3 items lets put it in a for loop.

for (int i = 0; i < 3; i++) {
			System.out.println(menuitemArray.getJSONObject(i)
					.getString("value").toString());
			System.out.println(menuitemArray.getJSONObject(i).getString(
					"onclick").toString());
		}

Basically thats it , u should see the output in the DDMS

4> The full code is as below,

import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;

public class JsonParser extends Activity {
	private JSONObject jObject;
	private String jString = "{\"menu\": {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\",   \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		try {
			parse();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void parse() throws Exception {
		jObject = new JSONObject(jString);

		JSONObject menuObject = jObject.getJSONObject("menu");
		String attributeId = menuObject.getString("id");
		System.out.println(attributeId);

		String attributeValue = menuObject.getString("value");
		System.out.println(attributeValue);

		JSONObject popupObject = menuObject.getJSONObject("popup");
		JSONArray menuitemArray = popupObject.getJSONArray("menuitem");

		for (int i = 0; i < 3; i++) {
			System.out.println(menuitemArray.getJSONObject(i)
					.getString("value").toString());
			System.out.println(menuitemArray.getJSONObject(i).getString(
					"onclick").toString());
		}
	}
}

Android, Android API, Mobile Market , , , , , , ,

Styles and Themes

October 27th, 2009

Hello coders,

Lets look at a fairly simple example of creating a transparent theme and how to apply it to a Dialog……

Step 1> Create a colors.xml file in the ‘values’ folder under ‘res’ and add the following line..

<drawable name="transparent">#00000000</drawable>

Step 2> Create a styles.xml file in the ‘values’ folder under ‘res’ and the following lines…

<style name="Transparent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">
@android:style/Animation.Translucent
</item>
<item name="android:windowBackground">@drawable/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>

( I guess the tags and attributes are self explanatory …. )

Step 3> Actually thats it……………………

Let’s add this to a dialog…..

Step 4> Create a class with the following lines……

public class DialogBox extends Dialog {

    public DialogBox(Context context, int theme) {
        super(context, theme);
        setContentView(R.layout.dialog);
        okButton = (Button) findViewById(R.id.dialog_OkButton);
        setListeners();
    }
}

(Make sure you create a layout for the dialog )

Step 5> Next create an activity class as follows….

public class T_Temp extends Activity {

    private DialogBox dialog;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialog = new DialogBox(this, R.style.Transparent);
        dialog.show();
    }
}

When creating the dialog just add the transparent theme ……

Step 6> That’s it …

( don’t forget to add the activity in the android manifest file..)

When you add the widgets and background it will look something like this..

before

Before

after

After

Step 7> Another trick is you can make the background blurred ….

just add the following line in the dialog box…

   getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

Output looks somthing like this…….

blurred

So the final code is:

public class DialogBox extends Dialog {

    public DialogBox(Context context, int theme) {
        super(context, theme);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        setContentView(R.layout.dialog);
        okButton = (Button) findViewById(R.id.dialog_OkButton);
        setListeners();
    }
}

Android, Mobile Market , , , , , , , , , , , ,

T-Mobile G1 Accounting for 20% of UK Contract Sales? | Android Central

March 10th, 2009

AndroidCentral reports that T-Mobile G1 is accounting for 20% sales in UK for T-Mobile. Read the complete report here. So good news for Androiders is that the market is picking up for Android based smart phones in Europe. With rise in sales for Android, we should see more and more cool applications developed on the platform.

By the way, we have been completely busy with few client projects and hence blog took a backseat for almost a month. But good news is we are back!

Mobile Market, News ,

Samsung’s Android handset will reach market in 2009

January 24th, 2009

Samsung is also launching an Android based smartphone. The phone is expected to be released in mid 2009. All the device features are not known yet but the phone will be touch-screen based with minimum buttons and design similar to Omnia (see image).

Samsung Omnia

After Sony Ericsson and Motorola, Samsung is now the third major phone manufacturer to have announced Android based phones. Android market is getting hot.

Android, Mobile Market, News , , ,

Android market place opens for Europe

January 19th, 2009

The new year has already been splendid for Android. Lot of companies, enterprises are already building apps for Android instead of iPhone, which has been said to be the biggest competition for Android.

Telecom companies and players are already shifting focus to Android as per our blog posts 1, 2.

Now this news, Android has opened up market place for applications in Europe for selected countries. So we should see a launch of phone there in coming weeks. Google it seems has done all the required home work to take on giants like Blackberry, Symbian, iPhone and Windows mobiles.

It now seems that coming one is really going to change the mobile market, one way or the other.

Mobile Market, News ,

Sony Ericssons losses, Androids gain?

January 17th, 2009

As per latest news, Sony Ericsson has taken a hit in its yearly revenues and filed losses of more than 70 million Euros. With this news the rumours of split between Sony Ericsson and its parent company Sony starts to grow even stronger. But for Android community, there is that silver lining around all these bad news coming in. We already talked here, about how Motorola is going to focus on Android to help itself come out of these negative results and same will happen with Sony Ericsson too. 

Essentially companies are looking to cut costs by using Android open operating system and save costs of licensing costly proprietary solutions avaialble. It will be interesting to see how Windows Mobile and Symbian tackle this contuning shift by handset manufacturers in year 2009. If the way things are going, and industry experts believes things to get even worse for mobile manufacturers, we might see a altogether different market situation by end of this year.

Mobile Market, News