How to Make an Android Studio Calling App
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<android.support.constraint.ConstraintLayout 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:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:gravity="center_horizontal|center_vertical" > <EditText android:id="@+id/et_phone_no" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="80dp" android:hint="Enter Phone number" android:inputType="phone" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btn_dial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="80dp" android:text="Dial" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/et_phone_no" /> </android.support.constraint.ConstraintLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class PhoneCallsactivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_callsactivity); Button mDialButton = (Button) findViewById(R.id.btn_dial); final EditText mPhoneNoEt = (EditText) findViewById(R.id.et_phone_no); mDialButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String phoneNo = mPhoneNoEt.getText().toString(); if(!TextUtils.isEmpty(phoneNo)) { String dial = "tel:" + phoneNo; startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(dial))); }else { Toast.makeText(PhoneCallsactivity.this, "Enter a phone number", Toast.LENGTH_SHORT).show(); } } }); } } |