Posts tagged Layout
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 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.


