How to create a custom AlertDialog in Android Studio?

Please Share On:

You are reading my article, which means you are an android developer and want to create your own custom AlertDialog box.

You might be thinking that this is difficult coding. Don’t worry. I am here to help you. You just follow my procedure, you will end up designing a very beautiful custom AlertDialog box as below.

Step 1:

First, you need to design an XML file. Let design an XML file agreement_dialog.xml. It has

  1. Textview (for title) – Agreement
  2. Textview inside Scrollview. If your message is longer than your device height, you can scroll down to see all texts.
  3. 2 buttons: one for “Accept” and one for “Reject”

Now see an XML source code to design.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:background="@color/black"
        android:text="@string/dialog_title"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/dialog_title">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relativeLayout"
            android:background="@color/background"
            android:padding="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/agreementlayout"
                android:layout_gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/agreement1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/agreement"
                    android:textColor="@color/black"
                    android:textSize="25sp"/>

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/buttonlayout"
                android:layout_marginTop="10dp"
                android:layout_gravity="center"
                android:orientation="horizontal"
                android:layout_below="@+id/agreementlayout">

                <Button
                    style="?android:attr/actionButtonStyle"
                    android:id="@+id/button_accept"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.50"
                    android:textColor="@color/white"
                    android:textSize="30sp"
                    android:background="@color/green"
                    android:text="@string/accept"
                    android:textStyle="bold|italic"/>

                <Button
                    style="?android:attr/actionButtonStyle"
                    android:id="@+id/button_reject"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.50"
                    android:textColor="@color/white"
                    android:textSize="30sp"
                    android:background="@color/watermelonRed"
                    android:text="@string/reject"
                    android:textStyle="bold|italic"/>

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

In the above agreement_dialog.xml file, you may have noticed that the text and colors are not written directly. They are defined under strings.xml and colors.xml files and provide the reference.

Now, let work on your strings.xml

</resources>
    <string name="dialog_title">Agreement</string>
    <string name="agreement">Write Your Agreement Here </string>
    <string name="accept">Accept</string>
    <string name="reject">Reject</string>
</resources>

Now colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>  
    <color name="background">#CED6E0</color>
    <color name="white">#ffffff</color>
    <color name="black">#000000</color>
    <color name="green">#006622</color>
    <color name="watermelonRed">#EB3B5A</color>
</resources>


Step 2:

Create a button on your activity.xml Whenever the button is clicked, an AlertDialogue button should open.

Here is a design for your button XML file.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:id="@+id/alertdialog"
    android:orientation="vertical">
  
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/openalertdialog"
        android:text="@string/open_alertdialog"
     />
</LinearLayout>

Now, add one more string in your strings.xml file.

<string name="open_alertdialog">Show AlertDialog</string>


Step 3:

Open custom AlertDialog on clicked on the activity button.

 openalertdialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(Activity.this, "Collect clicked", Toast.LENGTH_SHORT).show();
                final AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);

                // set the custom layout
                final View customLayout = getLayoutInflater().inflate(R.layout.agreement_dialog, null);
                builder.setView(customLayout);

                //set button accept and reject onclicklistener
                btnaccept = customLayout.findViewById(R.id.button_accept);
                btnreject = customLayout.findViewById(R.id.button_reject);

                btnaccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //If accept, go to accept activity
                        Intent intent_accept = new Intent(Activity.this, Accept.class);
                        startActivity(intent_accept); 
                    }
                });

                btnreject.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                         //If reject, go to reject activity
                        Intent intent_reject = new Intent(Activity.this, Reject.class);
                        startActivity(intent_reject); 
                    }
                });

                AlertDialog alert = builder.create();
                alert.show();
        });


Output:

That’s it. Thank you for reading my article.



You may interest in the following topics:

Where does my database store in Android Studio?

Please Share On:

This article explains to you where your project database store in android studio and how can you open your project database for testing purposes. It sounds hard, but when you follow my procedure, you will quickly learn to find out your android studio’s project database and can open it. You can figure out easily, whether your database is created or not, database table, and the columns and data are there or not. You need to find out things before you proceed ahead with your database project.

Let’s go step by step.

Step 1:

Run your android project either in real device or emulator, whichever you feel comfortable and keep working on.

Wait until the android virtual device runs properly.

Step 2:

Look at the bottom right corner of your android studio workspace. You will able to see “Device File Explore“.



Step 3:

Click on it and it will expand towards your workspace.

Device File Explorer Expand

Step 4:

Navigate to data -> data and expand it.



Step 5:

Open your recent android studio project which database you are looking.

android project database

Step 6:

Expand the database folder, you will able to see your database if created successfully while running an android emulator.

Step 7:

Right-click on your database and save your database file in your required location.

save android database



You may interest on the following topics:

How to create a new Android Virtual Device (AVD) in Android Studio?

Please Share On:

We use AVD to test our Android Application before launch into google play store and see how it looks like on different devices. AVD Manager is a tool that use to create and manage AVD for the Android Emulator. It helps us to test our application without real devices.

Here are the steps to create a new AVD.

Step 1: Launch Android Studio

Step 2: Select Tools-> AVD Manager.

It will open the below option to create a new virtual devices

Step 3: Click on “+ Create Virtual Device..”.

Step 4: Select Hardware

Now, the next step is to select the hardware device. You can select each category and your favorite device’s resolution. See the below diagram. I have chosen a phone in a category and Nexus 6. After you select your emulator, click on the Next.



Step 5: Select SDK Version

After you select AVD device, now your next step is to select SDK Version. If you have various SDK versions like kitkat, Lollipop and Marshmallow etc., select any one. Here, I have only installed Marshmallow SDK version. So, I choose marshmallow SDK version and click Next.

Step 6: Choose AVD Name

Here, you need to choose your AVD name. Write your AVD name in this format “Device Name API Version”. For example: Nexus 6 API 23. It will be easy for you what device are you using and which API level. Click Finish after writing AVD name.

Now, you can see that your Android Studio is creating your AVD emulator.

Step 7 : Android Virtual Device Created

Now, you can see your newly created AVD emulator is created successfully.

If you want to create more AVD, simply click the “+Create Virtual Device” and follow the step 4 to 6 again.

Run your application and see the output.

Android Emulator

That’s it. You have successfully created your AVD emulator.


AaA(irn(dodA



You may interest on the following topics:

Copyright @2023. All Right Reserved.


Social media & sharing icons powered by UltimatelySocial