title: How to make a dim inactive screen visible all the time in android
body: <p>I am very mush new to android. I am trying to overlay a View over everything on the screen. I made it reddish and translucent. I want to make the screen darker than the minimum brightness possible by overlaying a translucent view.
  So i made a linear layout in services for so.
  But my app is crashing every time on start. i cant figure out what is wrong. please help!</p>
  
  <blockquote>
    <p>My MainActivity</p>
  </blockquote>
  
  <pre><code>import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  
  
  public class MainActivity extends AppCompatActivity {
  
  Button button1,button2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      button1=(Button)findViewById(R.id.button);
      button2=(Button)findViewById(R.id.button2);
  
      final Intent intent=new Intent(this, OverlayService.class);
      button1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startService(intent);
          }
      });
  
      button2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              stopService(intent);
          }
      });
  
  }
  </code></pre>
  
  <blockquote>
    <p>Services Class</p>
  </blockquote>
  
  <pre><code>package g2.screendimmer;
  
  
  
  import android.app.Service;
  import android.content.Intent;
  import android.graphics.PixelFormat;
  import android.os.IBinder;
  import android.view.WindowManager;
  import android.widget.LinearLayout;
  
  public class OverlayService extends Service {
  
  LinearLayout oView;
  
  @Override
  public IBinder onBind(Intent i) {
      return null;
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      oView = new LinearLayout(this);
      oView.setBackgroundColor(0x88ff0000); // The translucent red color
      WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.MATCH_PARENT,
              WindowManager.LayoutParams.MATCH_PARENT,
              WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
              WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
              PixelFormat.TRANSLUCENT);
      WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
      wm.addView(oView, params);
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      if(oView!=null){
          WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
          wm.removeView(oView);
      }
   }
  }
  </code></pre>
  
  <blockquote>
    <p>Manifest file</p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="g2.screendimmer"&gt;
  
  &lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".MainActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;service android:name=".OverlayService"&gt;&lt;/service&gt;
  &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <blockquote>
    <p>Log</p>
  </blockquote>
  
  <pre><code>05-27 21:27:35.651 1477-1477/g2.screendimmer E/AndroidRuntime: FATAL     EXCEPTION: main
                                                             Process: g2.screendimmer, PID: 1477
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{g2.screendimmer/g2.screendimmer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                 at g2.screendimmer.MainActivity.onCreate(MainActivity.java:20)
                                                                 at android.app.Activity.performCreate(Activity.java:6010)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
  </code></pre>