Reputation: 507
I have an application that needs the same items [5 buttons acting as tabs] in every screen. Is it possible to create a "base XML layout" that has these 5 buttons and then have all the other XML files extend from the bas layout in some way so that I don't have to have multiple buttons that will ultimately have the same functionality.
Is there a better approach to this problem that can be supported by API 9
Upvotes: 11
Views: 10431
Reputation: 33996
Create a common layout for your base activity. and then include that layout in all the layout using the <include>
tagwhich you want to make the same.
After that create one Abstract Activity and then handle all the click of the buttons and code in this activity and then extends this activity in all other activity in which you have include the base layout.
For example
common buttons xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/tabhost_bg"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:id="@+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bottom_btn_active"
android:layout_weight="1"
android:text="@string/label_home"
style="@style/bottom_tab_btn"/>
<Button
android:id="@+id/btnSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bottom_btn_active"
android:layout_weight="1"
android:text="@string/label_settings"
style="@style/bottom_tab_btn"/>
<Button
android:id="@+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bottom_btn_active"
android:layout_weight="1"
android:text="@string/label_more"
style="@style/bottom_tab_btn"/>
</LinearLayout>
Here is a xml layout in which you can include above XML file
<include
android:id="@+id/bottombar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/bottom_bar" />
Here android:layout_width and android:layout_height and layout are compulsory attributes
Now here is a Base Activity which handles the click of the common controls
public abstract class BottomBar extends Activity implements OnClickListener {
protected Button btnHome;
Button btnSetting, btnMore;
private Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = this;
}
protected void mappingWidgets() {
btnHome = (Button) findViewById(R.id.btnHome);
btnSetting = (Button) findViewById(R.id.btnSetting);
btnMore = (Button) findViewById(R.id.btnMore);
btnHome.setOnClickListener(this);
btnSetting.setOnClickListener(this);
btnMore.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == null)
throw new NullPointerException(
"You are refering null object. "
+ "Please check weather you had called super class method mappingWidgets() or not");
if (v == btnHome) {
} else if (v == btnSetting) {
}else if(v == btnMore) {
}
}
protected void handleBackgrounds(View v) {
if (v == btnHome) {
btnHome.setBackgroundResource(R.drawable.bottom_btn_hover);
btnSetting.setBackgroundResource(R.drawable.bottom_btn_active);
btnMore.setBackgroundResource(R.drawable.bottom_btn_active);
} else if (v == btnSetting) {
btnHome.setBackgroundResource(R.drawable.bottom_btn_active);
btnSetting.setBackgroundResource(R.drawable.bottom_btn_hover);
btnMore.setBackgroundResource(R.drawable.bottom_btn_active);
} else if (v == btnMore) {
btnHome.setBackgroundResource(R.drawable.bottom_btn_active);
btnSetting.setBackgroundResource(R.drawable.bottom_btn_active);
btnMore.setBackgroundResource(R.drawable.bottom_btn_hover);
}
}
}
Now one step remaining is to extend this Base activity in all your activities.
You can extend the Base activity in an activity using the extends keyword. For example
public class MyActivity extends BottomBar
Note: From the child activity you must call the super method of the base class to handle the click of the common controls of your base layout.
You can thus implement multiple common layout within your single activity.
Hope this will help you. Enjoy!!
Upvotes: 17
Reputation: 5825
Includes would be the way to go, but I have never got this to work reliably myself. Maybe I am doing something wrong, but the compiler does not always pick up the ids in the merged layouts.
Upvotes: 1
Reputation: 36035
You may want to look in to the <include>
tag. It effectively takes an xml that you created and copy and pastes it in to your other layouts.
So what you would do is create a single layout with your button. Put them between a <merge>
tag so they do not create a FrameLayout
to get placed in. Then put use the <include>
tag to use the same layout in your other layouts.
NOTE: Always override the layout_width
and layout_height
attributes when using the <include>
tag. This is true even if you are overriding them when the same values. There is a bug in previous versions of Android where it would ignore certain attributes if you didn't override those attributes as well.
Upvotes: 3