1

I need popup window to show my user's options for example, I was wondering to add a long press botton and then when that button clicked, show a popup window with transparent background and when outside clicked close popup window

exactly like this

I have tried so many ways but non of them worked for me.

I have no clue how to use dialog window too.

No matter which one to use, dilog fragment or popup window but I need that my window to be like this(on the top is user name which has given from data base and then options)

here is where should I place popup window:

val mUserAdapter = UserAdapter()
        mUserAdapter.setOnclickListener(AdapterListener({
            if (it != 0L)
                this.findNavController().navigate(
                    UserListFragmentDirections.actionUserListFragmentToIncreaseMoneyFragment(it)
                )
            Log.d("TAG", "navTeat $it ")
        }, {
            deleteDialog(it)
        }
        ) {
            Toast.makeText(activity, "Long", Toast.LENGTH_SHORT).show() //Here instead of Toast, I need POPUP WINDOW

        })

THANKS :)

Amir
  • 333
  • 4
  • 16
  • What you did in the code just that also. – Saurabh Vadhva Oct 08 '21 at 10:47
  • u mean that image right? actually it is another app I mean It Is NOT my app . I posted this image to show others how my popup window should be – Amir Oct 08 '21 at 10:56
  • I want to see your code – Saurabh Vadhva Oct 08 '21 at 11:12
  • because those codes didn't work I was really angry so I deleted all of them and then push that project to git lab but if u want i can share the link of project and this is one of the souses that I have tried but still didn't worked https://www.youtube.com/watch?v=fn5OlqQuOCk – Amir Oct 08 '21 at 11:27

1 Answers1

2

1.Dialog fragment

assuming you're using navigation component, add the dialogFragment using the <dialog> tag in navGraph,

<dialog
    android:id="@+id/navigation_dialog_fragment"
    android:name="com.app.example.ui.dialogFragment"
    tools:layout="@layout/dialogFragment"/>

override dialog fragment's onCreate() to make the background translucent,

class mDialogFrag: DialogFragment(R.layout.dialog_fragment) {

   override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setStyle(STYLE_NO_FRAME, R.style.DialogTheme)

         //populate UI with data from DB

DialogTheme:

<style name="AppDialogTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">#40000000</item><!--dialog background-->
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

2.Alert dialog

in your target fragment,

lateinit var mAlert : AlertDialog
//inflate custom layout
val alertBinding = AlertBinding.inflate(LayoutInflater.from(requireContext()))
alertBinding?.apply{
  //populate UI with data from DB
}
val builder = AlertDialog.Builder(requireContext())
mAlert = builder.setView(alertBinding.root)
             .setCancelable(false)
             .create()
mAlert.window?.setBackgroundDrawable(
     ContextCompat.getDrawable(requireContext(),R.drawable.bg_card) //custom dialog background 
)
mAlert.show()

both these methods use custom layout to achieve your requirements. but the DialogFragment was meant for more complicated UI (i.e. gives more control over UI dynamically). whereas, the AlertDialog can be used for more simple UI such as yours.

or you can use the default list called traditional single-choice list AlertDialog like here which is more simple.

Ezhilan
  • 44
  • 4
  • Hi, @Ezhilan I can not understand AlertBinding. What is this?? – Amir Nov 01 '21 at 15:59
  • 1
    Hi @UnknownDeveloper if you're familiar with [View Binding](https://developer.android.com/topic/libraries/view-binding) use this else just inflate your custom view for alertDialog like [here](https://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-a-layout). – Ezhilan Mar 08 '22 at 15:25
  • thanks :) for your response after longtime – Amir Mar 08 '22 at 15:34