0

I am confused about the include layout:

  1. let's say I made a layout for an action bar named actionbar.xml
  2. I included this to all my other layouts

Now how and where should I write the Java-code for the actionbar? If I write the onclick-function in say main.java?

How will I be able to use the same in say, the second activity stored in second.java? Is there any other method than making an object of the class where the onclick for actionbar is defined?

Abel
  • 56,041
  • 24
  • 146
  • 247
Bharat Chhatre
  • 305
  • 3
  • 6

3 Answers3

2

If you had an action_bar.xml layout like this:

<?xml version="1.0" encoding="utf-8"?>
<com.your.package.ui.widget.ActionBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/actionBar"
    android:layout_width="fill_parent"
    android:layout_height="58dip"
    android:background="@drawable/action_bar_background" >

<ImageButton
        android:id="@+id/actionBarOpenButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent"
        android:contentDescription="open button"
        android:src="@drawable/action_bar_open_button" />

</com.your.package.ui.widget.ActionBar>

You would then have a class in the package com.your.package.ui.widget

Called ActionBar.java that looked like this:

package com.your.package.ui.widget;

public class ActionBar extends LinearLayout implements OnClickListener {

    public ActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ActionBar(Context context) {
        super(context);
    }

      @Override
      protected void onFinishInflate() {
          super.onFinishInflate();

          findViewById(R.id.actionBarOpenButton).setOnClickListener(this);
      }

    @Override
        public void onClick(View v) {                
            switch (v.getId()) {
            case R.id.actionBarOpenButton:
                     // Do something
                  break;
                default:
                 break;
            }
        }
}

You would then include it in another layout say `activity_main.xml' like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include layout="@layout/action_bar" />

    <!-- Rest of your layout -->

</LinearLayout>

You can then include it in any Actvitity you want and your custom widget will do the same onClick event everywhere.

Blundell
  • 75,855
  • 30
  • 208
  • 233
0

If you want to make something visible in all your activity, then you can do that in a way: creating a Header XML and Header ACTIVITY.

Write code regarding Header XML in the Header Activity, and then all other Activity of the App will extend from Header Activity.

For an Example you can see how do I create a header or footer button bar for my android application.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

There is way for achieving this. You should implement an interphase callback function for your actionbar.

Abel
  • 56,041
  • 24
  • 146
  • 247
Anand M Joseph
  • 787
  • 7
  • 7