Reusing Layout (Include-Merge Tag) Tutorial For Android.

This tutorial is about how to reuse the same layouts which is used in many layouts(xml files) for example most of the apps have footer whose layout repeats.It is done by using include and merge tags.

<include> tag basically means ‘take that file and paste it’s contents here’

<merge > The layout which we have to use must be enclosed under merge tag, so that we can include layouts from other xmls.

You can download its complete source code from here .

footer.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">
	<LinearLayout android:id="@+id/LinearLayout01"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/Button11"
			android:text="Button11"></Button>
		<Button android:layout_below="@id/Button01"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:id="@+id/Button12" android:text="Button12"></Button>
	</LinearLayout>
</merge>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<RelativeLayout android:id="@+id/RelativeLayout02"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_centerInParent="true">
		<Button android:id="@+id/Button01" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Next Activity   "></Button>
	</RelativeLayout>
	<RelativeLayout android:id="@+id/RelativeLayout03"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_alignParentBottom="true">
		<include layout="@layout/footer" />
	</RelativeLayout>
</RelativeLayout>

Splash.java

public class Splash extends Activity implements OnClickListener {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViewById(R.id.Button01).setOnClickListener(this);
		findViewById(R.id.Button11).setOnClickListener(this);
		findViewById(R.id.Button12).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Button01:
			startActivity(new Intent(this, Act2.class));
			break;
		case R.id.Button11:
			Toast.makeText(this, "Button 11 - Activity 1 ....",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.Button12:
			Toast.makeText(this, "Button 12 - Activity 1 ....",
					Toast.LENGTH_SHORT).show();
			break;
		}
	}

}
You can see in the code that Button11 and Button12 have their separrate onClicklisteners on two seperate activities. Also to make common their actions see my Blog.

http://stackoverflow.com/questions/2732682/simple-example-of-merge-and-include-usage-in-android-xml-layouts

http://developer.android.com/resources/articles/layout-tricks-merge.html


free counters

Advertisements
This entry was posted in Android and tagged , . Bookmark the permalink.

One Response to Reusing Layout (Include-Merge Tag) Tutorial For Android.

  1. Kerellane says:

    watch satellite tv on pc
    great blog , how are you doing now eeh?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s