I have a button, which onClicked takes to another activity. Now I want the activity to open as bottomDialog. How o do that??
if (success) {
val intent = Intent(this, PaymentActivity::class.java)
intent.putExtra("total_amount",totalAmount)
startActivity(intent)
finishAffinity()
}
I want the PaymentActivity mentioned here to be opened as bottomSheet.
change your Payment Activity to BottomSheetDialogFragment
class PaymentActivity : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.activity_payment, container, false)
}
}
and you can call it like this,
if (success) {
val b = PaymentActivity()
b.show(supportFragmentManager, "Hi")
}
Add total amount in Bundle.
Related
Im using Compose via ComposeView in a Fragment backed by Graph Navigation.
Im using ModalBottomSheetLayout and need to hide it on back Press.
I have tried using BackHandler, but it is not working.
class fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
BackHandler(true) {
Log.i("compose_check","back Pressed") // not getting triggered
}
}
}
}
}
I have Overrided onBackPressed() in the Activity. After removing that, it worked fine.
I am looking to send a string from FragmentA to FragmentDialog by means of an interface Communicator through my Main Activity (which automatically opens FragmentDialog). Some code to illustrate what I have:
class FragmentA: Fragment() {
private lateinit var communicator: Communicator
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_A, container, false)
val title = "My Title"
button.setOnClickListener { communicator.passData(title) }
return view
}
}
class FragmentDialog : DialogFragment() {
var title: String? = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.dialog, container, false)
view.button_close.setOnClickListener { dismiss() }
title = arguments?.getString("title")
view.dialog_title.text = title
return view
}
}
interface Communicator {
fun passData (titleInput: String)
}
and in my Main Activity, which extends the interface:
private val fragmentDialog = FragmentDialog()
override fun passData(titleInput: String) {
val bundle = Bundle()
bundle.putString("title", titleInput)
fullScreenDialog.arguments = bundle
val transaction = supportFragmentManager.beginTransaction().add(android.R.id.content, fragmentDialog).addToBackStack(null).commit()
}
When I click the button in FragmentA, I want to send the string title to Communicator, set the XML element with id dialog_title in FragmentDialog and automatically open the fragment. In fact, this works fine. The issue is that my "back button" in FragmentDialog, i.e. view.button_close.setOnClickListener { dismiss() } only works exactly once. If I click button in FragmentA again, FragmentDialog opens fine with the correct title still but button_close no longer dismisses the dialog. What could be causing this? I am not getting any consol errors.
I want to change the color of a button in fragment by control of another button in mainActivity.
The code for the fragment is :
lass build_8p : Fragment() {
var brush_chosen = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val brush_color = arguments?.getString("brush")
ib0.setOnClickListener {
if (brush_chosen==1)
{
//val con = this.context
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(this.context,R.color.rndcolor1))
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_build_8p, container, false)
}
The variable brush_chosen will be took from the mainActivity.
Here I want to know how to put a correct input for
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(this.context,R.color.rndcolor1))
}
Since that line does not work.
Please tell me how and Thank you very much.
You can just use requireContext():
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(requireContext(),R.color.rndcolor1))
To use fragment context :
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(requireContext(),R.color.rndcolor1))
To use Activity context :
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(requireActivity(),R.color.rndcolor1))
both should work in your case
I capture Key Events (from an external keyboard) within my App. I use onKeyDown() method from Activity. In my app I switch between different Fragments. If I am in a normal Fragment then Activity's onKeyDown() is triggered when pressing buttons. But when I use a DialogFragment as a Dialog then pressing the button does not trigger Activity'sonKeyDown()` any more.
Here some sample code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onClick(view: View) {
// a) Key Event works if adding it via a fragment transaction by my own
// val fragment = MyDialogFragment.newInstance()
// val fragmentTransaction = supportFragmentManager.beginTransaction()
// fragmentTransaction.add(R.id.fr_container, fragment, fragment.javaClass.name)
// fragmentTransaction.commit()
// b) Key Event doesn't work if showing as a dialog
val fragment = MyDialogFragment.newInstance()
fragment.show(supportFragmentManager, fragment.javaClass.name)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
Log.i(javaClass.name, "onKeyDown() keyCode: $keyCode")
return true
}
}
And my two fragments:
class MyNormalFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_my_normal, container, false)
}
}
class MyDialogFragment : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_my_dialog, container, false)
}
companion object {
fun newInstance() = MyDialogFragment()
}
}
As soon as I call a) show() to open the MyDialogFragment then the key events are not captured any more. But if I open MyDialogFragment b) via custom Fragments transaction then the key events are still captured, but my Fragment isn't shown as a Dialog any more.
What do I have to do to let the event also trigger when my dialog is displayed?
Ridcully's answer is right. I just wanted to post what I changed inside MyDialogFragment to keep on capturing key events:
class MyDialogFragment : DialogFragment() {
private val keyEventListener = DialogInterface.OnKeyListener { dialog, keyCode, event ->
Log.i(javaClass.name, "onKey() keyCode: $keyCode")
true
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog.setOnKeyListener(keyEventListener)
return inflater.inflate(R.layout.fragment_my_dialog, container, false)
}
override fun onDestroyView() {
dialog.setOnKeyListener(null)
super.onDestroyView()
}
companion object {
fun newInstance() = MyDialogFragment()
}
}
A Dialog is shown in/as a separate Window, so your Activity doesn't have the focus for keypresses any more. However, the Dialog has it's own onKeyDown method, so you can make use of that.
Just add a simple DialogInterface.OnKeyListener directly on DialogFragment
Is it possible to create a Navigation Fragment to contain my navigation back button click logic.
Multiple Fragment's that have a back button would then be able then inherit from the Navigation Fragment.
I'm new to Kotlin development. As you see below the SigninFragment inflates the view, I'm not sure how to get a reference to the view & back button in a parent Navigation Fragment
class SigninFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_signin, container, false)
// Navigation back button logic
var headerBackButton = view.findViewById<ImageButton>(R.id.headerBackButton)
headerBackButton.setOnClickListener {
val navController = NavHostFragment.findNavController(this#SignInFragment)
navController.navigateUp()
}
return view
}
}
I'm not sure if I got your problem right but could this be the trick?
open class NavigationFragment() : Fragment() {
fun asignNavigationBackClickListener(backButton: View) {
backButton.setOnClickListener {
val navController = NavHostFragment.findNavController(this#NavigationFragment)
navController.navigateUp()
}
}
}
class SigninFragment : NavigationFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_signin, container, false)
asignNavigationBackClickListener(view.findViewById(R.id.headerBackButton))
return view
}
}
I think you can use this code for going back to the previous activity:
headerBackButton.setOnClickListener {
finish()
}