[embedyt] https://www.youtube.com/watch?v=UDwj5j4tBYg[/embedyt]
AndroidManifest.xmlactivity_main.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codinginflow.phonecallexample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.CALL_PHONE" /> </manifest>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.codinginflow.phonecallexample.MainActivity"> <EditText android:id="@+id/edit_text_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginTop="147dp" android:inputType="phone" /> <ImageView android:id="@+id/image_call" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_phone" /> </RelativeLayout>
package com.codinginflow.phonecallexample; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final int REQUEST_CALL = 1; private EditText mEditTextNumber; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditTextNumber = findViewById(R.id.edit_text_number); ImageView imageCall = findViewById(R.id.image_call); imageCall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { makePhoneCall(); } }); } private void makePhoneCall() { String number = mEditTextNumber.getText().toString(); if (number.trim().length() > 0) { if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL); } else { String dial = "tel:" + number; startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial))); } } else { Toast.makeText(MainActivity.this, "Enter Phone Number", Toast.LENGTH_SHORT).show(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CALL) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { makePhoneCall(); } else { Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show(); } } } }