hi i have a layout.xml with 3 buttons linked to separate layouts.
i have managed to code for 1 button using intent. however, i do not know how to add in the next buttons so that they each go to a separate layout. this is my code.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity1 extends Activity implements OnClickListener {
Button hello1, hello2, hello3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hello1 = (Button)findViewById(R.id.hello1);
hello2 = (Button)findViewById(R.id.hello2);
hello3 = (Button)findViewById(R.id.hello3);
hello1.setOnClickListener(this);
hello2.setOnClickListener(this);
hello3.setOnClickListener(this);
}
public void onClick(View src) {
Intent hello1 = new Intent(this, Hello1Activity.class);
startActivity(hello1);
Intent hello2 = new Intent(this, Hello2Activity.class);
startActivity(oltp);
Intent hello3 = new Intent(this, Hello3Activity.class);
startActivity(oltp);
}
}
this does not work at all.. the first button when clicked goes to hello3. if i remove the hello2 and hello3, then hello1 works well. any ideas please.
You’ve attached the same click-listener to all the three buttons and you are starting all the three activities in the onClick method. The Hello3 activity is the one that is started after Hello2Activity and Hello1Activity and hence it is always launched. To achieve what you are trying to do you should attach a different listener to every button and put the code specific to the button there. Something like: