I am a beginner Level Android Learner ,using kotlin. I want to call the contact no.(whatever user save their profile) by pressing this marked icon(phone icon). Idk how can I do this properly. I spent much time to find out this solution but I couldn't.
Here is Adapter Code:
class PostAdapter(private var postUser: ArrayList<PostUser>):
RecyclerView.Adapter<PostAdapter.PostViewHolder>() {
class PostViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val name: TextView = itemView.findViewById(R.id.patientNameTextView)
val bloodGroup: TextView = itemView.findViewById(R.id.patientBGTextView)
val contact: TextView = itemView.findViewById(R.id.patientCNTextView)
val patientAddress: TextView = itemView.findViewById(R.id.PatientaddressTextView)
val patientDistrict: TextView = itemView.findViewById(R.id.PatientDistrictTextView)
val patientAge: TextView = itemView.findViewById(R.id.patientAgeTextView)
val bloodNeed: TextView = itemView.findViewById(R.id.patientBNTextView)
val patientType: TextView = itemView.findViewById(R.id.patientTypeTextView)
}
fun setFilteredList(postUser: ArrayList<PostUser>){
this.postUser= postUser
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
val itemView=LayoutInflater.from(parent.context).inflate(R.layout.group_donor,parent,false)
return PostViewHolder(itemView)
}
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
val pUser:PostUser=postUser[position]
holder.name.text=pUser.name
holder.patientAge.text=pUser.age
holder.patientAddress.text=pUser.address
holder.bloodNeed.text=pUser.bloodBag
holder.bloodGroup.text=pUser.bloodGroup
holder.contact.text=pUser.mobile
holder.patientType.text=pUser.illness
holder.patientDistrict.text=pUser.district
}
override fun getItemCount(): Int {
return postUser.size
}
}
The id of this icon is "@+id/callButton". here is my data class's code:
data class PostUser(
val name:String?=null,
val age:String?=null,
val address:String?=null,
val mobile:String?=null,
val bloodGroup:String?=null,
val bloodBag:String?=null,
val district:String?=null,
val illness:String?=null
)
And finally my Activity Code is here:
private lateinit var userList: ArrayList<com.example.blood.utilities.PostUser>
private lateinit var db: FirebaseFirestore
private lateinit var myAdapter: PostAdapter
private lateinit var searchView: SearchView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_o_negative)
db = FirebaseFirestore.getInstance()
recyclerView = findViewById(R.id.userList)
searchView = findViewById(R.id.searchView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
userList = arrayListOf()
myAdapter = PostAdapter(userList)
recyclerView.adapter = myAdapter
Post()
}
private fun Post() {
db.collection("patients")
.addSnapshotListener(object : EventListener<QuerySnapshot> {
override fun onEvent(
value: QuerySnapshot?,
error: FirebaseFirestoreException?) {
if(error!=null){
Log.e("Firestore Error",error.message.toString())
return
}
for (db : DocumentChange in value?.documentChanges!!){
if(db.type== DocumentChange.Type.ADDED){
userList.add(db.document.toObject(com.example.blood.utilities.PostUser::class.java))
}
}
myAdapter.notifyDataSetChanged()
}
})
}
}
Use this permission (In Manifest file )
<uses-permission android:name="android.permission.CALL_PHONE" />
Call this method with number in parameter when user clicked that icon.
fun dialPhoneNumber(phoneNumber: String) {
val intent = Intent(Intent.ACTION_DIAL).apply {
data = Uri.parse("tel:$phoneNumber")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
You can set onclick listener in onBindViewHolder method or you can also create interface and get click listener in activity.
eg:- In onBindViewHolder ...
holder.callIcon.setOnClickListener {
dialPhoneNumber(pUser.mobile)
}