Uninstall an app programmatically

Posted by on Jun 25, 2011 in Android | 0 comments

Uninstall an app programmatically

In a previous tutorial we created a list of all apps that are currently installed on the phone. Upon clicking on an app entry, the user was shown additional information about the selected app and had the opportunity to start it. A follow-up also displayed the associated icons by loading them asynchroneously.

This time I will show you how to enhance this demo program by displaying the associated package file location of a selected app as well as the amount of disk space it consumes on your phone. In addition, we will add the possibility to uninstall a selected app.

Displaying the package location and file size

The first improvement that we will make on the demo app is to display the path where the app is actually installed and the size on disk that it consumes. To store this information our App bean will get two new String fields: installDir and installSize complete with accompanying getters and setters.

We then have to extend our listInstalledApps(...) method inside our main activity:


The calculateSize(...) method is a custom method that takes the path to the installed package as a parameter and returns a human-readable form of the file’s size, e.g. “420.23 KB” or “1.23 MB”. It is implemented as follows:

private String calculateSize(String installPath) {
  File file = new File(installPath);

  String unit = "Bytes";
  double sizeInUnit = 0d;

  if (file.exists()) {
    double size = (double) file.length();

    if (size > 1024 * 1024 * 1024) { // Gigabyte
      sizeInUnit = size / (1024 * 1024 * 1024);
      unit = "GB";
    } else if (size > 1024 * 1024) { // Megabyte
      sizeInUnit = size / (1024 * 1024);
      unit = "MB";
    } else if (size > 1024) { // Kilobyte
      sizeInUnit = size / 1024;
      unit = "KB";
    } else { // Byte
      sizeInUnit = size;

  // only show two digits after the comma
  return new DecimalFormat("###.##").format(sizeInUnit) + " " + unit;

The only thing that is left to do now is to display the gathered information to the user. We do this inside our dialog box that is shown upon selection of an app in the list:

AlertDialog dialog = new AlertDialog.Builder(this).create();

String msg = app.getTitle() + "\n\n" +
  "Version " + app.getVersionName() + " (" + app.getVersionCode() + ")" +
   "\n" + app.getDescription() + "\n" +
  app.getInstallDir() + "\n" + app.getInstallSize();

The user can now see where the selected app is installed and how much space it consumes.

Adding the ability to uninstall a selected app

We will also add the ability to uninstall a selected app. This can be achieved very easily: all you have to do is execute an intent to remove a given package name. We will add a third button to our dialog box which enables the user to unsinstall the selected app:

dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Uninstall",
  new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
      Intent intent = new Intent(Intent.ACTION_DELETE);
      intent.setData(Uri.parse("package:" + app.getPackageName()));
      mAdapter.notifyDataSetChanged(); // update the UI

That’s all there is to it! Android will do the rest for you including showing an additional activity where the user has to confirm his choice to actually remove the selected app for good.

As always you can download the sample application containing all source code and layout resource files:

DownloadDownload now.zip (71 KB)

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>