Android screen filter source code | Eye Filter Tutorial 2017

Android Screen filter source code example 2017

Android screen filter is nothing then an running an service in mobile screen which layout background is transparent and its layout touch is disabled by program. Android service filter always remain on top of the screen which layout is with opaque and transparent in quality. Any color can be add as layout color to cover mobile screen which is normally known as Screen filter or mobile eye filter. Protect your aye from high light in dark screen filter help to decrease its light intensity to be appear in dark. Here is complete source code of android screen filter. Android light filter source code support with android newer version Lollipop, Marshmallow, Nougat and Android Oreo.

You have to care your screen filter layout is full screen and its layout with disable touch. You can create screen filter in two ways one with using service and its layout another with transparent activity. In this example I am showing you how to use service layout as screen filter. Choose wisely which you want to use in your future project. Know more about Android service from Android official page.

android screen filter

Android screen filter service source code

Must change the package name with your own package name. Create an service class in your project name it any here I named it FilterTwo.

package utube.attraction.eyeprotector;

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class FilterTwo extends Service {

private WindowManager wm;
private LinearLayout ll;

public static int STATET;
public static final int INACTIVET = 0;
public static final int ACTIVET = 0;

static {
STATET = INACTIVET;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();

ll = new LinearLayout(this);

//LinearLayout.LayoutParams llParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
ll.setBackgroundColor(Color.argb(100, 30, 70, 25));
//ll.setLayoutParams(llParameters);
ll.setClickable(false);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT);

wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(ll, params);
}
}

Android screen filter AndroidManifest.xml source code

As you are using service as screen filter and your filter layout will be top of screen always so you need to add permission in AndroidManifest file. Add this line in your project manifest file. Add service in AndroidManifest.

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

Below the complete source code.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”utube.attraction.eyeprotector”>

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

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>

<activity android:name=”.MainActivity”>

<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

<service
android:name=”.FilterTwo”
android:enabled=”true”
android:icon=”@drawable/notif”
android:label=”@string/app_name” />

</manifest>

Now start your screen fitter service by click a button and stop it to disable your filter.

Add two button for enabling and disabling service in MainActivity.java page.

package utube.attraction.eyeprotector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button enableFilter = (Button) findViewById(R.id.enable_filter);
Button disableFilter = (Button) findViewById(R.id.disable_filter);

enableFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(new Intent(getApplicationContext(), FilterTwo.class));
}
});

disableFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(new Intent(getApplicationContext(), FilterTwo.class));
}
});

}
}

activity_main.xml source code for android eye filter

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/activity_main”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”your project package name.MainActivity”>

<Button
android:id=”@+id/enable_filter”
android:layout_width=”160dp”
android:layout_height=”40dp”
android:background=”#ffabb2″
android:layout_marginTop=”203dp”
android:textSize=”20sp”
android:textStyle=”bold”
android:text=”On Screen Filter”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true” />

<Button
android:id=”@+id/disable_filter”
android:layout_width=”160dp”
android:layout_height=”40dp”
android:layout_below=”@+id/enable_filter”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”74dp”
android:textStyle=”bold”
android:textSize=”20sp”
android:text=”Disable Filter” />

</RelativeLayout>

In case any problem you faced please let us know. Visit our Blog NextGenEarn for more latest code and information.

Add a Comment

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