Android Activity without UI

Is it possible to create a Android activity without UI?

Yes it is. Android provides a theme for this requirement. Add the following line to your AndroidManifest.xml and call finish() at the end of the Activity’s onCreate() method.



public static final class – Added in API level 3
int Theme_NoDisplay
Default theme for activities that don’t actually display a UI; that is, they finish themselves before being resumed.

Constant Value: 16973909 (0x01030055)

How to know if the app goes to Background

With API Level 14 – Android 4.0, there is a Method in Application which is called onTrimMemory. The onTrimMemory is executed by the system with different levels ( TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, or TRIM_MEMORY_RUNNING_MODERATE). The interesting level is “TRIM_MEMORY_UI_HIDDEN”. This level can be used to know that the app is going to background.

The following example is a demonstration of the Application.onTrimMemory Method:

public class MyApplication extends Application {
// ...
public void onTrimMemory(int level) {
if (level == TRIM_MEMORY_UI_HIDDEN) {
// ...

With this simple piece of code, you can check if your app is being sent to the background!

How to Format cells in Excel for Android

Not a snippet, but a needed Information when using Microsoft Android Calc

How to Format cells in Excel for Android. (Sceenshot made on a german device)

Some more informations are available in the video at Excel for Android is very similar to Microsoft Excel for Windows. But some differences can confuse sometimes. The short video shows you how to get up and running with Microsoft Calc for Android.

How To use landscape and reverseLandscape in AndroidManifest.xml

If you develope a app or activity which is only available in landscape mode, you can enhance the app by adding the reverseLandscape mode (Landscape orientation in the opposite direction from normal landscape. Added in API level 9.) via editing the android:screenOrientation attribute of your activity in AndroidManifest.xml. Instead of using the “landscape” value, you can use “sensorLandscape” (Landscape orientation, but can be either normal or reverse landscape based on the device sensor. Added in API level 9.).

This is a simple solution and your users will enjoy the possibility of using the device even if it is rotated by 180 degrees. The same solution is possible for portrait apps and activities. In this case you have to use sensorPortrait instead of sensorLandscape in AndroidManifest.xml.

How to execute a shell command from android app

To execute a shell command from your android app you can grab the standard input of the su process just launched by Runtime.getRuntime().exec(“su”) and write down the command there, otherwise you are running the commands with the current UID.

With the following code you can excute the screenrecord command to grab your screen to a mp4 video file (Works on Android 4.4 API level 19 and higher). There are many othe commands you can execute. You can find some of them at


How to turn on/off (enable/disable) GPS in Android

If you want to develop an Android app to turn on/off GPS you can use the following code snippets.


How to set mobile input types in HTML?

Mobile devices have virtual keyboards. There is only limited space on the display to show the keyboard, so it will be helpful to the user to see only the needed keys. Using the right input type will dramatically improve the mobile experience.

There are many different input types available for html pages. Here are my favorites.

Input type text

The most common input type used. This will show the default keyboard on mobile devices.

Input type email

The input type email will show a virtual keyboard with easy access to the AT-Sign (@), the dot and sometimes there will be shown the most common top level domains, so you don’t have to enter “.com” letter by letter. The difference to the text input type is only marginal, but the mobile user experience will increase dramatically if you use the input type on the right field.

Input type tel

With the input type tel you can make it easy to enter a telephone number because you will get a virtual keyboard with a numeric keypad which includes everything you need to enter a phone number like sharp (#) or plus (+).

Input type number

The mobile input type “number” is correct if you have to enter numbers only. It is different from the input type tel and is the right type to enter numbers of barcodes, counters or anything else mainly containing digits but i think this type shouldn’t be using very often because in most cases tel will work better.

Input type password

If you ever made a login form you should know about it and I don’t want to describe it here, because there are millions of good descriptions about this input type in the World Wide Web.

Input type date

The name of the input type is date and this is what you can enter here. You don’t have to implement a JavaScript calendar widget or something else. Just set the tags attribute type to date and the mobile will do the rest for you. I like it.

Input type datetime

It is like the input type date, but it adds the time to the input. So you can enter date and time into your input field. Just replace the tag attrubte type from date to datetime and you have a input field for date and time.

Input type search

If your input field is a search field, you can use this type. It will show the (nearly) regular virtual keyboard with on exception. You get a search button direct on the virtual keyboard if you use this mobile input type. It is quite helpful and I use it often if I need a search function in my form.

Note: Some devices and virtual keyboards do not support every mobile input type. But If you user iOS or Android with a common keyboard, I’m sure you will get the right keyboard for your input type. And there are more…. but the above are the main types (IMHO).

How to get the version of your Android application from AndroidManifest.xml?

There are two parts of the version in AndroidManifest.xml.

  • android:versionCode
  • android:versionName

In your AndroidManifest.xml the versionCode is a number, and every version you want to submit to google play needs to have a higher number then the last. VersionName is just a string, and can be anything you want it to be. In VersionName you can define your app as “1.0” or “” or “Test Version” or whatever.

To get the android:versionCode and the android:versionName you can use the following code snippet where context is your actual context. If you execute this code in your Activity you can remove the “context.” from “context.getPackageManager()”. Because the Activity is a indirect Subclasses of Context.

Note: Other known Subclasses of Context are:


If you are not in in a class that is a subclass of Context you have to identify your context to get the version from your AndroidManifest.xml.

How to get the name of the current method in Java

There is a simple solution, to get the name of the current method in Java. It uses the stack trace to get the name of the mehod. It works with different virtual machines like oracle, dalvik (Android), Open JDK, etc.

But don’t use this in time-critical code, because working with the StackTrace is not recommended for high performcnce activities.


Android Background Service Template

This is a simple Android background Service Template. You can use it to run updates in the background without blocking the UI. To start and stop the service use the following lines in your activity:

This is all you need to execute a service on android devices. Don’t forget to add the service to your AndroidManifest.xml file.