XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/lll">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Java
package com.kc.nightmode;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.graphics.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu mmm)
{
// TODO: Implement this method
mmm.add("Night Mode").setCheckable(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem iii)
{
LinearLayout lll=(LinearLayout) findViewById(R.id.lll);
// TODO: Implement this method
if(iii.isChecked()){
iii.setChecked(false);
lll.setBackgroundColor(Color.parseColor("#ffffff"));
}else{
iii.setChecked(true);
lll.setBackgroundColor(Color.parseColor("#333333"));
}
return true;
}
}
Watch video and try it
0 comments:
Post a Comment