I have a code like that this, I want to delete the imageview after I clicked on it. I use relative layout. What must I do?
I must DELETE it not hide it because I have many imageview and I want to destroy it so the program can work faster.
banyakmusuh= new CountDownTimer(50000,1500) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
Random a = new Random();
int posisix = a.nextInt(500);
btn = new ImageView(level2.this);
btn.setImageResource(R.drawable.salju);
i= i+1;
btn.setId(i);
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(posisix, 0, 0, 0);
btn.setLayoutParams(params);
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.tingkat2);
linearLayout.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
data.nilai+=10;
TextView score1 = (TextView) findViewById(R.id.skore);
score1.setText(String.valueOf(data.nilai));
//what the code for delete imageview?
//some people say to write this code but it cant, there are a green line when i debug it
//((RelativeLayout)v.getParent()).removeView(v);
}
});
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
//banyakmusuh.start();
}
}.start();
Android works on Java. You can’t destroy or delete an object in java. You can only say to the java machine that the machine is free to destroy the object if needed.
Yes, I think, you should start by
removeView(btn). But don’t forget also to setbtn=null. Thus you are telling the java machine that the memory could be freed.