0

It looks to be a simple problem but I am struggling with it since 2 days ago. I want to create a textView dynamically on a Button Click. Here is a sample piece of code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_main);
Button bn = (Button) findViewById(R.id.button2);
bn.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       TextView tv1 = new TextView(getApplicationContext());
       tv1.setText("Show Up");
       layout.addView(tv1);
        }   
});
}

I can see the button, but on clicking the button, I can't see the textView in my layout. Any issue in code?

Sule
  • 137
  • 8
Mevrick
  • 1
  • 1
  • 1

3 Answers3

1

layout isnt defined as far as I can see, try findViewById on the layout and then set a child element on it, then it should work

Androider
  • 724
  • 3
  • 12
1

Hi read my earlier post, it contains the sample code. (In the UI there is an edittext and a button, after you click the button the new textview shows up with the entered text.) I think this will help for you.

I updated the answer with the working code.

The code only in onCreate() method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dinamic_textview);
    final LinearLayout layout = (LinearLayout) findViewById(R.id.root_layout);
    final Button bn = (Button) findViewById(R.id.btnaddnewtext);
    bn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tv1 = new TextView(v.getContext());
            tv1.setText("Show Up");
            layout.addView(tv1);
        }
    });
}

Layout xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/root_layout">
    <Button 
        android:id="@+id/btnaddnewtext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add"  
    />
</LinearLayout>
Community
  • 1
  • 1
kameny
  • 2,372
  • 4
  • 30
  • 40
  • In continuation to my question, I have layout initialisation code in OnCreate method only – Mevrick Aug 08 '11 at 03:29
  • Hi, I attached the working example above, the logic is only in the onCreate method. I'm only changing the getApplicationContext to v.getContext() because this returns the actual view. And I initialize the layout, because i don't see where is it. – kameny Aug 08 '11 at 06:13
0

Get the layout in which you want the TextView to showup from your layout.xml.

Then add your TextView to that Layout like so:-

LinearLayout myLayout = (LinearLayout)findViewById(R.id.mLayout);
Button bn = (Button) findViewById(R.id.button2);
bn.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       TextView tv1 = new TextView(getApplicationContext());
       tv1.setText("Show Up");
       myLayout .addView(tv1);
        }   
});
Pooja Gaonkar
  • 1,546
  • 17
  • 27