andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Nesting XML Layouts

Goto page 1, 2  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
mmin18
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 19
Location: China

PostPosted: Wed Feb 13, 2008 2:28 pm    Post subject: Re: How to nest XML layout files?? Reply with quote

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
View user's profile Send private message Send e-mail
krystox
Freshman
Freshman


Joined: 22 Jan 2008
Posts: 7

PostPosted: Thu Feb 14, 2008 3:55 am    Post subject: Reply with quote

Great! I used the similar way to work around, but the wrapper classes are very annoying. Anyways, thanks for your example.
Back to top
View user's profile Send private message
selmi
Freshman
Freshman


Joined: 05 Mar 2008
Posts: 7
Location: Košice, Slovakia

PostPosted: Wed Mar 05, 2008 4:56 am    Post subject: I have one problem.... Reply with quote

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
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2618
Location: College Park, MD

PostPosted: Wed Mar 05, 2008 9:59 am    Post subject: Reply with quote

Hello selmi,

Does it work with explicit numbers Question

The internal representation of "fill_parent" is -1 or 0, what could produce that error. Confused

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
selmi
Freshman
Freshman


Joined: 05 Mar 2008
Posts: 7
Location: Košice, Slovakia

PostPosted: Thu Mar 06, 2008 12:09 am    Post subject: Reply with quote

plusminus wrote:

Does it work with explicit numbers Question

The internal representation of "fill_parent" is -1 or 0, what could produce that error. Confused


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 Sad always same error
Back to top
View user's profile Send private message
chitgoks
Junior Developer
Junior Developer


Joined: 11 Mar 2008
Posts: 15

PostPosted: Mon Mar 17, 2008 9:42 am    Post subject: Reply with quote

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://chitgoks.freehostia.com
Casino Games: http://chitgoks.tripod.com
Blogs:
http://chitgoks.blogspot.com
http://wuhtevah.blogspot.com
http://disneyusa.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
mmin18
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 19
Location: China

PostPosted: Mon Mar 17, 2008 10:03 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
chitgoks
Junior Developer
Junior Developer


Joined: 11 Mar 2008
Posts: 15

PostPosted: Mon Mar 17, 2008 10:04 am    Post subject: Reply with quote

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://chitgoks.freehostia.com
Casino Games: http://chitgoks.tripod.com
Blogs:
http://chitgoks.blogspot.com
http://wuhtevah.blogspot.com
http://disneyusa.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
mmin18
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 19
Location: China

PostPosted: Mon Mar 17, 2008 10:18 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
selmi
Freshman
Freshman


Joined: 05 Mar 2008
Posts: 7
Location: Košice, Slovakia

PostPosted: Mon Mar 17, 2008 10:29 am    Post subject: Reply with quote

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
View user's profile Send private message
chitgoks
Junior Developer
Junior Developer


Joined: 11 Mar 2008
Posts: 15

PostPosted: Mon Mar 17, 2008 10:36 am    Post subject: Reply with quote

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://chitgoks.freehostia.com
Casino Games: http://chitgoks.tripod.com
Blogs:
http://chitgoks.blogspot.com
http://wuhtevah.blogspot.com
http://disneyusa.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
chitgoks
Junior Developer
Junior Developer


Joined: 11 Mar 2008
Posts: 15

PostPosted: Mon Mar 17, 2008 11:04 am    Post subject: Reply with quote

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://chitgoks.freehostia.com
Casino Games: http://chitgoks.tripod.com
Blogs:
http://chitgoks.blogspot.com
http://wuhtevah.blogspot.com
http://disneyusa.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
mmin18
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 19
Location: China

PostPosted: Mon Mar 17, 2008 12:02 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
chitgoks
Junior Developer
Junior Developer


Joined: 11 Mar 2008
Posts: 15

PostPosted: Mon Mar 17, 2008 4:18 pm    Post subject: Reply with quote

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://chitgoks.freehostia.com
Casino Games: http://chitgoks.tripod.com
Blogs:
http://chitgoks.blogspot.com
http://wuhtevah.blogspot.com
http://disneyusa.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
chitgoks
Junior Developer
Junior Developer


Joined: 11 Mar 2008
Posts: 15

PostPosted: Tue Mar 18, 2008 2:23 am    Post subject: Reply with quote

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://chitgoks.freehostia.com
Casino Games: http://chitgoks.tripod.com
Blogs:
http://chitgoks.blogspot.com
http://wuhtevah.blogspot.com
http://disneyusa.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.