Dialer Intent - Android Studio - Java

This video tutorial will show you how to Input Phone number or *# code and pass it to Dialer Intent using Java language and Android Studio.
>>Watch For Kotlin

Video:

Step 1: Create a new project OR Open your project

Step 2: Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!--Edit text to input number/code-->
    <EditText
        android:id="@+id/numberEt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_edittext"
        android:hint="Enter Number/Code..."
        android:inputType="phone"
        android:minWidth="200dp"
        android:padding="10dp" />

    <!--Button to dial number/code-->
    <Button
        android:id="@+id/dialBtn"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableStart="@drawable/ic_call_white"
        android:drawableLeft="@drawable/ic_call_white"
        android:drawablePadding="10dp"
        android:minWidth="120dp"
        android:text="Dial"
        android:textSize="20sp" />

</LinearLayout>

MainActivity.java

package com.blogspot.atifsoftwares.dialerintent_java;

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

public class MainActivity extends AppCompatActivity {

    //Views
    EditText mNumberEt;
    Button mDialBtn;

    //String variable to store text from edit text
    String number;

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

        //initialize views
        mNumberEt = findViewById(R.id.numberEt);
        mDialBtn = findViewById(R.id.dialBtn);

        //button click to dial
        mDialBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get text from edittext
                number = mNumberEt.getText().toString().trim();

                //Dialer Intent
                //Uri.encode(string) allows number with * and # symbols to dial
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Uri.encode(number)));
                startActivity(intent);

            }
        });
    }
}

Step 3: Run Project

Output
Dialer Intent - Android Studio - Java

Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)