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. Timing and using Intent MediaAction.ImageCapture to take pictures


I'm still working only on the emulator, but I am seeing some unexpected behavior when using an Intent to take a picture via MediaAction.ImageCapture. At first, I thought everything was working as I wished: from my application, control was transferred to the Camera application, which allowed me to take a picture, review it, and then control transferred back to my original application. I did notice that the picture taken seemed to be the same regardless of how long I waited, and since the 2.1 emulator has a moving block in the picture stream, that seemed odd. The block should have been in a different location depending on when I took the picture. After adding more logging, it appears that the picture is being taken immediately, but control doesn't transfer back to my application, it stays in the camera, until I've taken another picture and accepted it. I would prefer the first behavior, put the user in Camera mode, let them take and review the picture, and then return to my application. Has anyone else dealt with this in a way other than writing your own camera app? I know that there is also an outstanding issue having to do with image size, but I'm hoping by the time I'm finished with the rest of my application, some of the Intent/MediaAction stuff will have been resolved, like picture resolution. Thanks, Ray --

2. $200 per live wallpaper

paying $200 per original, cool, live wallpaper for 2.1

hit me up with ideas or your portfolio. thanks!

-- 

3. Help with listview!

4. play audio file from weblink in android

5. Picasa web album sync with nx1

6. urgent help Integrating code with Android home screen code.

7. Adding contacts to a custom account