Re: How to nest XML layout files??

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: How to nest XML layout files??

Postby mmin18 » Wed Feb 13, 2008 2:28 pm

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)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.         xmlns:app="http://schemas.android.com/apk/res/org.XXXX"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <org.XXXX.ViewWrapper android:layout_width="fill_parent"
  6.                 android:layout_height="wrap_content" app:wrapLayout="@layout/text" />
  7. </LinearLayout>
  8.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


and the res/layout/text.xml is simply as follows:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:text="TEXT" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


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)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.         <declare-styleable name="ViewWrapper">
  5.                 <attr name="wrapLayout" />
  6.         </declare-styleable>
  7. </resources>
  8.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


and the ViewWrapper.java:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.XXXX;
  2.  
  3. import java.util.Map;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9. import android.view.ViewInflate;
  10. import android.widget.FrameLayout;
  11.  
  12. public class ViewWrapper extends FrameLayout {
  13.  
  14.         public ViewWrapper(Context context) {
  15.                 this(context, null, null);
  16.         }
  17.  
  18.         public ViewWrapper(Context context, AttributeSet attrs, Map inflateParams) {
  19.                 super(context, attrs, inflateParams);
  20.                 android.content.Resources.StyledAttributes a = context
  21.                                 .obtainStyledAttributes(attrs, R.styleable.ViewWrapper);
  22.                 int id = a.getResourceID(R.styleable.ViewWrapper_wrapLayout, -1);
  23.                 if (id > 0) {
  24.                         setView(id);
  25.                 }
  26.                 a.recycle();
  27.         }
  28.  
  29.         public View setView(int resId) {
  30.                 ViewInflate vi = getContext() instanceof Activity ? ((Activity) getContext())
  31.                                 .getViewInflate()
  32.                                 : ViewInflate.from(getContext());
  33.                 View v = vi.inflate(resId, this, null);
  34.                 return v;
  35.         }
  36. }
  37.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


that's it.
anyproblem, mailto: mmin18 [at] gmail

regards
mmin18
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Feb 03, 2008 2:33 pm
Location: China

Top

Postby krystox » Thu Feb 14, 2008 3:55 am

Great! I used the similar way to work around, but the wrapper classes are very annoying. Anyways, thanks for your example.
krystox
Freshman
Freshman
 
Posts: 7
Joined: Tue Jan 22, 2008 2:39 am

I have one problem....

Postby selmi » Wed Mar 05, 2008 4:56 am

something weird is happening

if i use your ViewWrapper like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <com.google.android.gx5weather.ViewWrapper
  2.  
  3.       android:id="@+id/st_wrap"
  4.  
  5.       android:layout_weight="1.0"
  6.  
  7.       android:layout_width="fill_parent"
  8.  
  9.       android:layout_height="0px"
  10.  
  11.       app:wrapLayout="@layout/screen_today_highlow"
  12.  
  13. />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


then everything works....

but when i try to encapsulate it in layout like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.     xmlns:app="http://schemas.android.com/apk/res/com.google.android.gx5weather"
  4.  
  5.     android:orientation="horizontal"
  6.  
  7.     android:layout_width="fill_parent"
  8.  
  9.     android:layout_height="0px"
  10.  
  11.     android:layout_weight="1.0"
  12.  
  13.     >
  14.  
  15.      <com.google.android.gx5weather.ViewWrapper
  16.  
  17.          android:id="@+id/st_wrap"
  18.  
  19.          android:layout_width="fill_parent"
  20.  
  21.          android:layout_height="fill_parent"
  22.  
  23.           app:wrapLayout="@layout/screen_today_highlow"
  24.  
  25.      />
  26.  
  27. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


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...
selmi
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 05, 2008 4:49 am
Location: Košice, Slovakia

Postby plusminus » Wed Mar 05, 2008 9:59 am

Hello selmi,

Does it work with explicit numbers :?:

The internal representation of "[font=Lucida Console]fill_parent[/font]" is [font=Lucida Console]-1[/font] or [font=Lucida Console]0[/font], what could produce that error. :?

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby selmi » Thu Mar 06, 2008 12:09 am

