Showing Android Wear style AlertDialog - android

I'm looking for a way to recreate the alert dialog in the Setting application of Android Wear:
Which is swipe to dismissable.
But instead, what I got is this:
Just a barebone Android dialog. How can I show the AlertDialog in the Settings.apk style? (Which I think must be default for Android Wear application)

I found no default way to do this, also setting a custom view to an AlertDialog did not look good. You can still try though, maybe a different Theme works.
What I did was create a new Activity and create my own layout which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
app:layout_box="all">
<TextView
android:id="#+id/tv_longtext"
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:gravity="bottom"
android:padding="5sp"
android:text="Ambient screen reduces battery life."
android:textSize="16sp" />
<TextView
android:id="#+id/tv_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:gravity="center_horizontal|top"
android:paddingBottom="15sp"
android:paddingTop="5sp"
android:text="Turn on?"
android:textSize="18sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5sp">
<android.support.wearable.view.CircledImageView
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:src="#drawable/ic_cross"
app:circle_color="#AFAFAF"
app:circle_radius="25dp"
app:circle_radius_pressed="20dp" />
<android.support.wearable.view.CircledImageView
android:id="#+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="#drawable/ic_tick"
app:circle_color="#0EB695"
app:circle_radius="25dp"
app:circle_radius_pressed="20dp" />
</FrameLayout>
</LinearLayout>
</android.support.wearable.view.BoxInsetLayout>
It looks just like the confirmation screen from the settings. Maybe it still needs some tweaks, but I think this is the way to go.

I had a similar problem and indeed I didn't find a default way to do this. I tried to use AlertDialogs for WearOs and they don't look well, because even if you pass them a custom view, the AlertDialog class crops the layout in some unexpected ways.
How I ended up solving the problem is using the Dialog class (AlertDialog's parent class) and passing it a custom view. The Dialog class doesn't alter the layout and you can attach the dialog to an activity's lifespan (which is the idea of dialogs, creating a custom activity doesn't fit with this requirement).
So you could create a function like this inside your activity:
private void showDialog() {
Dialog dialog = new Dialog(this);
View myLayout = getLayoutInflater().inflate(R.layout.my_layout_id, null);
Button positiveButton = myLayout.findViewById(R.id.positive_button);
positiveButton.setOnClickListener(
v -> {
/* Your action on positive button clicked. */
}
);
Button negativeButton = myLayout.findViewById(R.id.negative_button);
negativeButton.setOnClickListener(
v -> {
/* Your action on negative button clicked. */
}
);
dialog.setContentView(myLayout);
dialog.show();
}

I created a similar fragment with custom layout like this:
Kotlin code:
/**
* Created by nmbinh87#gmail.com on 4/12/21.
*/
class ConfirmationDialog private constructor() : DialogFragment(R.layout.confirmation_dialog) {
var listener: Listener? = null
var longMessage: String = ""
var shortMessage = ""
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tv_longtext.text = longMessage
tv_question.text = shortMessage
tv_longtext.visibility = if (longMessage.isEmpty()) View.GONE else View.VISIBLE
tv_question.visibility = if (shortMessage.isEmpty()) View.GONE else View.VISIBLE
btn_cancel.setSafeOnClickListener {
dismiss()
listener?.onCancel()
}
btn_ok.setSafeOnClickListener {
dismiss()
listener?.onConfirm()
}
}
override fun onStart() {
super.onStart()
val params = dialog?.window?.attributes
params?.width = WindowManager.LayoutParams.MATCH_PARENT
params?.height = WindowManager.LayoutParams.MATCH_PARENT
dialog?.window?.attributes = params as WindowManager.LayoutParams
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
}
open class Listener {
fun onCancel() {
}
open fun onConfirm() {
}
}
companion object {
fun show(
fm: FragmentManager,
longMessage: String = "",
shortMessage: String = "",
listener: Listener
) {
val fragment = ConfirmationDialog()
fragment.longMessage = longMessage
fragment.shortMessage = shortMessage
fragment.listener = listener
fragment.show(fm, fm::class.simpleName)
}
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout 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:background="#color/colorPrimaryDark"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
app:layout_box="all">
<TextView
android:id="#+id/tv_longtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:padding="5sp"
android:textColor="#color/white"
android:textSize="16sp"
tools:text="Ambient screen reduces battery life." />
<TextView
android:id="#+id/tv_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:layout_weight="1"
android:gravity="center"
android:paddingTop="5sp"
android:paddingBottom="15sp"
android:textColor="#color/white"
android:textSize="18sp"
tools:text="Turn on?" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5sp">
<android.support.wearable.view.CircledImageView
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:src="#drawable/ic_cc_clear"
app:circle_color="#AFAFAF"
app:circle_radius="25dp"
app:circle_radius_pressed="20dp" />
<android.support.wearable.view.CircledImageView
android:id="#+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="#drawable/ic_cc_checkmark"
app:circle_color="#0EB695"
app:circle_radius="25dp"
app:circle_radius_pressed="20dp" />
</FrameLayout>
</LinearLayout>
</android.support.wearable.view.BoxInsetLayout>
Usage:
ConfirmationDialog.show(
childFragmentManager,
"",
"Turn alarm off?",
object : ConfirmationDialog.Listener() {
override fun onConfirm() {
super.onConfirm()
turnAlarm(false)
}
})

Related

Add an extra button in AlertDialog in DialogFragment

I have a custom DialogFragment, and was trying to use AlertDialog in onCreateDialog(). The AlertDialog only has title, singleChoiceItem, positiveButton, etc. But I would like to add an extra button on the top left to be a back button that I could add a onClickListener. How could I do that? Thanks!
You can create your own xml file with a button on left corner.Now that you have created an Xml file as Custom layout. inflate that file using below code
AlertDialog.Builder builder
= new AlertDialog.Builder(this);
builder.setTitle("Name");
// set the custom layout
final View customLayout
= getLayoutInflater()
.inflate(
R.layout.custom_layout,
null);
builder.setView(customLayout);
After creating your own custom layout set a listener for the button that you have positioned on top left corner in xml by calling findViewByID on Dialog and then calling closeButton.setOnClickListener.
You can use custom dialog. Your Kotlin Class:
class CustomDialog(var c: Activity) : Dialog(c), View.OnClickListener {
var d: Dialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.dailog_custom)
}
override fun onClick(v: View) {
}
}
XML file of dialog:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginHorizontal="#dimen/extra_padding"
android:background="#drawable/bg_ripple_secondary_variant_less_rounded"
android:padding="20dp">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textStyle="bold"
android:textSize="#dimen/_12sdp"
android:textColor="#color/red"
android:text="Title" />
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/less_padding"
app:layout_constraintTop_toBottomOf="#id/tvTitle"
app:layout_constraintStart_toStartOf="parent"
android:text="Your Message"
android:textStyle="bold"
android:textSize="#dimen/_12sdp"
android:textColor="?attr/colorOnPrimary" />
<TextView
android:id="#+id/btnYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tvMessage"
app:layout_constraintEnd_toEndOf="parent"
android:padding="#dimen/fab_padding"
android:textColor="#color/red"
android:text="#string/yes" />
<TextView
android:id="#+id/btnNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tvMessage"
app:layout_constraintEnd_toStartOf="#id/btnYes"
android:padding="#dimen/fab_padding"
android:textColor="?attr/colorOnPrimary"
android:text="#string/no" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to create dialog which ask for pin if it is correct a following operation will be done

This code is where I am creating Alert dialog. whenever I press ok it crashes .password_dialog has 1 editText and 2 buttons.
private fun passwordCheck(position: Int){
val view=LayoutInflater.from(requireContext()).inflate(R.layout.password_dialog,null,false)
val builder=AlertDialog.Builder(requireContext())
with(builder){
setTitle("Enter your Pin")
setPositiveButton("Ok"){dialog,which->
if(pin_text.text.toString()=="1234"){
Toast.makeText(requireContext(),"Right Pin",Toast.LENGTH_LONG).show()
}
else{
pin_text.requestFocus()
pin_text.error="Incorrect"
}
}
setCancelable(false)
setNegativeButton("Cancel"){dialog,which->
dialog.dismiss()
}
setView(view)
show()
}
}
Create a layout with the view, for example, an EditText that only accepts numbers and two buttons, accept or cancel.
Then inflate the view
val view = LayoutInflater.from(context).inflate(R.id.layout, null, false)
Then with that view you get the reference of the buttons and the EditText with
val button = view.findViewById<Button>(R.id.button)
//I'm not going to declare all views, but assuming you already did.
button.setOnClickListener {
if (editText.text.toString.toInt() == 1234 //your correct pin) {
//CORRECT PIN TODO
} else {
editText.requestFocus()
editText.error = "Incorrect pin"
}
}
Then create the dialog and add that view to it
AlertDialog.Builder(context).setView(view).create().show()
Do not add the validation methods in the accept button of the dialog.
They are what are declared as follows.
setPositiveButton("Accept"){diálog, which -> }
This is the way by which you can create your own dialog with custom layout.
Implement your PIN UI in custom_dialog.xml and your logic in Kotlin file.
Here is example how you can create custom Dialog.
custom_dialog.xml
<androidx.constraintlayout.widget.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="wrap_content"
android:background="#424242">
<TextView
android:id="#+id/popup_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16sp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16sp"
android:layout_marginBottom="10dp"
android:text="Custom Dialog Box"
android:textColor="#color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/linearLayoutOpt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16sp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16sp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/popup_dialog">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="#+id/no_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="18dp"
android:text="No"
android:textStyle="bold"
android:textAllCaps="false"
android:textColor="#color/white" />
<Space
android:layout_width="32sp"
android:layout_height="12sp" />
<TextView
android:id="#+id/yes_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="18dp"
android:text="Yes"
android:textStyle="bold"
android:textAllCaps="false"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showDialog()
}
private fun showDialog() {
val customDialog = Dialog(this)
customDialog.setContentView(R.layout.custom_dialog)
customDialog.window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
val yesBtn = customDialog.findViewById(R.id.yes_opt) as TextView
val noBtn = customDialog.findViewById(R.id.no_opt) as TextView
yesBtn.setOnClickListener {
//Do something here YOUR LOGIC
customDialog.dismiss()
}
noBtn.setOnClickListener {
customDialog.dismiss()
}
customDialog.show()
}
}
example app screenshot

BottomSheet not hiding fully when swipe down. Partially Hidden

I have a BottomSheet on my App but the problem is when I swipe it down instead of hiding it fully it stays partially. Here is the picture
My goal here is to hide it fully when swiping down
Here is my code
open class RoundedBottomSheetFull : BottomSheetDialogFragment() {
override fun getTheme(): Int = R.style.BottomSheetDialogTheme
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.setOnShowListener {
val bottomSheetDialog = it as BottomSheetDialog
val parentLayout =
bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
parentLayout?.let { it ->
val behaviour = BottomSheetBehavior.from(it)
setupFullHeight(it)
behaviour.state = BottomSheetBehavior.STATE_EXPANDED
}
dialog.behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss()
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
if (BottomSheetBehavior.STATE_HALF_EXPANDED == 1) {
dismiss()
BottomSheetBehavior.STATE_HIDDEN
}
}
})
}
return dialog
}
}
private fun setupFullHeight(bottomSheet: View) {
val layoutParams = bottomSheet.layoutParams
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
bottomSheet.layoutParams = layoutParams
}
and here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
app:behavior_hideable="false"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:id="#+id/btnBarManagement"
android:layout_width="match_parent"
android:layout_height="50dp"
android:elevation="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgBack"
android:backgroundTint="#f39c12"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:padding="10dp"
android:layout_gravity="center"
android:background="#drawable/rounded_border_edittext"
android:src="#drawable/ic_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
<LinearLayout
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvTitle"
android:textSize="15dp"
android:fontFamily="#font/man_bold"
android:text="Comments about Bar Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="#+id/tvCommentCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/man_reg"
android:text="Total Comment(s) : 0"
android:textSize="11dp"></TextView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.core.widget.NestedScrollView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical"
android:fillViewport="true">
<androidx.recyclerview.widget.RecyclerView
tools:listitem="#layout/layout_list_comments"
android:id="#+id/rvComments"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.core.widget.NestedScrollView>
<View
android:layout_width="match_parent"
android:layout_height="0.50sp"
android:background="#757575" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:weightSum="3"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_weight="3"
android:paddingTop="15sp"
android:paddingBottom="15sp"
android:drawablePadding="10sp"
android:id="#+id/etComment"
android:backgroundTint="#F0F0F0"
android:layout_marginTop="5sp"
android:layout_marginStart="13sp"
android:layout_marginBottom="10sp"
android:singleLine="false"
android:textSize="13sp"
android:fontFamily="#font/man_reg"
android:paddingStart="10sp"
android:inputType="textMultiLine"
android:paddingEnd="10sp"
android:hint="Type here to start commenting..."
android:text=""
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/rounded_border_edittext"></EditText>
<LinearLayout
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:alpha="0.5"
android:id="#+id/lnSumbmit"
android:layout_gravity="bottom"
android:src="#drawable/ic_send"
android:layout_width="30dp"
android:layout_height="30dp"></ImageView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
In case you need more just tell me. This is the only code I think needed. My target is to hide the bottomsheet when swipping down
val behavior = BottomSheetBehavior.from<LinearLayout>(binding.root)
behavior.addBottomSheetCallback(mBottomSheetCallback)
private val mBottomSheetCallback: BottomSheetCallback = object : BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
// ignores
}
}
use skipCollapsed in order to avoid BottomSheetDialogFragment to have the collapsed state
behaviour.state = BottomSheetBehavior.STATE_EXPANDED
// Add this
behaviour.skipCollapsed = true
Also, you dont need addBottomSheetCallback any more because the dialog will dismess it self when the user slide the dialog down.
you can set:
app:behavior_hideable="true"
or
bottomSheetBehavior.isHideable = true
and then, set:
bottomSheetBehavior.skipCollapsed = true
Add this property in XML
app:behavior_skipCollapsed="true"
When the BottomSheet is in EXPANDED state and you swipe down, it initially enters COLLAPSED state where it is partially visible. The flag behavior_skipCollapsed allows it to skip this COLLAPSED state and directly enter HIDDEN state where it is completely hidden.

Unable to match parent the custom dialog's width

I am have created a custom dialog and defined the width as match_parent. But when I ran the application, it appears not even wrapping the content as most of the content is getting clipped. I tried some of the solutions but getting the same result, I have no idea what am I doing wrong:
dialog_messages.xml
<com.google.android.material.card.MaterialCardView
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="wrap_content"
android:orientation="vertical"
app:cardCornerRadius="#dimen/corner"
app:cardElevation="#dimen/elevation"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_msg_sm"
android:drawablePadding="#dimen/normal"
android:drawableTint="#color/colorDanger"
android:padding="#dimen/normal_2x"
android:text="Send Message"
android:textColor="#color/colorBlack"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorGrey" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/normal_2x"
android:text="Tap a message to send"
android:textColor="#color/colorGrey"
android:textSize="#dimen/font_sm1" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/normal_2x" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#color/colorGrey" />
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/normal"
android:background="#null"
android:paddingHorizontal="#dimen/normal_4x"
android:text="Cancel"
android:textColor="#color/colorDanger" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
and the setup class
MessageDialog.kt
class MessageDialog(
private val activity: Activity,
private val onMessageClickListener: MessageClickListener
) : Dialog(activity) {
private val messageList = arrayListOf<String>(
"Can't locate you. Send instructions",
"Heavy Traffic. I'll be late 10 minutes",
"Just turning around the block",
"Taxi arrived",
"I am coming to your location",
"I am arriving in 5 minutes",
"Ok"
)
private lateinit var listView: ListView
private lateinit var btnCancel: Button;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.dialog_messages)
val adapter = ArrayAdapter(activity, android.R.layout.simple_list_item_1, messageList)
listView = findViewById(R.id.listView)
listView.adapter = adapter
listView.setOnItemClickListener { parent, view, position, id ->
onMessageClickListener.onMessageClick(messageList.get(position))
dismiss()
}
btnCancel = findViewById(R.id.btnCancel)
btnCancel.setOnClickListener {
dismiss()
}
}
}
But this is the result:
I have tried some other answers already provided here, but nothing works.
Create a Dialog object and do this in the show() part.
val dialog = MessageDialog(activity, messageClickListener)
val lp = WindowManager.LayoutParams()
lp.copyFrom(dialog.window!!.attributes)
lp.width = WindowManager.LayoutParams.MATCH_PARENT
lp.height = WindowManager.LayoutParams.WRAP_CONTENT
dialog.show()
val window = dialog.window
window!!.attributes = lp
For my case, i was using afollestad material dialog library, and it had default margin. I overrided that margin on my dimens.xml file
<dimen name="md_dialog_horizontal_margin">8dp</dimen>
Here 8dp means, dialog both from left and right margin will be 16 dp so if you want margin to be 16dp type it as 8dp, divided by two :)

Handling click events in android kotlin: setOnClickListener only works when double-clicked

I have a button in a DialogFragment to go back to another activity. But it only works when double-clicked. I'm running out of ideas. In my XML file, I've already tried the following (combined in different ways, but none of them worked)
My whole fragment XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="500dp"
android:layout_gravity="center"
android:background="#drawable/bg_rounded_white"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/vertical_margin">
<TextView
android:id="#+id/tv_payment_done_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/vertical_margin"
android:gravity="center_horizontal"
android:text="#string/payment_approved_title"
android:textColor="#color/bg_color"
android:textSize="#dimen/text_large"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_payment_message"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/vertical_margin"
android:gravity="center_horizontal"
android:minHeight="100dp"
android:text="Payment done!"
android:textColor="#color/black_overlay"
android:textSize="#dimen/text_medium"
android:textStyle="normal" />
<LinearLayout
android:layout_width="600dp"
android:layout_height="80dp"
android:layout_marginTop="46dp"
android:clickable="false"
android:focusable="false"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/rpv_btn_no_payment_done"
android:layout_width="250dp"
android:layout_height="?android:attr/actionBarSize"
android:background="#drawable/bg_rounded_light_blue"
android:duplicateParentState="true"
android:text="#string/txt_btn_no"
android:textAllCaps="false"
android:textColor="#color/white_text_color"
android:textSize="#dimen/text_medium"
android:textStyle="bold"
android:visibility="gone" />
<Button
android:id="#+id/rpv_btn_yes_payment_done"
android:layout_width="250dp"
android:layout_height="?android:attr/actionBarSize"
android:layout_marginLeft="#dimen/vertical_margin"
android:background="#drawable/bg_rounded_light_blue"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:focusedByDefault="true"
android:text="#string/txt_btn_exit"
android:textAllCaps="false"
android:textColor="#color/white_text_color"
android:textSize="#dimen/text_medium"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
As for my DialogFragment, I've tried to put my setOnClickListener inside the onViewCreated() method, and then inside the onStart() and after inside the onResume() methods. It keeps working only when double-clicked. I also have a timer that makes the dialog disappear after 10 secs and then it leads me to the activity I want anyway, so it isn't so much of a problem, but I really need and want to fix it. If I set the timer to 5 secs or lower, of course, it gives the impression that the button works when clicked once, but it actually doesn't.
class PaymentDoneDialogFragment : DialogFragment() {
private lateinit var mYesBtn: Button
private lateinit var mActionYes: () -> Unit
override fun onViewCreated(view: View, #Nullable savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog.setCanceledOnTouchOutside(false)
dialog.window.requestFeature(Window.FEATURE_NO_TITLE)
dialog.window.setDimAmount(.85f)
dialog.window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
dialog.setCancelable(false)
dialog.setCanceledOnTouchOutside(false)
dialog.window.decorView.systemUiVisibility = activity?.window!!.decorView.systemUiVisibility
dialog.window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
mDialogTitle = view.find(R.id.tv_payment_done_title)
if (mTitle != null) {
mDialogTitle.text = mTitle
}
mYesBtn = view.find(R.id.rpv_btn_yes_payment_done)
if (mYesTitle != null) {
mYesBtn.text = mYesTitle
}
mYesBtn.setOnClickListener {
if (mMessage == null) {
(activity as PaymentActivity).nextScreensaverActivity()
} else {
mActionYes()
}
activity?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
}
if (mMessage != null) {
mMessageTextView = view.find(R.id.tv_payment_message)
mMessageTextView?.text = mMessage
mNoBtn = view.find(R.id.rpv_btn_no_payment_done)
if (mNoTitle != null)
mNoBtn.text = mNoTitle
mNoBtn.visibility = View.VISIBLE
mNoBtn.setOnClickListener {
mActionNo()
activity?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
}
}
}
Does anyone have some clue?
You should remove android:clickable="false" and android:focusable="false" from LinearLayout child

Categories

Resources