I'm working on an android app that we're targeting for all screen sizes? How do i go about making my layouts? Shall i make a different layout.xml for each type of screen or is there any other "efficient" way of doing things?
Read Supporting Multiple Screens, specially the section "Best practices for Screen Independence".
Basic rules:
Use wrap_content, fill_parent, or the dp unit (instead of px), when specifying dimensions in an XML layout file
Do not use AbsoluteLayout
Do not use hard coded pixel values in your code
Use density and/or resolution specific resources
In practice, even if your layout will work on tablets, you will also want to provide different layouts for those extra large devices to enhance user experience.
Edit concerning your screenshots.
<ImageButton
android:id="#+id/btnSubmit"
android:src="#drawable/submit"
android:layout_height="22dp"
android:layout_width="85dp"
android:layout_marginTop="15dp"
android:layout_below="#+id/confirmpassword"
android:layout_centerInParent="true" />
You specify here two vertical constraints that might not play well together. Instead of layout_centerInParent, try layout_centerHorizontal.
You could also give a gravity="top|center_horizontal" to your RelativeLayout. So by default the elements get centered in the view horizontally and get sticked to the top.
Also try to align firstname below btnSignin instead of username. You might be lucky.
RelativeLayout is the most complicated layout to use. If you can't get it right after some time, you could simply decide to fall back on using combinations of nested LinearLayout
For landscape and portrait mode, you can use different xmls, incase u need to display your screen according to the orientations. I have been using this method for my apps.
As for support of multiple screen, you can go with relative layout as parent node. Dont go with absolute layout.
you have to take relative layout for any dynamic screen & all measurement will be in percent(%) or u can use the property(fill parent or wrap content), by do so u can somewhat manage layout for different screen
Related
I want to be my Player Activity in the way that main things show user(such as play button ,rewind,forward ,cover ,date) and other things show when user scroll activity in all device with each inch size.I do that in way that I get screen height of device in pixel and give to relative layout programmatically and other things put below of this relative layout.Is that true?
if you ask about how to support different screen size it is devices
you should not add any layout_width or layout_height fixed size
and use weights in linear layout
Use best practices in dimensions use “dp” and in text size use “sp”.
Use nested layouts for better control and to make your activity xml less complex
You can make two designs one for big screens and one for small ones.
it is for layout , I hope it will help you .
I'm creating an application in Android Studio, but the application does not work on multiple screen sizes. I've seen on the internet, people using different xml files with the same name (just change the "setting" it, how large and etc). The best way to do the layout of my application is to use different xml for each screen size? Or does it have an easier way to create an xml and use this for the various screen sizes?
And to complete, I'm using the relative layout, but on the internet, people say that to use LinearLayout, which is better?
Thank you for your help and sorry for my english!
I'm creating an application in Visual Studio, but the application does not work on multiple screen sizes. I've seen on the internet, people using different xml files with the same name (just change the "setting" it, how large and etc). The best way to do the layout of my application is to use different xml for each screen size? Or does it have an easier way to create an xml and use this for the various screen sizes?
Yes. If you want to support a different layout for a larger / smaller screen size, you have to provide a diffrent xml file. You can do this by creating a layout folder. https://developer.android.com/training/multiscreen/screensizes
On the other hand, if you think your project can reuse other layouts of your app and jam them together if the screen is larger, you probably use fragments for these kinds of situations.
And to complete, I'm using the relative layout, but on the internet, people say that to use linear layout, which is better?
The type of layout you use depends on your use-case. LinearLayout for example is very useful for a simple layout with components arrange vertically or horizontally. If your layout is more complex, a RelativeLayout would be better to use BUT...
I would recommend you to use ConstraintLayout instead of a RelativeLayout. The two layouts position items relative to other components but ConstraintsLayout is way more powerful that RelativeLayout. You should take a look at it.
After reading through Android's guide on supporting multiple screen sizes I now know that I need to create multiple layouts for each screen size in order to make sure the elements are located where I want them.
One thing the guide didn't mention is how to programmatically specify in my Activities which layout will get loaded. Is this because Android will automatically detect which layout to use based on the name of the folder it's located in? For example, a layout in a folder named "layout-sw700dp" will be selected for devices with that screen width?
In-short, I want to know if utilizing alternate layouts for different screen sizes is simply a matter of placing the different layouts in the appropriately named and located folders. Here's a picture of the layout in Xamarin Studio with the properties pane displayed. It looks the way I want it on Nexus 4; everything centered. But when I switch to a different-sized device, the elements are no longer centered. See here.
As cricket_007 sad, you should use diferent xmls when using diferent orientations or making your app more tablet friendly.
When it comes to phone you should practise creating one xml that will cover large range of phones.
And dont use padding for placing elements on screan. For example, dont use pading to set TextView on right side of screan. If you use it lite that and set paddingLeft=250dp on some scren your textView will be placed on right edge but on tablet it will be around center.
Proper way is to set something about this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp"/>
an this must be in RelativeLayout to be affective. This textView will be positioned on every phone and tablet on the right side of screen with small padding.
My question revolves around the general problem represented by a specific example.
When I create a button with the following markup:
<Button
android:id="#+id/btnLookup"
style="?android:attr/buttonStyleSmall"
android:layout_height="match_parent"
android:layout_width="40dp"
android:layout_weight="1"
android:enabled="false"
android:text="#string/go_button"
android:textColor="#color/black" />
And deploy the app to an API Level 10 device, the height of the button is considerably less than the height of the same button when deployed to an API Level 17 device - that is, the smaller button does not fill the height of the other items in it's horizontally aligned liner layout whereas the larger version does.
This may have to do with fill_parent vs match_parent, I'm not sure.
The problem I'm having with this is how to define a layout that makes fundamental properties like this the same across devices.
If the answer is, you don't - you use multiple layout files then my next question is what resources do people use to iron this stuff out? For example, is there a list somewhere of the differences between api levels so that someone could consult it, our do others just suss these things out through trial and error?
The difference in rendering of a layout from one device to another should not be related to the version of Android running on the device. The difference, however, can be(and mostly is) due to different screen densities and dimensions.
This problem is (to some extent) nullified by using dp(density pixel) instead of px(pixel) while dimensioning views. You can further reduce the oddities by using different dimens.xml for different screen densities. This way, you will have a dimens.xml inside values-mdpi, another one inside values-hdpi, and yet another in values-xhdpi.
I prefer another approach. I like to handle positioning of items using the screen dimensions. I get the screen width and height at runtime, and programmatically add margins and padding to layout items.
// Get screen dimensions
// Add a ViewTreeObserver.OnGlobalLayoutListener() to the view
// Update height and width for items
// Position items using screenWidth and screenHeight
This, kind of, fool-proofs the rendering. Not sure if this approach fails in some case.
Edit:
You can read through 'Providing Resources' to know about the criteria that android uses to decide on the best possible resource for a given device/situation.
I'm making a little music app.
I`m trying to make my layout as flexible as possible, for this example this means i want to scale up the albumart and leave the rest (buttons for control, title,..) unscaled.
1) is this "good practice"? may i leave the other controls umscaled?
2) how to do this? Which atributes? i have uploaded a sample screenshot here of the design http://www.flickr.com/photos/59647462#N08/7038458497/
for your info this is highlevel overview of my xml:
<linearlayout>
/// Title
</linearlayout
<framelayout>
/// albumart | resize me block
</framelayout>
<relativelayout>
/// subfooter
</relativelayout>
<linearlayout>
/// footer
</linearlayout>
</linearlayout>
extra question: how to stick the footer to bottem of page? is this possible without relative layout?
thank you in advance, hope it's clear what i'm trying to do.
I dont know why you dont want to use relative layout... but if you use a relative layout.. I think it will be easy...Here is an example with three fields..
You can use LinearLayout. Use the layout_weight & weightSum values for stretching. Example Here -
http://developer.android.com/resources/tutorials/views/hello-linearlayout.html
http://developer.android.com/guide/topics/ui/layout-objects.html#linearlayout
1) is this "good practice"? may i leave the other controls umscaled?
I think that this depends on how you prefer your app to look on different devices. Personally I would rather use weights to scale proportionatly. (e.g. top and bottom taking up 25% each and the center section could be 50%). This would avoid controls becoming too small when moving from one resolution to the next.
2) how to do this? Which atributes? i have uploaded a sample screenshot here of the design http://www.flickr.com/photos/59647462#N08/7038458497/
I agree with Shaiful. Linear layout would provide a good layout for your design.
1) Yes, your controls should not scale but they must handle screen configurations changes and different screen densities.
2)The most useful attributes to manage that are:
android:layout_width
android:layout_height
with some
android:padding
android:layout_margin
To make it look good
Generally make sure your UI handles:
Screen size changes. For instance they change if you go from portrait mode to landscape mode.
Screen densities. Phones have different screen resolutions so make sure your images and controls scale properly (use dp instead of px).