Night Mode state not detected in app - even when toggled ON - android

If night mode is ON, I want to pick a different layout in my activity.
What I have done:
added separate layout folders for night and notnight as follows:
res
-----layout-night
----------my_layout.xml
-----layout-notnight
----------my_layout.xml
Now, when I toggle night mode and reopen the app, I was hoping the layout would change, but it doesn't.
Is there something extra required for this to work?
Is there a permission i need to add for this to work? If yes, which one?
Not looking for an alternate approach. My use case specifically needs a layout to be picked based on night mode status. I'm trying to figure out why the right layout doesn't get picked automatically, when the folders are in place.
UPDATE : Narrowed down problem. The layout layout-notnight gets picked always. Hence, folder structure is working fine. App is not being able to detect the night mode. (Night mode is toggled ON on the device- tried toggling on and off it works on device but app wont detect)
So question now is:
Is a permission required for it being able to detect the state of night mode? like it needs for detecting wifi state and network state.

You could try using UiModeManager. It provide methods setNightMode() and getNightMode(). Maybe it not solve the problem but this is the way to do it.

The documentation says :
On API 22 and below, changes to the night mode are only effective when
the car or desk mode is enabled on a device. Starting in API 23,
changes to night mode are always effective.
MODE_NIGHT_AUTO automatically switches between night and notnight based on the device's current location and certain other
sensors
And from here(notice the BOLD text):
This can change during the life of your application if night mode is
left in auto mode (default), in which case the mode changes based on
the time of day.
So you can try to adjust the device time to a proper value.
But please also be aware that the night mode detection may as well depend on other certain sensors(which the developer site doesn't say clearly). Guess the light sensor might be taken into consideration.

If you are using a toggle button or checkbox, then you can store a Boolean value in SharedPreferences and can use to that to check whether the user wants night mode or not. This would work even after the app is closed and used again.
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
SharedPreferences sharedPreferences = getSharedPreferences("MyData", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean("dark", isChecked);
editor.commit();
}
});
Then, in the next activity
SharedPreferences preferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter;
if(preferences.getBoolean("dark",false) == true)
adapter = new ArrayAdapter<String>(this,R.layout.list_item_dark,name);
else
adapter = new ArrayAdapter<String>(this,R.layout.list_item,name);
Similarly, you can change layout using this method.

Related

Activity Background change dynamic

I have just started developing an android weather app and I was wondering how to change activity background automatically. For example, in daytime it should show day time or in the night it should show night photos.
This is the app of Sony which has a feature (mentioned above)
Check the screenshots.
Okay Credit goes to SteD;so for you check this(beginner's guide)
Follow this
//set an ID for Relative Layout in content_main.xml(Android Studio)
RelativeLayout rlayout=(RelativeLayout)findViewById(R.id.rlayout);
if(something){Drawable drawble=getResource().getDrawable(R.drawable.your_image);rlayout.setBackgroundDrawable(drawable);}
//If it works,destroy the upvote
The only automatic way is the newly released (Day/Night theme for android app)
For finer control you check the condition yourself and call the normal Java methods, like this:
if(something) {
getWindow()
.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.image));
}
of if you don't care about the newly introduced context themed styling, you just call the deprecated method (which will keep working without issues for all the foreseeable future)
if(something) {
getWindow()
.setBackgroundDrawable(
getResources().getDrawable(R.drawable.image));
}

Different menu items for Activity in debug and release mode

