How to create a Custom Dialog box in android?

后端 未结 22 2761
囚心锁ツ
囚心锁ツ 2020-11-21 07:06

I want to create a custom dialog box like below

\"enter

I have tried the foll

相关标签:
22条回答
  • 2020-11-21 07:36
    public static void showCustomAlertDialog(Context context, String name,
                String id, String desc, String fromDate, String toDate,
                String resions) {
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog, null);
            alertDialogBuilder.setView(view);
            alertDialogBuilder.setCancelable(false);
            final AlertDialog dialog = alertDialogBuilder.create();
            dialog.show();
            txt_empId = (TextView) view.findViewById(R.id.txt_dialog_empcode);
            txt_empName = (TextView) view.findViewById(R.id.txt_dialog_empname);
            txt_desc = (TextView) view.findViewById(R.id.txt_dialog_desc);
            txt_startDate = (TextView) view.findViewById(R.id.txt_dialog_startDate);
            txt_resions = (TextView) view.findViewById(R.id.txt_dialog_endDate);
            txt_empId.setTypeface(Utils.setLightTypeface(context));
            txt_empName.setTypeface(Utils.setLightTypeface(context));
            txt_desc.setTypeface(Utils.setLightTypeface(context));
            txt_startDate.setTypeface(Utils.setLightTypeface(context));
            txt_resions.setTypeface(Utils.setLightTypeface(context));
    
            txt_empId.setText(id);
            txt_empName.setText(name);
    
            txt_desc.setText(desc);
            txt_startDate.setText(fromDate + "\t to \t" + toDate);
            txt_resions.setText(resions);
    
    
    
            btn_accept = (Button) view.findViewById(R.id.btn_dialog_accept);
            btn_reject = (Button) view.findViewById(R.id.btn_dialog_reject);
            btn_cancel = (Button) view.findViewById(R.id.btn_dialog_cancel);
            btn_accept.setTypeface(Utils.setBoldTypeface(context));
            btn_reject.setTypeface(Utils.setBoldTypeface(context));
            btn_cancel.setTypeface(Utils.setBoldTypeface(context));
    
            btn_cancel.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
    
                }
            });
    
        }
    
    0 讨论(0)
  • 2020-11-21 07:36

    Create alert Dialog layout something like this

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:text="Custom Alert Dialog"
            android:layout_height="40dp">
        </Button>
    </LinearLayout>
    

    and Add below code on your Activity class

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        LayoutInflater inflate = LayoutInflater.from(this);
        alertView = inflate.inflate(R.layout.your_alert_layout, null);
        Button btn= (Button) alertView.findViewById(R.id.btn);
    
        showDialog();
      }
    
     public void showDialog(){
            Dialog alertDialog = new Dialog(RecognizeConceptsActivity.this);
            alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            alertDialog.setContentView(alertView);
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            alertDialog.show();
        }
    
    0 讨论(0)
  • 2020-11-21 07:38

    I am posting the kotlin code that I am using and it works fine for me. you can also set click listener for dialog buttons.

    this is my XML code:

    layout_custom_alert_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:layoutDirection="ltr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <View
            android:id="@+id/view6"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="@color/colorPrimary" />
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/view6"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp">
    
    
            <TextView
                android:id="@+id/txt_alert_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                tools:text="are you sure?"
                android:textAlignment="center"
                android:textColor="@android:color/black"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
    
            <Button
                android:id="@+id/btn_alert_positive"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView2"
                android:layout_marginTop="8dp"
                android:background="@android:color/transparent"
                tools:text="yes"
                android:textColor="@color/colorPrimaryDark"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/btn_alert_negative"
                app:layout_constraintTop_toBottomOf="@+id/txt_alert_title" />
    
            <Button
                android:id="@+id/btn_alert_negative"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:background="@android:color/transparent"
                tools:text="no"
                android:textColor="@color/colorPrimaryDark"
                app:layout_constraintEnd_toStartOf="@+id/btn_alert_positive"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/txt_alert_title" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </RelativeLayout>
    

    mAlertDialog.kt

    class mAlertDialog(context: Context) {
    
        private val btn_positive : Button
        private val btn_negative : Button
        private val txt_alert_title : TextView
        private val dialog : AlertDialog
        init {
            val view = LayoutInflater.from(context).inflate(R.layout.layout_custom_alert_dialog,null)
    
            val dialog_builder = AlertDialog.Builder(context)
            dialog_builder.setView(view)
    
            btn_negative = view.findViewById(R.id.btn_alert_negative)
            btn_positive = view.findViewById(R.id.btn_alert_positive)
            txt_alert_title = view.findViewById(R.id.txt_alert_title)
    
            dialog = dialog_builder.create() 
        }
    
        fun show()
        {
            dialog.show()
        }
    
        fun setPositiveClickListener(listener :onClickListener)
        {
            btn_positive.setOnClickListener { v ->
                listener.onClick(btn_positive)
                dialog.dismiss()
            }
        }
    
        fun setNegativeClickListener(listener: onClickListener)
        {
            btn_negative.setOnClickListener { v ->
                listener.onClick(btn_negative)
                dialog.dismiss()
            }
        }
    
        fun setPoitiveButtonText(text : String)
        {
            btn_positive.text = text
        }
    
    
        fun setNegativeButtonText(text : String)
        {
            btn_negative.text = text
        }
    
        fun setAlertTitle(title : String)
        {
            txt_alert_title.text = title
        }
    }
    

    interface for click listeners:

    onClickListener.kt

    interface onClickListener{
        fun onClick(view : View)
    }
    

    Sample Usage

    val dialog = mAlertDialog(context)
                    dialog.setNegativeButtonText("no i dont")
                    dialog.setPoitiveButtonText("yes is do")
                    dialog.setAlertTitle("do you like this alert dialog?")
    
                    dialog.setPositiveClickListener(object : onClickListener {
                        override fun onClick(view: View) {
                            Toast.makeText(context, "yes", Toast.LENGTH_SHORT).show()
                        }
                    })
    
                    dialog.setNegativeClickListener(object : onClickListener {
                        override fun onClick(view: View) {
                            Toast.makeText(context, "no", Toast.LENGTH_SHORT).show()
                        }
                    })
    
                    dialog.show()
    

    I hope, this will help you!

    0 讨论(0)
  • 2020-11-21 07:38

    Following is the code for creating custom view dialog with kotlin. Following is the dialog layout file

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical" android:layout_width="300dp"
        android:layout_height="400dp">
    
        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    creating dialog and updating the text in the text view

    val dialog = Dialog(activity!!)
    dialog.setContentView(R.layout.my_dialog_layout)
    dialog.tvTitle.text = "Hello World!!"
    dialog.show()
    
    0 讨论(0)
  • 2020-11-21 07:39

    It's a class for Alert Dialog so that u can call the class from any activity to reuse the code.

    public class MessageOkFragmentDialog extends DialogFragment {
    Typeface Lato;
    String message = " ";
    String title = " ";
    int messageID = 0;
    
    public MessageOkFragmentDialog(String message, String title) {
        this.message = message;
        this.title = title;
    }
    
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
        LayoutInflater inflater = getActivity().getLayoutInflater();
    
        View convertview = inflater.inflate(R.layout.dialog_message_ok_box, null);
    
    
        Constants.overrideFonts(getActivity(), convertview);
        Lato = Typeface
                .createFromAsset(getActivity().getAssets(), "font/Lato-Regular.ttf");
    
    
        TextView textmessage = (TextView) convertview
                .findViewById(R.id.textView_dialog);
    
        TextView textview_dialog_title = (TextView) convertview.findViewById(R.id.textview_dialog_title);
    
        textmessage.setTypeface(Lato);
        textview_dialog_title.setTypeface(Lato);
    
    
    
        textmessage.setText(message);
        textview_dialog_title.setText(title);
    
    
    
        Button button_ok = (Button) convertview
                .findViewById(R.id.button_dialog);
        button_ok.setTypeface(Lato);
    
        builder.setView(convertview);
        button_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dismiss();
    
            }
        });
    
    
        return builder.create();
    
    }
    }
    

    Xml file for the same is:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:gravity="center_vertical|center"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/blue_color"
                android:gravity="center_horizontal"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/textview_dialog_title"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:textColor="@color/white_color"
                    android:textSize="@dimen/txtSize_Medium" />
    
    
            </LinearLayout>
    
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/txt_white_color" />
    
            <TextView
                android:id="@+id/textView_dialog"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/margin_20"
                android:textColor="@color/txt_gray_color"
                android:textSize="@dimen/txtSize_small" />
    
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/txt_white_color"
                android:visibility="gone"/>
    
            <Button
                android:id="@+id/button_dialog"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/margin_40"
                android:layout_gravity="center"
                android:background="@drawable/circular_blue_button"
    
                android:text="@string/ok"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="@dimen/margin_10"
                android:textColor="@color/txt_white_color"
                android:textSize="@dimen/txtSize_small" />
        </LinearLayout>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-21 07:41

    Full Screen Custom Alert Dialog Class in Kotlin

    1. Create XML file, same as you would an activity

    2. Create AlertDialog custom class

      class Your_Class(context:Context) : AlertDialog(context){
      
       init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setCancelable(false)
       }
      
       override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.your_Layout)
        val window = this.window
        window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                           WindowManager.LayoutParams.MATCH_PARENT)
      
        //continue custom code here
        //call dismiss() to close
       }
      }
      
    3. Call the dialog within the activity

      val dialog = Your_Class(this)
      //can set some dialog options here
      dialog.show()
      

    Note**: If you do not want your dialog to be full screen, delete the following lines

          val window = this.window
          window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                             WindowManager.LayoutParams.MATCH_PARENT)
    

    Then edit the layout_width & layout_height of your top layout within your XML file to be either wrap_content or a fixed DP value.

    I generally do not recommend using fixed DP as you would likely want your app to be adaptable to multiple screen sizes, however if you keep your size values small enough you should be fine

    0 讨论(0)
提交回复
热议问题