Reusing Layouts(Master Layout) Tutorial For Android

This tutorial is about how to make master layout in Android like you make the Master Web Page in .Net. For example in most of the apps we have a footer which is used in many activities(in xmls). Using this tutorial you will be able to write one class of footer and use this where ever you want to use footer. This avoids repetition of code and just to change in one footer class whenever you want to change it in future.

Here  its complete source code.

footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/Button11"
		android:text="Activity 1"></Button>
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/Button12"
		android:text=" Activity 2   "></Button>
</LinearLayout>

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">
	<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:background="#ffffff"
		android:text="Activity -- 1          "></TextView>
	<RelativeLayout android:id="@+id/RelativeLayout02"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_centerInParent="true">
	</RelativeLayout>
	<com.farhan.masterLayout.Footer
		android:id="@+id/layoutFooter" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_alignParentBottom="true" />
</RelativeLayout>

Activity1.java

public class Activity1 extends Activity {

	Footer footer;

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

		footer = (Footer) findViewById(R.id.layoutFooter);
		footer.setActivity(this);

	}

}
Footer.java
public class Footer extends LinearLayout {
	private Context mContext;
	private Button btn11;
	private Button btn12;
	// use Activity Object to call finish() method which is not possible using
	// context
	private Activity mActivity;

	public Footer(Context context, AttributeSet attrs) {
		super(context, attrs);

		mContext = context;

		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li;

		li = (LayoutInflater) getContext().getSystemService(infService);
		li.inflate(R.layout.footer, this, true);

		btn11 = (Button) findViewById(R.id.Button11);
		btn11.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(mContext, Activity1.class);
				mActivity.finish();
				mActivity.startActivity(myIntent);
			}

		});

		btn12 = (Button) findViewById(R.id.Button12);
		btn12.setOnClickListener(mFooterListener);

	}

	public void setActivity(Activity activity) {
		// set init otherwise of ctor and call externally...
		mActivity = activity;
	}

	// Create an anonymous implementation of OnClickListener
	private OnClickListener mFooterListener = new OnClickListener() {
		public void onClick(View v) {
			Intent myIntent;
			switch (v.getId()) {
			case R.id.Button12:
				myIntent = new Intent(mContext, Activity2.class);
				mActivity.finish();
				mActivity.startActivity(myIntent);
				break;
			}

		}
	};
}
Explanation
In this code i simply extend a footer class with LinearLayout, inflate it with  footer.xml and perform actions on inflated views.
Then i use the tag
<com.farhan.masterLayout.Footer
android:id=”@+id/layoutFooter” android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:layout_alignParentBottom=”true”/>
in main.xml to use the footer class.
Related Blog
Other Important Links.

free counters

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

5 Responses to Reusing Layouts(Master Layout) Tutorial For Android

  1. 床墊 says:

    Tremendous! This particular is all I can think pertaining to a blog post like this excellent. This kind of is literally a notably explanatory article post on the blog. You just need to know a lot about this amazing

  2. Pingback: Reusing Layout (Include-Merge Tag) For Android. | Farhan's Blog

  3. Gopi says:

    Fantastic!!! This is exactly what I was looking for.

  4. RC says:

    Great tutorial…need a lil help tho..when i add the footer in a class which has a Gallery in it, on trying to open that activity the application just stop unexpectedly..cant figure out what i am doing wrong..please help..
    Here’s the code:

    public class Gallery1 extends Activity {
    Footer footer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery_1);

    // Reference the Gallery view
    Gallery g = (Gallery) findViewById(R.id.gallery);
    // Set the adapter to our custom adapter (below)
    g.setAdapter(new ImageAdapter(this));

    // Set a item click listener, and just Toast the clicked position
    g.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
    Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
    }
    });

    footer = (Footer) findViewById(R.id.layoutFooter);

    footer.setActivity(this);
    }

    public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;

    public ImageAdapter(Context c) {
    mContext = c;
    // See res/values/attrs.xml for the that defines
    // Gallery1.

    }

    public int getCount() {
    return mImageIds.length;
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(mContext);

    i.setImageResource(mImageIds[position]);
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setLayoutParams(new Gallery.LayoutParams(136, 88));

    // The preferred Gallery item background
    i.setBackgroundResource(mGalleryItemBackground);

    return i;
    }

    private Context mContext;

    private Integer[] mImageIds = {
    R.drawable.icon,
    R.drawable.back_icon,
    R.drawable.fb_icon,
    R.drawable.in_icon

    };

    }

    }

  5. RC says:

    Got it working…thanks..great tutorial..

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