For the application that I am developing, there are some menu items that are helpful for debugging the app (something like resetting counts and stuff).
Is there a way (a directive in xml file or else) to tell android to show/hide certain menu items depending on the app being in debug mode or not?
The only thing I know is the following code, that we can do in the Activity itself, I wonder if it can be used to show/hide menu items:
boolean isDebugBuild = (0 != ( //Check if the app is in debug mode
getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
Or maybe is there a way to do this in onCreateOptionsMenu()
Add this in your AndroidManifest application tag
android:debuggable=true
and in onCreateOptionsMenu check
if (BuildConfig.DEBUG) {
//true for debuggable mode
} else {
}

how to set the screen brightness and Font-Scale in android

iam setting the device settings into certain values. i want to adjust the screen brightness and the Font-Scale iam using the following code:
//For Font-Scale
Settings.System.putFloat(this.getContentResolver(),Settings.System.FONT_SCALE,(float) 1.3);
//For Brightness
Settings.System.putString(this.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,255);
the value of the brightness does change but to set activate it i must go to the screen settings and open the brightness settings and press OK.
the value of the Font-Scale Does not change.
i think maybe a way to refresh the settings to get the new values. can any one help me?
This cannot be done by apps. One way to get these settings to take effect is by rebooting the phone. If you have root access you can use non-public apis. use below code to do it using reflection.
To change brightness you need to call
PowerManagerService.setBacklightBrightness(brightness);
And to change
ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig)
check the source code DisplaySettings.java, here is the method to control font size scale
public void writeFontSizePreference(Object objValue) {
try {
mCurConfig.fontScale = Float.parseFloat(objValue.toString());
ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
} catch (RemoteException e) {
Log.w(TAG, "Unable to save font size");
}
}
here is a solution keep the font size in different fontscale.
float textSize = 22f;
mTextView.setTextSize(textSize / getResources().getConfiguration().fontScale);
and I think #shoe rat's answer is more better for an activity or application context.

Android disable button with case differentiation

So I really have no clue why this isn't wokring - basic stuff I did hundred times before:
I want to disable the button 'loadGame' by doing the following if the boolean is false:
if(saveExists == false){
loadGame.setEnabled(false);
}else{
loadGame.setEnabled(true);
}
saveExists is a boolean taken from a sharedPreferences object which is by default false (works, because I already checked saveExists in an output line).
Basically no matter what cases I create with if/elseif/else the button doesn't seem to get disabled at any point - did I really miss something big?
Okay so here's some code concerning the SharedPreference:
SharedPreferences settings;
SharedPreferences.Editor preferenceEditor;
...
In the onCreate of the class where I want to disable the button:
Resource.settings = getSharedPreferences(Resource.PREFERENCE_AUDIO, MODE_PRIVATE);
Resource.preferenceEditor = Resource.settings.edit();
saveExists = Resource.settings.getBoolean("settings", false);
Log.d(TAG, "saveExists="+saveExists);
and finally, the part to disable the Button:
loadGame = (ImageButton) findViewById(R.id.loadButton);
loadGame.setEnabled(saveExists);
loadGame.setOnClickListener(new OnClickListener() {...}
Oh btw we're talking about a ImageButton here
SOLUTION: You won't see this coming
God I hate to break it to you but that was clearly my fault. My projectpartner toggled the buttons invisible/visible as part of the AsyncTask and it's executed after I'm doing what I tried here all the time - yup shame on me I guess :x
It's very hard to determine the problem without more information. If the is/elseif/else doesn't seem to be working, you can try modifying your code to this to see what's going on:
if(saveExists == false){
Log.v("TEST", "saveExists is false");
}else{
Log.v("TEST", "saveExists is true");
}
If that is working then the problem seems to be with disabling the button. Does execution this line without the if/else work?
loadGame.setEnabled(false);
EDIT
From your update, I see you're using an ImageButton. For disabling an ImageButton, use setClickable(saveExists)
why don't use saveExist itself into the argument no need to used if/else
loadGame.setEnabled(saveExists);
if it is true then it is enabled otherwise not

Android 3.0 Use Physical Keyboard Setting

Background:
I recently purchased a Motorola XOOM Tablet along with the Desktop Dock and Bluetooth Keyboard accessories.
The dock and keyboard work great, but when I take the tablet off the dock to move away from my desk, the keyboard still remains paired with the device and I have to manually change the settings to use the soft keyboard. The same goes for when I set it back on the dock, I need to manually switch it back. It's not a huge problem, but it would be nice not to have to think about it.
So I tried downloading an app from the market that simply toggled Bluetooth on and off when connected or disconnected from a power source, which worked well for a while, but the background service would die after period and become useless until I manually restarted that.
TO THE POINT: I'm trying to write a little app/service for my tablet that will recognize when it has been docked/undocked and switch the "Use Physical Keyboard" setting accordingly.
I have started with a BroadcastReciever to recognize the Dock State:
public class DockBroadcastReciever extends BroadcastReceiver {
private final String DOCK_STATE_LABEL = "android.intent.extra.DOCK_STATE";
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String message = (extras.getInt(DOCK_STATE_LABEL) == Intent.EXTRA_DOCK_STATE_UNDOCKED) ? "Undocked" : "Docked";
Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toast.show();
}
}
But I'm having trouble figuring out the best way to update the setting after the event is fired. I've poked around some examples using InputMethodManager, but all the methods seem to need a specific EditText or some other input to bind to.
Furthermore, I can't seem to find a corresponding constant that represents that setting anywhere in the docs, but graphically, it is located here: http://i.stack.imgur.com/esFaw.png
Can anyone help me out with this?
I would like for there to be a solution for changing the setting, but I am open to other ideas as well.
I have an app that does something similar. It can toggle wifi and bluetooth based on power.
You'll need to register some of this stuff in the AndroidManifest.xml file.
http://code.google.com/p/futonic-wifioncall/source/browse/AndroidManifest.xml
Project Open Source Site: http://code.google.com/p/futonic-wifioncall/
This isn't the solution but hopefully will give guidance on what you're trying to accomplish.

Categories

Resources