Kapat

Android Police Light & Siren Example

excel  pairing spin button with textbox Android Screen Animation
With Audio

When Android app starts, the police light animation is automatically activated and the police siren starts to sound uninterruptedly.

Java codes of MainActivity : android police light siren open source codes

package com.bb.policelightbar;

import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.media.MediaPlayer;

public class MainActivity extends AppCompatActivity {
    private ImageView frame1, frame2;
    private final Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide(); 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);

        setPointer();
        final MediaPlayer ses = MediaPlayer.create(this, R.raw.policesiren);
        ses.setLooping(true);
        ses.start();

    }

    private void setPointer() {
        frame1 = findViewById(R.id.frame1);
        frame2 = findViewById(R.id.frame2);
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (((ColorDrawable) frame1.getBackground()).getColor() ==
                        getResources().getColor(R.color.colorBlue)) {
                    frame1.setBackgroundColor(getResources().getColor(R.color.colorRed));
                    frame2.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                } else {
                    frame1.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                    frame2.setBackgroundColor(getResources().getColor(R.color.colorRed));
                }
                handler.postDelayed(this, 500);

            }
        };
        MainActivity.this.runOnUiThread(runnable);
      }
}

In the original of the sample file, the police light animation was turned on and off with a button. I removed this toggle button and made some additions to make the animation work full screen. android app no title full screen
// Hide the Title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getSupportActionBar().hide();
// Make this activity full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Keep the screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Codes of activity_main.xml : android studio activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">

        <ImageView
            android:id="@+id/frame1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/colorWhite" />

        <ImageView
            android:id="@+id/frame2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/colorWhite"
            />

    </LinearLayout>
</LinearLayout>

📥 Project files can be downloaded here

android open source codesProject resource