Using buttons in Android
Screen change using buttons
Create two activities.
Here I'm using MainActivity(1st Screen) and the FunctionActivity(2nd Screen).
Add a button to the splash activity layout and give a method name to the onClick action.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Function"
android:id="@+id/button3"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:width="150dp"
android:onClick="fdemo"/>
</Button>
Then write the following method in the MainActivity
public void fdemo (View v){
Intent i= new Intent(MainActivity.this, Function.class);
startActivity(i);
}
Adding About message button(Using toast)
Add a button to the activity layout and give a method name to the onClick action
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:id="@+id/button"
android:onClick="about"
android:background="#e2dddd"
android:width="150dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_marginTop="171dp" />
<Button
Then on the Activity write the following code inside the parent class
public void about(View v){
Toast.makeText(this, "about",
Toast.LENGTH_LONG).show();
}
Adding Exit button with a prompt message
Add a button to the activity layout and set the onClick action to "exit".
Then write the following code on the activity
You can reduce the size of the above code by replacing "null" for the below part.
Then the code will be as follows.
Then write the following code on the activity
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"
android:id="@+id/button2"
android:onClick="exit"
android:layout_marginTop="77dp"
android:width="150dp"
android:background="#e2dddd"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
</Button>
public void exit(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
You can reduce the size of the above code by replacing "null" for the below part.
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}
Then the code will be as follows.
public void exit(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.setNegativeButton("No", null);
AlertDialog alert = builder.create();
alert.show();
}
Adding Exit button without a prompt message
Add a button to the activity layout and set the onClick action to "exit".
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"
android:id="@+id/button2"
android:onClick="exit"
android:layout_marginTop="77dp"
android:width="150dp"
android:background="#e2dddd"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
</Button>
Then write the following code on the activity
public void exit(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Comments
Post a Comment