View database records in Android Device with Stetho

Today we will talk about pretty cool tool for every Android developer who uses SQLite. And this is the Stetho who developed by Facobook.

With Stetho you can view your SQLite records on the device very easily. All you need to do is add this line to your build.gradle file:

compile 'com.facebook.stetho:stetho:1.4.2'

And add this line to your Activity onCreate() method:

protected void onCreate(Bundle savedInstanceState) {

And that it. When the app is running, you can browse very easily inside database, by opening chrome  in the url:


Then you will see something like that:








Then the Developer Tools window will pop up. In the Resources tab, under Web SQL you will be able to find your database:







The records display is something like this:





And that’s all!

More details can be found here:

Android : Draw View on top of other application

Let’s say that you want to write an app that waiting in the background of the device and listening to something that going to happen, in order to pop-up some window offering information or button to perform a specific action.  It can be even something simple like URL namespace that the user just copied into the clipboard and you want to offer him to do something with this URL.

In order to do this, you need to use  WindowManager inside a Service . The WindowManager have the ability to draw a View on top of everything else, with the right LayoutParams, and specifically, the right WindowManager.LayoutParams Flags.

Let’s show how we can do this. First thing you have to use this permission:

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

And your target API need to be 22 or below, or you will have to deal with permission issues.
Now, you don’t have to have any Activity at all, but for this tutorial, we will use very simple MainActivity:

public class MainActivity extends AppCompatActivity {

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        startService(new Intent(this, OverlayButtonService.class));

Inside the OverlayButtonService (don’t forget to register your Service in the manifest) we have the interesting code. We will use very simple View that we will define in R.layout.button.xml file:

<?xml version="1.0" encoding="utf-8"?>

And the OverlayButtonService code is:

public class OverlayButtonService extends Service {

    public void onCreate() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
        layoutParams.gravity = Gravity.TOP | Gravity.START;
        layoutParams.x = 50;
        layoutParams.y = 50;
        View btn = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.button, null);
        final WindowManager windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
        windowManager.addView(btn, layoutParams);
        btn.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                //here you can do whatever action you want after click
                return false;


    public void close(View view){
        try {
            final WindowManager windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
        } catch (Exception e){
        } finally {

    public IBinder onBind(Intent intent) {
        return null;

What we do is very simple. We define the LayoutParams to draw the Button in the top left corner, and we use FLAG_NOT_FOCUSABLE as we want that the user will be able to keep communicate the screen without we interrupt it.

The output of this code can be something like that:

The orange button is our button and in the moment the user will touch it, the button will disappear.

The full code in this post can be found here:


How to write simple Android Lock Screen

Hello everyone,

I will start this blog by explaining how you can write simple Lock Screen for Android.

First, it important to understand that officially, Android have built it Lock Screen mechanism, and there isn’t any official API for bypass or replace this Lock Screen.
However, if you want to do that nevertheless, you can use some tricks that can help you achieve your purpose. I hope this post will help other programmers as I know I wasn’t able to find too much information when I tried to build Lock Screen from scratch.

The Lock Screen I’m going to show is quite a real one, as it disables completely the user ability to communicate with the device as long as the Lock Screen is displayed.
In order to do that, I needed to find a way to bypass Home Screen button, and also hide the Status Bar.
We will use very simple UI for this demo:

The basic concept for building your own Lock Screen is to be able to draw your layout on top of any other component in the user device so the user won’t be able to use the device unless he will follow your own security demands, like type in a PIN or swipe left, or any other measure you choose to implement.

So all you need to do is run your Service that will listen to any Screen Off events, and each time this event happens you will push your UI on top of everything.


First, there is two Permission must be including in the Manifest.xml file:

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

android.permission.SYSTEM_ALERT_WINDOW permission enables you to draw your layout on top of everything in the device.

android.permission.RECEIVE_BOOT_COMPLETED permission needed in order to run your service right after user switch on his device.

Now, the Service need to listen to Intent.ACTION_SCREEN_OFF event. So after the Service will start we will create and register simple BroadcastReciever inside the Service:

BroadcastReceiver screenReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) && linearLayout == null) {

linearLayout == null is the indicator to check if our layout is already displayed, as we don’t want to draw duplicate layouts again and again.

We will register this receiver in the Service onCreate() method, and will define the LayoutParams we will use for drawing our layout on top of everything:

private LinearLayout linearLayout;
private WindowManager.LayoutParams layoutParams;
private WindowManager windowManager;

public void onCreate() {
 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
 registerReceiver(screenReceiver, intentFilter);

 windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
 layoutParams = new WindowManager.LayoutParams(
 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD

WindowManager.LayoutParams.TYPE_SYSTEM_ERROR is a window flag that gives us the ability to put our layout on top of everything.
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN simply makes our layout to be drawn on the entire screen.
WindowManager.LayoutParams.SYSTEM_UI_FLAG_HIDE_NAVIGATION hide the device navigation buttons.

Pay attention: we must target targetSdkVersion not higher than 22! otherwise will get Permission exception.

So right now, each time screen off event will be intercepted by our screenReceiver and linearlayout will be null, we will call our init() method:

private void init() {
    linearLayout = new LinearLayout(this);
    windowManager.addView(linearLayout, layoutParams);
    ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.lock_screen, linearLayout);
    View btnClose = linearLayout.findViewById(;

lock_screen.xml layout is very simple and contains just a simple button:

<?xml version="1.0" encoding="utf-8"?>



Now, all left is to remove all UI component after the user clicked our botton:

public void onClick(View view) {
    linearLayout = null;

MainActivity.class will be super simple. It will just trigger the service.

public class MainActivity extends Activity {

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        startService(new Intent(this, LockScreenService.class));

Hope you enjoyed! The whole code for this demo can be found here: