Home > Enterprise >  I want to make a call from my adapter class in recyclerview
I want to make a call from my adapter class in recyclerview

Time:11-12

normal intent isn't working in adapter class for action call. how can I make a call from my adapter class for recyclerview?

  callButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String phoneNo2 = itemNumber.getText().toString();
        Intent intent = new Intent(Intent.ACTION_CALL, 
                                   Uri.parse("tel:"   phoneNo2));
       context.startActivity(intent);
    }
});

I had to add this context to write startactivity..Otherwise startactivity isn't coming.

CodePudding user response:

View has a reference to Context (documentation):

v.context.startActivity(intent)

CodePudding user response:

  1. add recyclerview to design

  2. define recyclerview and onclick call button like below,

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>

    {

     // RecyclerView recyclerView;  
     public MyAdapter(MyListData[] listdata) {  
    
     }  
     @Override  
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
         LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());  
         View Itemis= layoutInflater.inflate(R.layout.your_xml_file, parent, false);  
         ViewHolder viewHolder = new ViewHolder(Itemis);  
         return viewHolder;  
     }  
    
     @Override  
     public void onBindViewHolder(ViewHolder holder, int position) {  
    
     }  
    
     public static class ViewHolder extends RecyclerView.ViewHolder {  
         public Button callbutton;  
    
         public ViewHolder(View itemView) {  
             super(itemView);  
             this.callbutton = itemView.findViewById(R.id.callbutton);  
             ///here is button clikc
    
             callButton.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     String phoneNo2 = itemNumber.getText().toString();
                     Intent intent = new Intent(Intent.ACTION_CALL, 
                     Uri.parse("tel:"   phoneNo2));
                     v.context.startActivity(intent);
                 }
             }); 
         }  
     }
    

    }

if any query comment it.

CodePudding user response:

  • add permission in manifest...
 <uses-permission android:name="android.permission.CALL_PHONE" />
  • add this code on click..
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"   "YOUR_PHONE_NUMBER"));
context.startActivity(intent);
  • Related