
In this alert dialog android example, we learn how I make a customer alert box in android. In android use the latest language kotlin for android app development so that we make it a custom alert dialog android. You can customize it according to your requirement so that make an XML file for UI creation.
alert dialog android
Firstly we make an XML layout for customization and in this XML layout, we use the title of the alert dialog. Then description and the bottom of the alert make two buttons. also, we make the alert dialog corner rounded.
The alert dialog box XML file source code.
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="20dp" android:minHeight="230dp" android:background="@drawable/layout_bg" > <TextView android:id="@+id/alertTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="50dp" android:textColor="@color/colorPrimary" android:textSize="16sp" android:textStyle="bold" android:text="Alert !" android:padding="10dp" android:gravity="center" /> <View android:id="@+id/tView" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/gray_border"/> <TextView android:id="@+id/textMessage" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="100dp" android:textColor="@color/blackcolor" android:textSize="14sp" android:textStyle="bold" android:padding="8dp" android:gravity="center"/> <!-- <View--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="0.5dp"--> <!-- android:background="@color/gray_border"/>--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/alertButtoncancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="100dp" android:minHeight="35dp" style="@style/AlertButton" android:inputType="textCapWords" android:background="@drawable/button_red" android:layout_margin="5dp" android:paddingStart="5dp" android:paddingEnd="5dp" android:text="@string/cancel"/> <TextView android:id="@+id/alertButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="100dp" android:minHeight="35dp" style="@style/AlertButton" android:layout_margin="5dp" android:paddingStart="5dp" android:paddingEnd="5dp" android:text="Ok"/> </LinearLayout> </LinearLayout>
you can use the above code for making a UI layout and implement it in your kotlin file. Below code user where you want to show alert and pass your title and discretion in the method call.
Alert dialog android kotlin Code.
private fun alertDialog(title: String, cType: String) { val wDialog = Dialog(context!!) wDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // wDialog.setCanceledOnTouchOutside(false); wDialog.requestWindowFeature(Window.FEATURE_NO_TITLE) wDialog.setContentView(R.layout.alert_dialog) wDialog.setCanceledOnTouchOutside(false) wDialog.setCancelable(true) val message = wDialog.findViewById<TextView>(R.id.textMessage) message.text = cType val tilltle = wDialog.findViewById<TextView>(R.id.alertTitle) tilltle.text = title val cancel = wDialog.findViewById<TextView>(R.id.alertButtoncancel) cancel.visibility = View.GONE val ok = wDialog.findViewById<TextView>(R.id.alertButton) ok.setText("ok") ok.setOnClickListener(View.OnClickListener { wDialog.dismiss() }) cancel.setOnClickListener { wDialog.dismiss() } wDialog.show() val window = wDialog.window window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT) }
you just call the alertDialog method and pass the value of header and body in for showing alert with value.
Also, you can read more on new technologies like 4g LTE , IoT