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);
}
}