Xamarin Android - Setting TextSize from Dimension file - android

I have a Xamarin Android project where I am using dimension files to specify text sizes, paddings, margins etc for different screen sizes/resolutions.
I have noticed that there is a pronounced difference between setting the value directly within the layout and within code.
For example, if I add the following to a TextView the layout XML
android:textSize="#dimen/nav_tile_text_size"
then I would expect the result to be the same as doing this in code
myTextView.SetTextSize(ComplexUnitType.Sp, _context.Resources.GetDimension(Resource.Dimension.nav_tile_text_size))
The dimension file contains the following:
<dimen name="nav_tile_text_size">17sp</dimen>
but the resulting UI shows that the size has not been correctly applied
What am I missing here??

Thats correct. You would need to either use
myTextView.SetTextSize(Android.Util.ComplexUnitType.Sp, Resources.GetDimension(Resource.Dimension.nav_title_text_size)/Resources.DisplayMetrics.Density);
Unit, otherwise gets multiplied by the density of the phone. I have personally checked this code, and it works

Related

Using relative layout placement on 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

android app development compatible to multiple screen sizes

consider a requirement of an app which need to be run on different screen sizes. lets say app has text fields ediboxes list view etc... and in each screen size the width/height of each ui component has to vary. how to desing an UI for this kind of requirement. please suggest.
Use Width and Height as fill_Parent or wrap_content..
if you are using images make different sizes images and put it in drawable-hdpi,drawable-ldpi,xhdpi,xxhdpi.The Sizes for those imagaes are in developer.android.com
In the res folder,there exists folder named layout,that the default folder which the system find the layout defination.But people also can make other folder like
layout-480x800
layout-1024x768
and so on ,and put the layout files in it.
When the app run on a device which have the certain size,the app will auto find the layout defination in the same folder,if cant it will use the default defination.

Custom Android screen size

I would like to know whether there is such possibility to customize the screen size on Android devices? Something like using half part of the screen and the other half is blank.
For this kind of problems, you can work with percentage. There are 2 common ways:
in java:
You can get the display height from your used lcd-panel and then multiply with your number of percents. In this way, you have to format the button in your activity.
in layout xml:
You should design your layout within the xml-files and not in java code (my oppinion). Use layout-weight like following post: set position size of ui element as percentage of screen size

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.

Android - How to create layout as generic as possible?

I am having trouble in creating generic layouts for my application. As expected, it can be used in a variety of devices and I want it to work properly for each of them. There are several approaches to achieve this problem but I want to create an xml file (similar like web.config files) and at the very beginning of my application I want to take the device's screen width and height and calculate each control's (textview, spinner, button etc.) attributes (such as margin, padding,width, height...) according to this width and height and save these calculated values into my xml file. Finally I want to reach these values from my layout xmls so my layout's visual will be independent from the device and will work properly for each device. Can this be achieved? I could not find any similar solution on the internet. Can anyone help me?
Thanks in advance.
You can do most of this without hard coding values using RelativeLayout and similar mechanisms. The two pass dynamic layout system is made for exactly what you're describing.
However, when you need to be more specific, that's where the dynamic resource system can help you out. For everything you define in res/drawable, res/layout, res/values, etc, you can define specific implementations for device orientation, pixel densities, screen size or even language by qualifying sibling directories with the correct format. Provide a resource with the same name in different folders, and the system will decide which to use based on the runtime environment.
Give this a look:
http://developer.android.com/guide/topics/resources/providing-resources.html
I would not use custom measurements to dynamically set layout parameters. Android specifically has a variety of functionality to address this for you (including supplying multiple image resources, or layouts specific to a screen size).
I have discovered that the more you try to customize the Android layout with hard-coded values (always use DP if you do want to set specific parameters).
Bottom line, you should not try to re-invent the wheel, and just use the well-designed functionality that Android has already built-in to accomplish what you want.

Categories

Resources