Android XML Code format

It’s been a while since I last posted something about android programming.
From my point of view, this is a great thing, there is an untold story lying here! :)

Working with the Android UI requires a lot of XML “syntaxing”. (I’m sorry, I do not like the use of the words “programming” or “develpment” for XML).
Eclipse was a beautiful feature, called “Code formatter” where you press Ctrl + Shift + F (Format) and it nicely formats your code.
It works like a charm as long as the code is Java. When the code is XML, it does tide the code, but it is still non easily readable.

example of formatted but not well readable XML code:

Fortunately, there is a way for the eclipse code formatter to tide the XML code even more and make it way more readable.
Just go to Preferences -> XML -> XML Files -> Editor and check the “Split multiple attributes each on a new line”.

Now you have this:

enjoy! =)

Share

startActivityForResult and onActivityResult issue

The use of startActivityForResult is a very common and useful practice to do things within your application.

But it suffers from a architecture issue, very rare and difficult to find out.

The onActivityResult method gets called immediately after the startActivityForResult. This happens because the started activity lacks the correct launchMode which must be set to “standard”.

You may also experience the “Activity is launching as a new task, so cancelling activity result.” from the ActivityManager which explains why is behaves like this.

Share

Back in Android’s WebView

When a user navigates a website through a webview, pressing the back button will finish the current activity, instead of going back in the browsing history.

You can change this behavior like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
		mWebView.goBack();
		return true;
	}
	return super.onKeyDown(keyCode, event);
}
Share

Android shape xml example

Shapes in Android are extremely useful but there are not enough examples.

The values for the shape attribute are: rectangle, oval, line and ring.

Here is a simple (and ugly) example but you can get the point:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">

        <solid android:color="#ff0000"/>

	<gradient android:startColor="#ffffff" android:endColor="#000000"
		android:angle="180" />


	<stroke android:width="1dp" android:color="#FF000000"
		android:dashWidth="1dp" android:dashGap="2dp" />
         <!-- or -->
	<stroke android:width="1dp" android:color="#FF00FF00" />


	<padding android:left="7dp" android:top="7dp" android:right="7dp"
		android:bottom="7dp" />

	<corners android:bottomRightRadius="9dip"
		android:bottomLeftRadius="9dip" android:topLeftRadius="9dip"
		android:topRightRadius="9dip" />

</shape>

Check the documentation here for more details.

Share