Android provide an excellent feature of Merging Layout. The <merge /> tag can be used for optimizing layouts in android application. Basically it reduces the number of levels in view hierarchy, hence it helps to appear screen fastly with less processing.
It provides good optimization because you may have a complex layout with a lot of small views anywhere, and still have your Activity load up really fast. Since in XML you must have a single parent element and rest of the XML elements should be included in it, you should use merge as single parent element and can avoid adding unnecessary parent layout.
Here we can easily understand it using the following code snippet.
activity_main.xml
<!– make marge tag root element of layout xml. –>
<merge xmlns:android=“http://schemas.android.com/apk/res/android” >
<!– Here we put content of screen. Currently we are using an imageview with image to fill screen. –>
<ImageView
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:scaleType=“center”
android:src=“@drawable/background” />
<!– Here we put header of screen. Also we provide some transparency to it. –>
<TextView
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:background=“@drawable/header_bg”
android:padding=“12dip” />
<!– Here we put Footer of screen. We are using action buttons to perform some action. –>
<RelativeLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_gravity=“bottom”
android:layout_marginBottom=“40dp”
android:gravity=“center” >
<Button
android:id=“@+id/buttonView”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:background=“@drawable/view_book” />
<Button
android:id=“@+id/buttonEdit”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true”
android:layout_marginLeft=“20dp”
android:layout_toRightOf=“@+id/buttonView”
android:background=“@drawable/edit_book” />
</RelativeLayout>
</merge>
Here is java code of class.
public class MainActivity extends Activity implements OnClickListener {
// Declaring Action buttons here.
Button buttonEdit;
Button buttonView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Providing view to screen.
setContentView(R.layout.activity_main);
// Getting reference from view and storing it into buttons.
buttonEdit = (Button) findViewById(R.id.buttonEdit);
buttonView = (Button) findViewById(R.id.buttonView);
// Setting button action listener here
buttonEdit.setOnClickListener(this);
buttonView.setOnClickListener(this);
}
// Invoked when clicked
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
// Matching id of clicked view with available views id.
case R.id.buttonEdit:
// If matched, perform required action.
Toast.makeText(getApplicationContext(), “Edit Clicked.”, Toast.LENGTH_SHORT).show();
break;
case R.id.buttonView:
// If matched, perform required action.
Toast.makeText(getApplicationContext(), “View Clicked.”, Toast.LENGTH_SHORT).show();
break;
}
}
}
In the above code we can see how to use layout xml in our java code. <merge/> tag is useful because it can help us to get rid of unneeded ViewGroups, i.e. layouts that are simply used to wrap other views and serve no purpose themselves. Basically it provides a top level element to deal with this kind of redundancy issues.
Related Posts...
Mobile AppsTechnologies
Dec 24th, 2024
The real estate industry is undergoing a massive transformation, with technology playing a pivotal role in shaping its future. From online property listings to virtual tours, apps have revolutionised how […]
Read more
Dec 17th, 2024
Planning a holiday can be both exciting and overwhelming. With so many details to organise – from booking flights to reserving hotels and planning itineraries – having the right travel […]
Read more
Dec 12th, 2024
Technology is advancing at an unprecedented pace, reshaping industries and redefining the roles of professionals. As we approach 2025, staying ahead of the curve requires understanding emerging trends and their […]
Read more