I'm new in flutter and haven't used android before. When I complete pages, I simply set the width and height of a widget according to the XD designed by UI designers. As I know UI designers usually set the size according to some one phone type. My question is: If the flutter program run in different devices, how can I change widgets' size automatically to adapt to the phone(or ipad). As we can imagine, if my program is based on ipad mini(1024x768), the designed widget size will be a little big, then some error maybe occur if I run the program in a samller device(like modbile phone) I think. Thanks for any help.
If all widget's are placed static and have proportional size, then you can use https://pub.dev/packages/flutter_screenutil, this library or MediaQuery widget.
Or design have ratio size, then you can use Flexible and Expandable widgets as you need.
Related
i am a new flutter developer. I have problem with application responsiveness, I have worked with layout widgets such as column rows etc but the results in different screens differ.
I need help on how to achieve responsiveness with less dependencies help please.
You haven't provided any code which makes it difficult to answer. So I am going to take a very generic approach here. Checkout the below points and try to use them wherever you can:
Expanded Widget - Can make its child/children grow/shrink based on available screen space
Flexible Widget - Similar as above but with slight difference
MediaQuery - Can give the exact screen height and width of the physical device. This information you can use to define different screens/layouts for mobile/tab/desktop
Visibility Widget - Can hide/show based on certain condition. Use MediaQuery to get screen size and then based that hide/show sections of your screen
LayoutBuilder - provides the parent widget's constraints at layout time.
And finally, checkout this official blog from flutter team on Responssive UI Design.
I'm designing an AppWidget and I have run into such problem: I define in xml
android:minWidth ="296dp"
android:minHeight="146dp"
That means 4x2 cells size of my widget.
In my program code I am creating a custom view by drawing text and figures on canvas. For proper scaling of my custom view I must know the boundary dimensions of my appwidget's placement on homescreen. I couldn't have found any useful information how to determine real appwidget size on homescreen (the all resources have told - "you can't!" - is it really so?) and so I estimated my appwidget size using android:minWidth and android:minHeight="146dp".
The problem is that I can't give proper appearance of my widget on all devices which I've used for testing, especially
it concerned tablets.
I've read https://developer.android.com/guide/practices/ui_guidelines/widget_design.html and other official docs, but I can't come to the right conclusion.
1 - I've found two formulas for determine size of one cell:
1)minWidth or minHeight = 70 * n - 30 and
2)minWidth or minHeight = 74 * n - 2
I've used the second and identified that it works perfectly for usual handsets (not for Samsung Galaxy Tab), but the officials offer to use the first one. If I used the first one on handsets it would work too, but I`ve got a less size widget components (size widget components - text, rects and etc. which I draw on canvas), hence the appwidget appearance
is worse than in second case.
In tablet case I've got an empty spaces at right/left of my custom view and having regard to my widget layout (where I have custom widget and two buttons) it looks bad. I think that empty spaces on tablet occured because of mismatch real and estimated width of appwidget.
How can I calculate a size widget that it would look proper on tablets?
2 - I can't use 9-patch resources because I create my view programmatically. I've tried using android:scaleType in layout widget
<ImageView
android:id="#+id/custom_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="#string/image_description" />
but, for example, in Motorola Xoom I've got bad appearance of my appwidget after stretching custom view.
3 - I figured out that Android device resize widget by itself but I couldn't quiet catch the logic of such resizing.
After all, how to design appwidget size for handset and tablets in my case?
I'll be very grateful to you for your help.
I do not think it is possible - I had the same problem with my app widget.
Instead you can create different versions of your app widget (i.e. 1x4, 2x3 etc), but I know that is not what you really want to.
The scaling done within the app widget of for instance text inside TextView (even with sp) seeems to be beyond our control in appwidgets.
I am currently writing a remote control program to control a robot on all android devices.
I am trying to display the layout to fill the screen fully on different sizes. My first try was on a samsung 10.1' tablet and it was working well but when I port it to smaller devices like 4.3' the layout goes wrong. I am thinking of creating several layouts to match with different screen sizes but how do I check which layout to set according to screen sizes?
I have tried getwidth and getheight but it only works after you have set the layout.
Please give me a short sample code if possible as I am very new to this.
If there are any other better ways please advice me on it.
Please note that I am using API level 8, android 2.2.
Go through this and this. Basically, you create layout for various screen, each with same name. They are put in different folders (each named according to factors like- landscape, portrait, screen density and screen size). OS will decide on it's own about which layout to use.
This is great article about that. But in overall, never expect the screen to be any static size. But if you're deciding to create layout for each size, you don't have to worry about choosing the best layout, android will do it for you if you provide multiple versions of same layout.
You do not have to check the screen dimensions. Just create your layouts, and Android will automatically pick the correct one for your screen size, orientation, API level, etc. See here for more details: http://developer.android.com/guide/practices/screens_support.html
I am developing an application for whole android devices. But resolation of screens are different and that is the biggest problem how it looks. So, I want to make resizing controls and also I used absolutelayout but It is still same.. I give value to controls as dp ..
How can I solve this problem ?
You don't resize the screen of an android device - you make your app instead work with the various screen sizes.
The relevant docs are here.
You cannot hardcode the dimensions of your layout and expect it to work on every screen size. And there is no method which automatically does it unless you write it.
You might want to change your approach, use Relative Layout or Linear Layout instead and use values like fill_parent and wrap_content while designing your layout.
Another approach Android developers follow is use different resource files for different screen sizes and Android loads them automatically at runtime.
Refer to this for more info on how to work with different screen sizes effectively.
I have built an appwidget with an square layout, and so it doesn't fit exactly in the standard widget sizes as recommended in http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#sizes.
I chose a 3x2 size (android:minWidth="220dip" android:minHeight="146dip") as it is the smalllest that covers the widget's layout.
In http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#design Google recommends:
"All widgets must fit within the bounding box of one of the six supported widget sizes, or better yet, within a pair of portrait and landscape orientation sizes, so your widget looks good when the user switches screen orientations"
My widget looks good in portrait mode. When switched to landscape mode (in the emulator) the layout is clipped. I tried inverting the minWidth and minHeight values in the provider's XML and then it looked perfect in landscape mode but clipped in portrait mode. Setting the size to 3x3 solves the problem, but then the widgets takes a lot of unnecessary space.
I know I can define different layouts in res/layout and res/layout-land, but in this case the layouts are not different at all, in both modes I want the widget to look square.
What I would need is something like 'xml' and 'xml-land', AFAIK this is not supported in Android.
Ideas?
What I would need is something like 'xml' and 'xml-land', AFAIK this is not supported in Android.
It is supported. All resource set qualifiers (e.g., -land) are supported for all resource types.
Whether it will help you is another matter entirely, as I am not aware that you can change actual app widget size on the fly this way.
Setting the size to 3x3 solves the problem, but then the widgets takes a lot of unnecessary space.
You are the one who is trying to force a particular pixel size (or, at least, aspect ratio). This will be fragile, as you are discovering. Furthermore, app widget cells are not guaranteed to be the same size on all devices and home screen implementations.
Hence, you are either going to need to choose an app widget size that gives you tons of extra space (your 3x3 scenario), or design a fluid app widget layout that adapts to the actual size that you are given (and therefore will not be square). Personally, I recommend the latter.