Wednesday, December 18, 2019

How to create AlertDialog with theme in Android

How to create AlertDialog with theme in Android

Step 1. Creat a alayout activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#f4ee67"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change BG Color"
        />
</RelativeLayout>
Below all theme alertdialog in android
 THEME_DEVICE_DEFAULT_DARK
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
THEME_DEVICE_DEFAULT_LIGHT
 AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
 THEME_HOLO_DARK
AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_HOLO_DARK);
THEME_HOLO_LIGHT
AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_HOLO_LIGHT);
THEME_TRADITIONAL
AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_TRADITIONAL);
Step 2. Create a class MainActivity.class
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get reference of widgets from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Build an AlertDialog
                /*
                   AlertDialog.Builder(Context context, int theme)
                        Constructor using a context and theme for
                        this builder and the AlertDialog it creates.
                 */
                // THEME_DEVICE_DEFAULT_DARK
                /*AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);*/
                // THEME_DEVICE_DEFAULT_LIGHT
                /*AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);*/
                // THEME_HOLO_DARK
                /*AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_HOLO_DARK);*/
                // THEME_HOLO_LIGHT
                /*AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_HOLO_LIGHT);*/
                // THEME_TRADITIONAL
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_TRADITIONAL);
                // Set a message/question for alert dialog
                builder.setMessage("Want to change the background color?");
                // Specify the dialog is not cancelable
                builder.setCancelable(false);
                // Set a title for alert dialog
                builder.setTitle("THEME_TRADITIONAL");
                // Set the positive/yes button click listener
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click positive button
                        rl.setBackgroundColor(Color.parseColor("#FF60D4A4"));
                    }
                });
                // Set the negative/no button click listener
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click the negative button
                    }
                });
                // Set the neutral/cancel button click listener
                builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click the neutral button
                    }
                });
                AlertDialog dialog = builder.create();
                // Display the alert dialog on interface
                dialog.show();
            }
        });
    }
Result AlertDialog theme in Android

How to create AlertDialog with theme in Android

1 comment: