Why is createView never called? - kotlin

My ultimate goal is to be able move my content widgets into a custom view and instantiate that view in an Anko layout in my MainView. I thought I had this working at one point, but I can't reproduce it.
When I run with the following code, the content in the createView of the MainContextView is never displayed and I never see the message "creating main context view", but I do see the message "main content view".
I start by creating a MainContextView
class MainContextView(context: Context) : ViewGroup(context), AnkoComponent<Context> {
lateinit var textBox: EditText
lateinit var button: Button
lateinit var clickCount: TextView
override fun createView(ui: AnkoContext<Context>) = with(ui) {
println("creating main context view")
verticalLayout {
themedEditText {
hint = "hi from main context"
}
button = themedButton {
text = "ok"
}
textBox = themedEditText {
hint = "hi"
}
clickCount = themedTextView {
text = "0"
}
}
}
override fun onLayout(p0: Boolean, p1: Int, p2: Int, p3: Int, p4: Int) {
println("onLayout called")
}
}
and call it from my main view
class MainView : AnkoComponent<MainActivity> {
lateinit var mainCtx: MainContextView
lateinit var textBox: EditText
lateinit var button: Button
lateinit var clickCount: TextView
lateinit var mainMenu: Menu
lateinit var settingItem: MenuItem
lateinit var otherItem: MenuItem
lateinit var floatingActionButton: FloatingActionButton
override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
coordinatorLayout {
verticalLayout {
themedAppBarLayout {
themedToolbar(theme = R.style.Base_ThemeOverlay_AppCompat_Dark_ActionBar) {
title = resources.getString(R.string.app_name)
popupTheme = R.style.AppTheme
mainMenu = menu
settingItem = mainMenu.add("My Settings")
otherItem = mainMenu.add("My Other")
}
}.lparams(width = matchParent, height = wrapContent)
// ************************************
// HERE IS THE CALL TO THE CONTEXT VIEW
mainCtx = mainContextView { println("main content view ") }
// *************************************
}.lparams(width = matchParent, height = wrapContent) {
}
floatingActionButton = floatingActionButton {
imageResource = android.R.drawable.ic_dialog_email
}.lparams {
margin = dip(10)
gravity = Gravity.BOTTOM or Gravity.END
}
}
}
}
The MainView is called set as the content view from the MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var presenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mainView = MainView()
mainView.setContentView(this)
presenter = MainPresenter(mainView)
}
}
And finally the ViewManger extensions
inline fun ViewManager.mainContextView(theme: Int = 0) = mainContextView(theme) {}
inline fun ViewManager.mainContextView(theme: Int = 0, init: MainContextView.() -> Unit): MainContextView {
return ankoView({ MainContextView(it) }, theme, init)
}

I found my problem, it had to do with my subView being defined as a subclass of View/ViewGroup, but not really implementing any of the methods.
As it turns out that was not really the right direction. The solution is based on a comment on this issue
Here are some of the key parts of the solution, I also created a Gist with the full code.
The extension function, creates an instance of the "SubView" class and then calls the createView() method in the ankoView() method. It also passes the created instance into the closure that is passed to the extension function, which is key as it allows access to the widgets contained within the view.
inline fun ViewManager.mainContentView(theme: Int = 0) = mainContentView(theme) {}
inline fun ViewManager.mainContentView(theme: Int = 0, init: View.(mainContentView: MainContentView) -> Unit): View {
val mainContentView = MainContentView()
return ankoView({ mainContentView.createView(AnkoContext.create(it)) }, theme, { init(mainContentView)} )
}
The "content view" creates the layout and holds a reference to the widgets.
class MainContentView : AnkoComponent<Context> {
lateinit var textBox: EditText
lateinit var button: Button
lateinit var clickCount: TextView
override fun createView(ui: AnkoContext<Context>) = with(ui) {
verticalLayout {
button = themedButton {
text = "ok"
}
textBox = themedEditText {
hint = "hi"
}
clickCount = themedTextView {
text = "0"
}
}
}
}
In the main view I have fields to refer to the widgets in the "subView" which I then initialize in the closure passed to the mainContentView instance.
lateinit var textBox: EditText
lateinit var button: Button
lateinit var clickCount: TextView
private lateinit var mainMenu: Menu
lateinit var settingItem: MenuItem
lateinit var otherItem: MenuItem
private lateinit var floatingActionButton: FloatingActionButton
override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
coordinatorLayout {
verticalLayout {
themedAppBarLayout {
themedToolbar(theme = R.style.Base_ThemeOverlay_AppCompat_Dark_ActionBar) {
title = resources.getString(R.string.app_name)
popupTheme = R.style.AppTheme
mainMenu = menu
settingItem = mainMenu.add("My Settings")
otherItem = mainMenu.add("My Other")
}
}.lparams(width = matchParent, height = wrapContent)
mainContentView {
button = it.button
textBox = it.textBox
clickCount = it.clickCount
}.lparams(width = matchParent, height = wrapContent)
}.lparams(width = matchParent, height = wrapContent)
floatingActionButton = floatingActionButton {
imageResource = android.R.drawable.ic_dialog_email
}.lparams {
margin = dip(10)
gravity = Gravity.BOTTOM or Gravity.END
}
}
}

