here's my code to change the brightness settings of my phone.
After running the app, if I go to the settings>display>brightness of my phone, I see the value changed however not 'updated'. So when I click on the dialog box's ok/cancel button, that's when it gets updated. So am I missing a call that finally after changing the values will update the screen brightness? Thanks.
The code is in C# though, I am using Xamarin and monodroid to develop my App. Thanks people!
Android.Provider.Settings.System.PutInt(ContentResolver, Android.Provider.Settings.System.ScreenBrightnessMode, 0);
int brightnessInt = (int)(1.0 * 255); //To set max brightness
Android.Provider.Settings.System.PutInt(ContentResolver, Android.Provider.Settings.System.ScreenBrightness, brightnessInt);
View v = Window.DecorView.FindViewById(Android.Resource.Id.Content);
v.Invalidate();
Make sure it's not on auto adjust
Android 2.2: Adjusting screen brightness
The last few lines solve your problem.
Also make sure android:minSdkVersion is at least v 2.3.3
Related
In my AIR mobile application I am trying to resize my application when the softkeyboard is activated so that the bottom of my application will not be covered by the keyboard. From what I have read this is suppose to be the default behavior but I have never been able to get it to work and I have tried everything I have found online, instead the keyboard overlays ontop of the application.
I then decided I would resize my application myself based on the height and y position of the softkeyboard only to find out that the built in AIR methods for determining the height and y position of the keyboard are not correct (YAY). I even tried using a 3rd party ANE for determining the screen height by FreshPlanet and their code does not always return correct values either. I feel like this shouldn't be that hard to figure out but I am stuck. Please help!
TLDR:
How can I resize my application to be the remaining screen space when the softkeyboard is activated?
What renderMode are you using? (in manifest.xml) I found that only cpu is working to get the correct keyboard height.
Also try to set softKeyboardBehavior to none: <softKeyboardBehavior>none</softKeyboardBehavior>
I've been trying to set the brightness under the limit of Android settings. There are two similar apps in Google Play doing this:
Shades
Screen Filter
The second one, even allows to use the phone with a black screen, just what I need in my app.
There are a lot of other questions about brightness but not to set it under this system limit, and I've found one question asking almost the same but not helping me:
Brightness Screen Filter
So after a lot of search I've got following code:
int brightness=0; //0 to 255, so I set it to a low level (not enough for me)
ContentResolver cResolver = getContentResolver();
//set the system brightness value, and if active, disable auto mode.
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
System.putInt(cResolver, System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//previous lines don't set the brightness to the current screen so we set it like this:
LayoutParams layoutpars = getWindow().getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
//Now we have brightness to the minimum allowed by Android but
//to achieve what these apps do, I have to reduce alpha of the window to the min
//then the screen is black like I want, and totally usable!
layoutpars.alpha=0.05f;
window.setAttributes(layoutpars);
Here the alpha is the key, this is doing what I want, but only for current window/screen, and I need to save to the system this alpha as default, like I'm doing with the brightness and the Manual mode for applying everywhere.
Do you know how I should do this? I'm unable to find a value "System.SCREEN_ALPHA" to set it with System.putInt or another way to do this.
In the other question I linked before, pheelicks replied with a suggestion of using a transparent non-touchable over screen, but this is not working for me, and the result does not give the feeling of a turned off screen like previous apps.
Thanks.
I decided to implemented this feature just inside my app with alpha. So I couldn't solve the initial question at all....
Anyways, in the end seems that the solution to this question is the one Pheelicks replied here: https://stackoverflow.com/a/4287597/1667990
-This is launching an activity to be always on top,
-with alpha to 0.0f so it be transparent,
-that redirects the touch to the activity behind:
//Let touches go through to apps/activities underneath.
Window window = activity.getWindow();
window.addFlags(FLAG_NOT_TOUCHABLE);
-And most important thing not explained in the previous link, is to set the dim behind our window to 1 (like the dim in a messagebox, but setting to the max to be black behind :D )
window.setDimAmount ((float)1.0) ;
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
I haven't tried that and decided not to do it in my app as it could bring unexpected behavior in my circumstances, but I think it should definitely work, if anyone try please add your feedback!
I have SeekBars in an application which operate as expected and return values that I expect. However, on Android 2.x they display oddly.
I've looked for other similar things such as Android Drawable setLevel(); not filling SeekBar appropriately, but that is for a custom drawable. Otherwise I'm having trouble coming up with search terms for this issue to get anywhere.
In this case, all I'm doing is
SeekBar seekBar = (SeekBar)dialogLayout.findViewById(R.id.my_seek);
seekBar.setProgress(5);
seekBar.setMax(20);
If I move the slider, it properly fills the SeekBar the moment it's moved.
Any idea on what is going on?
After posting this, I realized the problem was the order of the method calls.
Incorrect
SeekBar seekBar = (SeekBar)dialogLayout.findViewById(R.id.my_seek);
seekBar.setProgress(5);
seekBar.setMax(20);
Correct
SeekBar seekBar = (SeekBar)dialogLayout.findViewById(R.id.my_seek);
seekBar.setMax(20);
seekBar.setProgress(5);
From what I gather if you first call setProgress(5) in my case, the system will set the drawable to display the progress as 5% since it is 5 / 100 and 100 is the default maximum.
Then if you call setMax(20), the value of 5 is still valid but the drawable is no longer valid and is not recalculated to display as 25% (5 / 20) of the bar.
Doing setMax(20) first will compel the drawable to be calculated correctly once you use setProgress(5).
In case it's of use to anyone, I tested this on Android 2.1, 2.2, 4.1 and 4.2.
Android 2.1 and 2.2 have this bug, the order matters
Android 4.1 and 4.2 do not have this bug, the order doesn't matter
Essentially to remain backwards compatible, always do setMax(int) first and then setProgress(int).
I created a virtual Android device, 480x800, ran it, and as I already found how to change the screen orientation (portrait-landscape, CTRL+F11/CTRL+F12/KP7/KP9), I used these keys to change the orientation.
But when I try one of these keys, the virtual screen rotates 90 degrees, but the orientation does not change. Hence, the Buttons are vertically placed on the screen, and all the text too. Just like a picture that is getting rotated 90 degrees.
How do I get the virtual degrees to actually switch over to the other layout file (/res/layout-land/activity_entry.xml)? Eclipse's graphical viewer does switching between landscape/portrait correctly.
EDIT
I added this part in the activity right under this the onCreateOptionsMenu(Menu menu)
method, but the application does not recognize screen orientation change:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Context context = getApplicationContext();
CharSequence text = "Orientation changed";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
EDIT 2
When I call this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) from within the onCreate() method, it works nicely.
check in settings tab if auto rotated is enabled, if it's turned off turn it on, then whenever you click rotation button it will rotate by himself
Check whether you have set any android:screenOrientation in the Manifest file for that acitvity. If yes, remove it and check.
If not working, it seems like a bug reported in android issues forums. Check here
First off number lock and then press 7 or press 9 and check emulator's orientation is changed and other way to change orientation of emulator is press ctrl+f12.
You may find, like I did that the orientation change was actually effected but the screen only changed a few seconds later than you would expect. In other words, the answer might be "patience grasshopper."
I ran into this recently and the answer provided here did not solve it. However I found this post:
Android emulator not rotating to landscape
Which mentions that there is a bug in some of the emulator targets (19 for example which I was using).
Simply changing the target to 17 made the rotation start working correctly.
First need to check in manifeast file you are setting android:screenOrientation="portrait" if yes then remove it. It's working for me.
Cold booting android emulator helps me to resolve the orientation not working issue.
I have been having trouble getting the screen dimensions for android. I do not want the size of the status bars included in my dimenions. The app that I'm developing is to be permanently in landscape mode. I have been testing with the android honeycomb emulator. When I get the dimensions using the code in How can I get android Honeycomb system's screen width and height? I get 1280x800. I was curious as to why the person in the linked question has the size of the status bars subtracted while mine does not work that way. Also, how can I make it so that the size of the status bars is actually subtracted? If any additional details are needed I will be checking periodically to provide. Thanks!
Okay! I finally figured it out. I pieced together code from several different sources so I don't know exactly why things work out the way they do. Here is the relevant code.
Rect rect = new Rect();
Window win = getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
screenWidth = Math.abs(rect.left-rect.right);
screenHeight = Math.abs(rect.top-rect.bottom);
There is one caveat though. This code WILL NOT work in the onCreate() method. Instead, place it in
public void onWindowFocusChanged (boolean hasFocus)
This should solve any problems with getting window dimensions!
EDIT: I believe this solution should also work in portrait mode as well. I have not tested it though.