Created
July 12, 2021 00:52
-
-
Save codinginflow/e4a5c395a6460a7b5d3af8552eb6779d to your computer and use it in GitHub Desktop.
In App Phone Call Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know why in my case its saying permission denied when trying to make a call.I already gave the necessary permission in manifest fiel.Kindly help me out.