ANDROID layout gets messed up from emulator to tablet - android

I've recently developed an android game using Eclipse. I'm really a newbie at this so forgive me if what I will ask is too simple. >.<
The application runs and looks perfectly in the Android emulator installed in Eclipse. However, when we tried to install it in a Samsung Galaxy tab which obviously has a bigger screen than the emulator, the layout gets messed up. Buttons are not in their right order etc. I do have screens using XML layouts and some are simple canvases with sprites. Any idea how I could retain the original layout in the emulator to the tablet? I've tried using RelativeLayout instead of LinearLayout but it still doesn't work.
Any help would be appreciated. Thank you!
One XML file code and it's corresponding screen from emulator shown below.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/MenuLayout" >
<Button android:id="#+id/btnNew"
android:layout_width="140px"
android:layout_height="65px"
android:layout_marginTop="160px"
android:background="#drawable/newgame"/>
<Button android:id="#+id/btnInst"
android:layout_width="140px"
android:layout_height="65px"
android:background="#drawable/instructions" />
<Button android:id="#+id/btnCredits"
android:layout_width="140px"
android:layout_height="65px"
android:background="#drawable/credits"/>
<Button android:id="#+id/btnExit"
android:layout_width="140px"
android:layout_height="65px"
android:background="#drawable/exit"/> </LinearLayout>

You need to use dpi and not pixels.
Here's a good resource.
http://developer.android.com/guide/practices/screens_support.html

As others have mentioned, you should be using "dp" instead of "px". These are device independent pixels and will scale to match different screen resolutions.
You should also follow this guide on how to use a folder structure to support different screen resolutions and sizes.
Cheers

