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)
}
Related
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)
}
}
`
Code:
class ProfileFragment : Fragment() {
lateinit var ProfileSetting : ImageView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
var view = inflater.inflate(R.layout.fragment_profile, container, false)
ProfileSetting = view.findViewById(R.id.imgProfileSetting)
return view
}
}
Clicking the button doesn't switch the page in app.
imgProfileSetting is button.
Your Button does not have any ClickListener, implement it :
ProfileSetting.setOnClickListener {
//Do your stuff
}
I am a novice in Android development. I am currently working in fragments. So basically I am having two fragments each have their own Edittext and buttons. On back press between fragments works fine but when using the button to move to the next fragment the second fragment fails to hold the edit text value. I tried OnSavedInstance it doesn't help me.
FirstFragment.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_first, container, false)
view.findViewById<Button>(R.id.btn1).setOnClickListener {
ediText1 = view.findViewById<EditText>(R.id.et1).text.toString()
Toast.makeText(activity?.baseContext,ediText1,Toast.LENGTH_SHORT).show()
Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment)
}
return view
}
SecondFragment.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_second, container, false)
view.findViewById<Button>(R.id.btn2).setOnClickListener {
ediText2 = view.findViewById<EditText>(R.id.et2).text.toString()
Toast.makeText(activity?.baseContext,ediText2, Toast.LENGTH_SHORT).show()
Navigation.findNavController(view).navigate(R.id.action_secondFragment_to_thirdFragment) }
return view
}
There are many ways to retain the values, most easy is to use ViewModel , since you are using Navigation Components , ViewModel can be scoped to navGraph , see this
Im new in programming, and i got stuck with adding onClickListener in my FragmentHome.kt
i added this code to my existing activity:
val exc = this.findViewById<Button>(R.id.execute)
exc.setOnClickListener {
Toast.makeText(this, "You clicked me.", Toast.LENGTH_SHORT).show()
}
I tried set onClicklistener on a blank activity and it worked, but when i added it to an existing
Fragment activity it does nothing (it should display a toast with some text)
I see no error messages so i don't know where the problem could be.
Thank you for your responses.
enter code here
public class FragmentHome : Fragment() {
public class HomeFragmentElements : AppCompatActivity() {
private lateinit var spinView: Spinner
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_home)
val exc = this.findViewById<Button>(R.id.execute)
exc.setOnClickListener {
Toast.makeText(this, "You clicked me.", Toast.LENGTH_SHORT).show()
}
spinView = findViewById(R.id.spinner)
spinView.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
TODO("Not yet implemented")
}
override fun onNothingSelected(parent: AdapterView<*>?) {
TODO("Not yet implemented")
}
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
}
enter code here
Try to put your code into "onViewCreated":
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val exc = this.findViewById<Button>(R.id.execute)
exc.setOnClickListener {
Toast.makeText(this, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
onViewCreated is executed after "onCreateView". View bindings and initializations should be into onViewCreated
change the following in your onCreateView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view: View = inflater.inflate(R.layout.fragment_home, container, false)
collectId(view)
return view
}
fun collectId(view: view){
val exc = view.findViewById<Button>(R.id.execute)
exc.setOnClickListener {
Toast.makeText(this, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
You can consider viewBinding instead of findViewById
It looks like you have defined an AppCompatActivity subclass inside your Fragment class's definition, kind of like this:
class MyFragment: Fragment {
class MyActivity: AppCompatActivity() {
//...
}
}
The ability to define a non-inner class in a nested way is purely a code organization tool. There is absolutely no connection between your Fragment and Activity classes here. The code above is no different than if you did this, with each class in a completely different file:
class MyFragment: Fragment {
}
class MyActivity: AppCompatActivity() {
}
It also doesn't make sense to think of an Activity as a child of a Fragment. A Fragment is a piece of an Activity, so it's the other way around.
All the code that you have in this Activity's onCreate should be put in the Fragment's onViewCreated() function, except for the call to setContentView(), because that's what your overriding of onCreateView() does. And remove the unused nested Activity class.
Also, you don't need to override onCreateView if you're simply inflating a layout and returning it. You can put the layout directly in the super-constructor call like this:
public class FragmentHome : Fragment(R.layout.fragment_home) {
private lateinit var spinView: Spinner
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// your view setup code
}
}
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