I'm having an issue with seemingly inexplicable margins that appear on the left and right sides of my layouts when using a Nexus 10 device in landscape mode.
I'm sure it's something embarrassingly straightforward, but I can't find any mention of this when searching around.
I'm pretty sure it's not related to my code, because the default Hello World project created by Eclipse exhibits the phenomenon. The following screenshot excerpts are taken from a brand new project and the only change I have made is to make the TextView textSize a bit bigger for clarity:
You can see that the default margins from the layout XML file (shown below) are applied correctly in portrait mode, but there is a considerable additional margin applied in landscape mode - indicated by the red bar underneath the screenshot.
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
Has anyone else seen this or have any idea how I can get rid of them? I don't really know where to start, because it seems to be specific this one device and screen orientation. Plus it affects the simplest program possible, as well as my own, so there's no sense in pulling apart my own code until I find out how to fix the default case.
The same screens display correctly on emulated devices and my Samsung Galaxy S2 (running Gingerbread). Changing the Project Build Target from API level 17 to 10 didn't alter the unwanted effect either. Any thoughts?
Ok, so it was embarrassingly straightforward! The margin for the Nexus 10 in landscape mode is taken from the 'values-sw720dp-land' resource folder, which declares a much larger value:
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
-->
<dimen name="activity_horizontal_margin">128dp</dimen>
After nearly a year of lurking on SO and finding answers to all my questions, the first one I finally decide to ask properly turns out to be as trivial as this...
Thanks, danj1974! You saved much time for me! I changed a setting in res/values-w820dp/dimens.xml to
<dimen name="activity_horizontal_margin">0dp</dimen>
and it works good.
Related
I have a strange bug on Samsung Galaxy Tab E (Nougat 7.0 API 24): I have 2 layout directory (layout and layout-sw600dp) when I change the "Screen Zoom" on the "Settings" to "Huge" the device for some reason is taking the XML from the "layout" directory and not from the "layout-sw600dp", for all the others density it works perfectly except the this one.
Is there a solution for this bug?
It's not only in Galaxy devices but you can have this in any device exceeding the sw system.
So instead of making specific folders like layout-sw600 etc, I recommend you to make specific layouts using ConstraintLayout.
ConstraintLayout not only makes your layout flexible but also adjusts it's size based on the increase or decrease in Density Pixels of the devices and also saves your. Development Time. I faced a similar issue long time back and decided to use ConstraintLayout for the same.
A good codelab from Google on ConstraintLayout can be found here:
https://codelabs.developers.google.com/codelabs/constraint-layout/index.html?index=..%2F..%2Fio2018#0
Hope it helps. Good luck.
The system will use the one which is closest to (without exceeding) the device's smallestWidth, but when Screen Zoom set to Huge, the smallest width of device exceeds 600dp.
And your layout files only have two folders (layout and layout-sw600dp), so finally the system choose the layout folder.
I have a (RelativeLayout) layout xml that was designed to work for 10 inch tablets. I now want the very same layout to be displayed when the app runs on a phone. Instead of it being only displayed partly, I want it to be scaled, so that the structure is the same, just everything a bit smaller.
I have already:
Searched the web - without a result
Read about LayoutInflaters - but have no idea where I could set a scale there
Tried to "override" the defaultWindow's density using Reflection - without a result
Please note that I am aware that this is bad practice, but it's just a personal project and I am curious to know if this is possible.
Any help is highly appreciated!
EDIT: What I want is to really scale the screen as a whole.
I am not the author of the original layout, as it's an open source project for tablets that I want to use on my phone.
Here is the source: https://github.com/amigold/Gui2Go
The layout I am talking about is main.xml.
My reputation is too low to ask you to include a sample layout, and my proposal does not use the exact same layout. However, You can create layouts with the same id tags and filename.xml having the 10 inch tablet version in the folder res/layout-sw600dp and the phone version in res/layout. The layout in /layout-sw600dp will be automatically used for larger screens & the layout in /layout will be used for smaller screens. You will not have to change your setContentView() call at all.
see: http://developer.android.com/guide/practices/screens_support.html
I've small problem - I've and app (home screen widget) for android ICS and higher. But different phones with different OS version (4.0.4, 4.1.2, 4.2, etc) and also different brands (Samsung, HTC, LG, ...) makes my app look diferent.
Ok, it doesn't happens always, but on some phones just Android adds automatic margins to my own - and on some not!!! How is this possible, and how to avoid this? Looks like every brand does it in their own way - which is fine for them, but bad for me :)
EDIT:
I use dp, not px. The main problem is not in dp/px nor in portrait/landscape changes. Main problem is, that for example on phone1 my widget has NO margins (it's like sticked to screen borders) and on other phones it has automatic margins like it should. When I add my own margin to widget provider, it has this margins on phone 1, but on other phones it has (my margins + automatic margins). Hope, now it's clear :)
Welcome to Reality show, when the Android it isn't platform independent, either Java it isn't.
For this reason a single android layout.xml should use elements which are doing layout acceptable in all targeted devices. The screen ratio differs, the dpi differs the resolution differs... As best practice:
use dp size instead of pixels
do different layout for portait and landscape case
have multiple folders based on screen size ( and different layout)
I hope it helps!
well, to close this answer - it looks like there is really no option to have same margin on all devices - as different devices uses different home screen implementation. I can't do anything with that...Closed.
I have an app, which provides a switchable set of night mode (red) controls using a custom Theme. The controls are in the Shared Preferences view. I have copied and colored all of the necessary images for my controls from Android git sources' hdpi and mdpi folders. The red theme works perfectly fine on the 160 dpi screen (480x800 Android 3.2), but on the 240 dpi one (480x854 Android 2.1) custom check boxes and radios are invisible, they do not occupy any space on the line with label either. Though the drop down list control on a simple linear view seems to be shown perfectly fine. The only difference I can see is that the later one has .9.png images.
What is wrong and how to fix that?
HURRAY! Found the solution by an accident. I have added a hidden checkbox item to one of the first views of my application, and all of the styled checkboxes magically appeared as they should be in every other view! I believe it's a bug in the 2.1 version of Android. I saw something like that mentioned once while googling for the problem here. It might be irrelevant though.
I have a problem with missing pixels on QVGA screen on Android (all versions including 2.2). The original problem was with LinearLayout and margin="1px". It was OK on the normal screen but on QVGA there was sometimes no space between GUI controls, like if margin="0px" and not 1px. So I tried to make the example simple (to be sure that it is not a LinearLayout bug), I have just put transparent (height = 10px) and white (height = 1px) images into ImageViews into the main screen. 25% of the white images are not shown on the QVGA screen. The behaviour on the normal screen is OK. The screenshots are from the emulator but the problem exists on real devices too.
See screenshots on my web page.
Is it an Android bug? Can I do anything with it? Note that my original problem was with LinearLayout and margin="1px". Is there something like margin="1px_that_is_not_hidden"? (pt, dp, dip, ... don't seem to be solution)
Many thanks for a reply.
You are probably in a compatibility mode, where Android is attempting to scale your dimensions for you, rather than thinking that you know what you're doing. It's been ages since I targeted a QVGA device, so I forget the precise details. However, I recommend you read the multiple-screen-size instructions. In particular, you probably need to set android:anyDensity="true" in your <supports-screens> element.