0

Is it possible to generate a textview when click a button?

Currently I have is a button that just get the id of a button

 public void xxx(View v){
    System.out.println(v.getId());
    textView.setText("the button clicked was" + v.getId());
}

Is there a way were I can generate a textview for everytime I click on a button?

After looking at some replied this is the full one but it does not work:

package com.example.earth.stacktest1;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    public class SampleActivity extends MainActivity {

        int counter = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);

            Button button = (Button) findViewById(R.id.add_button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    counter++;

                    TextView textView = new TextView(SampleActivity.this);
                    textView.setText("TextView " + counter);
                    textView.setBackgroundColor(Color.parseColor("#FF0000"));
                    textView.setTextColor(Color.parseColor("#00FF00"));

                    linearLayout.addView(textView);
                }
            });
        }
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Instant
  • 23
  • 1
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/6971285/android-how-to-create-dynamic-view-elements-on-button-click – GoRoS Apr 27 '15 at 20:00

1 Answers1

4

This example adds a TextView every time you press the button:

public class MainActivity extends ActionBarActivity {

    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);

        Button button = (Button) findViewById(R.id.add_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                counter++;

                TextView textView = new TextView(MainActivity.this);
                textView.setText("TextView " + counter);
                textView.setBackgroundColor(Color.parseColor("#FF0000"));
                textView.setTextColor(Color.parseColor("#00FF00"));

                linearLayout.addView(textView);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is the layout of the activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/add_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add TextView" />

</LinearLayout>
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94