programmatically add a layout existing in xml to a view

by rukiman » Wed, 14 May 2008 05:28:19 GMT


Sponsored Links
 Ok this is what I want to do. Assume I have a XML file that described
my screen.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/page1layout"
        android:layout_width="fill_parent"
android:layout_height="fill_parent"
        xmlns:android=" http://schemas.android.com/apk/res/android" ;>
        <AnalogClock android:id="@+id/clock"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true">
        </AnalogClock>
</RelativeLayout>


Now what I want to do is programmatically add this to a view. I am
doing this at the moment but it is giving me a NullPointer exception

                RelativeLayout layout2 = (RelativeLayout)
findViewById(R.id.page1layout);
                RelativeLayout.LayoutParams linear2layout = new
RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.FILL_PARENT,
                                RelativeLayout.LayoutParams.FILL_PARENT);
                linear2layout.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP);
                page2.addView(layout2, linear2layout);

where page2 is of type RelativeLayout.

Any idea what is going wrong here? If I dynamically create a TextView
like below then it works...


                TextView text2 = new TextView(this);
                text2.setText("can be any bunch of widgets here...blah blah");
                RelativeLayout.LayoutParams linear2layout = new
RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.FILL_PARENT,
                                RelativeLayout.LayoutParams.FILL_PARENT);
                linear2layout.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP);
                page2.addView(text2, linear2layout);

So all I was trying to do is replace the TextView with the UI
described in the XML file.
--~--~---------~--~----~------------~-------~--~----~



programmatically add a layout existing in xml to a view

by rukiman » Wed, 14 May 2008 06:07:15 GMT


 Perphaps this might help I have

main.xml <--- main view
page1.xml <-- the xml I pasted above.


So basically I want to set a RelativeLayout widget in main.xml with
the components which I defined in page1.xml
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


programmatically add a layout existing in xml to a view

by rukiman » Wed, 14 May 2008 06:08:34 GMT


 Perphaps this might help I have

main.xml <--- main view
page1.xml <-- the xml I pasted above.


So basically I want to set a RelativeLayout widget in main.xml with
the components which I defined in page1.xml



--~--~---------~--~----~------------~-------~--~----~



programmatically add a layout existing in xml to a view

by rukiman » Wed, 14 May 2008 06:29:43 GMT


 OK I found out that the NullPointer exception is caused by this line

RelativeLayout layout2 = (RelativeLayout)
findViewById(R.id.page1layout);

I guess this is because I haven't done a setContentView on page1.xml.
But I don't want to do this. I want to basically grab the UI layout
described in page1.xml and stick it into main.xml's relativelayout. Is
this possible? The reason I want to do this is so that I can have
multiple views defined in seperate XML files. If this is not possible
can I describe the multiple views in one xml file and somehow
programmatically show them.




--~--~---------~--~----~------------~-------~--~----~



programmatically add a layout existing in xml to a view

by Romain Guy » Wed, 14 May 2008 06:39:01 GMT


 You don't have to use setContentView(). Instead, you need to use the
ViewInflate class to load your 2nd XML file as a View.








-- 
Romain Guy
www.curious-creature.org

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Reviewing book on Android testing

Hi,

I am an acquisition editor with Packt Publishing and we're coming out
with a book on Android testing. The book is in the development stage
and we need technical reviewers to help us improve the quality of the
content.

Packt welcomes anyone who is proficient in this field to be a part of
the project.

I should point out that there is no payment for reviewing, instead you
will receive full credit in the book (including a bio where you can
include a link to your website or blog, or write anything about
yourself) and 2 free books--one eBook or print copy of the book you
review and an eBook of your choice from our catalog.

Please get in touch at tar...@packtpub.com for further details.

Thanks and Regards,

Tarun Singh
Acquisition Editor
[Packt Publishing]

-- 

2. Problem with INTERNET permission

Hi,

I'm making socket connection to my localhost i.e. http://localhost/fetch.php
is the URL.
Now I have added in permission for internet in manifest. Still it
says
Connection refused to http://localhost

I even tried http://127.0.0.1  Still it gives the same error.

I'm using emulator; not android device.

Please help me out, as I'm not able to continue to my app development.

-- 

3. Background apps (Instant Messaging) being killed without user notification

4. Why i cann't login my account of android market?

5. Android - Large Application size (Memory) issue

6. Is there a way to request permissions from auser as you need them?

7. stretch_copybit::run_render fail causing noticeable pauses in my display