Home > Software engineering >  Direct phone call from RecyclerView. (Kotlin)
Direct phone call from RecyclerView. (Kotlin)

Time:12-23

Currently I am a Beginner,

I am using firestore, from where all the Data are showing in RecyclerView like name, address, phone number etc. Now, I want to make phone calls using the "button" besides "phone number ".

In which position do I have to add Call Intent?


    private lateinit var recyclerView: RecyclerView
    private lateinit var userList: ArrayList<User>
    private lateinit var db: FirebaseFirestore
    private lateinit var myAdapter: MyAdapter
    private lateinit var searchView: SearchView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_newsfeed)

        db = FirebaseFirestore.getInstance()
        recyclerView = findViewById(R.id.userList)
        searchView=findViewById(R.id.searchView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.setHasFixedSize(true)
        userList = arrayListOf()
        myAdapter = MyAdapter(userList)
        recyclerView.adapter = myAdapter
        EventChangeListener()
        searchView.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                filterList(newText)
                return true
            }

        })
    }

    private fun filterList(query: String?) {
        if(query!=null){
            val filteredList=ArrayList<User>()
            for (i in userList){
                if(i.city!!.lowercase(Locale.ROOT).contains(query)){
                    filteredList.add(i)
                }
            }
            if(filteredList.isEmpty()){
            }else{
                myAdapter.setFilteredList(filteredList)
            }
        }
    }

    private fun EventChangeListener() {
        db = FirebaseFirestore.getInstance()
        db.collection("users").orderBy("bloodGroup", Query.Direction.ASCENDING)
            .addSnapshotListener(object : com.google.firebase.firestore.EventListener<QuerySnapshot> {
                override fun onEvent(
                    value: QuerySnapshot?,
                    error: FirebaseFirestoreException?
                ) {
                    if (error != null) {
                        Log.e("Firestore Error", error.message.toString())
                        return
                    }
                    for (dc: DocumentChange in value?.documentChanges!!) {
                        if (dc.type == DocumentChange.Type.ADDED) {
                            userList.add(dc.document.toObject(User::class.java))

                        }
                    }
                    myAdapter.notifyDataSetChanged()
                }
            }
            )
    }

MyAdapter code is here:


class MyAdapter(private var userList: ArrayList<User>) :
    RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name: TextView = itemView.findViewById(R.id.nameTextView)
        val bloodGroup: TextView = itemView.findViewById(R.id.bGTextView)
        val contact: TextView = itemView.findViewById(R.id.cNTextView)
        val cityName: TextView = itemView.findViewById(R.id.addressTextView)

    }
    fun setFilteredList(userList: ArrayList<User>){
        this.userList= userList
        notifyDataSetChanged()
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.donor_list, parent, false)
        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val user: User = userList[position]

        holder.name.text = user.name
        holder.contact.text = user.contactNumber
        holder.bloodGroup.text = user.bloodGroup
        holder.cityName.text = user.city

    }


    override fun getItemCount(): Int {
        return userList.size
    }

}

How can I make a phone call directly by their given number through tapping an icon, in RecyclerView?

Thank you!

CodePudding user response:

You can do phone calls through Intent here is the demo code

    val phoneIntent = Intent(Intent.ACTION_CALL)
    phoneIntent.data = Uri.parse("tel:1234567890")
    startActivity(phoneIntent)   

add relevant permission to your manifest

<uses-permission android:name="android.permission.CALL_PHONE" /> 

CodePudding user response:

<uses-permission android:name="android.permission.CALL_PHONE" />

I think you are looking for something like this:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"   "Your Phone_number"));
startActivity(intent);
  • Related