Sai Geetha started off as a Java Developer and moved on to work as a Solution Architect and an Enterprise Architect. However, as a hobby she continues to don the hats of a Java Developer as well as an Android Developer. She loves to share what she learns with the larger community. In her interest to share her knowledge, she blogs Android tutorials at saigeethamn.blogspot.com and her other technical musings at sageethatechnical.blogspot.com. Sai Geetha is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

Creating Android UI Programmatically

07.20.2011
| 5744 views |
  • submit to reddit
So far, in all my examples, I have been using the declarative way of creating an Android UI using XML. However, there could arise certain situations when you may have to create UI programmatically. Sincere advice would be to avoid such a design since android has a wonderful architecture where the UI and the program are well separated. However, for those few exceptional cases where we may need too… here is how we do it. Every single view or viewgroup element has an equivalent java class in the SDK. The structure and naming of the classes and methods is very similar to the XML vocabulary that we are used to so far.

Let us start with a LinearLayout. How would we declare it in an XML?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

This just contains a TextView embedded in a LinearLayout. A very trivial example. But serves the purpose intended. Let me show how almost every single element here corresponds to a class or a method call in the class.  So the equivalent code in the onCreate(…)  method of an activity would be like this:

         super.onCreate(savedInstanceState);

lLayout = new LinearLayout(this);
lLayout.setOrientation(LinearLayout.VERTICAL);
//-1(LayoutParams.MATCH_PARENT) is fill_parent or match_parent since API level 8
//-2(LayoutParams.WRAP_CONTENT) is wrap_content
lLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
tView = new TextView(this);
tView.setText("Hello, This is a view created programmatically! " +
"You CANNOT change me that easily :-)");
tView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
lLayout.addView(tView);
setContentView(lLayout);
Like this any layout view can be created. But from this small example you can notice two outstanding things – very tedious to code for every attribute of the view. And any simple change in the view, you need to change the code, compile, deploy and only then you see the effect of the change – unlike in a layout editor. 
You can download the sample code here.
References
Published at DZone with permission of Sai Geetha M N, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gagan Peter replied on Sat, 2012/04/14 - 5:17am

Hi,

I am looking for dynamic table layout which can render SQLite table...
diff number of columns and width

may I am greedy :-)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.