| Author |
Message |
mmin18 Junior Developer

Joined: 03 Feb 2008 Posts: 19 Location: China
|
Posted: Wed Feb 13, 2008 2:28 pm Post subject: Re: How to nest XML layout files?? |
|
|
| krystox wrote: | Hi,
I have one XML layout file which defines a view, but I want to include it into another view as a portion. That view is also defined in XML. What is the syntax to include external files in the layout file? Anyone knows about it?
Thanks! |
i haven't found a way to include a XML in another XML directly, so i create a ViewWrapper class that you can use as follows: (replace org.XXXX to your android package)
| XML: |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/org.XXXX"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.XXXX.ViewWrapper android:layout_width="fill_parent"
android:layout_height="wrap_content" app:wrapLayout="@layout/text" />
</LinearLayout>
|
and the res/layout/text.xml is simply as follows:
| XML: | <?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="TEXT" android:layout_width="fill_parent"
android:layout_height="fill_parent" /> |
if you want to use this ViewWrapper class, it only needs two files:
the res/values/attrs.xml: (defines the attribute app:wrapLayout in the ViewWrapper class)
| XML: | <?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ViewWrapper">
<attr name="wrapLayout" />
</declare-styleable>
</resources>
|
and the ViewWrapper.java:
| Java: | package org.XXXX;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewInflate;
import android.widget.FrameLayout;
public class ViewWrapper extends FrameLayout {
public ViewWrapper(Context context) {
this(context, null, null);
}
public ViewWrapper(Context context, AttributeSet attrs, Map inflateParams) {
super(context, attrs, inflateParams);
android.content.Resources.StyledAttributes a = context
.obtainStyledAttributes(attrs, R.styleable.ViewWrapper);
int id = a.getResourceID(R.styleable.ViewWrapper_wrapLayout, -1);
if (id > 0) {
setView(id);
}
a.recycle();
}
public View setView(int resId) {
ViewInflate vi = getContext() instanceof Activity ? ((Activity) getContext())
.getViewInflate()
: ViewInflate.from(getContext());
View v = vi.inflate(resId, this, null);
return v;
}
}
|
that's it.
anyproblem, mailto: mmin18 [at] gmail
regards |
|
| Back to top |
|
 |
krystox Freshman

Joined: 22 Jan 2008 Posts: 7
|
Posted: Thu Feb 14, 2008 3:55 am Post subject: |
|
|
| Great! I used the similar way to work around, but the wrapper classes are very annoying. Anyways, thanks for your example. |
|
| Back to top |
|
 |
selmi Freshman

Joined: 05 Mar 2008 Posts: 7 Location: Košice, Slovakia
|
Posted: Wed Mar 05, 2008 4:56 am Post subject: I have one problem.... |
|
|
something weird is happening
if i use your ViewWrapper like this:
| XML: | <com.google.android.gx5weather.ViewWrapper
android:id="@+id/st_wrap"
android:layout_weight="1.0"
android:layout_width="fill_parent"
android:layout_height="0px"
app:wrapLayout="@layout/screen_today_highlow"
/> |
then everything works....
but when i try to encapsulate it in layout like this:
| XML: | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.google.android.gx5weather"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1.0"
>
<com.google.android.gx5weather.ViewWrapper
android:id="@+id/st_wrap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:wrapLayout="@layout/screen_today_highlow"
/>
</LinearLayout> |
then when i start activity i get crash:
ava.lang.RuntimeException: binary XML file line #46: You must supply a layout_width attribute
any idea what is causing this? maybe i am missing something, but with ordinary views i never had such problem and as you can see layout_width is there... |
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2618 Location: College Park, MD
|
Posted: Wed Mar 05, 2008 9:59 am Post subject: |
|
|
Hello selmi,
Does it work with explicit numbers
The internal representation of "fill_parent" is -1 or 0, what could produce that error.
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials |
|
| Back to top |
|
 |
selmi Freshman

