September 14, 2023

Phone dialer

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button dialButton = findViewById(R.id.dialButton);
        final EditText phoneNumberEditText = findViewById(R.id.phoneNumberEditText);

        dialButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Retrieve the phone number entered by the user
                String phoneNumber = phoneNumberEditText.getText().toString();

                // Check if the phone number is not empty
                if (!phoneNumber.isEmpty()) {
                    // Create an Intent to dial the phone number
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + phoneNumber));

                    // Check if the CALL_PHONE permission is granted
                    if (checkSelfPermission(android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        // Request the permission if not granted
                        requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE}, 1);
                        return;
                    }

                    // Start the phone call
                    startActivity(intent);
                }
            }
        });
    }
}
<EditText
    android:id="@+id/phoneNumberEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Phone Number" />

<Button
    android:id="@+id/dialButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dial Number" />

Phone dialer

Androidmencest

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