Android making Dialer application
In this post I’m going to share code which I made in my spare time. The app will be making calls and requesting ussd code to know the information of your sim network. I’m assuming that you already know how to make a new project in Android studio. If not please see this link now.
Make a new application in android studio. Make a new layout file in your project named dialer.xml (res>layout>dialer.xml).
dialer.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:orientation="vertical" android:background="#fff" android:weightSum="10"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="5dp" android:layout_marginTop="10dp" android:layout_weight="2" android:paddingBottom="1dp"> <EditText android:id="@+id/edtPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:layout_alignParentBottom="true" android:background="#fff" android:gravity="center" android:hint="Phone No." android:textSize="25sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@color/colorAccent" android:text="Del" android:id="@+id/btndel" android:onClick="buttonClickEvent" android:textColor="#fff" android:textSize="25sp" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="5dp" android:id="@+id/lblinfo" android:text="Hello!!!" android:textColor="@color/colorPrimary" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="7" android:orientation="vertical" android:weightSum="5"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="1" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnOne"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="2" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnTwo"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="3" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnThree"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="4" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnFour"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="5" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnFive"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="6" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnSix"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="7" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnSeven"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="8" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnEight"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="9" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnNine"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="*" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnAterisk"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="0" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnZero"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="#" android:onClick="buttonClickEvent" android:background="@drawable/btnstyle" android:layout_margin="1dp" android:textSize="30sp" android:id="@+id/btnHash"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="2" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Clear All" android:onClick="buttonClickEvent" android:background="@color/colorAccent" android:textColor="#fff" android:layout_margin="1dp" android:textSize="20sp" android:id="@+id/btnClearAll"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Make Call" android:onClick="buttonClickEvent" android:background="@color/colorPrimaryDark" android:textColor="#fff" android:layout_margin="1dp" android:textSize="20sp" android:id="@+id/btnCall"/> </LinearLayout> </LinearLayout> </LinearLayout>
Now create a new class file (.java file) named Dialer.java:
Dialer.java :
package parallel.callerexample; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class Dialer extends AppCompatActivity { EditText edtPhoneNo; TextView lblinfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialer); edtPhoneNo = (EditText) findViewById(R.id.edtPhoneNumber); lblinfo = (TextView) findViewById(R.id.lblinfo); } public void buttonClickEvent(View v) { String phoneNo = edtPhoneNo.getText().toString(); try { switch (v.getId()) { case R.id.btnAterisk: lblinfo.setText(""); phoneNo += "*"; edtPhoneNo.setText(phoneNo); break; case R.id.btnHash: lblinfo.setText(""); phoneNo += "#"; edtPhoneNo.setText(phoneNo); break; case R.id.btnZero: lblinfo.setText(""); phoneNo += "0"; edtPhoneNo.setText(phoneNo); break; case R.id.btnOne: lblinfo.setText(""); phoneNo += "1"; edtPhoneNo.setText(phoneNo); break; case R.id.btnTwo: lblinfo.setText(""); phoneNo += "2"; edtPhoneNo.setText(phoneNo); break; case R.id.btnThree: lblinfo.setText(""); phoneNo += "3"; edtPhoneNo.setText(phoneNo); break; case R.id.btnFour: lblinfo.setText(""); phoneNo += "4"; edtPhoneNo.setText(phoneNo); break; case R.id.btnFive: lblinfo.setText(""); phoneNo += "5"; edtPhoneNo.setText(phoneNo); break; case R.id.btnSix: lblinfo.setText(""); phoneNo += "6"; edtPhoneNo.setText(phoneNo); break; case R.id.btnSeven: lblinfo.setText(""); phoneNo += "7"; edtPhoneNo.setText(phoneNo); break; case R.id.btnEight: lblinfo.setText(""); phoneNo += "8"; edtPhoneNo.setText(phoneNo); break; case R.id.btnNine: lblinfo.setText(""); phoneNo += "9"; edtPhoneNo.setText(phoneNo); break; case R.id.btndel: lblinfo.setText(""); if (phoneNo != null && phoneNo.length() > 0) { phoneNo = phoneNo.substring(0, phoneNo.length() - 1); } edtPhoneNo.setText(phoneNo); break; case R.id.btnClearAll: lblinfo.setText(""); edtPhoneNo.setText(""); break; case R.id.btnCall: if (phoneNo.trim().equals("")) { lblinfo.setText("Please enter a number to call on!"); } else { Boolean isHash = false; if (phoneNo.subSequence(phoneNo.length() - 1, phoneNo.length()).equals("#")) { phoneNo = phoneNo.substring(0, phoneNo.length() - 1); String callInfo = "tel:" + phoneNo + Uri.encode("#"); Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse(callInfo)); startActivity(callIntent); } else { String callInfo = "tel:" + phoneNo; Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse(callInfo)); startActivity(callIntent); } } break; } } catch (Exception ex) { } } }
To make phone calls in android you have to use this code :
Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse(callInfo));
Here I’m first declaring the Call Action intent and then sending the phone number to call to it.
And also you have to specify the permission in the AndroidManisfest.xml file :
AndroidManisfest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="parallel.callerexample"> <uses-permission android:name="android.permission.CALL_PHONE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Dialer" android:windowSoftInputMode="stateAlwaysHidden" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
If you have any doubts or queries, please let me know in the comment section below, I’ll try to help you out. If you find something wrongs or errors in my code please let me know, I’ll openly accept the mistakes if any.
And lastly please opt for newsletters and like my social networks pages for regular updates.