Android Alert Dialog with custom layout
The following code will display an Android Alert Dialog with a custom layout. When the login button is clicked, the dialog will show up in the center of the screen. The dialog will have a title, and two EditText for email and password, alone with cancel and submit buttons.
MainActivity.kt
import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.View import android.widget.EditText import android.widget.Toast class MainActivity : AppCompatActivity() { private var etEmail: EditText? = null private var etPassword: EditText? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } fun login(view : View) { // Inflates the dialog with custom view val dialogView = LayoutInflater.from(this).inflate(R.layout.login_dialog, null) etEmail = dialogView.findViewById(R.id.et_email) etPassword = dialogView.findViewById(R.id.et_password) var title = "Login" val builder = AlertDialog.Builder(this) .setView(dialogView) .setTitle(title) .setPositiveButton("Submit", null) .setNegativeButton("Cancel", null) val dialog = builder.show() dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ dialog.dismiss() Toast.makeText(applicationContext, "Login is submitted, verify the credential and log the user into the app", Toast.LENGTH_SHORT).show() } ) } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myapplication.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:onClick="login" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
login_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"> <EditText android:id="@+id/et_email" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:ems="10" android:hint="Email" android:inputType="textEmailAddress" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/et_password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:ems="10" android:hint="Password" android:inputType="textPassword" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/et_email" /> </android.support.constraint.ConstraintLayout>
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts