i want simply make this code
i have one button
but when i click that button i want a timer start ..
and every timer tick it make a button in main.xml ..how can i do that?
please help me sir!
Button a = (Button) findViewById(R.id.button1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CountDownTimer(5000,1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
//how to create a button here!
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
}
});
i try this .. but this words appear when i click the button (“Unfortunately program has been stoped”)
final int i=1;
Button a = (Button) findViewById(R.id.button1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CountDownTimer(5000,1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
//how to create a button here!
LinearLayout linearLayout = (LinearLayout) findViewById(R.layout.main);
Button btn = new Button(NyaActivity.this);
btn.setId(i+1);
linearLayout.addView(btn);
btn.setText("Button"+(i+1));
final int index = i;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "The index is" + index);
}
});
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
}
});
}
try this way for adding button dynamically with action on CountDownTimer onTick:
and your layout look like as: