Reputation: 83
How can I go from page A to Page B with a simple Button ?
XML Code is the following
<Button
android:text="Page Suivante"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="137dp"
android:id="@+id/btnPSuiv"
android:onClick="pageSuivante"/>
and code in MainActivity is:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** send the user to the second form **/
public void goToContact(View view){
Intent intent new Intent(this,MainActivity.class);
}
}
I know I have to create an Intent Object but I dont fully understand how to procede.
Upvotes: 0
Views: 57
Reputation: 1077
change the onClick attribute in XML:
<Button
android:text="Page Suivante"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="137dp"
android:id="@+id/btnPSuiv"
android:onClick="goToContact"/>
change the goToContact() to:
public void goToContact(View view){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
Upvotes: 0
Reputation: 12953
you need start the intent
to navigate
/** send the user to the second form **/
public void goToContact(View view){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
Upvotes: 0
Reputation: 1853
Button has a method setOnClickListener which also have a great example in the Button widget deference page, which will tell exactly what you want.
Simply fetch the button from the activity using findViewById method with the id of button given as parameter
final Button button = (Button) findViewById(R.id.button_id);
after that you can setOnClickListener simply by passing the listener performing the action as a parameter, usually as a anonymous class
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
After that you only need to perform moving from view a to view b, which is usually performed using Activitys, therefore Intent is used to start the new Activity
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(CurrentActivity.this, MainActivity.class));
}
});
Upvotes: 1