Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Strings
Open res\values\strings.xml file and add new string as below:
<resources>
<string name="app_name">Learn Android with Real Apps</string>
<string name="show_dialog">Show Dialog</string>
<string name="confirm">Confirm</string>
<string name="are_you_sure">Are you sure?</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
</resources>
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonShowDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/show_dialog" />
</LinearLayout>
Main Activity Class
Add code to MainActivity.java in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button buttonShowDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
buttonShowDialog = findViewById(R.id.buttonShowDialog);
buttonShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonShowDialog_onClick(view);
}
});
}
private void buttonShowDialog_onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(R.string.confirm);
builder.setMessage(R.string.are_you_sure);
builder.setCancelable(false);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), R.string.ok, Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), R.string.cancel, Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
builder.show();
}
}
Structure of Project
Run App
Load Main Activity
Show Information Dialog When Click Button
Click OK Button in Confirm Dialog
Click Cancel Button in Confirm Dialog