Android - How to show alert dialog inside linearlayout in a Layout file? - android

I have a linearlayout in a layout file. I need to show alertdialog inside that linearlayout. How can i achieve this?
I tried this but it doesn't work correctly
llChartPopup.getLocationInWindow(chartPositions)//llChartPopup is sub linearlayout inside my layout file
var viewGroup = findViewById<ContentFrameLayout>(android.R.id.content)
var dialogView = LayoutInflater.from(this).inflate(R.layout.pass_sa_chart_popup, viewGroup, false)
var builder = AlertDialog.Builder(this)
builder.setView(dialogView)
alertDialog = builder.create()
alertDialog.window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
alertDialog.window.setGravity(Gravity.TOP)
var wmlp = alertDialog.window.attributes
wmlp.x = chartPositions[0]
wmlp.y = chartPositions[1]
alertDialog.show()

val alertDialog = AlertDialog.Builder(context)
val customView = LayoutInflater.from(context).inflate(R.layout.dialog_custom_layout, null)
//Your views inside dialog
val btnClose = customView.yourView
alertDialog.setView(customView)
val customDialog = alertDialog.create()
customDialog.show()

Related

how to make function simpler

I tried to follow solid principles and want to make my code better. So I want to separate the function that may not be used in some problems.
this is the code that I already to separate but still redundant
private lateinit var dialogView : View
private lateinit var b : AlertDialog
override fun modalDialog(
view:Int,
listener: bodyModalDialog, submitButtonModalDialog: submitButtonModalDialog,
btnSubmit:Int,
btnCancel:Int) {
val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
val inflater = this.layoutInflater
dialogView = inflater.inflate(view, null)
dialogBuilder.setView(dialogView)
dialogBuilder.setCancelable(false)
var uiSubmitButton = dialogView.findViewById<View>(btnSubmit)
var uiCancelButton = dialogView.findViewById<View>(btnCancel)
listener.bodyDialog(dialogView)
uiSubmitButton.setOnClickListener {
submitButtonModalDialog.submitButton(dialogView)
b.cancel()
}
uiCancelButton.setOnClickListener {
b.cancel()
}
b = dialogBuilder.create()
b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
b.show()
}
override fun modalDialogNoBody(
view:Int,
listener: submitButtonModalDialog,
btnSubmit:Int,
btnCancel:Int) {
val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
val inflater = this.layoutInflater
dialogView = inflater.inflate(view, null)
dialogBuilder.setView(dialogView)
dialogBuilder.setCancelable(false)
var uiSubmitButton = dialogView.findViewById<View>(btnSubmit)
var uiCancelButton = dialogView.findViewById<View>(btnCancel)
uiSubmitButton.setOnClickListener {
listener.submitButton(dialogView)
b.cancel()
}
uiCancelButton.setOnClickListener {
b.cancel()
}
b = dialogBuilder.create()
b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
b.show()
}
from that code I tried to separate when the problem need the body and some of them not. So if i let them can access the body it will be just empty so I separate like that. But, the code are redundant becouse it write the same code with one of them there is no body. So i tried to make one of them just take the other code and add body procedure, but I have no idea how to do that. I tried to make an object so when it called true then means there is no body and if false there is body like this code
object Modal{
fun isNoBody(view:Int,
listener: submitButtonModalDialog,
btnSubmit:Int,
btnCancel:Int):Boolean{
val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
val inflater = this.layoutInflater
dialogView = inflater.inflate(view, null)
dialogBuilder.setView(dialogView)
dialogBuilder.setCancelable(false)
var uiSubmitButton = dialogView.findViewById<View>(btnSubmit)
var uiCancelButton = dialogView.findViewById<View>(btnCancel)
uiSubmitButton.setOnClickListener {
listener.submitButton(dialogView)
b.cancel()
}
uiCancelButton.setOnClickListener {
b.cancel()
}
b = dialogBuilder.create()
b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
b.show()
return true
}
}
but it won't work because AlertDialog and View can't be called there. even if I change them to be public. please help
I got my answer. So what I do is I make button and body with two different interfaces. and because if want to use body you need button automatically so I just make it call when it needed only like this
override fun modalDialog(view:Int,
listener: SubmitButtonModalDialog,
btnSubmit:Int,
btnCancel:Int) {
val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
val inflater = this.layoutInflater
dialogView = inflater.inflate(view, null)
dialogBuilder.setView(dialogView)
dialogBuilder.setCancelable(false)
var uiSubmitButton = dialogView.findViewById<View>(btnSubmit)
var uiCancelButton = dialogView.findViewById<View>(btnCancel)
uiSubmitButton.setOnClickListener {
listener.submitButton(dialogView)
b.cancel()
}
uiCancelButton.setOnClickListener {
b.cancel()
}
b = dialogBuilder.create()
b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
b.show()
}
override fun modalDialogBody(listener: BodyModalDialog) {
listener.bodyDialog(dialogView)
}

Android Kotlin AlertDialog wrap_content not working

I'm trying to create a view for use in an alert and I can't get wrap_content to work in an alert dialog. I've tried both a LinearLayout and a ConstraintLayout with the same results.
I've tried several things including these from :
val selectView = layoutInflater.inflate(R.layout.font_size_sel, null)
val builder = androidx.appcompat.app.AlertDialog.Builder(this)
builder.setView(selectView)
val dialog = builder.create()
dialog.window!!
.setLayout(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT)
dialog.show()
and
val selectView = layoutInflater.inflate(R.layout.font_size_sel, null)
val builder = androidx.appcompat.app.AlertDialog.Builder(this)
builder.setView(selectView)
val dialog = builder.create()
dialog.setOnShowListener {
dialog.window?.setLayout(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
dialog.show()
and I've from here I've tried:
val selectView = layoutInflater.inflate(R.layout.font_size_sel, null)
val builder = androidx.appcompat.app.AlertDialog.Builder(this)
builder.setView(selectView)
val dialog = builder.create()
val lp = dialog.window!!.attributes
lp.width = WindowManager.LayoutParams.WRAP_CONTENT
lp.height = WindowManager.LayoutParams.WRAP_CONTENT
dialog.window!!.attributes = lp
dialog.show()
I've tried putting the dialog.show() both before and after setting wrap_content in the code above.
I've tried both ConstraintLayout and LinearLayout with the same results.
Both the layout and the textview have wrap_contents for both the width and height.
This is what the design window from AndroidStudio's layout editor says the window should look like:
And this is a snapshot of the device with the debugger's "show padding..." turned on.
I'm stuck. Any ideas out there?? What am I doing wrong?
Thanks
Steve S.

Button in dialog always return null in onStart() in a Fragment

I have Dialog in Fragment. when I click on the OK button inside the dialog, I get null object reference error.
fun showDialog(){
//get Dialog : Numbers
val dialogView = LayoutInflater.from(context)
.inflate(R.layout.dialog_b, null)
//AlertDialog builder
val dialogBuilder = AlertDialog.Builder(context)
.setView(dialogView)
//Show Dialog
val alertDialog = dialogBuilder.show()
//setOnClickListener(android.view.View$OnClickListener)' on a null object reference
dialogView.btn_ok.setOnClickListener {
Log.d("Clicked", et_one.text.toString())
alertDialog.dismiss()
}
}
Logcat
java.lang.IllegalStateException: et_one must not be null at com.example.algorithmsapp.AlgorithmsFragments.BFragment$onStart$1$1.onClick(BFragment.kt:63)
You need to intialise Edittext here to fetch value from edittext. Add below line your code
var et_one: EditText
var btn_ok: Button
et_one= layoutInflateView.findViewById(R.id.et_one)
btn_ok= layoutInflateView.findViewById(R.id.btn_ok)
So Your Code Look Like This:
fun showDialog(){
//get Dialog : Numbers
val dialogView = LayoutInflater.from(context)
.inflate(R.layout.dialog_b, null)
//AlertDialog builder
val dialogBuilder = AlertDialog.Builder(context)
.setView(dialogView)
//Show Dialog
val alertDialog = dialogBuilder.show()
var et_one: EditText
var btn_ok: Button
et_one= layoutInflateView.findViewById(R.id.et_one)
btn_ok= layoutInflateView.findViewById(R.id.btn_ok)
//setOnClickListener(android.view.View$OnClickListener)' on a null object reference
dialogView.btn_ok.setOnClickListener {
Log.d("Clicked", et_one.text.toString())
alertDialog.dismiss()
}
}

why Dialog doesn't show in Kotlin?

I want to create a Dialog window when I click the Floating Action Button. But, when I click the button, just appears the Toast message.
This is what I've tried so far:
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
val users = ArrayList<User>()
users.add(User("John", "USA"))
val adapter = CustomAdapter(users)
recyclerView.adapter = adapter
fab.setOnClickListener {
val dialog = Dialog(this)
Toast.makeText(this, "It's working...", Toast.LENGTH_LONG).show()
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(R.layout.dialog_add)
dialog.setTitle("Add person")
dialog.setCancelable(false)
val nameText = dialog.findViewById(R.id.name) as EditText
val addressText = dialog.findViewById(R.id.address) as EditText
val btnAdd = dialog.findViewById(R.id.btn_ok) as Button
val btnCancel = dialog.findViewById(R.id.btn_cancel) as Button
btnAdd.setOnClickListener{
users.add(User(nameText.text.toString(), addressText.text.toString()))
adapter.notifyDataSetChanged()
dialog.dismiss()
}
btnCancel.setOnClickListener {
dialog.dismiss()
}
}
}
How can I change the code so it show the Dialog Window when I click the FAB?
UPDATE:
You guys're right! It worked just fine after I put dialog.show(). Thank you.
You forgot to invoke show() on dialog.
dialog.show()
You need to call dialog.show(). Just call it anywhere below dialog.setCancelable(false).
After create dialog , we need to call show method to show dialog on screen
Add This line after create dialog dialog.show()
Please replace or add this code
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
val users = ArrayList<User>()
users.add(User("John", "USA"))
val adapter = CustomAdapter(users)
recyclerView.adapter = adapter
fab.setOnClickListener {
val dialog = Dialog(this)
Toast.makeText(this, "It's working...", Toast.LENGTH_LONG).show()
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(R.layout.dialog_add)
dialog.setTitle("Add person")
dialog.setCancelable(false)
val nameText = dialog.findViewById(R.id.name) as EditText
val addressText = dialog.findViewById(R.id.address) as EditText
val btnAdd = dialog.findViewById(R.id.btn_ok) as Button
val btnCancel = dialog.findViewById(R.id.btn_cancel) as Button
btnAdd.setOnClickListener{
users.add(User(nameText.text.toString(), addressText.text.toString()))
adapter.notifyDataSetChanged()
dialog.dismiss()
}
btnCancel.setOnClickListener {
dialog.dismiss()
}
//add this line
//Call show() method to show dialog
dialog.show()
}
}
You have to invoke show() like the following.
//...
dialog.setTitle("Add person")
dialog.setCancelable(false)
// ...
btnAdd.setOnClickListener{
//...
}
btnCancel.setOnClickListener {
dialog.dismiss()
}
//show dialog adding below line.
dialog.show();

Dismissing Anko Dialog in onClickListener

I would like to dismiss Anko Dialog when pressing the button defined in my customLayout
val dialog = alert {
val view = layoutInflater.inflate(R.layout.match_stats, null)
val closeButton = view.findViewById<ImageButton>(R.id.closeButton)
closeButton.setOnClickListener { _ -> dialog.dismiss()}
customView = view
}
dialog.show()
I tried above code, unfortunately, I can’t get a reference to dialog in my onClickListener. Do you have any idea how to solve it?
You could declare variable before and assign null:
var dialog: DialogInterface? = null
dialog = alert {
val view = layoutInflater.inflate(R.layout.match_stats, null)
val closeButton = view.findViewById<ImageButton>(R.id.closeButton)
closeButton.setOnClickListener { _ -> dialog?.dismiss()}
customView = view
}.show()
Of course now your dialog variable is muttable and optional.

Categories

Resources