Related

Expose value from SharedPreferences as Flow

I'm trying to get a display scaling feature to work with JetPack Compose. I have a ViewModel that exposes a shared preferences value as a flow, but it's definitely incorrect, as you can see below:
#HiltViewModel
class MyViewModel #Inject constructor(
#ApplicationContext private val context: Context
) : ViewModel() {
private val _densityFactor: MutableStateFlow<Float> = MutableStateFlow(1.0f)
val densityFactor: StateFlow<Float>
get() = _densityFactor.asStateFlow()
private fun getDensityFactorFromSharedPrefs(): Float {
val sharedPreference = context.getSharedPreferences(
"MY_PREFS",
Context.MODE_PRIVATE
)
return sharedPreference.getFloat("density", 1.0f)
}
// This is what I look at and go, "this is really bad."
private fun densityFactorFlow(): Flow<Float> = flow {
while (true) {
emit(getDensityFactorFromSharedPrefs())
}
}
init {
viewModelScope.launch(Dispatchers.IO) {
densityFactorFlow().collectLatest {
_densityFactor.emit(it)
}
}
}
}
Here's my Composable:
#Composable
fun MyPageRoot(
modifier: Modifier = Modifier,
viewModel: MyViewModel = hiltViewModel()
) {
val densityFactor by viewModel.densityFactor.collectAsState(initial = 1.0f)
CompositionLocalProvider(
LocalDensity provides Density(
density = LocalDensity.current.density * densityFactor
)
) {
// Content
}
}
And here's a slider that I want to slide with my finger to set the display scaling (the slider is outside the content from the MyPageRoot and will not change size on screen while the user is using the slider).
#Composable
fun ScreenDensitySetting(
modifier: Modifier = Modifier,
viewModel: SliderViewModel = hiltViewModel()
) {
var sliderValue by remember { mutableStateOf(viewModel.getDensityFactorFromSharedPrefs()) }
Text(
text = "Zoom"
)
Slider(
value = sliderValue,
onValueChange = { sliderValue = it },
onValueChangeFinished = { viewModel.setDisplayDensity(sliderValue) },
enabled = true,
valueRange = 0.5f..2.0f,
steps = 5,
colors = SliderDefaults.colors(
thumbColor = MaterialTheme.colors.secondary,
activeTrackColor = MaterialTheme.colors.secondary
)
)
}
The slider composable has its own viewmodel
#HiltViewModel
class PersonalizationMenuViewModel #Inject constructor(
#ApplicationContext private val context: Context
) : ViewModel() {
fun getDensityFactorFromSharedPrefs(): Float {
val sharedPreference = context.getSharedPreferences(
"MY_PREFS",
Context.MODE_PRIVATE
)
return sharedPreference.getFloat("density", 1.0f)
}
fun setDisplayDensity(density: Float) {
viewModelScope.launch {
val sharedPreference = context.getSharedPreferences(
"MEAL_ASSEMBLY_PREFS",
Context.MODE_PRIVATE
)
val editor = sharedPreference.edit()
editor.putFloat("density", density)
editor.apply()
}
}
}
I know that I need to move all the shared prefs code into a single class. But how would I write the flow such that it pulled from shared prefs when the value changed? I feel like I need a listener of some sort, but very new to Android development.
Your comment is right, that's really bad. :) You should create a OnSharedPreferenceChangeListener so it reacts to changes instead of locking up the CPU to constantly check it preemptively.
There's callbackFlow for converting listeners into Flows. You can use it like this:
fun SharedPreferences.getFloatFlowForKey(keyForFloat: String) = callbackFlow<Float> {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (keyForFloat == key) {
trySend(getFloat(key, 0f))
}
}
registerOnSharedPreferenceChangeListener(listener)
if (contains(key)) {
send(getFloat(key, 0f)) // if you want to emit an initial pre-existing value
}
awaitClose { unregisterOnSharedPreferenceChangeListener(listener) }
}.buffer(Channel.UNLIMITED) // so trySend never fails
Then your ViewModel becomes:
#HiltViewModel
class MyViewModel #Inject constructor(
#ApplicationContext private val context: Context
) : ViewModel() {
private val sharedPreference = context.getSharedPreferences(
"MY_PREFS",
Context.MODE_PRIVATE
)
val densityFactor: StateFlow<Float> = sharedPreferences
.getFloatFlowForKey("density")
.stateIn(viewModelScope, SharingStarted.Eagerly, 1.0f)
}

Android Bottom Sheet is not working in Fragment

I have added the bottom sheet dialog on a fragment.
Right now I can't open it.
Whenever I click on the show button it doesn't work at all.
How can I fix this problem?
CreateFragment.kt
class CreateFragment : Fragment() {
lateinit var binding: FragmentCreateBinding;
val viewModel: NotesViewModel by viewModels()
private var color = -1
private val currentDate = SimpleDateFormat.getInstance().format(Date())
private lateinit var result: String
private val job = CoroutineScope(Dispatchers.Main)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentCreateBinding.inflate(layoutInflater, container, false)
val animation = MaterialContainerTransform().apply {
drawingViewId = R.id.createFragment
scrimColor = Color.TRANSPARENT
duration = 300L
}
binding.backbutton.setOnClickListener {
requireView().hideKeyboard()
Navigation.findNavController(it).popBackStack()
}
sharedElementEnterTransition = animation
sharedElementReturnTransition = animation
binding.fabcolorpick.setOnClickListener {
val bottomSheetDialog = BottomSheetDialog(
requireContext(),
R.style.BottomSheetDialogTheme
)
val bottomSheetView: View = layoutInflater.inflate(
R.layout.bottomsheetlayout,
null,
)
val bottomSheetBinding = BottomsheetlayoutBinding.bind(bottomSheetView)
bottomSheetBinding.apply {
colorpicker.apply {
setSelectedColor(color)
setOnColorSelectedListener { value ->
color = value
binding.apply {
createFragmentxmlid.setBackgroundColor(color)
toolbarfragmentnotecontent.setBackgroundColor(color)
bottombar.setBackgroundColor(color)
activity?.window?.statusBarColor = color
}
bottomSheetBinding.bottomSheetparent.setCardBackgroundColor(color)
}
}
bottomSheetparent.setCardBackgroundColor(color)
}
bottomSheetView.post {
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
binding.btndonenotes.setOnClickListener {
createNotes(it)
}
try {
binding.edittextnote.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
binding.bottombar.visibility = View.VISIBLE
binding.edittextnote.setStylesBar(binding.styleBar)
} else {
binding.bottombar.visibility = View.GONE
}
}
} catch (e: Throwable) {
Log.d("TAG", e.stackTraceToString())
}
return binding.root
}
this is how I would do it
create the view
set view as content of dialog
call the dialog's show() method to show it or dismiss() method to dismiss it
// the content view
val bottomSheetView: View = layoutInflater.inflate(
R.layout.bottomsheetlayout,
null,
)
// and the dialog
val bottomSheetDialog = BottomSheetDialog( requireContext(),R.style.BottomSheetDialogTheme).apply{
setContent(bottomSheetView)
// or if I am using viewBinding
setContent(bottomSheetBinding.root)
}
// then in button's onClick
button.setOnClickListener{
bottomSheetDialog.show()
}

How to show user dialog box after recyclerview item click

I have an image button in my recylerview and when users click it, I want a dialog box to pop up and allow the user to edit the data in the reyclerview and save the changes.
My Adapter code
class Adapter(private var records: ArrayList<AudioRecord>, var listener: OnItemClickListener) : RecyclerView.Adapter<Adapter.ViewHolder>() {
private var editMode = false
fun isEditMode() :Boolean{return editMode}
#SuppressLint("NotifyDataSetChanged")
fun setEditMode(mode: Boolean){
if(editMode != mode){
editMode = mode
notifyDataSetChanged()
}
}
inner class ViewHolder(val binding: ItemviewLayoutBinding): RecyclerView.ViewHolder(binding.root ), View.OnClickListener, View.OnLongClickListener{
private var tvFileName : TextView = itemView.findViewById(R.id.tvFilename)
private var tvMeta : TextView = itemView.findViewById(R.id.tvMeta)
var checkbox : CheckBox = itemView.findViewById(R.id.checkBox)
val editBtn: ImageButton = itemView.findViewById(R.id.btnEdit)
init {
itemView.setOnClickListener(this)
itemView.setOnLongClickListener(this)
}
fun binding (audioRecord: AudioRecord) {
tvFileName.text = audioRecord.filename
tvMeta.text = audioRecord.duration
// checkbox.text = audioRecord.
}
override fun onClick(p0: View?) {
val position = adapterPosition
if(position != RecyclerView.NO_POSITION)
listener.onItemClickListener(position)
}
override fun onLongClick(p0: View?): Boolean {
val position = adapterPosition
if(position != RecyclerView.NO_POSITION)
listener.onItemLongClickListener(position)
return true
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(ItemviewLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
#SuppressLint("SetTextI18n", "SimpleDateFormat")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (position != RecyclerView.NO_POSITION){
val record: AudioRecord = records[position]
val sdf = SimpleDateFormat("dd/MM/yyyy")
val date = Date(record.timestamp)
val strDate = sdf.format(date)
holder.binding.tvFilename.text = record.filename
holder.binding.tvMeta.text = "${record.duration} $strDate"
if(editMode){
holder.checkbox.visibility = View.VISIBLE
holder.checkbox.isChecked = record.isChecked
holder.editBtn.visibility = View.GONE
}else{
holder.checkbox.visibility = View.GONE
holder.checkbox.isChecked = false
holder.editBtn.visibility = View.VISIBLE
}
holder.binding.btnEdit.setOnClickListener {
}
}
}
override fun getItemCount(): Int {
return records.size
}
}
Summary
When users click image button. Input dialog pops up
User must be able to edit data in recyclerview and save it.
change class like this inner class ViewHolder(val binding: ItemviewLayoutBinding), var imageListener:(position:Int)->Unit)
where you call adapter , use imageListener function. Whatever you want to do you can do it inside this function. Then call it in your adapter.
in init
editBtn.setOnClickListener {
imageListener(adapterPosition)
}

How to change recyclerview item and sub items when clicked and also get the data in the position clicked from a fragment?

I have 2 Recyclerviews in a fragment. Each item consists of 2 textview. When clicked, i want to change the color of item background and the 2 tvs and get the listofData(position) clicked also then send these s pieces of data to an activity.
The problem is 2 rvs each have its own adapter so i can't call the activity and check if data is selected from both adapters. And when i try it from the fragment i get the adapter position right but the view colors are not being changed correctly.
My RV element
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="10dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:background="#drawable/date_time_background"
android:elevation="0dp"
android:gravity="center"
android:layout_marginEnd="5dp"
android:orientation="vertical"
android:id="#+id/item_linear_layout">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/dateNameTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Sat"
android:textColor="#color/black_65"
android:textSize="16sp" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/dateNumTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="23/5"
android:textColor="#color/black_65"
android:textSize="16sp" />
</LinearLayout>
My RV adapter
class TimesAdapter(private var availableTimes: List<String>?, private val onTimeListener: OnTimeListener) :
RecyclerView.Adapter<TimesAdapter.TimeViewHolder>() {
private var itemIndex = -1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TimeViewHolder {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.date_time_element, parent, false)
return TimeViewHolder(v, onTimeListener)
}
override fun getItemCount(): Int = availableTimes?.size!!
#SuppressLint("ClickableViewAccessibility")
override fun onBindViewHolder(holder: TimeViewHolder, position: Int) {
val currentDate: String? = availableTimes?.get(position) // i.e Sun 23/5
val parts = currentDate?.split(" ")
try {
val part1 = parts?.get(0)
holder.dateNameTV.text = part1
val part2 = parts?.get(1)
holder.dateNumTV.text = part2
} catch (e: IndexOutOfBoundsException) {
e.printStackTrace()
}
holder.itemLinearLayout.setOnClickListener {
itemIndex = position
notifyItemChanged(position)
}
val ctx = holder.itemLinearLayout.context
if (itemIndex == position) {
holder.itemLinearLayout.background = (loadDrawable(ctx, R.drawable.date_time_background_selected))
holder.dateNameTV.setTextColor(loadColor(ctx, android.R.color.white))
holder.dateNumTV.setTextColor(loadColor(ctx, android.R.color.white))
} else {
holder.itemLinearLayout.background = (loadDrawable(ctx, R.drawable.date_time_background))
holder.dateNameTV.setTextColor(loadColor(ctx, R.color.black_65))
holder.dateNumTV.setTextColor(loadColor(ctx, R.color.black_65))
}
}
class TimeViewHolder(itemView: View, onTimeListener: OnTimeListener) :
RecyclerView.ViewHolder(itemView), View.OnClickListener{
var dateNameTV: TextView = itemView.findViewById(R.id.dateNameTV)
var dateNumTV: TextView = itemView.findViewById(R.id.dateNumTV)
var itemLinearLayout: LinearLayout = itemView.findViewById(R.id.item_linear_layout)
private var onTimeListener: OnTimeListener? = null
init {
itemView.setOnClickListener(this)
this.onTimeListener = onTimeListener
}
override fun onClick(v: View?) {
onTimeListener?.onTimeClick(adapterPosition, itemView)
}
}
interface OnTimeListener{
fun onTimeClick(position: Int, itemView: View)
}
}
My fragment
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [DoctorAppointmentsFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class DoctorAppointmentsFragment : Fragment(),
DatesAdapter.OnDateListener, TimesAdapter.OnTimeListener {
private lateinit var datesRV: RecyclerView
private lateinit var timesRV: RecyclerView
private lateinit var linearLayoutManager: LinearLayoutManager
private lateinit var datesAdapter: DatesAdapter
private lateinit var timesAdapter: TimesAdapter
private lateinit var dateNameTV: TextView
private lateinit var dateNumTV: TextView
private lateinit var dateLinearLayout: LinearLayout
private var dates: List<String>? = null
private var times: List<String>? = null
private var isDateSelected = false
private var isTimeSelected = false
private var selectedDate: String? = null
private var selectedTime: String? = null
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
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_doctor_appointments, container, false)
datesRV = view.findViewById(R.id.datesRV)
datesRV.setHasFixedSize(true)
linearLayoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
linearLayoutManager.isAutoMeasureEnabled = false
datesRV.layoutManager = linearLayoutManager
timesRV = view.findViewById(R.id.timesRV)
timesRV.setHasFixedSize(true)
linearLayoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
linearLayoutManager.isAutoMeasureEnabled = false
timesRV.layoutManager = linearLayoutManager
val bookNowBT = view.findViewById<Button>(R.id.bookNowBT)
bookNowBT.setOnClickListener {
if (isDateSelected && isTimeSelected) {
val i = Intent(activity, ConfirmPaymentActivity::class.java)
i.putExtra(SELECTED_DATE, selectedDate)
i.putExtra(SELECTED_TIME, selectedTime)
startActivity(i)
}
}
getDoctorAvailableAppointments("7ab63fd2461bfb0008b72f5d8c0033fs", "basic")
return view
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment DoctorAppointmentsFragment.
*/
// TODO: Rename and change types and number of parameters
#JvmStatic
fun newInstance(param1: String, param2: String) =
DoctorAppointmentsFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
private fun getDoctorAvailableAppointments(doctorID: String, type: String) {
setProgressDialog(this.requireActivity())
if (!InternetConnection.isInternetAvailable(this.requireActivity())) {
alertError(this.requireActivity(),
R.string.no_internet_connection,
R.string.check_internet_connection)
} else {
showProgressDialog()
val builder = ServiceBuilder()
val appointments = builder.getDoctorAvailableReservations()
val call = appointments.getDoctorAvailableReservations(
RequestAvailableReservations(doctorID,type))
call?.enqueue(object : Callback<ResponseAvailableReservations?> {
override fun onResponse(call: Call<ResponseAvailableReservations?>, response: Response<ResponseAvailableReservations?>) {
dismissProgressDialog()
if (!response.isSuccessful) {
alertError(requireActivity(),
R.string.code_not_200,
R.string.try_later)
return
}
val body: ResponseAvailableReservations? = response.body()
if (body == null) {
alertError(requireActivity(),
R.string.null_body,
R.string.try_later)
return
}
val status = body.status
val message = body.message
val data = body.data
if (status == null || message == null || data == null) {
alertError(requireActivity(),
R.string.null_body,
R.string.try_later)
return
}
if (status == "error") {
alertError(requireActivity(), R.string.error, message)
} else if (status == "success") {
dates = data.availableDatesList
datesAdapter = DatesAdapter(dates, this#DoctorAppointmentsFragment)
datesRV.adapter = datesAdapter
times = data.availableTimesList
timesAdapter = TimesAdapter(times, this#DoctorAppointmentsFragment)
timesRV.adapter = timesAdapter
}
}
override fun onFailure(call: Call<ResponseAvailableReservations?>, t: Throwable) {
t.printStackTrace()
dismissProgressDialog()
alertError(requireActivity(),
R.string.fail,
R.string.login_fail)
}
})
}
}
private var dateIndex = -1
override fun onDateClick(position: Int) {
selectedDate = dates?.get(position)
isDateSelected = true
Log.e("selectedDate", "selectedDate")
dateIndex = position
datesAdapter.notifyDataSetChanged()
}
private var timeIndex = -1
override fun onTimeClick(position: Int, itemView: View) {
// selectedTime = times?.get(position)
// isTimeSelected = true
// Log.e("isSelectedTime", "true")
// timeIndex = position
// timesAdapter.notifyItemChanged(position)
// Log.e("timeIndex", timeIndex.toString())
//
// val ctx = itemView.context
// if (timeIndex == position) {
// Log.e("timeIndex", "timeIndex == position")
//
// itemView.background = loadDrawable(ctx, R.drawable.date_time_background_selected)
// itemView.dateNameTV?.setTextColor(loadColor(ctx, android.R.color.white))
// itemView.dateNumTV?.setTextColor(loadColor(ctx, android.R.color.white))
// } else {
// itemView.background = (loadDrawable(ctx, R.drawable.date_time_background))
// itemView.dateNameTV.setTextColor(loadColor(ctx, R.color.black_65))
// itemView.dateNumTV.setTextColor(loadColor(ctx, R.color.black_65))
// }
}
}
Solved
For anyone interested i ended up using a very simple idea. Instead of accessing the RecyclerView selected element from outside the adapter, i did all i wanted to do inside the adapter and used 4 static variables in the Fragment. 2 booleans to check
if date and time are selected or not and 2 Strings having the date and time actually selected.
Inside the Fragment:
companion object {
#JvmStatic var isTimeSelected = false
#JvmStatic var selectedTime = ""
#JvmStatic var isDateSelected = false
#JvmStatic var selectedDate = ""
....
}
Inside the Adapter's onBindViewHolder()
holder.itemLinearLayout.setOnClickListener {
selectedDate = dates?.get(position)!!
isDateSelected = true
listener?.onDateClick(position)
selectedIndex = position
notifyDataSetChanged()
}
val ctx = holder.itemLinearLayout.context
if (selectedIndex == position) {
holder.itemLinearLayout.background = (loadDrawable(ctx, R.drawable.date_time_background_selected))
holder.dateNameTV.setTextColor(loadColor(ctx, android.R.color.white))
holder.dateNumTV.setTextColor(loadColor(ctx, android.R.color.white))
} else {
holder.itemLinearLayout.background = (loadDrawable(ctx, R.drawable.date_time_background))
holder.dateNameTV.setTextColor(loadColor(ctx, R.color.black_65))
holder.dateNumTV.setTextColor(loadColor(ctx, R.color.black_65))
}
And i made sure static variables don't have old values using this
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
isTimeSelected = false
selectedTime = ""
isDateSelected = false
selectedDate = ""
}
I know it's bad practice to put onClickListener inside onBindViewHolder but i don't konw if the static variables thing is good or bad practice. Either way it's working fine for now.
There is many ways to do this, And I recommend the first one.
Using shared ViewModels for communication between fragments in the same activity. The idea is that you will tie up the ViewModel with activity and every fragment can access this ViewModel and in this ViewModel you will have a LiveData object that holds the data and observing it from both fragments and every change in the value of LiveData will affect each fragment. Shared ViewModel
Using interfaces for communication between fragments and it's a hard thing to do these days. Basic communication between fragments
Using EventBus it's easy but not recommended while you can use ViewModel at the first solution. EventBus
Try this
TimesAdapter
class TimesAdapter(
availableTimes: List<String>
) : RecyclerView.Adapter<TimesAdapter.TimeViewHolder>() {
var availableTimes:List<String> = availableTimes
set(value) {
field = value
notifyDataSetChanged()
}
var listener:TimeSelectedListener?=null
var selectedIndex:Int = -1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TimeViewHolder {
return TimeViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.date_time_element, parent, false)
)
}
override fun getItemCount(): Int = availableTimes.count()
override fun onBindViewHolder(holder: TimeViewHolder, position: Int) {
val currentDate: String? = availableTimes[position] // i.e Sun 23/5
val parts = currentDate?.split(" ")
try {
val part1 = parts?.get(0)
val part2 = parts?.get(1)
holder.dateNameTV.text = part2
holder.dateNumTV.text = part1
} catch (e: IndexOutOfBoundsException) {
e.printStackTrace()
}
holder.itemLinearLayout.setOnClickListener {
listener?.onTimeClick(position)
selectedIndex = position
}
if (selectedIndex == position) {
// Selection Code
} else {
// De selection code
}
}
class TimeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var dateNameTV: TextView = itemView.findViewById(R.id.dateNameTV)
var dateNumTV: TextView = itemView.findViewById(R.id.dateNumTV)
var itemLinearLayout: LinearLayout = itemView.findViewById(R.id.item_linear_layout)
}
interface TimeSelectedListener {
fun onTimeClick(position: Int)
}
}
Usage from fragment
times = data.availableTimesList
timesAdapter = TimesAdapter(times).apply {
listener = object :TimesAdapter.TimeSelectedListener{
override fun onTimeClick(position: Int) {
// TODO here is where you should implement when an item is selected
}
}
}
timesRV.adapter = timesAdapter

is there a way to change the textview text in the MainActivity whenever the text in recyclerview row changed in Kotlin

I an trying to do a simple app, in which I use RecyclerView.
Here are my files.
MainActivity.kt
activity_main.xml --> which has RecyclerView (recylerViewMain)
single_row.xml --> single row for recyclerViewMain
MainAdapter.kt --> where all bindings , ViewHolder and inflating
I have also
Product.kt --> model for Products
Now, here what i am trying to do.
I have added Plus and Minus buttons on the side of itemUnit and whenever i click those items it does the job i wanted, increasing the itemUnit and eventually, itemAmount
However, this happens only on Row, and it doesn't change the Sub Total (TextView) in main_activity.xml file.
Is there a way to change the main_activity textView whenever textView in the Row of Recyclerview changes (or whenever button clicked on the row) ?
I am editing the code here.
Sub Total amount doesn't change until I click an item button (Granola, Brownie etc.). Only after I click the these items Sub Total changes and gives the updated amount.
the interface solution didnt work for me, I think I couldnt implement it right.
here are the codes ;
class MainActivity : AppCompatActivity(), ItemChangeListener {
override var subTotalAmnt: Double = 30.0
//override var subTotalAmount: Double = 50.0
//override fun onItemPriceChange(20.0)
lateinit var mRecyclerView : RecyclerView
private lateinit var sDatabase: DatabaseReference
var trId: Long = 0
var discAmnt : Double = 0.00
var unt = 1
var tr :Trans ?= null
var sb = 0.00
var disc = sb*.1
var tt = sb-disc
var list = ArrayList<Product>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
//var subto = findViewById(R.id.txtSubAmount) as TextView
onItemPriceChange(20.0)
txtSubAmount.text = subTotalAmnt.toString()
sDatabase = FirebaseDatabase.getInstance().getReference().child("Sales")
sDatabase.child("Sales").addValueEventListener(object:ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
if(p0.exists()) {
trId = p0.childrenCount
println(trId)
}
}
})
mRecyclerView = findViewById(R.id.recyclerView_main)
mRecyclerView.layoutManager = LinearLayoutManager(this)
mRecyclerView.adapter = MainAdapter(this, list)
and my Adapter Class;
class MainAdapter(val context: Context, val items : List<Product>) : RecyclerView.Adapter<MainAdapter.PartViewHolder>() {
override fun onBindViewHolder(p0: MainAdapter.PartViewHolder, p1: Int) {
p0.bindItems(items[p1])
}
var itemListener: ItemChangeListener? = null
fun setListener(listener: ItemChangeListener) {
this.itemListener = listener
}
override fun getItemId(position: Int): Long {
return super.getItemId(position)
}
override fun getItemCount(): Int {
return items.size
}
// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PartViewHolder {
// LayoutInflater: takes ID from layout defined in XML.
// Instantiates the layout XML into corresponding View objects.
// Use context from main app -> also supplies theme layout values!
val inflater = LayoutInflater.from(parent.context)
// Inflate XML. Last parameter: don't immediately attach new view to the parent view group
val view = inflater.inflate(R.layout.sinlge_row, parent, false)
return PartViewHolder(view)
}
// Binds each product in the ArrayList to a view
inner class PartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// Holds the TextView that will add each product to
fun bindItems(prd: Product) {
val textViewName = itemView.txtOrderNumber
var textViewUnit = itemView.txtItemUnit
val textViewPrice = itemView.txtItemPrice
val textViewAmount = itemView.txtItemAmount
var id = adapterPosition
var unitN: Int = 1
textViewName.text = prd.pName
textViewUnit.text = prd.pUnit.toString()
textViewPrice.text = prd.pPrice.toString()
var itemPrice = prd.pPrice
var itemAmount = itemPrice.times(unitN)
textViewAmount.text = itemAmount.toString()
itemView.btnPlus.setOnClickListener {
println("item id : " + id)
//itemListener = ItemChangeListener
itemListener?.onItemPriceChange(10.0)
// increase the Product model single unit
prd.pUnit = unitN++
// println("Here is the " +MainActivity().list.get(id))
// bind txtItemUnit from single_row to changed unitN (single unit)
textViewUnit.text = unitN.toString()
// change the Product model single pAmount
prd.pAmount = prd.pPrice.times(unitN)
// bind txtItemAmount from single_row to Product pAmount
textViewAmount.text = prd.pAmount.toString()
//txtSubAmount.txt =
//MainActivity().doSomething(subTotalAmount)
}
itemView.btnMinus.setOnClickListener(View.OnClickListener {
if (unitN >= 1) {
prd.pUnit = unitN--
println(prd.pUnit)
textViewUnit.text = unitN.toString()
textViewAmount.text = prd.pPrice.times(unitN).toString()
} else
prd.pUnit = 1
textViewUnit.text = prd.pUnit.toString()
textViewAmount.text = prd.pPrice.times(prd.pUnit).toString()
})
}
}
}
and, Interface
interface ItemChangeListener {
var subTotalAmnt : Double
fun onItemPriceChange(subTotalAmount : Double){
this.subTotalAmnt = subTotalAmount
println("onItemPriceChange "+subTotalAmnt)
}
}
I am sorry for this terrible explanation as I am not native, but willing to explain more for help.
Kind Regards.
Edited MainAdapter
class MainAdapter(val context: Context, val items : List<Product>) : RecyclerView.Adapter<MainAdapter.PartViewHolder>() {
override fun onBindViewHolder(p0: MainAdapter.PartViewHolder, p1: Int) {
p0.bindItems(items[p1])
}
lateinit var itemListener: ItemChangeListener
fun setListener(listener: ItemChangeListener) {
this.itemListener = listener
}
override fun getItemId(position: Int): Long {
return super.getItemId(position)
}
override fun getItemCount(): Int {
return items.size
}
// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PartViewHolder {
// LayoutInflater: takes ID from layout defined in XML.
// Instantiates the layout XML into corresponding View objects.
// Use context from main app -> also supplies theme layout values!
val inflater = LayoutInflater.from(parent.context)
// Inflate XML. Last parameter: don't immediately attach new view to the parent view group
val view = inflater.inflate(R.layout.sinlge_row, parent, false)
return PartViewHolder(view)
}
// Binds each product in the ArrayList to a view
inner class PartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// Holds the TextView that will add each product to
fun bindItems(prd: Product) {
val textViewName = itemView.txtOrderNumber
var textViewUnit = itemView.txtItemUnit
val textViewPrice = itemView.txtItemPrice
val textViewAmount = itemView.txtItemAmount
var id = adapterPosition
var unitN: Int = 1
textViewName.text = prd.pName
textViewUnit.text = prd.pUnit.toString()
textViewPrice.text = prd.pPrice.toString()
var itemPrice = prd.pPrice
var itemAmount = itemPrice.times(unitN)
textViewAmount.text = itemAmount.toString()
itemView.btnPlus.setOnClickListener {
println("item id : " + id)
//itemListener = ItemChangeListener
itemListener.onItemPriceChange(20.0)
// increase the Product model single unit
prd.pUnit = unitN++
// println("Here is the " +MainActivity().list.get(id))
// bind txtItemUnit from single_row to changed unitN (single unit)
textViewUnit.text = unitN.toString()
// change the Product model single pAmount
prd.pAmount = prd.pPrice.times(unitN)
// bind txtItemAmount from single_row to Product pAmount
textViewAmount.text = prd.pAmount.toString()
//txtSubAmount.txt =
//MainActivity().doSomething(subTotalAmount)
}
itemView.btnMinus.setOnClickListener(View.OnClickListener {
if (unitN >= 1) {
prd.pUnit = unitN--
println(prd.pUnit)
textViewUnit.text = unitN.toString()
textViewAmount.text = prd.pPrice.times(unitN).toString()
} else
prd.pUnit = 1
textViewUnit.text = prd.pUnit.toString()
textViewAmount.text = prd.pPrice.times(prd.pUnit).toString()
})
}
}
}
I think you can do it using interface.
First you create interface definition (in a separate file or in adapter).
-- for eg: interface ItemChangeListener { fun onItemPriceChange(pass itemprice or totalprice) }
next you create an object of listener inside adapter
Like lateinit var listener: ItemChangeListener inside adapter (set using fun setListener(listener:ItemChangeListener){ // code } ).
Let the main activity implement the interface.
pass the mainActivity this to the adapter.setListener
on Clicking the button, you can call listener.onItemPriceChange(pass itemprice or totalprice)
you can get the parameter on main activity in this way.
Check this link for some details.. Same can be done using constructor parameter I guess.