1

i have in my layout.xml this lines :

<include android:id="@+id/promo1" android:layout_width="fill_parent"
         android:layout_height="fill_parent" layout="@layout/one_promo">
</include>
<include android:id="@+id/promo2" android:layout_width="fill_parent"
         android:layout_height="fill_parent" layout="@layout/one_promo">
</include>
<include android:id="@+id/promo3" android:layout_width="fill_parent"
         android:layout_height="fill_parent" layout="@layout/one_promo">
</include>

I want to do this programmatically in my Java code.

Please any help?

thanks in advance

Houcine
  • 24,001
  • 13
  • 56
  • 83

1 Answers1

4

Use LayoutInflater as i shown below

Define a Relative Layout in your xml

<RelativeLayout android:id="@+id/mylayout" android:layout_width="fill_parent"
         android:layout_height="fill_parent" />

Map it in your Java code

RelativeLayout mynewlayout = (RelativeLayout) findById(R.id.mylayout);

Use Layout Inflater

LayoutInflater layoutInflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
mynewlayout.addView(0, layoutInflater.inflate(R.layout.one_promo, this, false) );
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
  • thanks sankar for your reply , i think that i will integrate my includes in xml , but i need to change the attribute layout of each one , explanation : i have five includes in my layout xml, and i want to change the layout attribute of each one programmatically , is it possible ? – Houcine May 11 '11 at 09:32
  • @houcine: you are always welcome,try and convey me whether you reached your destination or not? – Sankar Ganesh PMP May 11 '11 at 09:33
  • thanks sankar for your reply , i think that i will integrate my includes in xml , but i need to change the attribute layout of each one , explanation : i have five includes in my layout xml, and i want to change the layout attribute of each one programmatically , is it possible ? – Houcine May 11 '11 at 09:36
  • @hounice: yes it is possible use LayoutInflater as i shown above, and then add change the value of child in the first parameter of addview method, mynewlayout.addView(0, layoutInflater.inflate(R.layout.one_promo, this, false) ); then for my second include give 1 as i shown here mynewlayout.addView(1, layoutInflater.inflate(R.layout.one_promo, this, false) ); – Sankar Ganesh PMP May 11 '11 at 09:41
  • i think i dont need to add the relativeLayout in my XML code , because i already have a linearLayout on my HorizontalScrollView , what do u think ?? – Houcine May 11 '11 at 09:49
  • @Houcine: i gave RelativeLayout for a Example, use the Layout which is mentioned in your xml – Sankar Ganesh PMP May 11 '11 at 09:50
  • mynewlayout.addView(0, layoutInflater.inflate(R.layout.one_promo, this, false) ); this : refer to the Activity context ? or the ViewGroup ( in ur example ,the relativeLayout) ??? because when i've tried this ,it gives me an error in this instruction any idea ? – Houcine May 11 '11 at 10:17
  • @Sankar : thanks a lot my friend ,that's work great , but just one thing , is u need to permute arguments of method mynewLayout.addView ( View view , int index ) , :) many thanks my friend – Houcine May 11 '11 at 10:56
  • @houcine: Welcome My dear friend,refer my blog for any android problem http://sankarganesh-info-exchange.blogspot.com/ – Sankar Ganesh PMP May 11 '11 at 11:41