0

I have set OnClickListener, on drawableRight in edittext for phone numbers so, can you help to open Call Log when I press the button, my phone's call log box opens and I select one of the recently used numbers for the edittext box. so, now What to write for the action for onclicklistener?

I have written code in MainActivity.java as:

public class MainActivity extends AppCompatActivity {

   CountryCodePicker ccp;
   TextInputEditText number;

   @SuppressLint({"ResourceType", "ClickableViewAccessibility"})
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       ccp = findViewById(R.id.ccp);
       number = (TextInputEditText) findViewById(R.id.Phone_Number);

       number.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;

                if(event.getAction() == MotionEvent.ACTION_UP) {
                     if(event.getRawX() >= (number.getRight() - number.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    // your action here
                       CallsProvider callsProvider = new CallsProvider (getApplicationContext());
                       callsProvider.getCalls();
                     }
                }

                return false;
            }
        });

    }

 }
Niraj G
  • 11
  • 4
  • Can you explain properly that what you want to do ? onClick on button should open Call Log? or onClick on EditText should open Call Log? – Anmol Dec 20 '18 at 12:33
  • in edit text i have put image button i.e. call log image button. i want to open call log when i click that image button so that i can select recently used numbers. – Niraj G Dec 20 '18 at 12:53
  • May be this will help https://stackoverflow.com/a/13135554/5901903 – MadLeo Dec 20 '18 at 13:10
  • @MadLeo no, that is for setting OnClickListener on drawable only. i have set onclicklistener already but i want to get access of call log when i click on that button. can you help? – Niraj G Dec 20 '18 at 13:40
  • what you have tried for getting that? – MadLeo Dec 20 '18 at 13:42
  • http://android2011dev.blogspot.com/2011/08/get-android-phone-call-historylog.html just go through it – MadLeo Dec 20 '18 at 13:53
  • i tried this: if(event.getAction() == MotionEvent.ACTION_UP) { if(event.getRawX() >= (number.getRight() - number.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { // your action here CallsProvider callsProvider = new CallsProvider (getApplicationContext()); callsProvider.getCalls(); } } return false; } – Niraj G Dec 21 '18 at 10:41
  • @MadLeo thanks for the link but i want call log only (that means recently used numbers so that i can select any of the numbers from the call log instead of writing number in edit text. i hope you got the query now. kindly help. – Niraj G Dec 21 '18 at 10:53
  • the link is for call log only you have to modify the code as per your need – MadLeo Dec 21 '18 at 13:53

1 Answers1

0

You have to get the permissions as below

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

Use this code to get call logs

if (Build.VERSION.SDK_INT >= 23) {
 requestPermissions(new String[] {
  Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
 }, 200);
}

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

while (c.moveToNext()) {
 String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for  number
 String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
 String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
 int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
 Log.e("TEST", num);
}

If you want to get it in list

ArrayList < String > list = new ArrayList < > ();

if (Build.VERSION.SDK_INT >= 23) {
 requestPermissions(new String[] {
  Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
 }, 200);
}

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

while (c.moveToNext()) {
 String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for  number
 String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
 String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
 int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
 list.add(num);
}

final String[] listAccounts = list.toArray(new String[0]);
final int[] currentApp = {
 0
};

// Dialog box for multiple card accounts
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
//mBuilder.setTitle("Choose an account :");
mBuilder.setTitle(Html.fromHtml("<font color='#FF7F27'>Num</font>"));
mBuilder.setCancelable(false);
mBuilder.setSingleChoiceItems(listAccounts, 0, new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialogInterface, int i) {
  Log.e("Debug_", "Clicked: " + listAccounts[i]);
  currentApp[0] = i;
 }
});
mBuilder.setPositiveButton("Select", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialogInterface, int which) {
  Log.e("Debug_", "Name: " + listAccounts[currentApp[0]] + " Index: " + currentApp[0]);
  number.setText(listAccounts[currentApp[0]]);
  dialogInterface.dismiss();
 }
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
Googlian
  • 6,077
  • 3
  • 38
  • 44
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/185641/discussion-on-answer-by-aslam-anver-how-to-open-call-log-when-button-pressed-thr). – Samuel Liew Dec 22 '18 at 04:34
  • 1
    It Worked. Thanks Aslam Anver... your code worked like a Charm. – Niraj G Dec 25 '18 at 07:09
  • 1
    It's my pleasure, Thank you and request you to upvote my answer – Googlian Dec 26 '18 at 06:09