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
login_dialog.xml
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts