Adding screen brightness controls to android application - java

I am looking to add controls to adjust screen brightness locally in my app menu but can't seem to figure out how to do it. I have seen examples to max-out or dim brightness but I am looking to add controls so that the user can control and set the brightness level. Does anyone have any examples, tutorials, source code, or just a place to point me in the right direction?

The internet claims this works, I haven't tried it though:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);

You can change the user's brightness setting like this (make sure you declare permission for WRITE_SETTINGS in the manifest)
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, brightpref);
The documentation calls that setting from 0 to 255. I am trying to figure out whether it shuts the screen off at 0, as at one point I had a widget installed with a slider and you'd cause the screen to shut off if you set it to 0.

This is answered by a similar question.
The only difference is that you will want to tie the screenBrightness member to the value of a user interface control.
Note that this might not work as expected on devices that have auto-dimming sensors.

Related

Layout is not working

I am creating different layouts for different screensizes using the layout-sw<>dp qualifier, but for some reason, it is not working. I have the following layouts:
I made a special sw320dp directory so that it looks nice on one of my test devices which was 569 dp x 320 dp. Then, for my LG-G3, which is 480 dp x 853 dp, I made the layout-sw480dp folder.
However, the 320dp layout is showing on both phones, including my LG G3.
Am I doing something wrong? Shouldn't the 480dp layout show on my LG G3? Please let me know.
Ruchir
This link tells to consider actual sizes that are available to your activity's window when choosing qualifiers
The smallestWidth of a device takes into account screen decorations
and system UI. For example, if the device has some persistent UI
elements on the screen that account for space along the axis of the
smallestWidth, the system declares the smallestWidth to be smaller
than the actual screen size, because those are screen pixels not
available for your UI.
so ensure you have at least 480dp available to your screen and it is not partially taken up by any system UI/ screen decoration.

Inconsistency between px and dp in android

OK, so here's a wobbly one... Before asking my question, I'll point out that I have read this:
What is the difference between "px", "dp", "dip" and "sp" on Android?
So here's my problem, I have some squares on screen (EditText) which I define in my XML using dp units, and identifying them square1, square2 etc., and they are positioned in a FrameLayout (since absolute layout is deprecated and that I don't see any other layout that would suit my purposes;
I want to control the position of the squares on screen by programming, soI use
square1.setX(cc1x);
square1.setY(cc1y);
square2.setX(cc2x);
square2.setY(cc2y);
and so on, and where cc1x, cc1y, cc2x, cc2y are int variables that change according to the logic of the app. The problem is that the java syntax doesn't allow adding a unit to this integer value of the variables, so on some screens my app works just perfectly, but on screens with a different density, everything is either too far apart, either overlapping...
And finally the question(s):
Is there a way of "forcing" the units into the setX / setY statements?
or
Is there a way to get the density of the screen of the device where my app will run? (in which case I could recalculate the X/Y coordinates accordingly)
(I didn't do research on this second possibility yet, as I just thought of it whilst writing this, so please accept my apologies if it is a duplicate of some other question).
Thanks in advance for your thoughts.
You can add this method to easily convert between DP on the screen and PX of the device
public static float convertDpToPx(Context context, int dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
I wanted to update on this thread, since I've found a far better solution to the problem, though the answers you guys (& girls?) really did teach me a lot, for which my gratitude. Finally, njzk2's answer has made me reconsider the whole approach to what I wanted to do. So, instead of moving my boxes around, which resulted in the dimensioning problems, now I just move the content of the boxes around, leaving the boxes nicely positioned in place, which is by far more easy. Anyway, I hope this will result usefull to other starters like me.

Scale drawable in Android to support multiple screens

Currently I seek a solution to simple situation, which appeares to become tricky. I need 7 togglebuttons in android app, which are a simple black circles, but I need them to be in the row and fill parent (screen) horizontally. As a resource I use big .jpeg image of a circle. To make them fill all screens in the same mode, I put them into LinearLayout with
#android:layout_width = "fill_parent";
#android:layout_height = "wrap_content";
#android:weight="70";
Weight is 70, so each button received 10. The problem is that source image is too big, which unfortunately results in...this:
(because I dont have enough reputation for posting images, here is the link
http://postimg.org/image/f8wvs5si1/ )
Sorry for small amount of code and this picture taken via phone, I do not have an internet access on the computer with eclipse and this project for some time. I tried of course to change layout_height on other possibilites, but it didnt work. I could put a weight sum also on the vertical position, but on different screens it wouldn't look same.
I also tried to change the height programmatically in onCreate method,
buttonX.setHeight(buttonX.getWidth());
or do the same with a layout, but nothing helped
Perhaps the question is dumm, but I would be glad to hear some ideas.
This is due to screen density variations. There are several things you can do to fix this:
Use different images for each density (but I'm assuming you're looking for another solution)
Use DisplayMetrics to get the actual width of the screen and then set width/height accordingly.
This code:
buttonX.setHeight(buttonX.getWidth());
probably doesn't work because you are calling it before the layout is drawn, and therefore the width is not the drawn width. You can fix this using ViewTreeObserver like here:
How can you tell when a layout has been drawn?
Use an XML attribute like scaleType="centerFit" Personally, I find these confusing because scaleType attributes don't always seem to behave the same way to me. You might have to try different ones, but sometimes they come in handy if you need an XML only solution.

Getting the actual orientation of the phone

I need to determine the actual orientation of the phone which is running my app and I can't find any way to do it. I have found the "getRequestedOrientation" function but it only retrieves the orientation set by the "setRequestedOrientation" function.
In my app, I let the phone choose the orientation according to it's physical orientation. And I have some customizing to do depending on the result.
How can I do?
Thanks in advance for the time you will spend trying to help me.
According to these two questions:
Check orientation on Android phone
How can I get the current screen orientation?
you can use:
Activity.getResources().getConfiguration().orientation
According to the comment on the second question, however, this value returns only landscape
or portrait. If you need reverse orientation as well, I would suggest querying the accelerometer
values to detect the difference. However, I'm not familiar with Android and
accelerometers enough to suggest how to do so exactly.
Use getRotation()
Its really to get the current orientation.
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();
See this, this, and this stackoverflow question. They all address how to get the Android phone orientation and the second one includes a detailed explanation of how the Android compass and orientation system works.

Zoom in on a point in an image in with VTK

I have an image in VTK that I'm viewing with vtkImageViewer2, and I want to zoom in on a point the user clicks on. I am working in Java. Does anyone know how to do this?
Thanks
I realize you ask for Java, but my experience doing this has been with c++; equivalent java syntax should work, minus the customizability.
Take a look at these examples for picking and zooming. Also, if you set the interactor style to 'image', the mouse wheel should cause a zoom to wherever the cursor is. You probably don't want to do literally what you asked, but rather either do rubberband zoom or have the mousewheel for zooming. Clicking should do something, not just change the view.
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/PickingAPixel2
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/RubberBandZoom
Depending on what you mean by zoom you want to either change the position and direction of the camera (likely) or change the frustum (unlikely).
Have a look at the methods setPosition() and setFocalPoint() in class vtkCamera. Here is the documentation of vtkCamera:
http://www.vtk.org/doc/nightly/html/classvtkCamera.html

Categories