Reputation: 3815
I am trying to make a reusable header. Here is my XML.
<?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="wrap_content"
android:background="@drawable/bg" >
<ImageButton
android:id="@+id/imagebuttonforheader"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_alignParentLeft="true"
android:background="@drawable/back_button"
/>
<ImageButton
android:id="@+id/imagebuttonforheader"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_alignParentRight="true"
android:background="@drawable/forward_button"
/>
</RelativeLayout>
I made a class for it:
public class Header extends RelativeLayout {
Context context;
Activity activity;
public Header(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.header, null, false);
}
}
And then added this layout into my main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<my.testy.view.Header
android:id="@+id/headerOnMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</my.testy.view.Header>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/headerOnMain"
android:text="@string/hello" />
</RelativeLayout>
But it's not showing up on the application.
What am I doing wrong here?
Upvotes: 0
Views: 233
Reputation: 87064
You have to attached your inflated layout to your custom class:
li.inflate(R.layout.header, this, true);
Upvotes: 1