Home » Android » android – custom dialog fragment from a fragment-Exceptionshub

android – custom dialog fragment from a fragment-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’ve created a constraint layout and I am calling a dialog fragment from a fragment. But when I click on the button all it does is fades the screen but I can’t see any dialog or the layout.

Here’s my code for dialog fragment:

package com.example.atry.MakeComplaint

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment

import com.example.atry.R


class PopupDialog : DialogFragment() {



    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {



        val builder = AlertDialog.Builder(activity)


        return builder.create()

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(R.layout.existing_complaint_popup, container, false)

        val yes = view.findViewById(R.id.YES) as Button
        val no = view.findViewById(R.id.NO) as Button

        // set onclicklistener
        yes.setOnClickListener(View.OnClickListener {
            // I want the dialog to close at this point
            dismiss()
            backFragment()

        })

        // set onclicklistener
        no.setOnClickListener(View.OnClickListener {
            // I want the dialog to close at this point
            dismiss()
            descriptionFragment()

        })

        return view
    }




    private fun backFragment() {
        val manager = (context as AppCompatActivity).supportFragmentManager
        manager.popBackStackImmediate()


    }

    private fun descriptionFragment() {
        val dFragment= Category_Description()
        val lFragment = LocationFragment()
        val manager = (context as AppCompatActivity).supportFragmentManager
        val transaction =  manager.beginTransaction()
        transaction.replace(
            R.id.location_screen,
            dFragment
        ) // give your fragment container id in first parameter
        transaction.show(dFragment)
        transaction.hide(lFragment)
        transaction.isAddToBackStackAllowed
        transaction.addToBackStack(lFragment.fragmentManager.toString())  // if written, this transaction will be added to backstack
        transaction.commit()
    }







}

Here’s the code inside the fragment I’m calling the dialog from:

 //opening up the checkExisting popup

    private fun existPopup(){
        val fm = activity!!.supportFragmentManager
        val dialog = PopupDialog() // creating new object
        dialog.show(fm, "dialog")
    }

And here’s my layout:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
            android:id="@+id/textViewComplaint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/complainDescription"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/textViewComplaint"
            >



        <TextView
                android:id="@+id/headingComplain"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="10dp"
                android:layout_weight="6"
                android:fontFamily="@font/pathway_gothic_one"
                android:gravity="center"
                android:padding="10dp"
                android:text="Is this the complaint you're looking for?"
                android:textColor="@color/white"
                android:textSize="30sp"
                />


    </LinearLayout>

    <RelativeLayout
            android:id="@+id/fazool"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true"
            android:background="@color/grey"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/textViewComplaint"
            android:background="@drawable/bottom_nav_border"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textViewComplaint"
            app:layout_constraintBottom_toBottomOf="parent"
            >
        <ScrollView
                android:layout_height="wrap_content"
                android:layout_width = "match_parent">


            <include layout="@layout/existing_complaint_popup_inner"/>
        </ScrollView>

    </FrameLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

How to&Answers:

Thats my custom notification dialog codes. I think thats way easy and clear. I do not know why you use to fragment for custom dialog but you can use my way.

 val mDialogView = LayoutInflater.from(this).inflate(R.layout.permission_for_notification, null)
        //AlertDialogBuilder
        val mBuilder = AlertDialog
            .Builder(this)
            .setView(mDialogView)
        //show dialog
        val  mAlertDialog = mBuilder.show()

 mAlertDialog.window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)


        mDialogView.notification_accept_button.setOnClickListener {

            mAlertDialog.dismiss()
        }

        //cancel button click of custom layout
        mDialogView.notification_reject_button.setOnClickListener {

            mAlertDialog.dismiss()
        }