Background color isnt showing in android studio?

I have added a color picker to my simple note app. Right now the problem is when the user clicks to update the note then the background color isn’t showing. How can I fix that?

………………………………………….. ………………………………………….. ………………………………………….. ………………………………………………………………………………

Adapter.kt

package com.example.noteapp.ui.Adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.view.menu.ActionMenuItemView
import androidx.cardview.widget.CardView
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import com.example.noteapp.databinding.ItemNotesBinding
import com.example.noteapp.model.Notes
import com.example.noteapp.ui.Fragments.HomeFragment
import com.example.noteapp.ui.Fragments.HomeFragmentDirections
import com.google.android.material.card.MaterialCardView
import io.noties.markwon.AbstractMarkwonPlugin
import io.noties.markwon.Markwon
import io.noties.markwon.MarkwonVisitor
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
import io.noties.markwon.ext.tasklist.TaskListPlugin
import org.commonmark.node.SoftLineBreak

class NotesAdapter(val requirContext : Context, val notesList : List<Notes>) :
    RecyclerView.Adapter<NotesAdapter.notesViewHolder>() {

    class notesViewHolder(val binding: ItemNotesBinding) : RecyclerView.ViewHolder(binding.root) {
        val content : TextView = binding.notessubtitle
        val markwon = Markwon.builder(itemView.context)
            .usePlugin(StrikethroughPlugin.create())
            .usePlugin(TaskListPlugin.create(itemView.context))
            .usePlugin(object : AbstractMarkwonPlugin() {
                override fun configureVisitor(builder: MarkwonVisitor.Builder) {
                    super.configureVisitor(builder)
                    builder.on(
                        SoftLineBreak::class.java
                    ){visitor, _ -> visitor.forceNewLine()}
                }

            })
            .build()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): notesViewHolder {
        return notesViewHolder(ItemNotesBinding.inflate(LayoutInflater.from(parent.context),parent,
            false
            )
        )
    }

    override fun onBindViewHolder(holder: notesViewHolder, position: Int) {

        val data = notesList[position]

        holder.binding.notestitle.text = data.title
        holder.binding.notessubtitle.text = data.notes
        holder.binding.noteItemLayoutParent.setCardBackgroundColor(data.color)
        holder.binding.noteItemLayoutParent.transitionName="recyclerView_${data.id}"

        holder.binding.root.setOnClickListener {
               val action = HomeFragmentDirections.actionHomeFragmentToEditnotesFragmemt(data)
            Navigation.findNavController(it).navigate(action)
        }

    }

    override fun getItemCount() = notesList.size
}

EditNoteFragment

package com.example.noteapp.ui.Fragments

import android.graphics.Color
import android.os.Bundle
import android.text.format.DateFormat
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.fragment.navArgs
import com.example.noteapp.R
import com.example.noteapp.ViewModel.NotesViewModel
import com.example.noteapp.databinding.BottomsheetlayoutBinding
import com.example.noteapp.databinding.FragmentEditnotesFragmemtBinding
import com.example.noteapp.model.Notes
import com.example.noteapp.ui.Adapter.NotesAdapter
import com.example.noteapp.utils.hideKeyboard
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.transition.MaterialContainerTransform
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.*


class EditnotesFragmemt : Fragment() {

    val oldNotes by navArgs<EditnotesFragmemtArgs>()
    lateinit var binding : FragmentEditnotesFragmemtBinding
    val viewModel : NotesViewModel by viewModels()
    private var color = -1
    private lateinit var result : String
    private val job = CoroutineScope(Dispatchers.Main)


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = FragmentEditnotesFragmemtBinding.inflate(layoutInflater,container,false)

        binding.editingedittextnote.setText(oldNotes.data.notes)
        binding.editingedittexttitle.setText(oldNotes.data.title)

        val animation = MaterialContainerTransform().apply {
            drawingViewId = R.id.createFragment
            scrimColor = Color.TRANSPARENT
            duration = 300L
        }
        sharedElementEnterTransition=animation
        sharedElementReturnTransition=animation

        ViewCompat.setTransitionName(
            binding.editFragmentxmlid,"recyclerView_${oldNotes.data?.id}"
        )

        binding.backbutton.setOnClickListener {
            requireView().hideKeyboard()
            Navigation.findNavController(it).popBackStack()

        }

        binding.editingedittextnote.setText(oldNotes.data.notes)
        binding.editingedittexttitle.setText(oldNotes.data.title)

        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 {
                            editFragmentxmlid.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
            }
            bottomSheetDialog.setContentView(bottomSheetBinding.root)
            bottomSheetDialog.show();
        }

        binding.btneditdonenotes.setOnClickListener {
            updateNotes(it)
        }

        try {
            binding.editingedittextnote.setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus)
                {
                    binding.bottombar.visibility = View.VISIBLE
                    binding.editingedittextnote.setStylesBar(binding.styleBar)
                }else{
                    binding.bottombar.visibility = View.GONE
                }
            }
        }catch (e: Throwable) {
            Log.d("TAG", e.stackTraceToString())
        }

        return binding.root
    }
    private fun updateNotes(it: View?) {

            val title = binding.editingedittexttitle.text.toString()
            val notes = binding.editingedittextnote.text.toString()

            val d = Date()
            val notesDate: CharSequence = DateFormat.format("MMM d, yyy ", d.time)

            val data = Notes(
                oldNotes.data.id,
                title = title,
                notes = notes,
                date = notesDate.toString(),
                color= color,
            )
        binding.apply {
            job.launch {
                editFragmentxmlid.setBackgroundColor(data.color)
            }
            toolbarfragmentnotecontent.setBackgroundColor(data.color)
            bottombar.setBackgroundColor(data.color)
            }
        activity?.window?.statusBarColor=data.color
        result = "Note Saved"
        setFragmentResult(
            "key",
            bundleOf("bundleKey" to result)
        )

            viewModel.updateNotes(data)
            Toast.makeText(context, "Edit Note Successfully", Toast.LENGTH_SHORT).show()
            Navigation.findNavController(it!!).navigate(R.id.action_editnotesFragmemt_to_homeFragment)
    }


}

Leave a Comment