How to create a Phone Call App in Kotlin Android?
Let’s understand workflow.
- We need to give uses Permission in Manifest file.
- After that we need a button on which we can click for calling on a particular Number in XML file.
- After that we need to set setOnClikListener{ } on button.
- After that we need to check permission (user allowed for phone calls or not ) if Yes then call on given number.
Let’s Coding
- Write codes in manifest file which is given bellow.
<uses-permission android:name="android.permission.CALL_PHONE"/>
2. Take button in activity_main.xml Or some where in xml file like this.
activity_main.xml
<Button android:id="@+id/makeCall_button"
android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginTop="231dp"android:text="call" />
3. And 4.
MainActivity.kt
class MainActivity : AppCompatActivity() {
var REQUEST_PHONE_CALL= 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
makeCall_button.setOnClickListener {// cheacking permission
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) ActivityCompat.requestPermissions(this,arrayOf(android.Manifest.permission.CALL_PHONE),REQUEST_PHONE_CALL)
}else{
makeCall()
}
}
}private fun makeCall(){
val numberText ="8678667787"
val intent=Intent(Intent.ACTION_CALL)
intent.setData(Uri.parse("tel:$numberText"))
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED
) {
Toast.makeText(this,"Permission denied",Toast.LENGTH_LONG).show()
return
}
startActivity(intent)
}override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode == REQUEST_PHONE_CALL){
makeCall()
}
}
}
It’s Working . Thanks…
👏👏👏👏👏👏👏👏👏👏