Based on your edited question, and on the comments, here is a way to change your layout to a relative one:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/MenuLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/btnNew"
android:layout_width="140dip"
android:layout_height="65dip"
android:layout_marginTop="160dip"
android:background="#drawable/newgame"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btnInst" />
<Button android:id="#+id/btnInst"
android:layout_width="140dip"
android:layout_height="65dip"
android:background="#drawable/instructions"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btnCredits"/>
<Button android:id="#+id/btnCredits"
android:layout_width="140dip"
android:layout_height="65dip"
android:background="#drawable/credits"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btnExit"/>
<Button android:id="#+id/btnExit"
android:layout_width="140dip"
android:layout_height="65dip"
android:background="#drawable/exit"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dip"/>
</RelativeLayout>
Though, if you want a perfect solution, you need to design the buttons and background pictures in 3 sizes, and put them in the respective folders drawable-hdpi, drawable-mdpi and drawable-ldpi(and for tablets, from 2.2, you can also have drawable-xhdpi!). Read carefully this article and test your layout in emulator for as many screens as possible (from ADT 10.0 it is really easy to preview your layout for different configurations directly from Graphical Layout tab in xml).
Also, android:layout_marginTop="160dip" and android:layout_marginBottom="25dip" are not really useful in a Relative Layout. I would suggest you to try a 9-patch picture for your background, with only the wood board stretchable for the buttons (though I am not sure it's working like that). Alternatively, I would have a separate picture with Main Menu, that I'd place above the buttons, and keep in the background only the trees and everything else.

itz better to provide diffrent layout for diffrent screen size . i also have same issue when developing a game.nw its done.
u may have provided layout ,so u just provide
layout-large,small,and xlarge.and vice verse to ur layouts u must provide drawable folders like hdpi,mdpi,ldpi,xdpi folders and u must save diffrent sized images 58*58,75*75,100*100 for following then u check it u will get it.

Related

match UI with defferent resolution in android

my android app runs on my mobile device correctly.
but the size of elements dose not change in tablet or another device with different resolution.
they are shown small .
i think my app should be able to fit to different resolution.but i do not know how can i do?
i write some my cod below.
their size do not change.
<View
android:id="#+id/view_top"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_below="#+id/view_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
/>
<Spinner
android:id="#+id/spinner_first"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_top"
android:layout_alignRight="#+id/view_top"
android:layout_toLeftOf="#+id/button1"
/>
<EditText
android:id="#+id/edt_number"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/spinner_first"
android:layout_toLeftOf="#+id/spinner_first"
android:ems="10"
android:inputType="phone" >
</EditText>
Use dimension resources:
For example:
android:layout_width="250dp"
This must be changed to:
android:layout_width="#dimen/mycustomwidth"
Want to know how to define custom width for each device resolution?
Go and thoroughly learn about "dimens.xml" file and much more from Android official guide Supporting Multiple Screens.
you must create dimensions files
in each file you put values used in ech device .
for example you create values-xlarge in it create dimen.xml file .
values-hdpi, values-xhdpi ...

Changing layout on multiple devices?

I'm sure there's a basic answer but I'm still learning and I'm not quite sure.
I have an app with a few button on the start screen. It works on a Nexus One (emulator) and a Samsung S2 (device); probably because the screen size is the same. However, when I preview all screens on tablets for example the button are completely spread out and really small. On other devices they're halfway into each other and just doesn't look good. I've tried match_parent and wrap_content but that doesn't help, how do I use some .xml code or layout properties to make sure the button's don't budge into each other yet fill the screen (I'm using relative layout)?! Thank's anyone that can answer.
Is there something in the layout_height/width/align that I could change?
<Button
android:id="#+id/button10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button9"
android:layout_alignBottom="#+id/button9"
android:layout_alignRight="#+id/button8"
android:text="Info" />
You need to make seperate layouts for different screen sizes and densities
http://developer.android.com/training/basics/supporting-devices/screens.html

Android Relative Layout not working properly on Galaxy S3

im working on a design and testing it on:
Galaxy Nexus 3
Samsung Galaxy S3
It seems like the S3 doesn't behave so well with some relative layout commands such as:
Layout_toStartOf
Layout_toEndOf
Simplified problem down below:
Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_toEndOf="#id/button1"
android:text="Button" />
</RelativeLayout>
Images
In editor and on Nexus 3
On Samsung S3
Since im working a lot with scaling and pictures relating to each other, this function is very importaint.
I'd appreciate if someone could share their experiences with me.
Thanks
edit: typos
first of all there is no resource such as android:layout_toEndOf="#id/button1" .
there are only 2 resources for relative layout to arrange according to any widget by its id toLeftOf and toRightOf check your code.
Edit:
toEndOf and toStartOf is added recently in API level 17 i.e. android 4.2 versions so it will not be found in lower versions of android
Have had the same problem:
I used the ToStartOf and toEndOf to align a graphic of fixed size to a 1px wide graphic that was scaled.
[Graphic 1] <-- [Graphic 2 (1px scaled to 20dp)] <-- [Graphic 3]
They wont align correctly when you build it from the right to the left. When you say toLeftOf a scaled graphic it will the new graphic left of the original size of the picture and not at the end of the scaled picture - Thus overlap.
[Grap[hic 1 Gra]phic 2][Graphic 3]
A workarround coud be to build the graphic from Left to the right, put all the elements in a relative layout as a Module and align the different modules to each other.
Thx for the info with the API level with the ToStartOf and ToEndOf functions!

Android layout issue for high end devices

Am a newbie to Android UI...
I got the layout below which is basically a image view, a image button (that is hidden until a certain logic is met) and another image view at the bottom.
This layout works fine in ldpi devices but look bad on other devices... I also had to resize the image by changing its height so it influence the quality of the image.
How can I resolve this by having a standard view that will work?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/welcomeTutorialImage"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:contentDescription="#string/welcome_tutorial"
android:src="#drawable/welcome_tutorial_browse" />
<ImageButton
android:id="#+id/welcome_tutorial_start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/welcomeTutorialImage"
android:layout_centerInParent="true"
android:layout_centerVertical="false"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:contentDescription="#null"
android:src="#drawable/btn_startsavingstatic" />
<ImageView
android:id="#+id/welcomeTutorialProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/welcome_tutorial_start_btn"
android:layout_centerInParent="true"
android:contentDescription="#string/welcome_tutorial"
android:src="#drawable/welcome_tutorial_bar1" />
</RelativeLayout>
You might want to look at android:fitXY property of ImageView. Also, post screenshots that could explain the issue further. There might be a need to have different sized images for different screen sizes/resolutions (ldpi, mdpi, hdpi, xhdpi)
most of the time, you have to adjust the layout and the image resolution for your application to make it flexible with other devices by placing them to the right folder like:
-drawable
-drawable-1280x752
-drawable-800x480
-drawable-hdpi
-drawable-ldpi
-drawable-mdpi etc.
-layout
-layout-land-1280x752
-layout-800x480
-layout-land
etc.
by placing your layout and image to there designated folder, you have to change each layout and image sizes that will much the requirement of each folder.
and if you need further reference you can check this out
http://developer.android.com/guide/practices/screens_support.html

how to design for all device resolution

hi all
i have to use linear layout with all resolution type device such as 480x800,480x854,320x480.
i use x,y position for placing element in linear layout while designing for 320x480. i have to redesign for other resolution device therefore it takes more time. please give common method for all the resolution with linear layout. thanks in advance.
DON'T design for a specific resolution. Use combinations of layout_weight attributes, layout_width and layout_height values of wrap_content and match_parent to get different proportional widths depending on the screen size, rather than hard coding pixel values.
For example, say you want to add a row of four buttons, each taking up equal width, along the top of the screen, offset from the top left by ~5 pixels:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:gravity="top"
android:orientation="horizontal"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 4"
/>
</LinearLayout>
This will give you a reliable result for any screen density, and you never have to worry about a certain pixel value. The use of dp (device-independent pixel) for the padding also changes slightly depending on the density, so that it stays a consistent physical size no matter what the resolution.
Read through the examples on the Android Developers site about LinearLayouts, FrameLayouts, and RelativeLayouts. You'll get a good feel on what each one is good for, and their advantages and disadvantages.
You can define different layouts for different screen categories. So you can adapt your layout so that it fits the actual screen. A good resource that describes how this is done is available here:
http://developer.android.com/guide/practices/screens_support.html
It's a good idea to use density-independent pixel (dp) in layouts. And I think this could be helpful: http://developer.android.com/guide/practices/screens_support.html
Very EXCELLENT ANSWERS ... I believe that alot of individuals and business alike think that Android is a "Device" and not clearly understanding Android is a "OS". Inorder to properly display images for all devices running Android OS one must go with the option to Support Mutiple Devices and develop according to Android OS and not a certain device running Android OS. I have encountered this problem before, my Boss (at that time) said "What size Images does your Test Device need? I said "just get me the images! I was forced to use 320x480 images (according to the test device) and when the Regional Manager saw the demo product on his ThinkPad (and 4 other device's) I was terminated from my position because my Supervisor thought Android was a Device and Not a OS ... and the company Android App has yet to be completed.My opinion holds "Supporting Mutiple Devices is developing for General Android OS while developing according to "Resolution" is for a certain device or type of device.

Categories

Resources