plusminus wrote:Does it work with explicit numbers :?:

The internal representation of "[font=Lucida Console]fill_parent[/font]" is [font=Lucida Console]-1[/font] or [font=Lucida Console]0[/font], 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
selmi
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 05, 2008 4:49 am
Location: Košice, Slovakia

Postby chitgoks » Mon Mar 17, 2008 9:42 am

my error is error parsing xml : unbound prefix

<LinearLayout
android:id="@+id/content1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff99ccff"
>

<com.boom.bam.wrapper.ViewWrapper
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:wrapLayout="@layout/login" />

</LinearLayout>
Website/Java Games: http://www.chitgoks.com
Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
chitgoks
Developer
Developer
 
Posts: 36
Joined: Tue Mar 11, 2008 3:06 am

Top

Postby mmin18 » Mon Mar 17, 2008 10:03 am

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"
mmin18
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Feb 03, 2008 2:33 pm
Location: China

Postby chitgoks » Mon Mar 17, 2008 10:04 am

ah i found whwat's causing it

<com.bigfoot.gymlobby.wrapper.ViewWrapper
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:wrapLayout="@layout/login" />

it is this line.

ah right i didnt add
xmlns:app="http://schemas.android.com/apk/res/org.XXXX"


my error now is no resource identifier found for hte attribute WrapLayout in package com.bla.bla.wrapper

mmind18
thanks
Website/Java Games: http://www.chitgoks.com
Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
chitgoks
Developer
Developer
 
Posts: 36
Joined: Tue Mar 11, 2008 3:06 am

Postby mmin18 » Mon Mar 17, 2008 10:18 am

if your ViewWrapper is in the package com.bigfoot.gymlobby.wrapper.ViewWrapper, then i think your xml should like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <com.bigfoot.gymlobby.wrapper.ViewWrapper
  2.  
  3. xmlns:app="http://schemas.android.com/apk/res/com.bigfoot.gymlobby.wrapper"
  4.  
  5. android:layout_width="fill_parent"
  6.  
  7. android:layout_height="wrap_content"
  8.  
  9. app:wrapLayout="@layout/login" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
mmin18
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Feb 03, 2008 2:33 pm
Location: China

Postby selmi » Mon Mar 17, 2008 10:29 am

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
selmi
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 05, 2008 4:49 am
Location: Košice, Slovakia

Postby chitgoks » Mon Mar 17, 2008 10:36 am

ratz. i wanted to hide my package name lolz

ahh.. i thought it has to be in LinearLayout. and i added it there now mmin18 but the same error shows
Website/Java Games: http://www.chitgoks.com
Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
chitgoks
Developer
Developer
 
Posts: 36
Joined: Tue Mar 11, 2008 3:06 am

Postby chitgoks » Mon Mar 17, 2008 11:04 am

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
Website/Java Games: http://www.chitgoks.com
Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
chitgoks
Developer
Developer
 
Posts: 36
Joined: Tue Mar 11, 2008 3:06 am

Postby mmin18 » Mon Mar 17, 2008 12:02 pm

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:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <com.bigfoot.gymlobby.wrapper.ViewWrapper
  2. xmlns:app="http://schemas.android.com/apk/res/com.bigfoot.gymlobby.android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. app:wrapLayout="@layout/login" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


the xmlns:app should always be your R.java package
mmin18
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Feb 03, 2008 2:33 pm
Location: China

Postby chitgoks » Mon Mar 17, 2008 4:18 pm

cool! ill do that.


but i dont understand though. why it's like that

i thought the xmlns:app would be the package name of the ViewWrapper. but instead it's actually the path to where R.java is?

thanks
Website/Java Games: http://www.chitgoks.com
Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
chitgoks
Developer
Developer
 
Posts: 36
Joined: Tue Mar 11, 2008 3:06 am

Postby chitgoks » Tue Mar 18, 2008 2:23 am

hi selmi, did you manage to make it work?

i also get that error. and another one Binary XML file line #31: Error inflating class
Website/Java Games: http://www.chitgoks.com
Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
chitgoks
Developer
Developer
 
Posts: 36
Joined: Tue Mar 11, 2008 3:06 am

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests