android - How to build complex views that stretch? - android

I'm at the planning stages of my first proper android app. Part of the brief is to have a very "good looking" interface (aka, plenty of graphics). As I'm a web developer, and I'm used to the CSS way of doing things, I'm struggling to understand how I will cater for all the different resolutions when building my layouts. To cut it short, my question is this: how do I build complex, image heavy views that can be seen in apps such as SoundHound (example view here, for those not familiar with this app: http://getandroidstuff.com/wp-content/uploads/2010/09/SoundHound-Android2.jpg)? Is there a way I can tile images in a similar way to the way I'd tile in CSS?

It is important that you know how all Layouts Views work before you start designing your main layout. Android OS by very design supports stretching of views and GroupViews. You need to use dp (density independent pixel) units.
In the example you provided, there are some nice nine patch PNGs. Check C:\android-sdk-windows\tools\draw9patch.bat if on PC. These pngs stretch properly as backgrounds for any resolution.
Linear layout have weight which allows you determine how much space will each child occupy, similar to the old column width for tables in %. Just like in old hml, to make a rich layout you use nested GroupViews (e.g. LinearLayout, RelativeLayout etc) and populate them with children views like ImageView, TextView and son on.
It is possible also to have text styled with <b>,<i> tags and have
<img src="DrawableName">
see an example: Html.ImageGetter
Opposite to a nested layout would be to use the RelativeLayout and determine the position of each child view by its top and left margin anywhere on the screen in relation to the top-left corner of their parent RelativeLayout.

Android has some conventions, that help you out when it comes to different screen sizes, screen densities, screen ratios ...
For example there is a directory called "drawable" where you can put the image resources for your application. However, if you want to ship your application for devices with different resolutions, you can create additional folders named "drawable-ldpi", "drawable-mdpi", "drawable-hdpi". The ldpi folder contains image resources for low density screens, mdpi is meant for medium density and hdpi meant for high density. The folder names are part of the Android conventions. When loading an image resource at runtime e.g. on a high density device, the system is looking for the image resource in the drawable-hdpi folder. It there is no image with that name, the system will look in the drawable folder.
This concept also applies to the xml layouts you define in your "layout" folder. Maybe you say that the layout homeScreen.xml needs to look different when the user switches from portrait to landscape mode. Then you can create a folder "layout-land". You create another homeScreen.xml in there and change its apperance to your needs. At runtime the system detects the current mode. If in landscape, it will first check if there ist a folder "layout-land" and if it contains the layout file with the correct name. If not, or when the user switches back in portrait mode and there is no folder "layout-port", the system looks for the layout file in the "layout" folder.
This is only a small part of it, but I hope, you got a first glance and everything was understandable. Further information can be found in the official refrence

Related

Is there a function in Android Studio to automatically adjust the layout to the display size?

I'm in the process of creating a small questionnaire. Although all views are constrained, the individual text views overlap or slide to the edge.
So far I have only created the design for the layout and have not yet linked any functions with anything. When I display the layout on a larger display, everything is shown as it should be.
Can someone help me and tell me how I can design my layout so that it automatically adjusts to the display size?
I'm really new about programming in Android and don't know all the backgrounds I may have disregarded.
I tried to use the AutoSizeMaxText fuction but I'm not sure if I used it the right way.
As far as I know there is no such thing as an automatic function to support different screen sizes.
What you can do:
Use constraintLayout
Use match_parent and wrap_content instead of hard-coded sizes in your xml
Create different layouts for almost every (if not every) screen resolution:
ldpi, mdpi, hdpi, xhdpi, xxhdpi
Use stretchable nine-patch bitmaps for image resources.
Consider using fragments and double-pane structure for large devices
You may want to chek this library: https://github.com/intuit/sdp
and this one for text size: https://github.com/intuit/ssp
Don't forget to test it in as many devices with different resolutions and pixel density as you can.
You can check the official documentation for this issue: Android developers: Support different screen sizes

How to make UI in android for different devices

I'm fresher in Android & Coding. I have a task to design a popular game dots and boxes, for references https://en.wikipedia.org/wiki/Dots_and_Boxes . In this I have 7X7 cicles and used imageview to display all the stuff. My problem is how to design for multiple devices which looks as designed.
There is a great tool that lets you turn one image into multiple images for different screen densities: https://romannurik.github.io/AndroidAssetStudio/
There a few options there for generating the images, i use the Generic icons option since it allows an option for resizing the image.
if you use the generic option then:
Source: click image and upload the image (the circle for example).
Size and Padding: the defaults for size and padding are usually good enough.
Color: defaults to blue just move the seekbar to the far left in order to use the original color of the uploaded image.
Name and Download: at the bottom of the page you can rename the image see a preview and download as a zip.
Import: in the zipped file will be a res folder already structured properly by drawable densities and ready for you to just drag and drop into you /src/main folder.
P.S. Thank you to the person that made this tool your a genius and a life saver.
First of all use the unit dp for defining measurements. In addition to that, you have to define several values folders, where you define dimensions for scaling tablets or phones. E.g. define two values folders like:
values-large
values-xlarge
Now define a dimen.xml file inside this folders, where you can put your measurements in (unit dp) for the corresponding screensize. Define a measurement like this:
<dimen name="value1">17dp</dimen>
Then embedd this sizes in your layout xml, like:
android:layout_height="#dimen/value1"
Depending on the screensize, the system will load the correct measurements from this folders, e.g. if you have a screen size large only the defined values in folder values-large will be loaded. So instead of creating different layouts, define different measurements that can be loaded by the system automatically. For more information about this, also have a look at https://developer.android.com/distribute/essentials/quality/tablets.html

Using layout in android xml

Which is the layout to use which doesn't have a difference in different screen sizes. Linear layout or relative. I had a relative layout and when I run the app on a 4' phone, the icons were way closer. In a 5' phone the icons were a bit away. What's the best way to have a constant XML which doesn't change by screen size.
For example.
I had this app in linear layout which contained 3 images in horizontal orientation. In the design view the icons were perfectly same and equidistant. But on the device, the third icon was smaller. Why
You can't achieve perfect layout for every device with just single layout file. For best results you should make different layout files for different folders used with images designed accordingly. For more information read Android Developers' tutorial from the following link
Supporting Multiple Screens
Over and all, you need to create different layouts/drawables for supporting all the screens. It depends on your requirement, say for example, if you would want to include 2 buttons in small screen and 4 buttons on large screens then obvious you have to create different layouts.
If you would want to display same number of buttons in all screens then I would suggest you to prepare different set of images/drawables and include them in your project. Here you don't need to do anything other than placing images in particular drawable folders.
One more thing, If its a plain background then you can create 9-patch image so that it can stretch with whatever size you want or your device supports.
More study:
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/screendensities.html
http://developer.android.com/training/multiscreen/adaptui.html

Android - common techniques for scaling screen layouts

I realise this topic has been covered to death, and I have read the official and unofficial developer guides on it so know the theory. But I'm still unsure which path to take to ensure my app looks good on multiple screen sizes. My app is a simple navigation style app which is designed to be portrait only. At this stage I'm not interested in making different designs for landscape or multi-pane screens for tablets etc. I just want each screen to be scaled up/down so it looks in proportion with the screen size it is being displayed on - i.e. text and images are sized up/down as appropriate. I designed the app for a classic 480x800 hdpi device which it looks great on, I just want to make it look in proportion for the Galaxy S3/Nexus 7 etc. I'm already following these guidelines :
Using RelativeLayouts
Using DP units for padding/margins
Using SP units for fonts
Using 9-patch images for buttons
However I'm finding it still looks small and a bit lost in the middle of the screen on the larger devices. In particular, my main menu screen is a grid of 6 image buttons which I can't get to scale well. I don't want to have multiple copies to maintain of the same screen (normal/large/xlarge), just want one layout. I was considering the following :
Using value-normal/value-large etc. folders to store XML files with DP/SP values for sizes of images/text
Changing RelativeLayouts to LinearLayouts with weights (although I initially struggled with LinearLayouts and changed to RelativeLayouts
Are either of these valid approaches, or is there a better approach?
Personally, I would go with the first option. While this will work well for phones, it will make the layout seem somewhat bloated on tablets. Hence, another approach would be to create separate layouts for tablets and place them in the layout-large and layout-xlarge folders for 7" and 10" tablets respectively.

How do I Update layouts for long and notlong resource qualifiers?

I noticed that although my drawable resources are being scaled down properly by android, the bottom of the layouts are getting chopped off on smaller screens(I assume notlong screens, as my testing device is a Droid which I believe is long, right?)
Should I be updating the layout files and putting them in a res/layout-notlong folder or something like that? Or should Android be handling this differently?
You rarely need to use -long and -notlong. You should be designing your layouts with layout managers that resize themselves to adjust to the screen size.

Categories

Resources