Android: custom views with proportional widths and same heights - android

I try to implement a good reusable color picker for my Sketcher application. Instructions and screenshots are here: http://bit.ly/sketcherapp
The problem is I'm stuck with a good "resizable" UI which enable me to support wide range of devices with different screen sizes.
The top two widgets should be the same height and have proportional widths: 80 to 20. Also it would be nice to specify paddings in XML.
Current implementation is not good. I hardcoded some values into code and also it looks bad on Xoom devices because of inaccurate layout measurements.
Is there any way to implement this behavior? Ideally, I need some way to do it like with HTML tables (pseudocode):
table.width=100%, td1.width=80%, td2.padding=5px, ...
or something like that.
Current implementation:
code:
https://github.com/wargoth/Sketcher/tree/master/src/org/sketcher/colorpicker
layout:
https://github.com/wargoth/Sketcher/blob/master/res/layout/color_picker.xml
Thank you.

The top two widgets should be the same height and have proportional widths: 80 to 20.
Use a horizontal LinearLayout, android:layout_width="0dip" for both widgets, and android:layout_weight="80" and android:layout_weight="20", respectively.
Also it would be nice to specify paddings in XML.
Use android:paddingLeft and kin.

OK. I stopped boring with it and created dedicated layouts for each screen size.

Related

How can I make widgets adjust their size to look the same on every device?

I was wondering how to make my interface's TextViews, ImageViews, Margins, etc... look the same on all devices no matter if I'm using it on a phone or a tablet. I was told that using fragments would help, but I don't seem to understand. Sorry for the babyish question...
You can use constraint layout and set the widget's height and width according to the screen's width and height. Or you can have different layout files for diffrent screen densities.

Designing for different screen - Android

I want my app to run perfectly on different screen sizes, Galaxy Tab 2, Galaxy Note 800 and Galaxy Note 2.
However, my app is fairly complex and I can't use wrap_content or fill_parent at all places. I've tried using layout_weight but even that couldn't be used with RelativeLayout.
Is there any smart way of resizing your screen components depending upon screen size other than making different layouts all together as suggested in Android documentation and is there any way to use layout_weight in RelativeLayout?
Thanks!
You can use TableLayout and in that TableRow. I have preferred these to RelativeLayout in one of my project because RelativeLayout does not support weight property as you said.
Its true you cannot use weight with Relative Layout. If you want to make sure your layout runs perfectly for every device. You must use Nested LinearLayouts with views and do that in weight your UI will be perfect if you have good resolution images for every device.
Moreover in RelativeLayout complex layout can't be perfect as you cannot use weight and sometimes images may get skew or stretch .
Of course using Nested LinearLayouts with views do increase some extra lines but this is the only perfect solution.
I found an efficient way round.
Instead of hard coding dimensions in layout I used a reference of them in Resources/values/dimensions.xml. So, for different screen sizes I simply made different dimension file in Resources, thereby reducing complexity and avoided making separate layouts for all screen sizes.

Android layout, stretch certain parts of the layout

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).

Android Layouts for Multiple Screen Sizes

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

What is the most flexible layout?

What is the best layout to use to support the app on different devices (Size of screen)?
EDIT
I am not just talking about resizing the layout, obviously the OS does that automatically. I am talking about repositioning the objects in my layout.
by repositioning I mean moving the objects according to the size of the screen. For instance i created my layout for a larger screen which looks great, but when i run the app on a smaller device (smaller screen) some of my User Interface elements were out of the bound of my screen.
There is no "best" layout. Almost all types of layouts will scale to different devices (Android is designed around this concept) other than AbsoluteLayout, which is deprecated anyway.
LinearLayout is best if you just have a row (horizontal or vertical) of content to insert. Using values such as dip values, fill_parent, or wrap_content will automatically adjust themselves to their content or screen size.
RelativeLayout, as Vladimir pointed out, is best for layouts where views are positioned relative to other objects within the layout. For instance, a TextView positioned beside a "Submit" button, is a common example.
FrameLayout is sort of a replacement for AbsoluteLayout; you can layer objects on top of each other, just specifying margin offsets from the sides of the frame.
TableLayout is, as it sounds, a layout for Table style design. You can have multiple rows and columns, and set certain columns to stretch to fit the size of the display, so that no matter the screen size, the layout fits as you designed.
EDIT: If you're having objects falling outside of the screen area, try wrapping your root layout in a <ScrollView>. This will allow the layout to be scrollable.
e.g.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
..... //and so on
</RelativeLayout>
</ScrollView>
All Layouts are flexible in terms of size... the rule is: don't use AbsoluteLayout. That's all.
And with regards to the repositioning concerns... well, use always dips instead of pixels and you are good to go. Again, don't use AbsoluteLayout, the rest of the layouts should work fine on every screen size. Sometimes you can anticipate those "disappearing acts" by wrapping your layout in a ScrollView.
RelativeLayout is what you should be looking at. It easily resizes the elements relative to their neighbors. Just make sure to include drawables for all resolutions and densities
LinearLayout,RelativeLayout,FrameLayout are import Layouts....

Categories

Resources