DialogFragment rounded corners - how to set transparency - android

I've made custom layout for dialogFragment with rounded corners but when dialog is called corners are rounded he looks like below.
https://i.imgur.com/aE4PMhZ.png
I know i need to set transparency dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
but I dont know where to put this in my code, additionally I'm using Kotlin.
Below is the part of code where Dialog is called.
myDialog = Event_Dialog.newInstance(args,args1)
myDialog.show(fragmentManager, "MyDialog")
This is Event_Dialog class.
class Event_Dialog : DialogFragment() {
companion object {
fun newInstance(bundle: String, bundle1: String): Event_Dialog {
//description
val args: Bundle = Bundle()
args.putString("desc", bundle)
//link
args.putString("link", bundle1)
val fragmentDialog = Event_Dialog()
fragmentDialog.arguments = args
return fragmentDialog
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val x = inflater.inflate(R.layout.event_detail, container, false)
some code.......
return x
Could you tell me guys where I should set the transparency of custom background ?
Thanks !

just put it in oncreatedialog like :
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
return dialog
}
and i suggest that you use a framelayout as a root view for your dialog ( match parent frame ) and design your center layout in it

Related

Updating View of AlertDialog from Fragment

I am not sure why I cannot update the imageView of a custom dialog layout xml which is opened from inside a fragment.
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(R.layout.qr_code_layout_dialog);
imageFromLayout.setImageBitmap(myBitmap);
builder.show();
I have also tried to use a Dialog object (not AlertDialog) but it will not even open, therefore AlertDialog opens but will not show any images. Nor will it allow me to update the images from within the java code.
How can I update the imageView image of the layout dialog from within the fragment?
You can try make realisation of it
abstract class ObservableDialog<T>(private val callBack: (T) -> Unit): DialogFragment(){
companion object{
const val TAG = "MY_TAG"
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setLayoutParam()
}
fun doMyChoice(obj: T){
callBack(obj)
dismiss()
}
private fun setLayoutParam(){
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}
}
Call it like
val dialog = ObservableDialog<String>{
//do something with result
}
dialog.show(supportFragmentManager, ObservableDialog.TAG)

Create a button programmatically in a fragment using Kotlin

I wish to generate a column of buttons inside a fragment in Kotlin from a database. For now I tried with just one button, but I cannot seem to do it without hardcoding it in the XML file. Here is my code so far:
class NotesFragment() : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_notes, container, false)
//val root : ViewGroup = inflater.inflate(R.layout.fragment_notes, null) as ViewGroup
val button_Id: Int = 1111
val button = Button((activity as MainActivity?)!!)
button.setText("Button 4 added dynamically")
button.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))
button.setId(button_Id)
button.x = 250f
button.y = 500f
button.setOnClickListener(this)
view.add(button)
return view
}
}
I know I should probably look for the layout somewhere in this and do an addView with it... What is the next step?
I did it like this
view.rootView.findViewById<RelativeLayout>(R.id.button_container).addView(button)

Can't set text on text view in dialog fragment

I have a dialog fragment and I use databinding to bind views.
I can't set text on a text view after the dialog is created.
Here is my code :
class MyDialogFragment : DialogFragment() {
private lateinit var layout : FragmentMyDialogBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
layout = FragmentMyDialogBinding.inflate(inflater,container,false)
return layout.root
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
layout = FragmentMyDialogBinding.inflate(LayoutInflater.from(requireContext()))
layout.textView.text = "Initial text"
layout.button.setOnClickListener{
layout.textView.text = "Text changed"
Log.wtf("Text","${layout.textView.text}")
// Log shows the changed text but it is not visible on the ui.
}
val builder = MaterialAlertDialogBuilder(requireContext(), R.style.RoundShapeTheme)
builder.setView(layout.root)
return builder.create()
}
}
Log shows the changed text but it is not visible on the UI.
Does anybody have a solution for this ?
I just tested your code and it is resolved by removing the onCreateView().. You already use onCreateDialog() and it's enough for setting the binding object.
The layout is inflated twice, and probably the textView gets changed on the layout that is not on the UI. That is probably because the onCreateView() gets called after onCreateDialog(). So, when you change the text in the onCreateDialog() inflated layout the change is not appeared because the inflated layout by onCreateView() is laid on top of it.
In onCreateDialog() method set the custom views by the following code:
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
binding = DialogLayoutBinding
.inflate(LayoutInflater.from(getContext())); AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(binding.getRoot());
binding.dialogTextView.setText("I am Dialog's TextView");
return builder.create(); }

Cannot change Views at Runtime in Dialogfragment

I want to set the drawable of a ImageView and a ProgressBar in a DialogFragment at runtime. Therefore i want to set the drawable in the onViewCreated and set the ProgressBar visible when the Positive Button of the Dialog is clicked.
But for reasons i do not know absolutely nothing happens. i also tried to call invalidate on the view to actively trigger a redraw but also nothing happens.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.dialog_view, container, false)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val inflater = requireActivity().layoutInflater
val layout = inflater.inflate(R.layout.dialog_view, null)
val dialog = AlertDialog.Builder(requireContext())
.setTitle(R.string.dialog_title)
.setMessage(R.string.dialog_text)
.setView(layout)
.setPositiveButton(R.string.start, null)
.setNegativeButton(R.string.cancel, null)
.create()
return dialog
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
imageView.setImageDrawable(image) //image is a drawable
}
override fun onResume() {
super.onResume()
val d = dialog as AlertDialog?
d?.getButton(Dialog.BUTTON_POSITIVE)?.setOnClickListener {
progressBar.visibility = View.VISIBLE
}
}
Edit:
Found out that if i directly call something on the layout i pass to the dialog it is possible to change visibility of views.
val layout = inflater.inflate(R.layout.dialog_view, null)
layout.findViewById ....
This works but i still wonder why i cannot simply call with kotlin synthetics on the view.
The action that you want to be performed when the positive button is pressed should be added in the dialog build
.setPositiveButton(R.string.start){ _, _ ->
progressBar.visibility = View.VISIBLE
}

Cannot use AnimatedVectorDrawable inside Fragment

I am trying to run an animated vector inside a fragment. First, I tried passing a drawable resource to fragment and then setting it as a background and starting the AnimatedVectorDrawable object created from the background. It only showed a static background with no animation. Alternatively, I set the background resource in the fragment's layout resource and then creating an AnimatedVectorDrawable from the background in the fragment code and start the AVD. Again, the result was the same: No animation.
I also tried passing the animation to the UI thread to run (just in case fragments use a different thread). Again, no success.
According to Android Developers, as of API 25, vector animations run on RenderThread. I am not sure if it has something with my failure to start the animation inside the fragment.
My code for the fragment is as follows:
class AvdFragment : Fragment() {
/**
* AVD resource file passed to this fragment to be animated
*/
private var mAvdResource: Int = 0;
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_avd, container, false)
}
override fun setArguments(args: Bundle?) {
super.setArguments(args)
mAvdResource = args!!.getInt(AVD_RES)
}
private val LOG_TAG = javaClass.name
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val root = activity!!.findViewById<ViewGroup>(R.id.fragment_root)
root.setBackgroundResource(mAvdResource)
val avd = root.background as AnimatedVectorDrawable
avd.start()
}
companion object {
val AVD_RES = "avd_resource"
}
}

Categories

Resources