Write a Program to create a button “Call”. When you click on this button it should make a call to the given number.
Code:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="@id/t1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="enter number"
android:ems="10"
android:id="@+id/e1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dial"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/t1"
/>
</RelativeLayout>
Main_Activity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
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 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e1=(EditText)findViewById(R.id.e1);
final Button b1=(Button)findViewById(R.id.t1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = e1.getText().toString();
Intent cinte =new Intent(Intent.ACTION_CALL);
cinte.setData(Uri.parse("tel:"+n));
startActivity(cinte);
}
});
}
}
Output:
1]Original Layout 2] After Click on Dial button
Comments
Post a Comment