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. Desire Oxygen V.2 RC 6 ga bisa GPS

Para master2 android mau nanya donk..g udah flash desire ke Oxygen RC
6 tapi kog GPS kaga bisa jalan ya? mohon pencerahannya..thks...

-- 
===============
"

2. Koneksi internet

Para Suhu yang baik,

Mohon ijin bertanya dan arahan.
Saya pengguna baru android.
Semalem baru melakukan update os android HT Desire saya untuk pertama kali. 
Saya mengikuti semua arahan dari handset, sampai notifikasi sudah sukses 
ter-update di OS seri 2.1.... (seri lebih baru dari bawan orisinil). 
Semua dilakukan dengan menggunakan jaringan internet.
Setelah selesai saya pun masih  bisa melakukan browsing beberapa menit.
Tetapi paginya saya tidak bisa melakukan koneksi sama sekali. Kira kira kenapa 
ya? Saya jadi parno.com

Apakah karena jaringan internet Telkomsel bermasalah? Secara saya tinggal di 
Gianyar Bali, yang jaringannya suka nggak OK. Atau, ada masalah dengan handset 
saya ya?

Mohon pencerahannya. Thanx,

Jaby

Sent from my BlackBerrypowered by Sinyal Kuat INDOSAT

-- 
===============
"

3. Mohon bantuannya..Spica saya pingsan.. slalu restart saat booting

4. Pengenalan robot ijo sejak dini

5. Fwd: Setting up ADB on hawkboard

6. Listening to HFP events

7. what is HIPRI connection