Reputation: 9926
I'm new in the android develop world. I created simple application and created a simple GUI with one button. If the user presses this button, I want to change the screen to show some other GUI.
How can I do this?
Upvotes: 9
Views: 42996
Reputation: 6019
A better word than "Screen" is "Activity", You switch between Activities in android.
A very simple way is to create a button on one activity (Lets call this First Activity) and assign it a method like onClick = startSecondActivity
in the .xml file.
Open FirstActivity.java, Add the method startSecondActivity inside the main method as shown below. This will work!
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
}
public void startSecondActivity(View view){
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
Upvotes: 2
Reputation: 75788
Another way,Button in an XML graphical layout and implement a button click listener together with an onclick
method. The onclick
method will start a new activity using an intent.
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
Now go to your Class
// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.buttonClick);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(getApplicationContext(),AddYourNewActivityName.class);
startActivity(myIntent);
}
});
Hope this helps .
For details http://www.vogella.com/tutorials/AndroidIntent/article.html
Upvotes: 2
Reputation: 31
Set the button's onClick to a method that uses Intent.
XML:
<Button
android:onClick="to_different_page"/>
</Button>
Java method:
public void to_different_page(View view) {
Intent intent = new Intent(this, browse_post_activity.class);
startActivity(intent);
}
Upvotes: 2
Reputation: 18489
You can do something like this:
import android.view.View;
/** Called when the activity is first created. */
public class YourActivity extends Activity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.your_button_id);
button.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent(your_present_activity.this, target_activity.class);
startActivity(intent);
}
}
Upvotes: 4
Reputation: 128428
For moving from one Activity to another Activity, you need to use Intent.
For example, you are having one activity "A" contains button and second activity "B", and you want to move from "A" to "B" then write:
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
Practical and informative examples for the Intent are here: Android Intents - Tutorial
Upvotes: 3