[Solved] Beginner: Dynamic layout (Contexts)

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

[Solved] Beginner: Dynamic layout (Contexts)

Postby yvecai » Sun Oct 18, 2009 5:46 pm

Hi all,

I am discovering the possibilities of dynamic layouts. My aim here is to create a new TextView each time I click on a button.

Now, the following code is wrong:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package yvecai.dyn_layout;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.LinearLayout;
  8. import android.widget.TextView;
  9.  
  10. public class dyn_layout extends Activity {
  11.     /** Called when the activity is first created. */
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.        
  16.         final LinearLayout ll = new LinearLayout(this);
  17.         ll.setOrientation(LinearLayout.VERTICAL);
  18.        
  19.         Button add = new Button(this);
  20.         add.setText("Add");
  21.         ll.addView(add);
  22.                
  23.         add.setOnClickListener(new View.OnClickListener() {
  24.             public void onClick(View v) {
  25.                TextView tv = new TextView(this);
  26.                 tv.setText("text");
  27.                 ll.addView(tv);
  28.             }
  29.         });
  30.  
  31.         setContentView(ll);
  32.     }
  33. }
  34.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


As Eclipse says
The constructor TextView(new View.OnClickListener(){}) is undefined


1) Somebody could help me to understand what is wrong in my method?
2) What would be the best way to do it?


Thanks, Yves
Last edited by yvecai on Mon Oct 19, 2009 10:44 pm, edited 1 time in total.
yvecai
Freshman
Freshman
 
Posts: 4
Joined: Sun Oct 18, 2009 5:06 pm

Top

Postby qlimax » Sun Oct 18, 2009 7:55 pm

Hi :)

TextView tv=new TextView(getApplicationContext());

instead of

TextView tv = new TextView(this);


in your case, the keyword this, does not refer to the Activity (the context), but it refers to the clicklistener you are implementing.



CYA :run:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby padde » Sun Oct 18, 2009 8:02 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package yvecai.dyn_layout;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.View;
  11.  
  12. import android.widget.Button;
  13.  
  14. import android.widget.LinearLayout;
  15.  
  16. import android.widget.TextView;
  17.  
  18.  
  19.  
  20. public class dyn_layout extends Activity {
  21.  
  22.     /** Called when the activity is first created. */
  23.  
  24.     @Override
  25.  
  26.     public static Context mycontext = this; //global context of the activity
  27.  
  28.  
  29.  
  30.     public void onCreate(Bundle savedInstanceState) {
  31.  
  32.         super.onCreate(savedInstanceState);
  33.  
  34.        
  35.  
  36.         final LinearLayout ll = new LinearLayout(this);
  37.  
  38.         ll.setOrientation(LinearLayout.VERTICAL);
  39.  
  40.        
  41.  
  42.         Button add = new Button(this);
  43.  
  44.         add.setText("Add");
  45.  
  46.         ll.addView(add);
  47.  
  48.                
  49.  
  50.         add.setOnClickListener(new View.OnClickListener() {
  51.  
  52.             public void onClick(View v) {
  53.  
  54.                TextView tv = new TextView(dyn_layout.mycontext);  
  55.  
  56.                 tv.setText("text");
  57.  
  58.                 ll.addView(tv);
  59.  
  60.             }
  61.  
  62.         });
  63.  
  64.  
  65.  
  66.         setContentView(ll);
  67.  
  68.     }
  69.  
  70. }
  71.  
  72.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


--- Edit ---
The use of getApplicationContext() like qlimax suggested is better in your case.
My solution is more general and works with all classes but requires a static variable in your activity.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby yvecai » Mon Oct 19, 2009 10:44 pm

Many thanks to both of you!

Yves
yvecai
Freshman
Freshman
 
Posts: 4
Joined: Sun Oct 18, 2009 5:06 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 10 guests