Latest News

Create Alertdialog With Custom Layout Programmatically (Kotlin)

How to create Create AlertDialog With Custom Layout Programmatically (Kotlin)?

     DESCRIPTION     

In this tutorial we will create AlertDialog with custom layout programmatically in Java using Android Studio IDE. We will create a button to show that AlertDialog and show output in a TextView. You can also display this information anywhere or save at some place like shared preferences , files , sqlite etc.
I've added two TextViews and Two EditTexts with LinearLayout.
Create AlertDialog With Custom Layout Programmatically (Kotlin)


     VIDEO     


     SOURCE CODE     

Step 1: Create a new Project or open new project

Step 2: Code

activity_main.xml
<?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="match_parent"android:orientation="vertical"android:gravity="center"tools:context=".MainActivity"><!--button to show alert dialog--><Buttonandroid:id="@+id/showAlertDialogBtn"android:text="Show Alert Dialog"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!--textview to show input values--><TextViewandroid:id="@+id/txtView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"android:textSize="30sp"/></LinearLayout>

MainActivity.kt
package com.blogspot.atifsoftwares.alertdialogcustomlayoutprogramaticallyimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.support.v7.app.AlertDialogimport android.util.TypedValueimport android.widget.Buttonimport android.widget.EditTextimport android.widget.LinearLayoutimport android.widget.TextViewclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//buttonval mShowBtn = findViewById<Button>(R.id.showAlertDialogBtn)//textviewval mTextView = findViewById<TextView>(R.id.txtView)//button click to show alert dialogmShowBtn.setOnClickListener {val mBuilder = AlertDialog.Builder(this)val mLayout  = LinearLayout(this)val mTvName  = TextView(this)val mTvEmail = TextView(this)val mEtName  = EditText(this)val mEtEmail = EditText(this)mTvName.text  = " Enter Name:"mTvEmail.text = " Enter Email:"mTvName.setTextSize(TypedValue.COMPLEX_UNIT_SP , 16f)mTvEmail.setTextSize(TypedValue.COMPLEX_UNIT_SP , 16f)mEtName.setSingleLine()mEtEmail.setSingleLine()mEtName.hint  = "Name"mEtEmail.hint = "Email"mLayout.orientation = LinearLayout.VERTICALmLayout.addView(mTvName)mLayout.addView(mEtName)mLayout.addView(mTvEmail)mLayout.addView(mEtEmail)mLayout.setPadding(50 , 40 , 50 , 10)mBuilder.setView(mLayout)//set positive button to alert dialogmBuilder.setPositiveButton("Done"){dialogInterface , i ->//get text from edit textsval name = mEtName.text.toString()val email = mEtEmail.text.toString()//set text to textViewmTextView.text = "Name: "+ name + "\nEmail: " + email}//set neutral/cancel buttonmBuilder.setNeutralButton("Cancel"){dialogInterface , i ->dialogInterface.dismiss()}//show dialogmBuilder.create().show()}}}

Step 3: Run Project
Output

0 Response to "Create Alertdialog With Custom Layout Programmatically (Kotlin)"