5 Best Maternity Apps for Tracking Your Journey
Congratulations on embarking on this incredible journey of pregnancy! As your body changes and your little one grows, you might find yourself seeking reliable information and tools to guide you. […]
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.
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>
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.
May 6th, 2025
Congratulations on embarking on this incredible journey of pregnancy! As your body changes and your little one grows, you might find yourself seeking reliable information and tools to guide you. […]
May 1st, 2025
The mobile app industry is changing quickly, and Singapore is leading the way in innovation across Southeast Asia. With 2025 on the horizon, businesses must keep up with the latest […]
Apr 17th, 2025
Have you ever wondered how hospitals and clinics are becoming smarter in Singapore? The answer is simple—Artificial Intelligence (AI). AI is no longer something we only see in science fiction […]