What I would like to do
I would like to acquire data that the user selected on dependent spinners.
On my current code, it was succeeded to acquire the data from the first spinner but I cannot do it from the second spinner.
Problem
How should I fix my code to acquire appropriate data of a user's selection?
In this case, the second textView should be BMW but it is Audi, which is the first candidate of the second spinner.
The first textView is changed following a user's selection. In this case, it changed to mobile from motor, but again, the second textView was remained as the first the first candidate of the second spinner.
Current Code
MainActivity.kt
package com.example.spinner
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.Spinner
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.Toast
class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
var spinner1: Spinner? = null
var spinner2: Spinner? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinner1 = findViewById(R.id.spinner1) as Spinner
spinner2 = findViewById(R.id.spinner2) as Spinner
val adapter1 = ArrayAdapter.createFromResource(
this,
R.array.array1, android.R.layout.simple_spinner_item
)
spinner1?.setAdapter(adapter1)
spinner1?.setOnItemSelectedListener(this)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the main; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.getItemId()
return if (id == R.id.action_settings) {
true
} else super.onOptionsItemSelected(item)
}
override fun onItemSelected(
parent: AdapterView<*>, view: View, position: Int,
id: Long
) {
if (spinner1?.getSelectedItem() == "mobile") {
Toast.makeText(
applicationContext, "Mobil dipilih",
Toast.LENGTH_SHORT
).show()
val adapter2 = ArrayAdapter.createFromResource(
this,
R.array.mobile_array, android.R.layout.simple_spinner_item
)
spinner2?.setAdapter(adapter2)
spinner2?.setOnItemSelectedListener(this)
} else {
val adapter2 = ArrayAdapter.createFromResource(
this,
R.array.motor_array, android.R.layout.simple_spinner_item
)
spinner2?.setAdapter(adapter2)
spinner2?.setOnItemSelectedListener(this)
}
// here I try to put the selected contents to variables
val spinner1_content = spinner1?.getSelectedItem() as String
textView1.text = spinner1_content
val spinner2_content = spinner2?.getSelectedItem() as String
textView2.text = spinner2_content
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.stackspinner.MainActivity" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f"
android:layout_marginTop="100dp"
android:textSize="40sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f"
android:layout_marginTop="100dp"
android:textSize="40sp" />
</LinearLayout>
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">Application Name</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="array1">
<item>mobile</item>
<item>motor</item>
</string-array>
<string-array name="mobile_array">
<item>Android</item>
<item>blackberry</item>
<item>apple</item>
</string-array>
<string-array name="motor_array">
<item>Audi</item>
<item>BMW</item>
<item>unicorn</item>
</string-array>
</resources>
current code after hearing an answer
MainActivity.kt
package com.example.spinner
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.Spinner
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.Toast
class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
var spinner1: Spinner? = null
var spinner2: Spinner? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinner1 = findViewById(R.id.spinner1) as Spinner
spinner2 = findViewById(R.id.spinner2) as Spinner
val adapter1 = ArrayAdapter.createFromResource(
this,
R.array.array1, android.R.layout.simple_spinner_item
)
spinner1?.setAdapter(adapter1)
spinner1?.setOnItemSelectedListener(this)
val adapter2 = ArrayAdapter.createFromResource(
this,
R.array.array1, android.R.layout.simple_spinner_item
)
spinner2?.setAdapter(adapter2)
spinner2?.setOnItemSelectedListener(this)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the main; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.getItemId()
return if (id == R.id.action_settings) {
true
} else super.onOptionsItemSelected(item)
}
override fun onItemSelected(
parent: AdapterView<*>, view: View, position: Int,
id: Long
) {
if (spinner1?.getSelectedItem() == "mobile") {
Toast.makeText(
applicationContext, "Mobil dipilih",
Toast.LENGTH_SHORT
).show()
val adapter2 = ArrayAdapter.createFromResource(
this,
R.array.mobile_array, android.R.layout.simple_spinner_item
)
spinner2?.setAdapter(adapter2)
} else {
val adapter2 = ArrayAdapter.createFromResource(
this,
R.array.motor_array, android.R.layout.simple_spinner_item
)
spinner2?.setAdapter(adapter2)
}
// here I try to put the selected contents to variables
val spinner1_content = spinner1?.getSelectedItem() as String
textView1.text = spinner1_content
val spinner2_content = spinner2?.getSelectedItem() as String
textView2.text = spinner2_content
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
Take note that every time you select a mobile (o a motor) you recreate your spinner2. You should use different listeners for each spinner:
spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
//your spinner1 listener code
}
}
spinner2.dothesame.....
I put the entire onCreate for better understanding:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinner1 = findViewById(R.id.spinner1) as Spinner
spinner2 = findViewById(R.id.spinner2) as Spinner
textView1 = findViewById(R.id.textView1) as TextView
textView2 = findViewById(R.id.textView2) as TextView
val adapter1 = ArrayAdapter.createFromResource(
this,
R.array.array1, android.R.layout.simple_spinner_item
)
spinner1?.setAdapter(adapter1)
spinner1?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
var spinner2Adapter: ArrayAdapter<CharSequence>? = null
//select the right adapter for de Spinner2
if (spinner1?.getSelectedItem() == "mobile") {
spinner2Adapter = ArrayAdapter.createFromResource(
applicationContext,
R.array.mobile_array, android.R.layout.simple_spinner_item
)
} else {
spinner2Adapter = ArrayAdapter.createFromResource(
applicationContext,
R.array.motor_array, android.R.layout.simple_spinner_item
)
}
//set the adapter and the listener for spinner2
spinner2?.setAdapter(spinner2Adapter)
spinner2?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val spinner2_content = spinner2?.getSelectedItem() as String
textView2?.text = spinner2_content
}
}
// here I try to put the selected contents to variables
val spinner1_content = spinner1?.getSelectedItem() as String
textView1?.text = spinner1_content
}
}
}
Related
I have a view model I have added a click event listener. How do I navigate from that ViewModel into a Fragment
The ViewModel Code
class PromoAdapter(var promo: ArrayList<PromoModal>) :
RecyclerView.Adapter<PromoAdapter.UserViewHolder>() {
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.bind(promo[position])
holder.promoRow.setOnClickListener() {
// Navigate to Fragment with name UserFragment
};
}
}
User Fragment Code
class UserFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view: View = LayoutInflater.from(container!!.context)
.inflate(R.layout.user_page_fragment, container, false)
}
}
I have tried writing the following code still not working
Inside the ViewModel Click Event
val intent = Intent(itemView.context, UserFragment::class.java)
itemView.context?.startActivity(intent)
I cant find something working any help will be appreciated
It's better to move the click listener to a fragment
In Adapter:
class PromoAdapter(
var promo: ArrayList<PromoModal>,
val openFragment: (promoModal: PromoModal) -> Unit
) :
RecyclerView.Adapter<PromoAdapter.UserViewHolder>() {
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.bind(promo[position])
val currentItem = promo.getOrNull(position) ?: return
holder.promoRow.setOnClickListener {
openFragment(currentItem)
};
}
}
In Fragment:
val mAdapter = PromoAdapter(
promo = promoList,
openFragment = { promoModal ->
// Navigate to Fragment with name UserFragment
}
)
I am trying to make a cardView that containts a title, a progressBar and a subTitle.
I generate all these three at runtime and it works fine but when I use ConstraintLayout params to put the progressBar below title it doesn t work.
Here is the XML:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView"
android:id="#+id/activityContent"
android:foregroundGravity="center">
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the Kotlin code:
fun createCardView(v:View,title:String,top: CardView,days:Int):CardView{
val cardView=CardView(requireContext())
val text=createTitle(title)
cardView.addView(text)
cardView.setBackgroundColor(Color.WHITE)
cardView.cardElevation=17f
cardView.id=ids
ids=ids+1
var layoutparams = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT,
)
layoutparams.setMargins(60,80,60,18)
cardView.setLayoutParams(layoutparams)
val item = v.findViewById(R.id.activityContent) as? ConstraintLayout
if (item != null) {
item.addView(cardView)
}
var par=cardView.layoutParams as? ConstraintLayout.LayoutParams
if (par != null) {
par.topToBottom=top.id
par.startToEnd=top.id
}
val progressBar=createProgressBar(title)
val subText=createSubTitle(title)
val params = RelativeLayout.LayoutParams(300, 150)
params.addRule(RelativeLayout.BELOW,subText.id)
cardView.addView(progressBar,params)
cardView.addView(subText)
reference.child(user).child(title).setValue(days)
return cardView
}
I am looking for something to set to cardView as I set "par" to cardView.
Scenario:
I am using Spinner for date range selection, which is working perfectly. The issue is, there is an option "Custom Range" on which I have to open a custom date range picker. Although it is working fine for the first time.
The issue is:
When users click it again, it doesn't call onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) (which is a functionality of Spinner). But from the user point of view, it's a bug.
Is there any way, we can get any selection event on an already selected item of Spinner?
To call an event on the already selected item in Spinner You can do it in this way:
Create Your own Spinner class by extending AppCompatSpinner:
import android.content.Context
import android.util.AttributeSet
import android.util.Log
class MySpinner(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatSpinner(
context,
attrs
)
{
var listener: OnItemSelectedListener? = null
override fun setSelection(position: Int)
{
super.setSelection(position)
if (position == selectedItemPosition)
{
listener!!.onItemSelected(this, selectedView, position, selectedItemId)
}
}
override fun setOnItemSelectedListener(listener: OnItemSelectedListener?)
{
this.listener = listener
}
}
Use it in Your layout:
<com.yourcompany.kotlintest.MySpinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
When creating a layout do this:
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val spinner = findViewById<MySpinner>(R.id.spinner)
spinner.adapter = ArrayAdapter(
this,
android.R.layout.simple_spinner_dropdown_item,
arrayListOf("One", "Two", "Three")
)
spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener
{
override fun onItemSelected(
parent: AdapterView<*>,
view: View, position: Int, id: Long
)
{
Log.d("MyTag", "Click item at pos $position")
}
override fun onNothingSelected(parent: AdapterView<*>)
{
Log.d("MyTag", "Nothing selected")
}
}
}
}
Now when You reselect the same item function will be executed
did you use
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here with selected **item position**
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I am new to android development and created a new project from android studio with a bottom navigation activity in Kotlin. Besides MainActivity.kt also dashboard, home and notifications Fragments and its ViewModels were generated. When I handle the button click inside the MainActivity class, everything works fine.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
//handle button click
val temporary_button = findViewById<Button>(R.id.temporary_button)
temporary_button.setOnClickListener{
makeText(this, "You clicked the button", LENGTH_LONG).show()
}
}
}
Here is the screenshot of a working button Button works fine
However I do not understand how to use the buttons inside different fragments. I tried to create a functionality for the second button inside a dashboard fragment Screenshot of a second button but I haven't found a solution. I have tried
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(this, Observer {
textView.text = it
})
//handle button click
val temporary_button = findViewById<Button>(R.id.temporary_button2)
temporary_button2.setOnClickListener{
makeText(this, "You clicked the button", LENGTH_LONG).show()
}
return root
}
}
but apperently this piece of code
//handle button click
val temporary_button = findViewById<Button>(R.id.temporary_button2)
temporary_button2.setOnClickListener{
makeText(this, "You clicked the button", LENGTH_LONG).show()
}
is wrong. Another thing that I have tried is changing the fragment_dashboard.xml file and setting the onClick property to a function name (android:onClick="button2click"). Here is the entire xml:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/text_dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="#+id/temporary_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="192dp"
android:layout_marginBottom="248dp"
android:onClick="button2click"
android:text="Button2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
I tried to use the function like this:
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(this, Observer {
textView.text = it
})
return root
}
fun button2click (view: View){
println("Button clicked")
}
}
but this way it doesn't work and the application crashes when I click the button.
Any help on how to use the buttons inside fragments would be appreceated.
This is what worked out for me in the end:
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(this, Observer {
textView.text = it
})
val button2 : Button = root.findViewById<Button>(R.id.temporary_button2)
button2.setOnClickListener{
println("clicked button 2")
Toast.makeText(view?.context, "Button Clicked", Toast.LENGTH_LONG).show()
}
return root
}
}
Try your inside onViewCreated instead of onCreateView using getView()/view.
e.g:
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
val temporary_button = getView().findViewById<Button>(R.id.temporary_button2)
temporary_button2.setOnClickListener{
makeText(this, "You clicked the button", LENGTH_LONG).show()
}
}
There is no findViewById in fragment, normally inside a fragment you should override the onCreateView method, inflate your own layout and try to get views from the view you inflated. For example :
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (mContentView == null){
mContentView = inflater.inflate(R.layout.family_fragment_scene_list, container, false);
}
type = getArguments().getInt(ARGS_KEY_TYPE);
adapter = new SceneAdapter(getContext());
// get views from mContentView
mSceneList = (RecyclerView) mContentView.findViewById(R.id.swipe_target);
mSceneList.setLayoutManager(new LinearLayoutManager(getContext()));
mSceneList.setAdapter(adapter);
return mContentView;
}
I was wondering how can i create something that is like instagram's backstack.
I have a an app, that has a bottom navigation bar which opens 3 different fragments (MapFragment, HostFragment, ProfileFragment), inside HostFragment, i create a new one called Host2Fragment, when the user clicks a button:
Tab1 Tab2 Tab3
[MapFragment][HostFragment][ProfileFragment]
.
.
.
[Host2Fragment]
Some examples:
NOTE: Tab1/MapFragment is the home fragment
User is on Host2Fragment, pressing back goes back to HostFragment
pressing back again goes to Tab1
User is on Host2Fragment, clicking on Tab2 goes back to HostFragment
Essentially, pressing back, should just peel off all the layers back to the home fragment (Tab1), and update the bottom navigation bar while doing so. I also want to keep the state of the fragments, so that when i click back to it, it should still be in the same state
What i have so far:
MainActivity
class MainActivity : AppCompatActivity(){
private val mapFragment: Fragment = MapFragment()
private val hostFragment: Fragment = HostFragment()
private val profileFragment: Fragment = ProfileFragment()
private val fm = supportFragmentManager
private var activeFragment = mapFragment
lateinit var toolbar: ActionBar
private val mOnNavigationItemSelectedListener =
BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_map -> {
fm.beginTransaction().hide(activeFragment).show(mapFragment).commit()
activeFragment = mapFragment
return#OnNavigationItemSelectedListener true
}
R.id.navigation_host -> {
fm.beginTransaction().hide(activeFragment).show(hostFragment).commit()
activeFragment = hostFragment
return#OnNavigationItemSelectedListener true
}
R.id.navigation_profile -> {
fm.beginTransaction().hide(activeFragment).show(profileFragment).commit()
activeFragment = profileFragment
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fm.beginTransaction().add(R.id.container, profileFragment, "profileFragment").hide(profileFragment).commit()
fm.beginTransaction().add(R.id.container, hostFragment, "hostFragment").hide(hostFragment).commit()
fm.beginTransaction().add(R.id.container, mapFragment, "mapFragment").commit()
toolbar = supportActionBar!!
val bottomNavigation: BottomNavigationView = findViewById(R.id.navigationView)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
//the home Fragment
bottomNavigation.selectedItemId = R.id.navigation_map
}
}
HostFragment
class HostFragment : Fragment(){
companion object {
private const val TAG = "HostFragment"
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_host, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val button = view.findViewById(R.id.button) as Button
button.setOnClickListener{
Log.d(HostFragment.TAG, "Clicked Host button")
fragmentManager?.beginTransaction()?.add(R.id.container, Host2Fragment(), "host2Fragment")?.commit()
}
}
}