Joined: 05 Mar 2008 Posts: 7 Location: Košice, Slovakia
|
Posted: Thu Mar 06, 2008 12:09 am Post subject: |
|
|
| plusminus wrote: |
Does it work with explicit numbers
The internal representation of "fill_parent" is -1 or 0, what could produce that error.  |
i tried to replace all "fill_parent" with "100px" and result is the same. so i replaced things not only in this wrapper but also in xml whicgh is loaded into it. andstill no luck always same error |
|
| Back to top |
|
 |
chitgoks Junior Developer

Joined: 11 Mar 2008 Posts: 15
|
|
| Back to top |
|
 |
mmin18 Junior Developer

Joined: 03 Feb 2008 Posts: 19 Location: China
|
Posted: Mon Mar 17, 2008 10:03 am Post subject: |
|
|
Hello, selmi:
why do you give a android:layout_height="0px"?
i don't know why your program crashes, it seems all right.
maybe you can try to modify the ViewWrapper class to extends other layouts such as AbsoluteLayout, LinearLayout.
Hello, chitgoks:
unbound prefix? may be you forgot to declear your xml namespaces: xmlns:app="http://schemas.android.com/apk/res/com.boom.bam.wrapper" |
|
| Back to top |
|
 |
chitgoks Junior Developer

Joined: 11 Mar 2008 Posts: 15
|
|
| Back to top |
|
 |
mmin18 Junior Developer

Joined: 03 Feb 2008 Posts: 19 Location: China
|
Posted: Mon Mar 17, 2008 10:18 am Post subject: |
|
|
if your ViewWrapper is in the package com.bigfoot.gymlobby.wrapper.ViewWrapper, then i think your xml should like this:
| XML: | <com.bigfoot.gymlobby.wrapper.ViewWrapper
xmlns:app="http://schemas.android.com/apk/res/com.bigfoot.gymlobby.wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:wrapLayout="@layout/login" /> |
|
|
| Back to top |
|
 |
selmi Freshman

Joined: 05 Mar 2008 Posts: 7 Location: Košice, Slovakia
|
Posted: Mon Mar 17, 2008 10:29 am Post subject: |
|
|
| mmin18 wrote: | Hello, selmi:
why do you give a android:layout_height="0px"?
i don't know why your program crashes, it seems all right.
maybe you can try to modify the ViewWrapper class to extends other layouts such as AbsoluteLayout, LinearLayout.
|
i use it with combination with android:layout_weight="1.0" , so it will take all available space which remains in parent layout.
problem with crash is that mostly it works, sometimes not. if not then after i delete something (almost randomly chosen) it works... i think the best would be if there would be way to find out WHAT is in binary xml on line which is reported... or if error message would write lines in source xml, but i doubt its possible |
|
| Back to top |
|
 |
chitgoks Junior Developer

Joined: 11 Mar 2008 Posts: 15
|
|
| Back to top |
|
 |
chitgoks Junior Developer

Joined: 11 Mar 2008 Posts: 15
|
|
| Back to top |
|
 |
mmin18 Junior Developer

Joined: 03 Feb 2008 Posts: 19 Location: China
|
Posted: Mon Mar 17, 2008 12:02 pm Post subject: |
|
|
| chitgoks wrote: | mmin18: it doesnt matter right? if my
R.java is in com.bigfoot.gymlobby.android?
while my ViewWrapper is in com.bigfoot.gymlobby.wrapper
thanks |
if your R.java is in com.bigfoot.gymlobby.android, then you should declear like this:
| XML: | <com.bigfoot.gymlobby.wrapper.ViewWrapper
xmlns:app="http://schemas.android.com/apk/res/com.bigfoot.gymlobby.android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:wrapLayout="@layout/login" /> |
the xmlns:app should always be your R.java package |
|
| Back to top |
|
 |
chitgoks Junior Developer

Joined: 11 Mar 2008 Posts: 15
|
|
| Back to top |
|
 |
chitgoks Junior Developer

Joined: 11 Mar 2008 Posts: 15
|
|
| Back to top |
|
 |
|