androidandroid-studioandroid-phone-call

How to Implement Contact Call Button in setOnItemLongClickListener?


I want to implement a Call A Number Button in My App, How Can I Do that?

mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {

        //Alert dialog to display options of update and delete

        final CharSequence [] items = {"Update","Delete","Call"};
        AlertDialog.Builder dialog = new AlertDialog.Builder(RecordListActivity.this);

        dialog.setTitle("Choose an Action");

        dialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if(i == 0){
                    //update
                    Cursor c = MainActivity.mSQLiteHelper.getData("SELECT id FROM RECORD");
                    ArrayList<Integer> arrID = new ArrayList<Integer>();
                    while(c.moveToNext() ){
                        arrID.add(c.getInt(0));
                    }
                    //Show update Dialog
                    showDialogUpdate(RecordListActivity.this,arrID.get(position));
                }
                if(i==1){
                        //delete
                    Cursor c = MainActivity.mSQLiteHelper.getData("SELECT id FROM RECORD");
                    ArrayList<Integer> arrID = new ArrayList<Integer>();

                    while(c.moveToNext()){
                        arrID.add(c.getInt(0));
                    }
                    showDialogDelete(arrID.get(position));
                }
                 if(i==3){
               //Call Method, I don't know how to do that, cause I already use a Model, I don't know how to do that.



          }

            }
        });
        dialog.show();
        return true;
    }
});

I am new to Android, How to make a contact call in setOnItemLongClickListener().from the top I added my setOnItemLongClickListener method to how can I implement with this.


Solution

  • If I understand correctly, then you want to know how to make a call by number?

    This code for your project and it will open a call app with phone number:

    TextView tvPhone = view.findViewById(R.id.textphone);
    String phone = tvPhone.getText().toString();
    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:" + phone));
    view.getContext().startActivity(intent);
    

    Official documentation