Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a generic Android XML layout for all activities

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

like image 552
zabawaba99 Avatar asked Oct 21 '12 04:10

zabawaba99


Video Answer


1 Answers

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!!

like image 67
Dharmendra Avatar answered Sep 29 '22 23:09

Dharmendra