DEV Community

SHREEHITH REDDY
SHREEHITH REDDY

Posted on

phone dailer app in android studio (java & xml)

mainactivity.java

Main Activity



package com.example.phonedailer;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText phoneNumber;
    private Spinner callTypeSpinner;
    private Button callButton, saveButton;
    private String selectedCallType;

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

        phoneNumber = findViewById(R.id.phoneNumber);
        callTypeSpinner = findViewById(R.id.callTypeSpinner);
        callButton = findViewById(R.id.callButton);
        saveButton = findViewById(R.id.saveButton);

        // Populate spinner with call types
        String[] callTypes = {"Voice Call", "Video Call", "WhatsApp Call"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, callTypes);
        callTypeSpinner.setAdapter(adapter);

        // Capture selected call type
        callTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedCallType = callTypes[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                selectedCallType = "Voice Call";
            }
        });

        // Call Button Click
        callButton.setOnClickListener(v -> {
            String number = phoneNumber.getText().toString().trim();
            if (isValidPhoneNumber(number)) {
                Toast.makeText(MainActivity.this, number + " " + selectedCallType + " initiating...", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Invalid Number", Toast.LENGTH_SHORT).show();
            }
        });

        // Save Button Click
        saveButton.setOnClickListener(v -> {
            String number = phoneNumber.getText().toString().trim();
            if (isValidPhoneNumber(number)) {
                Toast.makeText(MainActivity.this, number + " saved.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Invalid Number", Toast.LENGTH_SHORT).show();
            }
        });
    }

    // Phone number validation (10 digits only)
    private boolean isValidPhoneNumber(String number) {
        return number.matches("\\d{10}");
    }
}
Enter fullscreen mode Exit fullscreen mode

activity.xml

.XML


<?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:padding="20dp"
    android:orientation="vertical"
    android:gravity="center">

    <!-- Phone Number Input -->
    <EditText
        android:id="@+id/phoneNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter phone number"
        android:inputType="number"
        android:maxLength="10"
        android:textSize="18sp"
        android:padding="10dp"
        android:background="@android:drawable/edit_text" />

    <!-- Spinner for Call Type Selection -->
    <Spinner
        android:id="@+id/callTypeSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:padding="5dp" />

    <!-- Call Button -->
    <Button
        android:id="@+id/callButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:background="@android:color/holo_blue_light"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <!-- Save Button -->
    <Button
        android:id="@+id/saveButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:background="@android:color/holo_green_light"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

</LinearLayout>
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay