How To Run Ussd Code In Android Studio

0
844
Android Studio Make Ussd Calls
Android Studio Make Ussd Calls

In this Android Studio tutorial with source code example, i would put you through the step by step process on how you can run ussd code in android.

You can copy the source code example below or clone from my github page.

 

  1. Add Call permission to the Androidmanifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ussd.call">

<uses-permission android:name="android.permission.CALL_PHONE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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>

 

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:padding="20dp"
tools:showIn="@layout/activity_main">

<EditText
android:id="@+id/ed_inputUssd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:hint="Input Ussd Code"
/>
<Button
android:id="@+id/btnDialUssdCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@id/ed_inputUssd"
android:text="Dial Ussd Code"/>
</android.support.constraint.ConstraintLayout>

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

 

MainActivity.java

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText edInputUssdCode;
Button btnDialUssdCode;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);




edInputUssdCode = (EditText) findViewById(R.id.ed_inputUssd);
btnDialUssdCode = (Button) findViewById(R.id.btnDialUssdCode);

btnDialUssdCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String UssdCode = edInputUssdCode.getText().toString();

//check if edittext is empty
if (UssdCode.equalsIgnoreCase("")) {

Toast.makeText(MainActivity.this, "Please Input ussd code", Toast.LENGTH_SHORT).show();
return;
}

//check if its a valid ussd code
if (UssdCode.startsWith("*") && UssdCode.endsWith("#")) {

//we want to remove the last # from the ussd code as we need to encode it. so *555# becomes *555
UssdCode = UssdCode.substring(0, UssdCode.length() - 1);

String UssdCodeNew = UssdCode + Uri.encode("#");

//request for permission
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);

} else {
//dial Ussd code
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + UssdCodeNew)));

}


} else {
Toast.makeText(MainActivity.this, "Please enter a valid ussd code", Toast.LENGTH_SHORT).show();
}


}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

 

 

 

 

 

 

Download Source Code

LEAVE A REPLY

Please enter your comment!
Please enter your name here