How can I have html buttons proportional to screen size?
I am trying to code an android app using html and css, with buttons that produce certain results, and the program runs fine. the only problem is, when I send my friend the file, his buttons are so small that it's difficult to use.
he's gotten tired of me sending him files to try, so can someone please give me the css that makes everything proportional to all screen sizes?
for example, there are three buttons in one row, I want it to look like the same layout, but larger on larger screens, and smaller on smaller screens.thanks.
I probably should have mentioned this, but the px I changed was in the font size located in the body tag.
Use percents to define specific button proportions.
.button {
height:10%;
width:10%;
}
Would get you a more accurate and preferred result than:
.button {
height:100px;
width:100px;
}
The only reason you would get different on different browser sizes is if you defined the button sizes as specific pixel sizes.
Related
I have a question about checking layouts on Android. I know all basics about screen sizes (size vs density), I usually make layouts with listview or scrollview, so no matter height of device it'll just show more or less content. Problem is I often have a particular layout to make (from iPhone "render" usually). Let's take an example:
I had to made 3 buttons in one row, so I've made LinearLayout with 3 buttons inside, which has layout_width 0dp and layout_weight 1. That did the work. Or it seemed to. I use Android Studio (latest version) to build my layouts, so I can see a preview on different screen sizes and densities. I used "Preview all screen sizes" as well I've looked into "Generic phones and tablets". All were good. However I've sent my apk to a friend, which send me a screenshot that showed one letter from buttons went down to a second line. I was using;
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
His phone is LG L65. I had to make font size smaller, because it appeared really big and didn't fit. How can this be even smallest densities were all good and on this phone wasn't? How can I be sure any layout will fit? Isn't using built-in font sizes a good idea? So I have to own a lot of devices and make different font sizes, margins etc for all combinations of screen sizes and densities?
You have to take in account that the font size can be bigger than expected by a system settings. If you want your button to be single lined, you can use android:singleLine="true" on your button. This might ellipsized the text if needed.
If you want single lined button without ellipsized, you have to build a custom view that down scale the text when needed ...
So I have an Android forms-based app that fills a 7" screen beautifully, portrait only, and there are over 50 forms. Rather than have to redesign each form (or the entire app) to suit each different resolution, my business case solution is to just scale the form to fill the screen. A 10" tablet will look a bit spaced out (but I can add some margins), and on a phone you better have a stylus and good eyesight!
Rather than argue about whether these are good choices, my question is: can it be done? On Windows CE the forms scale with the text size via "dialog box units", which works quite well. Android doesn't have anything like that, unless there are tricky ways of using 'sp' units. Regardless, getting a form to fill a screen nicely on different resolutions looks like hair-pulling-out territory, unless I'm missing something.
In answer to comments, it's a native app defined using XML layouts. For an example, you could take just about any sample app and show how to make it scale to different screen sizes, so you if the screen is small you get tiny text regardless of resolution.
Just to be clear: I don't think this question can be helpfully answered by describing how to use the standard Android way of handling multiple screen sizes: http://developer.android.com/guide/practices/screens_support.html. This is what I don't want to do this time. This really is about how to do other thing -- scaling to fit -- for when you really need it.
I hope I understand your situation well:
If it is a web application (you are using a webview) then use the viewport tag:
<meta name="viewport" content="width=device-width;">
This will fit the page content to the device screen.
Or if it is a native application (also would work for a web application), you can add fill-parent for your layout heigth and width, in your layout XML.
Use this code:
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
To get the height and width of any Android screen, and with this particular dimension, set the proportion for your layouts.
For example,
RelativeLayout fieldMain = (RelativeLayout)findViewById(R.id.field_layout);
fieldMain.setLayoutParams(new RelativeLayout.LayoutParams((Int)(0.8*width), (int) (0.9*height)));
Currently i'm trying to develop a mobile app for the Android devices (using Appcelerator).
There are alot of Android devices out there with different screen resolutions. So i basically want the app to look the same on every Android device.
So suppose i have a background image in the center of the screen. Which is (in pixels) 550x300.
I just tried to set the width and height of the imageviews to dips (density independent pixels). So in my case to: 332dp x 226dp.
I tested this first on an HTC One X. In there the image in nicely centered and i have a small space left on the left and right side to the edge of the screen.
Then i tested it on a slightly older device, the HTC Desire Z. In there the image width is a little bit more than the actual width of the screen. (example screen. The blue square represents the image)
So that means setting the width and height as dp isn't a good choice either for images.
What would be a good way to set the image width and height so that it looks the same on both phones. i.e., so that they both have a small white spaces on the other edges of the image left (like i have now in the HTC One X)??
Any advice on this matter?
edit
Thanks for the info so far. Some of you posted links to resources etc and made some suggestions. I'll try to work them out in the next few days, so i might take a couple of days before i accept an answer. In the mean time, any ideas suggestions are welcome.
Use the various drawable folders, i.e. drawable, drawable-large, drawable-xlarge to store your image assets for your background in various sizes. Review http://developer.android.com/guide/topics/resources/providing-resources.html for more information.
Also refer to Android: Scale a Drawable or background image? for helpful information.
I would recommend using a size to fit.
in objective c it looks kinda like this... not much of a android programmer but this may help.
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
so if you could figure a way to get the frame size then you could set your image to that size any way the view is positioned.
and if you dont want it to take up the whole screen and just the sides then there might be a autoresizing function for android this way your image will be flexible with your frame which will vary based on the phone size.
I think the is problem is not that the image is wider, but the screen width of the phone is smaller on the HTC Desire Z.
I think the best way fot the image to look the same on all devices would be to set width/height programmatically.
But I think this doesn't really matter, as you will encounter much more complicated problems further wil developing for multiple devices. Both look good IMO.
What I would is set your android:layout_width to fill_parent and then add a android:layout_marginLeft and android:layout_marginRight in dip. You can also set a margin for the top and bottom, but based on your screenshots that doesn't seem to be an issue.
As a general rule, try to avoid setting fixed heights and widths for your widgets. Here is a great reference for dealing with different screen sizes:
http://developer.android.com/guide/practices/screens_support.html
So i basically want the app to look the same on every Android device.
No you don't. You think you do, but you really don't. That's like trying to fit a photograph in a 4x6, 5x7, and 8x10 frame -- something's gotta give. You have small phones, medium phones, large phones, 7" tablets, 10" tablets -- these are not the same experience and you simply have to allow some leniency to the design to make it work. If you just want a specific amount of space outside of the image, just give your ImageView a specific margin in DP units, e.g.:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dip"
//...
/>
You should be striving to make the experience the same (although different layouts for tablets are highly recommended) but you can't expect it to look identical across all screen sizes and densities.
Getting my head around Android, I've sort of trouble understanding support for multiple
devices. I've gone through the dev-docu, now for the clear-up:
The idea behind using dp is, that e.g. a button has the same 'real-world'-size, independent of the actual screen-size, right?
If so, that means, I provide the images in ldpi, hdpi and so on. That results in a button, that has always the same size.
If still correct, I understand that now, that my buttons always has the same size, I should provide different layouts, because on a small device, there might only be space for two next to one another, where on a larger device there's room for three.
If all of the above is correct, my question would be, how I deal graphics-tool-wise with it. Say I have a button 100px x 100px at 300dpi, what sizes and dpi's would that be for the new documents in e.g. Gimp for the different screensizes?
Thanx in advance for any smoke-lifting!
Marcus
What it means when you can use your custom assets for different screen densities is that when you put those respective assets, they will not scale again and use them as they are. Now it is true that the size of the screen also varies. That is when you can consider tweaking your layout.
For example in a large screen you may be able to fit two components beside each other and use a linear layout with horizontal alignment. But when it comes to a small screen, that might not be possible and one option is to define a new layout for small screens and say pt those two components in a vertical layout.
For more information regarding how to implement this and best practices, check these Android Docs
After some more study, it seems that the graphocs-tools DPI are actually the one referred to in e.g. the baseline. So to create a baseline-doc, you indeed create a document 470 x 320 pixel in size and a resolution of 160 dpi. The rest goes from there.
I have a simple web page that displays some text and images and trying to find a non complicated way to handle multiple screens.
I have looked through the docs and the only way I can see to do it right now is having to create three separate css files, one for low, one for medium, and one for high res. Unfortunately each one of these have to have different font sizes and dimensions of every element/image.
This seems rather wasteful considering every hdpi element should just be exactly 1.5x the size of the normal one. Is there not a way to make Android just automatically scale this for us while using hdpi versions of the images instead of mdpi ones?
Am I missing something here?
You can try using Media Queries in your stylesheet.
Basically you say if viewport is smaller than X amount then initiate (or make changes to) this style or if viewport is larger then Y amount, implement (or update) this style. You can have as many possibilities as you wish and there is no need for extra styles because the change is implemented on the particular class or ID you choose once a certain width is reached.
Check out this article on Responsive Web Design
Hope this is the sort of info you are after?
I just looked on the very same issue. Well, it seems that Android doesn't automatically adjust the content of Webview (the image, the font size, etc) according to the screen size like what it does automatically with the app icon.
But, regarding the icon, actually, it's not automatically adjusted as well. You still have to provide the icon in three different sizes to accommodate the ldpi, mdpi, and hdpi resolution.
That's why we need to provide three different css files. But, actually, if you read the doc you mentioned previously, you can write those three different css in one file, like this:
header {
background:url(medium-density-image.png);
}
#media screen and (-webkit-device-pixel-ratio: 1.5) {
/* CSS for high-density screens */
#header {
background:url(high-density-image.png);
}
}
#media screen and (-webkit-device-pixel-ratio: 0.75) {
/* CSS for low-density screens */
#header {
background:url(low-density-image.png);
}
}
The upper part is for the mdpi, the middle one for the hdpi, and the lower part is for the ldpi. So, you can easily manage one file for those three different screen resolutions.