hey I’m creating an app with two switches on it that change the background of the xml.
however, if the user touches button 1 then 2 I want it to change the background from pic1 to pic2(on first button hit) then pic4(on second button hit)
but if the user touches button 2 then 1 I want it to change the background from pic1 to pic3(on first button hit) then pic4(on second button hit)
at the moment this is my script;
package com.jamie.game;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class level2 extends Activity implements OnClickListener{
Button button1;
View targetView;
Button button2;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.level2);
targetView = (View)findViewById(R.id.level2);
button1 = (Button) findViewById(R.id.button1);
button1.setVisibility(View.VISIBLE);
button1.setBackgroundColor(Color.TRANSPARENT);
button1.setOnClickListener((android.view.View.OnClickListener)this);
button2 = (Button) findViewById(R.id.button2);
button2.setVisibility(View.VISIBLE);
button2.setBackgroundColor(Color.TRANSPARENT);
button2.setOnClickListener((android.view.View.OnClickListener)this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button1)){
targetView.setBackgroundResource(R.drawable.pic2);
if(v.getId()==(R.id.button2)){
targetView.setBackgroundResource(R.drawable.pic4);
}else
if(v.getId()==(R.id.button2)){
targetView.setBackgroundResource(R.drawable.pic3);
if(v.getId()==(R.id.button2)){
targetView.setBackgroundResource(R.drawable.pic4);
}
}
}
but all this does is alternate between pic2 and pic3 on button clicks
Probably you need to create a variable to store the ID of the button that was just clicked. Then, when you receive the next click, you can just check this variable to know if the sequence matches your condition. For example, if you click the first button – store its ID in an
int lastCheckedvariable, then when you click the second button – you should whetherlastCheckedequals to the first button ID. If it is – then you can run yourViewchanging code. Hope this helps.