Using relative layout placement on android - android

I may be trying to do something that is not possible. I am writing an app that is using a picture of a remote control as its background, and then I am placing buttons on top of the background using relative layout and margins to position the buttons correctly. I thought I would be able to specifiy different margins in different layout files, but it will only take the margins from the main layout file (the one in /layout). I have two layouts for xxhdpi and xhdpi, and the proper graphics are being picked up, but I can't for the life of me figure out how to move them a different amount based on the screen size. I can get one screen size to look fine, but then the other ones are messed up, no matter what I put in the respective xml files. Is it even possible to do this?
Thanks....

you can place the relative layout anywhere on the view in run time. Here is the sample code this may help you.
RelativeLayout DispView = new RelativeLayout(this);
RelativeLayout.LayoutParams DispViewLayoutParams = new RelativeLayout.LayoutParams(w,h);
DispViewLayoutParams.addRule(RelativeLayout.RIGHT_OF, someview.getId());
DispViewLayoutParams.addRule(RelativeLayout.BELOW, someview2.getId());
DispViewLayoutParams.setMargins(x,y,0,0);
Mainview.addView(DispView , DispViewLayoutParams );

You have to create dimens file under various values folders (values,values-ldpi-v6, values-mdpi-v6 etc.) to specify different margins and the system will automatically pick the relevant values based on the screen resolution.
For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="call_log_margin">4dp</dimen>
<dimen name="no_call_log_margin_left">10dp</dimen>
</resources>
Use these values "call_log_margin" and "no_call_log_margin_left" in your layouts .These will have different values in different dimens.xml

Related

Single layout for multiple screen without different layout folders in android

I have a doubt. If i have one single xml file let's say res folder--> layout folder--> my.xml. i want to display this my.xml to all multiple screen size without creating layout-mdpi, layout-hdpi etc.., May i know what is the simple and best way to achieve this concept ??
Thanks in advance
Place the file at res\layout\my.xml
Every single device will get that layout by default unless you create an alternative.
If you have a single layout file in the default location (res/layout/my.xml), then it will be used on all the devices that your app can be installed on.
However, that's usually not what you want because it will look very different on a device with a different size than what you developed on. To handle this, you can start by creating another layout file in another directory (res/layout-sw600dp/my.xml). This layout would be used for devices where the smallest width (sw) is 600dp, ie, tablets.

Android layout for different screen sizes

I am new to android and i am trying to create a single layout for different screen size.Is it possible or am i want to create different layout for different screens
Of course you can only provide the layout in res/layout, but it might look ugly on other screen sizes, even though (or because) Android tries to scale it.
It is not possible to define layouts for different reolutions in a single file. It's just possible to design the layout to be dynamic so Android can adjust it to the given resolution as good as possible.

how to design one xml layout for all devices without creating folders or different sized images.

I want to design app in which i am using one xml layout,I want to use that layout for every device but i dont want to create new folders for small,large or extra large images in drawable.
how we can design one layout which is fit to all devices without creating images for ldpi,mdpi,hdpi?
Thanks in advance!
just put that layout in the 'layout' folder and your image in 'drawable' folder, one place no more :)
Android system will auto scale resource for you. But the UI will look bad.

Android creating calculator app

I am trying to create a calculator app and I've been having problems with the xml layout.
Due to the amount of button even though I use dp's to determine the size of each button from screen to screen the result varies...
Am i supposed to create the layout in java so I can get the screen dimensions?? if so can I have an example?
You can design the layout file and then make a few versions of it with different sized buttons. put them in res/layout-small/, res/layout-large/, and res/layout-normal/. The system will pull the proper xml file according to what size display the device has.
Another option is to try to design the layout with specifying a size in dp, instead use android:layout_weight and android:weightSum to achieve the proportions you are wanting. Then it should scale itself based on the device without needing any different xml files.

Tablet-specific ImageView width/height/etc values using XML?

I'm making my app compatible with tablets, and I am trying to understand what the best way of doing this would be.
I have a GridView with images and text under each image. Currently the height of each image is set to 120dp rather than wrap_content so that it gets scaled and aligned properly to its height and everything looks even. The orientation change works great, and the GridView changes from 2xN to 3xN automatically.
Now that I want to put it on a tablet, the GridView ends up being 5xN and 7xN depending on orientation (on a 10" tablet). However, to make the GridView look better, I would rather change the image height to a larger value (say 300dp) and have fewer items that are larger in size. What's the best way of doing this?
I don't want to pull out just the element and replicate all its values, changing just one. Ideally, I'd like to simply be able to put the height value into something like strings.xml and strings-xlarge.xml, and then reference it from the layout. Is that possible? I really hate duplicating code/resources.
Thank you.
What you're looking for is the dimen tag. It works similarly to the string tag but for dimensions. You can have something like this in values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="image_size">120dp</dimen>
</resources>
And in values-xlarge/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="image_size">300dp</dimen>
</resources>
Your XML can reference the size by #dimen/image_size and everything will be taken care of for you. You can also programatically access it with getResources().getDimension(R.dimen.image_size) from Context.
Note that as of 3.2, you can use pixel requirements (such as w720dp) in your resource names instead of "buckets" like xlarge. More info here.

Categories

Resources