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.
Related
I have been set a project for college that needs to make use of the Android Gyroscope Functionality, the problem is, my application is quite simple in that it just displays information about a sports event to the user (e.g. current games, stadium info, team stats etc).
I initially thought that I could make use of the Gyroscope to change my layout to a different style depending on the orientation of the screen (portrait and landscape) but from what I know now, it seems that this isn't really something that the Gyroscope is used for? correct me if i'm wrong?
My question is, what could I use the gyroscope for for a simple like I mentioned?
Android already handles portrait and landscape orientation so as you mentioned there is no need to use gyroscope for this. I believe its the most popular usage is for a game controller when you should rotate your device to do something.
But sure you can use it for something similar in your app. Like to display an image always horizontally despite of the device rotation.
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.
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.
My android application uses the Y Axis accelerometer to control the character in the game.
This is working fine on most mobile phones, however on my Galaxy Tab 2 10.1, the axis seems to be reversed and I have to manualy change the input for it work.
I would like to write a simple conditional statement that would swap based on device type, however I am unsure what the best practice for this would be. As I am sure someone would have come accross this issue before I ask the question.
How can I determine if the device is a table or a mobile in order to change Accelerometer axis?
I think this relates to the default orientation of the device, which tends to be portrait for phones, or landscape for tablets. There's a question here about exactly that: How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout) , and based on my experiences I'd say that this is the best answer . An important point to understand is that whatever the display is doing (landscape, portrait, etc), the x-y-z axes used by the sensors don't change.
I found out the best way to do this is to check the screen size on the device and base a boolean conditional to device on which axis to use. This is tested and working great.
// Check the screen layout to determine if the device is a tablet.
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
// Changle Accelx or Accely input based on the above (Im using
OpenGLES 1.1)
if (isTablet(glGame)) {
world.update(deltaTime, game.getInput().getAccelX());
}
if (!isTablet(glGame)) {
world.update(deltaTime, game.getInput().getAccelY());
}
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.