Implementing a Calling Feature in Android Studio: A Step-by-Step Guide

Dear students, this blog provides a valuable opportunity to practice a calling activity in Android Studio. It guides you through the process of developing a calling feature, helping you understand key concepts such as intent handling, permissions, and user interface design. By following the steps outlined, you will learn how to integrate calling functionality into your Android applications effectively. This hands-on practice will enhance your coding skills and improve your understanding of Android development. Whether you are a beginner or looking to refine your expertise, this blog serves as a helpful resource to strengthen your knowledge and build real-world applications. 

activity_calling.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightGray"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <EditText
        android:id="@+id/edtMobileNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Number"
        android:inputType="numberDecimal"
        android:textSize="25sp" />

    <Button
        android:id="@+id/btnMakeCall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Make Call"
        android:textSize="25sp" />

</LinearLayout>

CallingActivity.java


package com.vipul.p6;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class CallingActivity extends AppCompatActivity {

    EditText edtMobileNumber;
    Button btnMakeCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calling);
        edtMobileNumber = findViewById(R.id.edtMobileNumber);
        btnMakeCall = findViewById(R.id.btnMakeCall);

        btnMakeCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                makeCall();
            }
        });
    }

    private void makeCall() {
        String number = edtMobileNumber.getText().toString().trim();

        if (number.isEmpty()) {
            Toast.makeText(this, "Please Enter Number", Toast.LENGTH_SHORT).show();
            return;
        }

        // Corrected condition: Check if length is less than 10 OR contains non-numeric characters
        if (number.length() < 10 || !number.matches("\\d+")) {
            Toast.makeText(this, "Enter a valid 10-digit number", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent dialIntent = new Intent(Intent.ACTION_CALL);
        dialIntent.setData(Uri.parse("tel:" + number));
        startActivity(dialIntent);
    }
}

Write your comments


Post a Comment

0 Comments