following What does android:layout_weight mean?
I have this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonToastSimple"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="English Toast" />
<Button
android:id="#+id/buttonToastFancy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="French Toast" />
</LinearLayout>
The link tells me buttonToastFancy will take up three quarters of the size (3/(3+1), but it's the other way round. The buttonToastSimple takes up three quarters of the screen size according to Eclipse/my AVD.
What am I doing wrong?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:weightSum="4"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonToastSimple"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="English Toast" />
<Button
android:id="#+id/buttonToastFancy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="French Toast" />
</LinearLayout>
try setting the wanted attribute to 0dp ..
ex. if you are setting the weight for the widths support, use android:layout_width="0dp" along with the android:layout_weight="3". Also, don't forget the android:weightSum="4" in the parent.
the amount of space your view is going to occupy is computed as
dimension/layout_weight hence the view with the lower layout_weight occupies more space in your layout. in the future, you may also need to user layout_weightSum.
They are further explained here.
Related
I have four image icons that I need to display with Imagebutton or Imageview. The problem is when I use the following code, they all are aligned left leaving an empty space to the right.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/sale"
android:layout_weight="1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/shirt"
android:layout_weight="1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/women"
android:layout_weight="1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/technology"
android:layout_weight="1"/>
</LinearLayout>
How can I get them equally distributed on the linearlayout so that they fill the whole width?
I just managed to fix the issue. It seems I was needed to use android:layout_width"fill_parent" instead of android:layout_width="wrap_content". I tried this after #grwww suggested android:layout_gravity="fill" which was not the needed attribute. Then I got to learn about fill_parent
I need to display a ListView next to a Webview (like the Master/Detail sample), but I need to be able to sort the ListView in a different order (to display some items by category or by name for example), but I did not succeed to make it possible with tabs, dropdown menu or SectionPagerAdapter because the Master/Detail sample uses Fragment, and the TabHost is not a Fragment. I am a bit lost with what I should use.
To make it clear, I want to display it like that :
1
Is there some open source project that use this kind of view, or do your have some advice to perform that ?
You can do it also manually. Just make two list views on top of each other and change their visibilities so only one at a time will be shown.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="64dp"
android:layout_height="match_parent" >
<ListView
android:id="#+id/list_view1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/list_view2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<WebView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
This is just skeleton of you're UI that you can use.
I'm building an app, which starts with a PIN screen similar to the stock PIN entry screen, the problem is that I cannot get the design to work on all kinds of phones, for simplicity's sake let's talk only about portrait mode ( I will link the layout xml below ):
on small-screen devices I needed to decrease the size in dp of the buttons, so that they don't hang out on the edge of the screen
on large devices the buttons are bigger, so they don't clump together in the middle of the screen
there are some old (2.3) devices I need to support, one of them is a 5"-ish tablet, on which the buttons hang out ( because the size factor falls in the large category ), but it should be in the normal size category. I know about smallest-width size group, but that is only for >3.2 devices.
I would like to have a somewhat consistent look on all the devices, (largely) independent of its form or size. I thought about dynamic sizing of elements, but there is no support for it, and I don't want to go near the AbsoluteLayout class.
Does anyone share my frustrations, or has developed a good solution?
The layout file I'm using:
<?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="fill_parent" >
<TextView android:id="#+id/enter_pin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/DIALOG_ENTERPIN" />
<EditText android:id="#+id/pin_box"
android:layout_below="#id/enter_pin"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_below="#id/pin_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow android:gravity="center" >
<Button android:id="#+id/pin_1"
style="#style/pinbutton"
android:text="1" />
<Button android:id="#+id/pin_2"
style="#style/pinbutton"
android:text="2" />
<Button android:id="#+id/pin_3"
style="#style/pinbutton"
android:text="3" />
</TableRow>
<TableRow android:gravity="center" >
<Button android:id="#+id/pin_4"
style="#style/pinbutton"
android:text="4" />
<Button android:id="#+id/pin_5"
style="#style/pinbutton"
android:text="5" />
<Button android:id="#+id/pin_6"
style="#style/pinbutton"
android:text="6" />
</TableRow>
<TableRow android:gravity="center" >
<Button android:id="#+id/pin_7"
style="#style/pinbutton"
android:text="7" />
<Button android:id="#+id/pin_8"
style="#style/pinbutton"
android:text="8" />
<Button android:id="#+id/pin_9"
style="#style/pinbutton"
android:text="9" />
</TableRow>
<TableRow android:gravity="center" >
<Button android:id="#+id/pin_back"
style="#style/pinbutton"
android:text="‹" />
<Button android:id="#+id/pin_0"
style="#style/pinbutton"
android:text="0" />
<Button android:id="#+id/pin_ok"
style="#style/pinbutton"
android:text="OK" />
</TableRow>
</TableLayout>
</RelativeLayout>
Update:
I am already using different layouts for different size-groups, and exclusively use dp & sp, FYI, so please refrain from writing generalities. Still the problem lies in the finer details, the size-bins are not well chosen IMO.
I thought about dynamic sizing of elements, but there is no support for it
What does this mean? Dynamic sizing is more or less the basis of Android layouts. You can use DP as you've mentioned for relative sizing based on the density, you can use layout_weight with LinearLayouts to work with fractional portions of the display, you can use wrap_content and match_parent to make the graphics size based on the size of the display, or the content inside. My suggestion to start with would be to give the buttons both a min_width/min_height attribute, that way on a larger device the buttons don't get so large as to be ridiculous, but also don't get too small to press on smaller devices.
An alternate method, if you don't mind having a few different versions of your layout file, is to have more resource-qualified layout folders (e.g. layout-large, layout-small, etc.) with slightly different layouts depending on the general size of the display.
EDIT: Okay, quick idea, just for the PIN pad, I didn't even look at this in a layout editor, but this is the basic idea of how I'd probably do this. There's no maxWidth or maxHeight attribute for LinearLayout, so I'd probably make a resource folder for layout_sw600dp and layout_sw720dp and hardcode a smaller size for the PIN pad for screens of that size.
<LinearLayout
android:id="#+id/pin_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max_width="480dp"
android:max_height
android:layout_margin="20dp"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/pin_row1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="#+id/num1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="1"
/>
<Button
android:id="#+id/num2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="2"
/>
<Button
android:id="#+id/num3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="3"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/pin_row2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="#+id/num4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="4"
/>
<Button
android:id="#+id/num5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="5"
/>
<Button
android:id="#+id/num6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="6"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/pin_row3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="#+id/num7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="7"
/>
<Button
android:id="#+id/num8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="8"
/>
<Button
android:id="#+id/num9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="9"
/>
</LinearLayout>
</LinearLayout>
Yes, UI design for Android is one of the most tedious and frustrating aspects of app development.
There are several ways to deal with the issue (none of which are quick and easy per se). Either lay things out in code, and deal with different densities/resolutions then.
Otherwise, duplicate your layouts, but adjust the values, and store them in separate layout folders which the OS will automatically select from depending on the manufacturer specified screen size.
Take advantage of the layout, layout-small, layout-large, layout-xlarge, layout-xlarge, layout-land, etc. This way, you can basically c&p your layouts in to each directory and modify the specific dimensions that way. Further, if you find you're reusing a lot of the different dimensions, specify dimensions.xml or styles.xml so that you can avoid having to type them repeatedly.
http://developer.android.com/guide/practices/screens_support.html
is a pretty good resource, but the answer in general is that ui design for multiple sizes simply takes a lot of work.
in a general way,define the size of Button or other View using dip or dp, define the size of text using sp, if you do so,then your app could fit most of the screens.
Can someone please give me directions on this:
I've got a ListView, the adapter got a LinearLayout and in this I got four more LinearLayouts.
Now I read about dp and it is relative to 160, but when I read different examples this number "160" seems to be different on different screens, so how do I work with dp?
I am used to work with % in this cases.
Now I want my four LinearLayouts to be:
55dp
35pd
35pd
35pd
Like if the dp was 160!
But as I mentioned above, this dosen't work on all screens.
Can someone tell me how I should work with this? Directions or a good tutorial or similar?
This is what I have tried, and come up with so far:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:paddingTop="15dip"
android:paddingBottom="15dip" >
<LinearLayout
android:orientation="vertical"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:text="Woho"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="Woho"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Woho"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:text="Woho"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="Woho"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Woho"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
As per the android documentation, 1 DP = 1 pixel on a 160 DPI screen. If the screen is 240 DPI, then 1.5pixel = 1 DP. the size of one DP is independent of underlying hardware resolution but a function of DPI of the screen.
To your layout question, if you assign a fixed width value to your layout, then it will not look good on larger screen sizes.
Better to use layoutweight to distribute the width between those layouts.
I have 6 textviews,3 editboxes and two spinners in a Relativelayout.I am trying to add further editboxes in the app. But the app is not showing the additional boxes.
My code is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:id="#+id/EditText01"
android:text="#string/type1"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_toLeftOf="#+id/Button01"
android:layout_height="wrap_content"></TextView>
<EditText
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"></EditText>
<TextView
android:id="#+id/EditText02"
android:text="#string/type2"
android:layout_alignParentLeft="true"
android:layout_below="#id/EditText01"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_toLeftOf="#+id/Button01"
android:layout_height="wrap_content"></TextView>
<EditText
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/Button01"
android:layout_height="wrap_content"></EditText>
<TextView
android:id="#+id/EditText03"
android:text="#string/type3"
android:layout_alignParentLeft="true"
android:layout_below="#id/EditText02"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_toLeftOf="#+id/Button01"
android:layout_height="wrap_content"></TextView>
<EditText
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/Button02"
android:layout_height="wrap_content"></EditText>
<TextView
android:id="#+id/EditText04"
android:text="#string/property"
android:layout_below="#id/EditText03"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_height="wrap_content"></TextView>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Button03"
android:prompt="#string/property"></Spinner>
<TextView
android:id="#+id/EditText05"
android:text="#string/propage"
android:layout_below="#id/spinner"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_height="wrap_content"></TextView>
<Spinner
android:id="#+id/widget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/EditText05"
android:prompt="#string/propage"></Spinner>
<TextView
android:id="#+id/EditText06"
android:text="#string/income"
android:layout_alignParentLeft="true"
android:layout_below="#+id/widget"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_toLeftOf="#+id/Button04"
android:layout_height="wrap_content"></TextView>
<EditText
android:id="#+id/Button04"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"></EditText>
</RelativeLayout>
</ScrollView>
The last editbox or any element further added is not showing in the app.
Please help.
Can you please update the correct and complete xml file content?
Also, from the GUI designer's point of view, I think these many controls are atrocious on a limited display. Though you have used a ScrollView, I would suggest to rethink over your GUI design.
I think they are actually showing but they may be overlapping previous edit boxes. As a test, I replaced all the #strings with sequential numbers as strings and added an extra edit box to the end of your code and it does appear in the layout but overlaps the last one. Try doing the same thing as a test and you will see what I mean. Hope it goes well for you.
Cheers,
Things that strike me as odd about the last EditText:
It's ID'd as a button. Consider using Button if it's a button.
It has no content, unless it's set programmatically. So wrap_content
on the width will give it a width of 0 (so it's invisible).
It's in a RelativeLayout, but you haven't said which other element it should
be placed relative to. I think the default is that it'll overlap
elements, and just go at the top. But because it has a width of 0,
you won't even be able to see it there.
Hope this helps. Death to RelativeLayout. (Not really, it's useful, but I hate it).