CopytoClipboard not working in Adapter to show in recyclerview - android

I have a recyclerview with adapter (to show external strings)and I'm trying to click a button "copy" so that it copies the strings in view to the clipboard.
CardLayout.xml Button to use as copy button is android:id="#+id/copyactivity"
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="0dp"
app:cardElevation="0dp"
app:cardMaxElevation="3dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
tools:context="ui.CardLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/idActivityName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="14dp"
android:layout_marginRight="10dp"
android:text="#string/placeholdertasktitle"
android:textColor="#color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/idActivityDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/idActivityName"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="30dp"
android:drawablePadding="2dp"
android:text="#string/let_s_get_started_creating_a_photo_album_for_you_so_you_can_share_with_the_kids_in_the_near_future"
android:textColor="#color/black"
android:textSize="15sp"
tools:ignore="UnknownId" />
<View
android:id="#+id/divider"
android:layout_width="380dp"
android:layout_height="2dp"
android:layout_below="#id/idActivityDescription"
android:layout_alignStart="#id/idActivityName"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:background="#EDEDED" />
<Button
android:id="#+id/saves"
style="#style/cardbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/divider"
android:layout_alignStart="#id/idActivityName"
android:background="#drawable/cardbutton"
android:contentDescription="#string/saves"
android:stateListAnimator="#null"
android:text="Save"
android:textAlignment="textStart"
android:textColor="#595959"
android:textSize="12sp"
app:icon="#drawable/ic_saves_blank"
app:iconGravity="textStart"
app:iconTint="#595959" />
<Button
android:id="#+id/calendar"
style="#style/cardbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/divider"
android:layout_marginStart="35dp"
android:layout_marginEnd="35dp"
android:layout_toStartOf="#+id/copyactivity"
android:layout_toEndOf="#id/saves"
android:background="#drawable/cardbutton"
android:contentDescription="#string/saves"
android:stateListAnimator="#null"
android:text="#string/calendar"
android:textAlignment="textStart"
android:textColor="#595959"
android:textSize="12sp"
app:icon="#drawable/ic_calendar"
app:iconGravity="textStart"
app:iconTint="#595959" />
<Button
android:id="#+id/copyactivity"
style="#style/cardbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/divider"
android:layout_alignEnd="#id/divider"
android:background="#drawable/cardbutton"
android:contentDescription="#string/saves"
android:stateListAnimator="#null"
android:text="#string/copy"
android:textAlignment="textStart"
android:textColor="#595959"
android:textSize="12sp"
app:icon="#drawable/ic_copytext"
app:iconGravity="textStart"
app:iconTint="#595959" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
And this is my TaskAdapter.kt
package com.example.what2do_v6.Adapter
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context.CLIPBOARD_SERVICE
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.recyclerview.widget.RecyclerView
import com.example.what2do_v6.Model.TaskResponse
import com.example.what2do_v6.R
import com.example.what2do_v6.Repository.TaskRepository
import com.example.what2do_v6.databinding.CardLayoutBinding
import com.example.what2do_v6.ui.CardLayout
import android.content.Context as ContentContext
class TaskAdapter: RecyclerView.Adapter<TaskAdapter.TaskViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = CardLayoutBinding.inflate(layoutInflater, parent, false)
return TaskViewHolder(binding)
}
override fun onBindViewHolder(holder: TaskViewHolder, position: Int) {
holder.bind(TaskRepository.list[position])
}
override fun getItemCount(): Int {
return TaskRepository.list.size
}
class TaskViewHolder(private val binding: CardLayoutBinding) : RecyclerView.ViewHolder(binding.root){
var bCopyText: Button? = null
fun bind(task: TaskResponse) {
binding.idActivityName.text = task.title
binding.idActivityDescription.text = task.description
bCopyText = findViewById<View>(R.id.copyactivity) as Button
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
bCopyText!!.setOnClickListener {
val clipData = ClipData.newPlainText("", task.description,)
clipboardManager.setPrimaryClip(clipData)
Toast.makeText(RecyclerView, "message...", Toast.LENGTH_SHORT).show()
}
}
private fun getSystemService(clipboardService: String): Any {
}
}
}
There are two lines in my code that are throwing up problems:
bCopyText = findViewById<View>(R.id.copyactivity) as Button
and
Toast.makeText(RecyclerView, "message...", Toast.LENGTH_SHORT).show()
I've tried different variances of the two above lines and research many articles on StackOverflow but with no winner, can anyone help?

Managed to get this working utilising help from Gobu:
class TaskViewHolder(private val binding: CardLayoutBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(task: TaskResponse) {
binding.idActivityName.text = task.title
binding.idActivityDescription.text = task.description
binding.copyactivity
binding.copyactivity.setOnClickListener {
val clipboardManager = itemView.context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText("", task.title +"\n\n" + task.description)
clipboardManager.setPrimaryClip(clipData)
Toast.makeText(itemView.context, "Activity copied to clipboard", Toast.LENGTH_SHORT).show()
}
}

First of all, if you're using ViewBinding, why would you declare a variable for your button? Just get it from the binding.
Second, it's giving you an error, because you're making sure (!!) that the button you declare isn't null, but most likely it is, because you're trying to get the ID of a view you don't have.
Third, RecyclerView is not a context, if you are using ViewBinding, you get the context from binding.root.context

Related

Duplicate and overlapping text in TextView when using LiveData or StateFlow

To be as clear as possible, I'm going to show the problem with a gif.
As you can see, the text is overlapping in my emulator and I don't know how to solve it, I don't know what I'm doing wrong.
It should be displayed correctly without overlapping. What I'm doing is subscribing to changes in both LiveData and StateFlow. When I press the button, I make it change the value, but instead of changing, it seems to return both values at once (Default and Hello). Could it be that the view is not refreshing? Or could it be a problem with my emulator?
Anyway, I'll leave all the code here in case you can figure it out.
FlowsFragment.kt
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.rr.stateflow_livedata_flow_sharedflow.databinding.FragmentFlowsBinding
import kotlinx.coroutines.flow.collectLatest
class FlowsFragment : Fragment(R.layout.fragment_flows) {
private lateinit var binding: FragmentFlowsBinding
private val viewModel: FlowsViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentFlowsBinding.bind(view)
binding.btnLiveData.setOnClickListener {
viewModel.triggerLiveData()
}
binding.btnStateFlow.setOnClickListener {
viewModel.triggerStateFlow()
}
subscribeToObservables()
}
private fun subscribeToObservables() {
viewModel.liveData.observe(viewLifecycleOwner) {
binding.tvLiveData.text = it
}
lifecycleScope.launchWhenStarted {
viewModel.stateFlow.collectLatest {
binding.tvStateFlow.text = it
}
}
}
}
FlowsViewModel.kt
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.*
class FlowsViewModel : ViewModel() {
private val _liveData = MutableLiveData("Default LiveData")
val liveData: LiveData<String> = _liveData
private val _stateFlow = MutableStateFlow("Default StateFlow")
val stateFlow: StateFlow<String> = _stateFlow.asStateFlow()
fun triggerLiveData() {
_liveData.value = "Hello LiveData!"
}
fun triggerStateFlow() {
_stateFlow.value = "Hello StateFlow!"
}
}
fragment_flows.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".FlowsFragment">
<TextView
android:id="#+id/tvLiveData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-condensed"
android:text="Hello World!"
android:textColor="#color/black"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnLiveData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="LIVEDATA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvLiveData" />
<TextView
android:id="#+id/tvStateFlow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-condensed"
android:text="Hello World!"
android:textColor="#color/black"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnLiveData" />
<Button
android:id="#+id/btnStateFlow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="STATEFLOW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvStateFlow" />
<TextView
android:id="#+id/tvFlow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-condensed"
android:text="Hello World!"
android:textColor="#color/black"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnStateFlow" />
<Button
android:id="#+id/btnFlow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="FLOW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvFlow" />
<TextView
android:id="#+id/tvSharedFlow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-condensed"
android:text="Hello World!"
android:textColor="#color/black"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnFlow" />
<Button
android:id="#+id/btnSharedFlow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="SHAREDFLOW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvSharedFlow" />
</androidx.constraintlayout.widget.ConstraintLayout>
I hope that the text doesn't overlap and returns the latest value of LiveData and StateFlow.
Edit with more info: The application displays the view directly from the fragment, MainActivity only has a fragment container that I show below.
MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.add
import androidx.fragment.app.commit
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager.commit {
setReorderingAllowed(true)
add<FlowsFragment>(R.id.mainActivityFragmentContainer)
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/mainActivityFragmentContainer"
android:name="com.rr.stateflow_livedata_flow_sharedflow.FlowsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_flows" />
</androidx.constraintlayout.widget.ConstraintLayout>
EDIT This is not the solution, it's a bad way to solve the problem.
I just have to add this line of code in fragment_flows.xml.
android:background="#color/white"
Just by setting the background of the fragment to white, the text is no longer duplicated and overlapping.
Does anyone know why this happens? Why does what is drawn on the screen stick to the background if there is no background in the fragment layout?

How to implement a SearchView to search specific buttons in a LinearLayout?

I'm opening this discussion because I need help for a school project.
At the moment, I have a scrollable activity with some buttons, everything is built like this:
a constraint layout, with SearchView, ScrollView and a BottomNavigationBar, and inside the ScrollView there's a LinearLayout with all my buttons.
Here is the full xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SearchView
android:id="#+id/BarraRicerca"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:gravity="top"
android:iconifiedByDefault="false"
android:imeOptions="actionDone"
android:queryHint="Cerca un tutorial"
android:labelFor="#id/BarraRicerca"
android:suggestionRowLayout="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:fillViewport="false"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginTop="60dp"
app:layout_constraintBottom_toTopOf="#id/BottomBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/BarraRicerca">
<LinearLayout
android:id="#+id/Linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.Flipper.FlipperList">
<Button
android:id="#+id/FlintstonesF"
android:layout_width="300dp"
android:layout_height="49dp"
android:layout_gravity="center_horizontal"
android:layout_margin="100dp"
android:text="Flintstones" />
<Button
android:id="#+id/tutorial2"
android:layout_width="300dp"
android:layout_height="49dp"
android:layout_gravity="center_horizontal"
android:layout_margin="200dp"
android:layout_weight="1"
android:text="ciao" />
<Button
android:id="#+id/tutorial3"
android:layout_width="300dp"
android:layout_height="49dp"
android:layout_gravity="center_horizontal"
android:layout_margin="100dp"
android:layout_weight="1"
android:text="hello" />
</LinearLayout>
</ScrollView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/BottomBar"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and the result is this, everything works
Now I need to implement the SearchView to actually search through my buttons, For example if I search Flintstones (or Flint, or Fli etc.) I want the Flintstones button to be the only one shown. The same goes for "ciao" and so on, I will obviously have more buttons than these.
I tried every possible solution I found on the internet but nothing seems to work. This is my code
package com.example.Flipper
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.example.pinballacademy.R
class FlipperList : AppCompatActivity() {
val passaFlintstones: Button get() = findViewById(R.id.FlintstonesF)
val bottone2: Button get() = findViewById(R.id.tutorial2)
val bottone3: Button get() = findViewById(R.id.tutorial3)
val searchbar: SearchView get() = findViewById(R.id.BarraRicerca)
val linLay: LinearLayout get() = findViewById(R.id.Linearlayout)
var listaFlipper: ArrayList<String> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_flipper_list)
/* listaFlipper.clear();
listaFlipper.add(passaFlintstones.text.toString())
listaFlipper.add(bottone2.text.toString())
listaFlipper.add(bottone3.text.toString())*/
searchbar.isSubmitButtonEnabled=true
searchbar.setOnQueryTextListener() {
fun onQueryTextChange(newText: String?): Boolean {
filter(newText)
return true
}
fun onQueryTextSubmit(query: String?): Boolean {
filter(query)
return true
}
// Do your task here
}
passaFlintstones.setOnClickListener()
{
val intent = Intent(this, FlintstonesScheda::class.java)
//avvia registrazione
startActivity(intent)
// finish()
}
}
fun filter(searchText: String?) {
for (i in 0 until linLay.childCount) {
val button: Button = linLay.getChildAt(i) as Button
if (button.text.toString().contains(searchText!!)) {
button.isVisible=true
} else button.isVisible=false
}
}
private fun SearchView.setOnQueryTextListener(function: () -> Unit) {
}
}
As you can see I'm not even near to a solution, I hope you can help! Thank you!

custom array adapter , can't access variables

im new on android
i try create my custom arrayadapter.
I want to access the layout variables from tmpview, but I can't do that. The man in the video I watched was doing the same and he could reach
how can i reach this variables ?
I also added "kotlin-android-extensions" to the plugins section
package com.example.burclar
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import java.security.AccessControlContext
import java.util.zip.Inflater
class myArrayAdapter(var gelencontext: Context, resource: Int,var burcAdlar: ArrayList<String>, var burcTarihler: ArrayList<String>, var burcResimler: ArrayList<Int>): ArrayAdapter<String>(gelencontext, resource){
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var tmpview = convertView
if(tmpview == null){
tmpview = LayoutInflater.from(gelencontext).inflate(R.layout.my_list_view_element,null)
}
tmpview.burcMiniYazi = burcAdlar[position] //there is a problem "unresolved reference"
tmpview.burcMiniResim = burcResimler[position] //there is a problem "unresolved reference"
tmpview.burcMiniTarih = burcTarihler[position] //there is a problem "unresolved reference"
return tmpview!!
}
}
my_list_view_element.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/burcMiniYazi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:text="TextView"
android:textSize="14sp"
app:layout_constraintStart_toEndOf="#+id/burcMiniResim"
app:layout_constraintTop_toTopOf="#+id/burcMiniResim" />
<ImageView
android:id="#+id/burcMiniResim"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/burcMiniTarih"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#+id/burcMiniResim"
app:layout_constraintStart_toEndOf="#+id/burcMiniResim" />
<View
android:id="#+id/divider2"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="24dp"
android:background="?android:attr/listDivider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/burcMiniResim" />
</android.support.constraint.ConstraintLayout>
thanks
enter image description here

RecyclerView OnClickListener not firing

I aim to make a simple to-do list and for that I have used RecyclerView. My issue is that the OnClickListener() in the RecyclerView adapter class is not getting called. I have no idea why this is happening.
P.S I am fairly new to Android Development so please excuse any stupid mistake I have made.
RecyclerView adapter class:
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView
import kotlinx.android.synthetic.main.activity_task.*
import kotlinx.android.synthetic.main.item_todo.view.*
import java.text.SimpleDateFormat
import java.util.*
class TodoAdapter(val list: List<TodoModel>) : RecyclerView.Adapter<TodoAdapter.TodoViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
return TodoViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.item_todo, parent, false)
)
}
override fun getItemCount() = list.size
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.bind(list[position],position)
holder.itemView.setOnClickListener {
Log.e("dbg","click")
Toast.makeText(holder.itemView.context,"This should be called",Toast.LENGTH_SHORT).show()
}
}
override fun getItemId(position: Int): Long {
return list[position].id
}
class TodoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(todoModel: TodoModel,pos:Int) {
with(itemView) {
txtShowTitle.text = todoModel.title
txtShowTask.text = todoModel.description
txtShowCategory.text = todoModel.category
updateTime(todoModel.time)
updateDate(todoModel.date)
}
}
private fun updateTime(time: Long) {
//4:55 AM
val myformat = "h:mm a"
val sdf = SimpleDateFormat(myformat)
itemView.txtShowTime.text = sdf.format(Date(time))
}
private fun updateDate(time: Long) {
//Mon, 5 Jan 2020
val myformat = "EEE, d MMM yyyy"
val sdf = SimpleDateFormat(myformat)
itemView.txtShowDate.text = sdf.format(Date(time))
}
}
}
item xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/item"
app:cardBackgroundColor="#818181"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginBottom="10dp"
android:elevation="10dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:theme="#style/allDetailsTextColor">
<TextView
android:id="#+id/txtShowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginEnd="5dp"
android:layout_toStartOf="#+id/txtShowCategory"
android:ellipsize="end"
android:maxLines="1"
android:textSize="20sp"
android:textStyle="bold"
tools:text="Task Title" />
<TextView
android:id="#+id/txtShowTask"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtShowTitle"
android:layout_alignParentStart="true"
android:layout_marginTop="4dp"
android:textSize="16sp"
tools:text="Task Subtitle" />
<TextView
android:id="#+id/txtShowCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#drawable/spinner_item_background"
android:padding="4dp"
android:ellipsize="end"
android:maxWidth="80dp"
android:maxLines="1"
android:textSize="12sp"
tools:text="Category" />
<TextView
android:id="#+id/txtShowDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtShowTask"
android:layout_marginTop="30dp"
android:layout_toLeftOf="#+id/txtShowTime"
android:layout_marginRight="10dp"
android:visibility="visible"
tools:text="22-12-2017" />
<TextView
android:id="#+id/txtShowTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtShowTask"
android:layout_alignParentEnd="true"
android:layout_marginTop="30dp"
android:fontFamily="#font/montserrat"
android:visibility="visible"
tools:text="22:12 AM" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
Any help would be appreciated.

Onclick not responding using kotlin in android studio

I am new to kotlin. Here I am implementing login screen to my application, and the implementation of class using kotlin as below with xml file, but when I am clicking on button or textview onclick is not responding. please help me to resolve this.
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.*
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
class login : AppCompatActivity(), View.OnClickListener {
#BindView(R.id.title) var title: TextView? = null
#BindView (R.id.email) var email : TextView? = null
#BindView (R.id.password) var pass : TextView? = null
#BindView (R.id.tv_fp) var fp : TextView? = null
#BindView (R.id.tv_register) var register : TextView? = null
#BindView (R.id.btn_login ) var login : Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
ButterKnife.bind(this)
// change to register!!.setOnClickListener(this) to make sure your view is injected
register?.setOnClickListener(this)
login?.setOnClickListener(this)
}
// v--- change the parameter type View? to View, it is never be null
override fun onClick(p0: View?) {
// v--- remove it
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
// v--- remove the redundant safe-call ?.
if(p0?.id == R.id.tv_register){
Log.e("login", "i")
var intent = Intent(this#login, signin:: class.java)
startActivity(intent)
}
if(p0?.id == R.id.btn_login){
Toast.makeText(this, "Login Button clicked", Toast.LENGTH_SHORT)
}
}
}
and layout file of this class is as below, i used constrainedLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:layout_margin="20dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Email id"
android:textCursorDrawable="#drawable/cursor_color"
android:backgroundTint="#color/skyBlue"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="100dp"
app:layout_constraintHorizontal_bias="0.0"
android:inputType="textEmailAddress"/>
<android.support.design.widget.TextInputEditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Password"
android:textCursorDrawable="#drawable/cursor_color"
android:id="#+id/password"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:inputType="textPassword"/>
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="36dp"
android:background="#color/yellow"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="#+id/password"
android:layout_marginStart="4dp" />
<TextView
android:id="#+id/tv_fp"
android:layout_width="171dp"
android:layout_height="22dp"
android:text="FORGOT PASSWORD?"
android:background="?attr/selectableItemBackground"
tools:layout_editor_absoluteY="301dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/tv_register"
android:layout_marginTop="6dp"
android:layout_marginBottom="7dp"
app:layout_constraintBottom_toTopOf="#+id/btn_login" />
<TextView
android:id="#+id/tv_register"
android:layout_width="156dp"
android:layout_height="18dp"
android:layout_marginStart="8dp"
android:text="Register"
android:background="?attr/selectableItemBackground"
android:gravity="right"
app:layout_constraintStart_toEndOf="#+id/tv_fp"
app:layout_constraintTop_toTopOf="#+id/button"
android:layout_marginTop="58dp"
app:layout_constraintBottom_toBottomOf="#+id/btn_login"
/>
</android.support.constraint.ConstraintLayout>

Categories

Resources