7 Best Astrology Apps for Accurate Horoscopes
Millions of people across the globe turn to astrology for guidance in love, career, health, and personal growth. A survey by Pew Research Center found that 29% of Americans believe […]
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.
Feb 20th, 2025
Millions of people across the globe turn to astrology for guidance in love, career, health, and personal growth. A survey by Pew Research Center found that 29% of Americans believe […]
Feb 11th, 2025
Remember when your parents warned you never to get into a car with a stranger? Fast forward to today, and getting into a stranger’s car has become a global business […]
Feb 6th, 2025
In today’s digital age, convenience is king, and online gift delivery services are thriving. With the global online gift market expected to grow significantly, now is the perfect time to […]