Reputation: 45
I have a scrollView that will list the status of the program (onCreate, onPause, onResume etc) line by line. What happens is everytime the status changes, the content of the TextView is just overwritten instead of making a new line.
I've tried several methods already but I'm really not sure what's wrong with it.
Here is my java code::
int numc= 0;
int nums= 0;
int numr= 0;
int nump= 0;
int numst=0;
int numd= 0;
TextView numCreate;
TextView numStart;
TextView numResume;
TextView numPause;
TextView numStop;
TextView numDestroy;
TextView tCreate;
TextView tStart;
TextView tResume;
TextView tPause;
TextView tStop;
TextView tDestroy;
LinearLayout linear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);
numc++;
Log.d("STATE", "onCreate executed");
displayCount();
linear= findViewById(R.id.linearLay);
TextView t1= new TextView(this);
t1.setText("onCreate executed");
linear.addView(t1);
}
@Override
public void onResume() {
super.onResume();
setContentView(R.layout.activity_lifecycle);
numr++;
Log.d("STATE", "onRes executed" + numr);
displayCount();
linear=findViewById(R.id.linearLay);
TextView t2= new TextView(this);
linear.addView(t2);
t2.setText("onResume executed");
}
@Override
public void onPause() {
super.onPause();
setContentView(R.layout.activity_lifecycle);
nump++;
Log.d("STATE", "onPause executed" + nump);
displayCount();
linear=findViewById(R.id.linearLay);
TextView t3= new TextView(this);
linear.addView(t3);
t3.setText("onPause executed");
Log.d("INCREMENT", String.valueOf(linear.getChildCount()));
}
Upvotes: 2
Views: 93
Reputation: 69681
Try this
Remove setContentView(R.layout.activity_lifecycle);
from onResume()
onPause()
SAMPLE CODE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);
numc++;
Log.d("STATE", "onCreate executed");
displayCount();
TextView t1= new TextView(this);
t1.setText("onCreate executed");
linear.addView(t1);
}
@Override
public void onResume() {
super.onResume();
numr++;
Log.d("STATE", "onRes executed" + numr);
displayCount();
TextView t2= new TextView(this);
linear.addView(t2);
t2.setText("onResume executed");
}
@Override
public void onPause() {
super.onPause();
nump++;
Log.d("STATE", "onPause executed" + nump);
displayCount();
TextView t3= new TextView(this);
linear.addView(t3);
t3.setText("onPause executed");
Log.d("INCREMENT", String.valueOf(linear.getChildCount()));
}
Upvotes: 2
Reputation: 696
Remove setContentView from the pause event and you can add Textview like this in LinearLayout
TextView htext =new TextView(this);
textView.setText("Any Text");
textView.setId(123);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
and then
linearlayout.addView(textView);
Upvotes: 0
Reputation: 89
When setting text to your TextView
, use "\n
,
e.g
t1.setText("onCreate executed"+ "\n");
Upvotes: 0
Reputation: 3235
You should remove setContentView(R.layout.activity_lifecycle);
from onResume()
and onPause()
.
Upvotes: 0