Simple daytime mode night mode

Source: Internet
Author: User

Package com.example.topnews.topnews;

Import android.content.SharedPreferences;
Import Android.content.SharedPreferences.Editor;
Import Android.graphics.PixelFormat;
Import Android.os.Bundle;
Import android.support.v4.app.FragmentActivity;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.view.WindowManager.LayoutParams;
Import Android.widget.ImageView;
Import Android.widget.TextView;

/**
* Created by Mr Lei on 2016/6/23.
*/
public class Dayornight extends Fragmentactivity {
Public WindowManager Mwindowmanger;

Set Night mode
Public Sharedpreferences SKINSP;
Public final static String day = ' Day ';
Public final static String night = "Night";
public Boolean isday;
Public View MyView;

Public ImageView imageview_night;

Public TextView text_night;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);
Mwindowmanger= (WindowManager) Getsystemservice (Window_service);
Init ();
}

Daytime Night Mode switching
public void init () {
SKINSP = getsharedpreferences ("Skinchange", mode_private);
String mode = skinsp.getstring ("Skin", "" ");
if (mode! = NULL | |!mode.equals ("")) {
if (Mode.equals (night)) {
Night ();
Isday = false;
} else {
Day ();
Isday = true;
}
}
}

public void Night () {
@SuppressWarnings ("deprecation")
Windowmanager.layoutparams params = new Windowmanager.layoutparams (
Android.view.ViewGroup.LayoutParams.FILL_PARENT, Android.view.ViewGroup.LayoutParams.FILL_PARENT,
Layoutparams.type_application,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Pixelformat.translucent);
params.gravity = Gravity.center;
Params.y = 10;
if (MyView = = null) {
MyView = new View (this);
Myview.setbackgroundcolor (0x80000000);
}
Mwindowmanger.addview (MyView, params);
Editor edit = Skinsp.edit ();
Edit.putstring ("Skin", night);
Edit.commit ();
}

public void Day () {
if (MyView! = null) {
Mwindowmanger.removeview (MyView);
Editor edit = Skinsp.edit ();
Edit.putstring ("Skin", day);
Edit.commit ();
}

}
@Override
public void OnDestroy () {
Super.ondestroy ();
String mode = skinsp.getstring ("Skin", "" ");
if (Mode.equals (night)) {
Removeskin ();
}
}

public void Removeskin () {
if (MyView! = null) {
Mwindowmanger.removeview (MyView);
}
}
}

-------------------------------------------Implementation------------------------------------------------

String mode = skinsp.getstring ("Skin", "" ");
if (mode! = NULL | |!mode.equals ("")) {
if (Mode.equals (night)) {
Imgmode.setimageresource (R.drawable.mode_night);
Tvmode.settext ("Night Mode");
Day ();
Isday = true;
} else {
Imgmode.setimageresource (R.drawable.mode_day);
Tvmode.settext ("day Mode");
Night ();
Isday = false;
}
}

Simple daytime mode night mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.