Posts tagged XML
Eclipse ADT has been updated. Brings nice features!
Dec 10th
I just noticed throught Eclipse that ADT had been updated.
The new ADT version brings some nice touches to the layout editor.
Now you can select items and drag and drop them onto the layout. It’s a nice touch that will certainly help new android developers a lot.
To get the new features, in eclipse go to Help and then Check for updates.
You can read more in the ADT changelog.
They have also tweaked the XML formatting. Which is always a good thing
I ran a quick test with a drawer layout on Android 4.0:
How to align your TabHost at the bottom of the screen
Jan 8th
This little tip will show you how to align your TabHost at the bottom of the screen in your Android app, like this:
I assume you know how to use the TabHost the usual way (I might write a tutorial on that later).
Say you are using your TabHost the following way:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" /> </LinearLayout> </TabHost>
You then have the tabs on top, like this:
But if you switch out the LinearLayout with a RelativeLayout, like this:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" /> </RelativeLayout> </TabHost>
Then you can specify the alignment of your TabWidget (on line 12), to align it at the bottom:
android:layout_alignParentBottom=”true”
And you get the result as seen in the first image!
Edit April 25:
As pointed out in the comments, there was an error in the layout.
Paul J. Ghosh made a comment on this, with working xml layout, which unfortunately got filtered out by the spam filter.
Thank you.
How to easily extract strings from your Android code into the strings.xml file
Dec 22nd
This handy little feature can save a lot of time and manual work when working with Android apps.
If you during Android development in Eclipse use hardcoded strings in your java code, like in the example below:
package test.layout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class LayoutTest extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.Button01);
btn.setText("New Button Text");
}
}
Maybe you want to gather them in the strings.xml file where they belong, to separate hardcoded strings from the code and to get an overview of your string resources.
This also makes it easier to internationalize your application later on.
How to create Gradient Lines with Drawables in Android
Dec 21st
This article will show you how to create simple gradient line separators to spice up the layout a little bit.
We are going to create gradients like these:
About Drawables
First off, we are going to create a drawable. A drawable resource is graphics that can be drawn on the screen,
it can be a couple of different things, here are some examples;
- Bitmaps – png, jpg or gif images
- Nine-Patch image – A png that can be stretched, to create buttons of varying sizes for example
- State List – Making it easier to use different images for button states for example (focused, pressed)
- Shape Drawable
Stretching and Spanning layouts in Android
Dec 20th
When I was working on my BF2Stats Android app, I had to find out how to place 2 widgets/layout elements on the same horizontal line, while keeping them stretched at the same time so they are filling the width of the screen.
It took me a little research before I found a solution, so I will share two methods with you today.
This tutorial will show you how to create layouts that stretches in the width.
In Eclipse I create a new Android Project by going to File -> New -> Other -> Android -> Android Project. The default project just using the main.xml file for these example layouts will do just fine.
We can create a stretching layout with two different methods; Layout Weights and Layout Spans, I will show a simple example of both, and then I will show how to center an element in a TableLayout with Layout Spans.
Formatting Android XML files in Eclipse
Dec 19th
The XML auto formatting in Eclipse ADT is currently not working properly.
When I am working on Android apps with big XML layout files in Eclipse ADT, the XML tends to get a little cluttered,
and sometimes the many attributes ends up on one single line like this:
This makes reading the XML layout far more difficult than if they were all ligned up properly below each other.
Pressing Ctrl + Shift + F to auto format the XML, didn’t actually make it any better:
So here is how to fix this problem.







