简体   繁体   中英

How to to implement a call button in the ListView?

I am working on an android project. It is blood donor searching app. When the user searches for blood donor, the current location of the user is identified by the GPS. The coordinates are then compared with the details in the database. Then matching donor contact details are retrieved.

The details are printed in the format of a ListView. I want to provide a calling option with these details. for eg. when the details are displayed, the user can long press on the phone number to call the donor. can this be implemented?

How can I implement this? Is it possible

Implement setOnLongClickListener on your Phone number textView. For example,

txtPhoneNumber.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
         Uri number = Uri.parse("tel:"+ txtPhoneNumber.getText().toString);
         Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
         startActivity(callIntent);

         return true;
        }
    });

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

BTW are you using a custom adapter for your listview?

listView.setOnItemLongClickListener(new OnItemLongClickListener() {  

      public boolean onItemLongClick(AdapterView<?> arg0, View v,  
              int position, long arg3) {  
          Toast.makeText(ListViewTestActivity.this, "content"+position, Toast.LENGTH_LONG).show();  
          return false;  
      }  
  });  

For TextView long click:

textView.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        callNumber(textView.getText().toString().trim());
        return true;
    }

});

Here is the method with which you can make a call:

private void callNumber(String telephoneNumber) {
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephoneNumber.replace("-", "")));
    startActivity(intent);
}

Dont forget to add permission in AndroidManifest.xml file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM