no event OnClickListener in fragment button kotlin - kotlin

I am working kotlin. I have a fragment on my Activity . l want when click on button inside of fragment activity go to another activity .
this code my used in fragment class
class fragment_Arr :Fragment(), View.OnClickListener {
override fun onClick(v: View?) {
val intent = Intent(activity, FlightDeatilasDep::class.java)
startActivity(intent)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_arrivel,container,false)
loadmorefilghtsbeforday.setOnClickListener{this}
}
}
when l debug app and click on button no event happening !

loadmorefilghtsbeforday.setOnClickListener{} body of the lambda is empty - nothing happen.
Please register your listener by
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_arrivel,container,false)
loadmorefilghtsbeforday.setOnClickListener(this)
return view
}
or if you prefer using lambdas
loadmorefilghtsbeforday.setOnClickListener{ onClick(null) } could be called but then your fragment don't need to implements View.OnClickListener.
and class names in Kotlin/java should start from capital later: FragmentArr.
_ is not recommended

Related

Is there a way to refresh a fragment in the onResume method?

My favourites fragment does not display the data given correctly unless the onCreateView function is called. This means that the fragment needs to be deleted and created again. Is there a way to appropriately refresh the fragment in the onResume method?
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragment = this
_binding = FragmentFavouritesBinding.inflate(layoutInflater)
binding.recyclerView.apply{
layoutManager = GridLayoutManager(activity,2)
adapter= CardAdapter(favouriteList, fragment)
}
return binding.root
}
override fun onResume() {
super.onResume()
orderFavourites()
sortFavourites()
if(favouriteList.isEmpty()){
Toast.makeText(this.context,"No Favourites Found", Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(this.context,"resumed", Toast.LENGTH_SHORT).show()
}
}
The favouriteList is updated in another activity and I have ensured that it contains the correct values. It displays as intended after deleting and creating the fragment once more.

How to delete background of rounded corner popup window?

I created a popup window with rounded corners in a fragment but when I open it I can see a dark background behind the popup. Can you tell how to delete it or make ir transparent?
I'm using kotlin.
This is my fragment code (the activity with an image that opens the popup on click):
`
class ProfileFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val bpMore: ImageView = view.findViewById(R.id.ic_bp_more)
bpMore.setOnClickListener {
val showPopUp = BadgesPopUpFragment()
showPopUp.show((activity as AppCompatActivity).supportFragmentManager, "showPopUp")
}
}
`
And this is my popup fragment code (popup window):
`
class BadgesPopUpFragment : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_badges_pop_up, container, false)
}
}
`

Problem trying to access a bottom from fragment

First of all sorry from my bad english. I'm newby with android and kotlin. I', trying to access from a fragment to a bottom in xml layouts. When I create the inflate is Ok because Kotlin recognizes de fragment. But when I declarete the view I can't find any id from the layout
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_pruebas, container, false)
view. //<-THE PROBLEM IS HERE I CAN'T SHOW THE BUTTON'S ID NEITHER IMPORT THE LAYOUT
return view
}
You are in the incorrect function, you have to do that on this override fun, you have to use "findViewById" to find your layout element
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val button: Button = view.findViewById(R.id.button) //Here your id button
button.setOnClickListener { }
}
And its in this way because you are not using binding, so you can't call your xml elements by the way that you're trying, check this to do something like you want.
https://developer.android.com/topic/libraries/view-binding

Activities vs Fragments for apps

I have been trying to figure this out for a bit now. I see a lot of developers and youtubers (tutorials) saying that we should use as little activities as possible in order for a faster, more efficient and less resource heavy code/app. I was wondering if there is a way to create a Log-in and Sign-up using only the MainActivity for both Log-in and Sign-Up in combination with fragments for navigation between them.
Or
Do we need atleast 2 or more activities to handle that process ( Log-in & Sign-Up )?
Example: 1 activity for log-in and 1 activity for sign-up.
Appreciate and welcome any answers regarding this topic!
Theoretically, you could have your entire application run on a single Activity and use Fragments for all of the pages.
Each fragment has its own lifecycle within the activity.
MainActivity can look like this
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadFragment(2)
}
public fun loadFragment(page: Int){
if(page == 1){
// load login
val manager = supportFragmentManager.beginTransaction()
manager.replace(R.id.fragment_holder, LoginFragment()).commit()
}else{
// load register
val manager = supportFragmentManager.beginTransaction()
manager.replace(R.id.fragment_holder, LoginFragment()).commit()
}
}
}
LoginFragment can look like this
class LoginFragment : Fragment() {
lateinit var myButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_login, container, false)
myButton = view.findViewById(R.id.my_button)
myButton.apply {
setOnClickListener { toRegister() }
}
return view;
}
fun toRegister(){
// replace the fragment in main activity with register fragment
(requireActivity() as MainActivity).loadFragment(1)
}
}
RegisterFragment can look like this
class RegisterFragment : Fragment() {
lateinit var mButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_register, container, false)
mButton = view.findViewById(R.id.my_button)
mButton.apply {
setOnClickListener { toLogin() }
}
return view;
}
fun toLogin(){
// replacing the fragment in the main activity with the login fragment
(requireActivity() as MainActivity).loadFragment(1)
}
}
Basically, we replace the fragment displayed in the activity by calling loadfragment.
This logic can be applied to as many fragments as is necessary.

How do I add an actionbar menu in my fragment using Kotlin

How do I add an actionbar menu in my fragments using Kotlin? When I run the code below nothing shows up.
class Fragment1: Fragment() {
private lateinit var fview: View
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,
savedInstanceState: Bundle?
): View?{
// Inflate the layout for this fragment
dissview = inflater.inflate(R.layout.fragment_dissability, container, false)
setHasOptionsMenu(true)
return fview
}//end on create
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.mainmenu, menu);
super.onCreateOptionsMenu(menu, inflater)
}