Introduction:-

As we all know guys that default alert dialog is not that much eye catching and gorgeous so we all need to learn how to make a custom alert dialog. That's why am going to teach you how to make a custom alert dialog to look just awesome. The advantages of using custom alert dialog are that we will make only one class and will pass it to the main activity or to the other activities so that we do not have to write the single piece of code for every alert dialog which we use to do in default alert dialog. And we can also design a color of our own to make it look gorgeous and awesome.Here we are passing the messages to be displayed in the alert dialog to the class created for the alert dialog so that we need not create an alert dialog in every activity we just need to call that class to get displayed.So let's begin with the coding part.

Platform used:-
  • Android Studio

Procedure:-
  1. Create a new project of your appropriate name and get the project synced to code it.
  2. Now check whether the main XML file is of Relative layout or not if not then make it relative.
  3. Now we need to create two button widgets on pressing these buttons we are going to pass an appropriate message to the alert dialog class which we are going to create later.
  4. Now create those two buttons and assign an appropriate id's to those two buttons so that we will be able to specify it in Main activity class
  5. After assigning those two button widgets now it's time to create one layout file and one class file which is essential for the alert dialog because in the layout file we are going to create the UI for the alert dialog and in the java class file we are going to code it for the alert dialog.
  6. I've created the layout file of the name custom_layout inside that I have created a top bar of orange color and after that, I've created a Textview where the message is going to get displayed. So after assigning those two align it with your own style to make it look better.
  7. So after creating the layout file now its time to create the java class file to code the layout file and in my previous video tutorial I've explained you about the layout inflater which I have used it here to get the layout which is going to act like a fragment.
  8. So now create a java class file of your appropriate name and extend it with Dialogfragment and make sure that it is of v4.
  9. Inside this java file create a constructor with newInstance where we are going to receive the messages which are being passed from the Main activity class to be displayed in the custom alert dialog.
  10. Create an object of the class and make an object of Bundle and put the received message using the object of bundle and put string to a string variable which we are going to fetch and display it in the alert dialog and make sure to return the object of the class because we are going to get that in creating dialog.
  11. Now using ctrl + o get the function named as oncreateDialog which means that on creating an alert dialog what the app is going to do.
  12. Now get the arguments which we have set in the above constructor to a string variable to set the text in the layout file which we have assigned in the layout file.
  13. Now we have to declare the layout inflater which is going to inflate the layout which I have created earlier.
  14. Now declare the Textview and assign the id of the Textview in the custom layout class where we are going to put the message.
  15. Now set the text message and create a builder which is going to build the alert dialog and return the builder which is going to display in the form of the fragment.
  16. Now assign those two instances of the button which we have created in the main activity file and create a SetOnclickListener which going to listen when the button is going to get clicked.
  17. Inside the clickListener create the object of the class which is the class of alert dialog which we are going to trigger when the button is pressed and using that object show function is used to display i.e it is going to display the fragment to the user.
  18. Now we are finished with the coding part compile and if you have any query then comment it below. Thank you have a nice day.

Source code:-
package com.example.nigamasis.beginnertopro;



import android.app.AlertDialog;

import android.app.Dialog;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.v4.app.DialogFragment;

import android.support.v4.view.LayoutInflaterCompat;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.TextView;



/**

* Created by NIGAMASIS on 27-Mar-17.

*/



public class Customdialog extends DialogFragment {



LayoutInflater inflater;

View v;

TextView msg;



public static Customdialog newInstance(String msg)

{

Customdialog frag = new Customdialog();

Bundle args = new Bundle();

args.putString("message",msg);

frag.setArguments(args);

return frag;

}



@NonNull

@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {



String message = getArguments().getString("message");



inflater = getActivity().getLayoutInflater();

v = inflater.inflate(R.layout.custom_layout,null);



msg = (TextView)v.findViewById(R.id.msg);

msg.setText(message);



AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder.setView(v).setPositiveButton("Ok",null);

return builder.create();

}

}


package com.example.nigamasis.beginnertopro;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;



public class MainActivity extends AppCompatActivity {



Button btn1,btn2;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



btn1 = (Button)findViewById(R.id.btn_1);

btn2 = (Button)findViewById(R.id.btn_2);



btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Customdialog newFragment = Customdialog.newInstance("Say Hello");

newFragment.show(getSupportFragmentManager(),"dialog");

newFragment.setCancelable(false);

}

});



btn2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Customdialog newFragment = Customdialog.newInstance("Hello World");

newFragment.show(getSupportFragmentManager(),"dialog");

newFragment.setCancelable(false);

}

});



}

}

Xml Files:-


Full Source code Zip:-


Video tutorial:-


Thank you for watching this Video tutorial
Please like share and subscribe
Have aa nice day