Android Basics Tutorial Night Mode Implementation Example _android

Source: Internet
Author: User

Copy Code code as follows:

Package Org.david.dayandnightdemo.cor;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.Context;
Import android.content.SharedPreferences;
Import Android.content.SharedPreferences.Editor;
Import Android.graphics.Color;
Import Android.graphics.PixelFormat;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.WindowManager;
Import Android.view.WindowManager.LayoutParams;
Import Android.widget.Button;
Import Android.widget.TextView;

public class Mainactivity extends activity implements Onclicklistener {

Private WindowManager Mwindowmanager;
Private View MyView;
Private Button btn_dayandnight;
Private Sharedpreferences SKINSP;
Private final static String day = ' Day ';
Private final static String NIGHT = "NIGHT";
private int flage = 0;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Mwindowmanager = (WindowManager) getsystemservice (Context.window_service);
Setcontentview (R.layout.activity_main);
Init ();
}

private void init () {
SKINSP = this.getsharedpreferences ("Skinchange", context.mode_private);
Btn_dayandnight = (Button) Findviewbyid (r.id.btn_dayandnight);
Btn_dayandnight.setonclicklistener (this);

String mode = skinsp.getstring ("Skin", "");
if (mode!=null| |! Mode.equals ("")) {
if (Mode.equals (NIGHT)) {
Night ();
}else{
Day ();
}
}

}

@Override
public void OnClick (View v) {
if (flage%2==0) {
Night ();
Btn_dayandnight.settext ("Daylight mode");
Btn_dayandnight.settextcolor (Color.White);
flage++;
}else{
Day ();
Btn_dayandnight.settext ("Night Mode");
Btn_dayandnight.settextcolor (Color.Black);
flage++;
}
}

public void Night () {
Windowmanager.layoutparams params = new Windowmanager.layoutparams (
Layoutparams.fill_parent, Layoutparams.fill_parent,
Layoutparams.type_application,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Pixelformat.translucent);
Params.gravity=gravity.bottom;
params.y=10;
if (myview==null) {
Myview=new TextView (this);
Myview.setbackgroundcolor (0x80000000);
}
Mwindowmanager.addview (MyView, params);
Editor edit = Skinsp.edit ();
Edit.putstring ("Skin", NIGHT);
Edit.commit ();
}

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


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

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

}

Layout file

Copy Code code as follows:

<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">

<button
Android:id= "@+id/btn_dayandnight"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/btn_changeskin"/>

</RelativeLayout>

Related Article

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.