Android – Hacking layouts

One common mis-understanding seem to be that Android starters seem to think that a layout can only be defined via a XML resource. While this is the most common way of defining a layout in most cases you can also define a complete layout via programming code.

For example create a project “de.vogella.android.codelayout” with the activity “CodeLayout” and the following coding.

package de.vogella.android.codelayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CodeLayout extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LayoutParams params1 = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);

		TextView text = new TextView(this);
		text.setText("Hello");
		text.setLayoutParams(params1);
		EditText edit = new EditText(this);
		edit.setHint("This is your input...");
		edit.setLayoutParams(params1);
		Button button = new Button(this);
		button.setText("Press me");
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast makeText = Toast.makeText(v.getContext(), "Pressed", 200);
				makeText.show();
			}
		});
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setPadding(10, 10, 10, 10);
		layout.addView(text);
		layout.addView(edit);
		layout.addView(button);
		setContentView(layout);
	}
}

As you can see the layout is completely created via programming code.

Why is it better to define the layout of an application via XML? See Android Layout Overview.

But even if you define your layout via XML then this layout will be inflated in Java objects by the class “LayoutInflator” which will parse the XML, creates elements for the ViewGroups and Views and calls the setter for the attributes defined in the XML.

Hope this helps.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Android and tagged , , . Bookmark the permalink.

7 Responses to Android – Hacking layouts

  1. Tor Norbye says:

    Note on “parse the XML” – the LayoutInflater operates on “binary XML”; at build time your XML files are compiled into a binary format.

  2. Lars Vogel says:

    Thanks Tor for the clarification.

  3. TeknoMatik says:

    Are you seriously? You call it “Hacking layouts”? Oh, come on….
    I will be call it “Understanding layouts for dummies” because it’s simple and obviously.

  4. Lars Vogel says:

    @TeknoMatik I generally try to select a title which does not insult the reader. 😉

  5. Fenton Travers says:

    Hey Lars, I’ve been educated by many of your posts and just wanted to say thank you for sharing what you’ve learned…it’s helped me a lot. You are a good human being!

  6. Lars Vogel says:

    Thanks Fenton! 🙂

  7. abdullah says:

    thanks

Comments are closed.