PopupWindow - Android Studio - Kotlin

In this example, we will display PopupWindow on button click. See the following example:

activity_main.xml

Add the following code in the activity_main.xml file.

<?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:layout_margin="15dp"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_popup_window"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textColor="#000000"
android:text="Popup Window"/>

<TextView
android:id="@+id/txt_display_info"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#000000"/>

</LinearLayout>

Create popup_window.xml:  res-> layout-> popup_window.xml

popup_window.xml

Add the following code in the popup_window.xml file.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9999"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_margin="10dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#757575">

<TextView
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info Form"
android:textColor="#FFF"
android:textSize="25dp"
android:gravity="center"/>

<EditText
android:id="@+id/edt_dia_first_name"
android:hint="Enter First Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>

<EditText
android:id="@+id/edt_dia_second_name"
android:hint="Enter Second Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>

<EditText
android:id="@+id/edt_dia_last_name"
android:hint="Enter Last Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center">

<Button
android:id="@+id/btn_dia_submit"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Submit"
android:textAllCaps="false"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btn_dia_cancel"
android:layout_margin="5dp"
android:layout_weight="1"
android:textAllCaps="false"
android:text="Cancel"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>

MainActivity.kt

Add the following code in the MainActivity.kt class.

package com.study.kotlinkatta

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.popup_window.view.*

class MainActivity : AppCompatActivity() {

lateinit var btn_popup_window: Button
lateinit var txt_display_info: TextView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

txt_display_info = findViewById(R.id.txt_display_info)
btn_popup_window = findViewById(R.id.btn_popup_window)
btn_popup_window.setOnClickListener {

initiatePopupWindow()
}
}

private fun initiatePopupWindow() {
val layoutInflater = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val popView: View = layoutInflater.inflate(R.layout.popup_window, null) // popup xml
val popupWindow: PopupWindow
popupWindow = PopupWindow(popView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT,true)
popupWindow.showAtLocation(popView, Gravity.CENTER, 0, 0)

val btn_dia_submit = popView.findViewById(R.id.btn_dia_submit) as Button
btn_dia_submit.setOnClickListener {

val str_first_name = popView.edt_dia_first_name.text.toString()
val str_second_name = popView.edt_dia_second_name.text.toString()
val str_last_name = popView.edt_dia_last_name.text.toString()

if(str_first_name.equals("") || str_second_name.equals("") || str_last_name.equals("")){
Toast.makeText(this,"Please enter details.",Toast.LENGTH_SHORT).show()
}else {
popupWindow.dismiss()
txt_display_info.setText("Info:-" + "\nFirst Name:" + str_first_name + "\nSecond Name: " + str_second_name + "\nLast Name: " + str_last_name)
}
}

val btn_dia_cancel = popView.findViewById(R.id.btn_dia_cancel) as Button
btn_dia_cancel.setOnClickListener {
popupWindow.dismiss()
}
}
}

Output:



Comments