2

I have a header that i have to include in multiple activities and i want to handle the OnClickListener for the Buttons from same activity. i followed this question Button Onclick Listener in included layouts

but in my case it doesnot work. i have common_header.xml as:

<?xml version="1.0" encoding="utf-8"?>
<merge 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
<RelativeLayout
    android:id="@+id/layout_top_bar"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:background="@color/titlebarBackground"
    >

    <ImageButton
        android:id="@+id/btn_menu"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="menu"
        android:background="@drawable/borderless_button_unselected"
        android:src="@drawable/menu"
        />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btn_account"
        android:textColor="@color/titlebarForeground"
        android:textSize="16sp"
        android:text="@string/signin"
        android:background="@drawable/transparent_signin_selector"
        />

    <ImageButton
        android:id="@+id/btn_account"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="menu"
        android:background="@drawable/borderless_button_unselected"
        android:src="@drawable/account"
        />

</RelativeLayout>
    <ListView
    android:id="@+id/listview_account"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@color/menuDivider"
    android:dividerHeight="1px"
    android:layout_below="@+id/layout_top_bar"
    android:visibility="gone"
    >
 </ListView>

<ListView
    android:id="@+id/listview_menu"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@color/menuDivider"
    android:dividerHeight="1px"
    android:layout_below="@+id/layout_top_bar"
    android:visibility="gone"
    >
 </ListView>
</merge> 

and in another layout i have used it like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/appBackground" >

<com.example.myApp.MenuView
    android:id="@+id/common_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<Button
    .../>
<Button
   .../>
</RelativeLayout>

my MenuView class is:

public class MenuView extends RelativeLayout {

private LayoutInflater inflater;

public MenuView(Context context, AttributeSet attrs) {
    super(context, attrs);

    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.common_header, this, true);

  }
}

it doesnot show me any error the app runs but the common_header is not merged in my layout.i couldnot figure out where is my mistake.so please help.

Community
  • 1
  • 1
Dharma
  • 2,425
  • 3
  • 26
  • 40

1 Answers1

0

You are overriding RelativeLayout so just have a RelativeLayout as the root element of your layout instead of the merge tag. You can then use it just as you are using it.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
    <RelativeLayout
        android:id="@+id/layout_top_bar"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:clickable="true"
        android:background="@color/titlebarBackground"
        >

    <ImageButton
        android:id="@+id/btn_menu"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="menu"
        android:background="@drawable/borderless_button_unselected"
        android:src="@drawable/menu"
        />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btn_account"
        android:textColor="@color/titlebarForeground"
        android:textSize="16sp"
        android:text="@string/signin"
        android:background="@drawable/transparent_signin_selector"
        />

    <ImageButton
        android:id="@+id/btn_account"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="menu"
        android:background="@drawable/borderless_button_unselected"
        android:src="@drawable/account"
        />

    </RelativeLayout>
    <ListView
        android:id="@+id/listview_account"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/menuDivider"
        android:dividerHeight="1px"
        android:layout_below="@+id/layout_top_bar"
        android:visibility="gone"
        >
     </ListView>

    <ListView
        android:id="@+id/listview_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/menuDivider"
        android:dividerHeight="1px"
        android:layout_below="@+id/layout_top_bar"
        android:visibility="gone"
        >
     </ListView>
</RelativeLayout> 

A short explanation about merge tags, even though it is not what you are looking for, but for other googlers that this may help:

Merge tags are used for reusing layout files and should be used as root of file that is then used from a different layout file with an include tag:

<include layout="@layout/file_name_of_file_with_merge_root">

This way the layouts are merged and the hierarchy is minimized but click listeners are not reused.

meir shapiro
  • 647
  • 7
  • 16
  • thanx for ur help ...i tried changing tag to but the problem is same.It doesnot show me any error but is not merged. And i tried you second option using tag in the common_header.xml and in another xml layout but the problem is same. – Dharma Jan 28 '14 at 03:27
  • try inflater.inflate(R.layout.common_header, this); instead of inflater.inflate(R.layout.common_header, this, true); – meir shapiro Jan 28 '14 at 07:10