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. Tmobile G1 update

I received an update just now on my G1 phone. I tapped ok. It took
about 3mins or less to upload and do whatever it was doing. After my
phone restarted, I noticed something different on the background
screen. The wallpaper usually never shows when the phone is locked and
the icon on the screen disappeared. However, when I unlock my phone
the icons appeared. I went to the menu and noticed a few added icons
and a change in background. Changes such as: a camcorder, google talk,
and the icons on the screen can no longer be seen when I pulled up the
drop menu with the arrow. Also with the messaging, IM, GMail, and the
contacts (added photos within call log menu). Everythings about the
same just different in a noticeable way. I which they would add voice
to the gps system that's about my only issue with that particular
application (Maps).

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

2. Playing MediaPlayer on android 1.5 SDK

Hi,
I am able to play media files successfully on 1.1 SDK using
mediaplayer_video code of APIDemos. However, the same code failes at
mediaplayer.prepare(); method.

Can anybody give me how to get rid of this problem? Many code samples
I tried from this forum but I coudn;t succeed with any on 1.5.

It would be great if some can put snippet of working code of video
mediaplayer here or mail to my id: chiragpa...@gmail.com

Thanks,
Aahna

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

3. Multiple SMS text messages don't show up in the history of each individual recipient

4. Localizing date using SimpleDateFormat

5. how to play audio and video files in android with simple example explaination

6. Bluetooth + rSAP car kit

7. Using SensorEventListener