How to create alertdialog with THEME_DEVICE_DEFAULT_DARK, THEME_DEVICE_DEFAULT_LIGHT, THEME_HOLO_DARK, THEME_HOLO_LIGHT, THEME_TRADITIONAL.
In this tutorial we are creating simple alert dialog with yes-no button with five all different themes.Every button opens alert dialog with different theme. So here is the complete step by step tutorial for Change AlertDialog theme in android programmatically.
How to Change AlertDialog theme in android programmatically.
Code for MainActivity.java file.
package com.alertdialogthemes_android_examples.com; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button button1,button2,button3,button4,button5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button)findViewById(R.id.button1); button2 = (Button)findViewById(R.id.button2); button3 = (Button)findViewById(R.id.button3); button4 = (Button)findViewById(R.id.button4); button5 = (Button)findViewById(R.id.button5); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialogTheme1(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialogTheme2(); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialogTheme3(); } }); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialogTheme4(); } }); button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialogTheme5(); } }); } public void AlertDialogTheme1() { // TODO Auto-generated method stub AlertDialog.Builder Alertbuilder = new AlertDialog.Builder( MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK); Alertbuilder.setTitle("This is DEFAULT DARK theme."); Alertbuilder.setMessage("Your Question Display Here"); Alertbuilder.setCancelable(false); Alertbuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected YES.", Toast.LENGTH_LONG).show(); } }); Alertbuilder.setNegativeButton("N0", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected NO.", Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = Alertbuilder.create(); dialog.show(); } public void AlertDialogTheme2() { // TODO Auto-generated method stub AlertDialog.Builder Alertbuilder = new AlertDialog.Builder( MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); Alertbuilder.setTitle("This is DEFAULT LIGHT theme."); Alertbuilder.setMessage("Your Question Display Here"); Alertbuilder.setCancelable(false); Alertbuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected YES.", Toast.LENGTH_LONG).show(); } }); Alertbuilder.setNegativeButton("N0", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected NO.", Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = Alertbuilder.create(); dialog.show(); } public void AlertDialogTheme3() { // TODO Auto-generated method stub AlertDialog.Builder Alertbuilder = new AlertDialog.Builder( MainActivity.this, AlertDialog.THEME_HOLO_DARK); Alertbuilder.setTitle("This is HOLO DARK theme."); Alertbuilder.setMessage("Your Question Display Here"); Alertbuilder.setCancelable(false); Alertbuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected YES.", Toast.LENGTH_LONG).show(); } }); Alertbuilder.setNegativeButton("N0", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected NO.", Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = Alertbuilder.create(); dialog.show(); } public void AlertDialogTheme4() { // TODO Auto-generated method stub AlertDialog.Builder Alertbuilder = new AlertDialog.Builder( MainActivity.this, AlertDialog.THEME_HOLO_LIGHT); Alertbuilder.setTitle("This is HOLO LIGHT theme."); Alertbuilder.setMessage("Your Question Display Here"); Alertbuilder.setCancelable(false); Alertbuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected YES.", Toast.LENGTH_LONG).show(); } }); Alertbuilder.setNegativeButton("N0", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected NO.", Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = Alertbuilder.create(); dialog.show(); } public void AlertDialogTheme5() { // TODO Auto-generated method stub AlertDialog.Builder Alertbuilder = new AlertDialog.Builder( MainActivity.this, AlertDialog.THEME_TRADITIONAL); Alertbuilder.setTitle("This is TRADITIONAL theme."); Alertbuilder.setMessage("Your Question Display Here"); Alertbuilder.setCancelable(false); Alertbuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected YES.", Toast.LENGTH_LONG).show(); } }); Alertbuilder.setNegativeButton("N0", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You Have Selected NO.", Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = Alertbuilder.create(); dialog.show(); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.alertdialogthemes_android_examples.com.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="78dp" android:text="AlertDialog Theme 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:text="AlertDialog Theme 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" android:text="AlertDialog Theme 3" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button3" android:layout_centerHorizontal="true" android:text="AlertDialog Theme 4" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button4" android:layout_centerHorizontal="true" android:text="AlertDialog Theme 5" /> </RelativeLayout>
Screenshots: