help me with dynamic element generation

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

help me with dynamic element generation

Postby arin » Fri Jul 17, 2009 1:25 pm

Hi,

I was looking for a solution for this concept but as I am new I cant... please help

I have a child Relative Layout which is inside a linear Layout. This child Relative layout contains some elements in it.
In my program I need to generate new Relative Layouts like this one 5 times and add it to the parent Layout (which is the Linear layout)...

My idea is to get a reference of the 1st Relative Layout (along with all its child) and then create 5 more layouts like that.

how can I achieve this? Is there a smart way possible? :? can I quickly grab a reference of the RelativeLayout along with the child...?

thanks in advance...
arin
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jul 09, 2009 10:17 am

Top

Postby arin » Fri Jul 17, 2009 10:15 pm

I solved this... heres my solution. This program dynamically generates a relative layout and adds it to the parent layout (referred from the XML)
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent" android:id="@+id/ll">
  11.  
  12.    
  13.  
  14. <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
  15.  
  16. <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="text1"></TextView>
  17.  
  18. </RelativeLayout>
  19.  
  20.  
  21.  
  22. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


and the Java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package pk1.pk2;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.graphics.Color;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.ViewGroup.LayoutParams;
  13.  
  14. import android.widget.LinearLayout;
  15.  
  16. import android.widget.RelativeLayout;
  17.  
  18. import android.widget.TextView;
  19.  
  20.  
  21.  
  22. public class Dynamic extends Activity{
  23.  
  24.  
  25.  
  26.         public void onCreate(Bundle savedInstanceState) {
  27.  
  28.                 super.onCreate(savedInstanceState);
  29.  
  30.                 this.setContentView(R.layout.main2);
  31.  
  32.                
  33.  
  34.                 LinearLayout parent= (LinearLayout) findViewById(R.id.ll);
  35.  
  36.                
  37.  
  38.                 try{
  39.  
  40.                         for(int i=0;i<5;i++)
  41.  
  42.                         parent.addView(getnewRL());
  43.  
  44.                 } catch(Exception e){
  45.  
  46.                         ((TextView)findViewById(R.id.TextView01)).setText(e.getMessage());
  47.  
  48.                 }
  49.  
  50.         }
  51.  
  52.        
  53.  
  54.         private RelativeLayout getnewRL(){
  55.  
  56.                
  57.  
  58.                 TextView tv = new TextView(this);
  59.  
  60.                 tv.setText("hello world");
  61.  
  62.                
  63.  
  64.                 RelativeLayout rl= new RelativeLayout(this);
  65.  
  66.                
  67.  
  68.                
  69.  
  70.                 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
  71.  
  72.                 (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  73.  
  74.                 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
  75.  
  76.                 //params.addRule(RelativeLayout.CENTER_VERTICAL);
  77.  
  78.                
  79.  
  80.                 tv.setLayoutParams(params);
  81.  
  82.                 tv.setBackgroundColor(Color.BLUE);
  83.  
  84.                
  85.  
  86.                 rl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
  87.  
  88.                 rl.setBackgroundColor(Color.GRAY);
  89.  
  90.                
  91.  
  92.                 rl.addView(tv);
  93.  
  94.                
  95.  
  96.                 return rl;
  97.  
  98.         }
  99.  
  100. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


note that getnewRL() does all the trick of dynamically generating the layout.

Happy Coding :D
arin
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jul 09, 2009 10:17 am

Postby vadimz » Thu Jul 30, 2009 9:31 pm

vadimz
Developer
Developer
 
Posts: 44
Joined: Thu Jul 30, 2009 5:04 pm
Location: Moldova

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests