Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple layouts with include tag in Android

Tags:

java

android

xml

I'm trying to create a title bar that is the same throughout my application and have been creating layouts like this for each of my activities:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent" android:layout_height="fill_parent">
 <include layout="@layout/title_bar_layout" android:id="@+id/title_bar_layout" />
 <include layout="@layout/main_body" android:id="@+id/main_body" />
</LinearLayout>

main_body.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/main_table" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:stretchColumns="1"
 android:orientation="vertical">
</TableLayout>

my onCreate contains:

setContentView(R.layout.main);
TableLayout t = (TableLayout) findViewById(R.id.main_table);

but t always ends up null.

like image 764
powerj1984 Avatar asked Aug 21 '26 05:08

powerj1984


1 Answers

Give this a try:

TableLayout t = (TableLayout) findViewById(R.id.main_body);

From this page:

you can use android:id to specify the id of the root view of the included layout; it will also override the id of the included layout if one is defined

like image 184
dchang Avatar answered Aug 22 '26 19:08

dchang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!