I am using two ListView in a screen. I want to divide the height of each ListView but I want it to be screen independent. For this I need to know the Height of the screen. I am using xml to do this.
Can any one please help me to achieve this?
Thank You
Deepak
Try this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
The important parts are having the ListViews android:layout_height="fill_parent" and android:layout_weight="1" and the LinearLayout android:orientation="vertical"
Documentation for more information about the layout_weight attribute.
Try setting the layout height of the views to "fill parent" and layout weight to 1.
Related
Hey I have a scrollview from the top of the screen to a bit before the bottom of the screen because this is where my ad banner takes place. I want to have some pixels in between these elements ( between buttons and banner ) due the google policy. So far I did this with setting a value in dp. But as devices differ, this results in having a huge gap between the ad and the scrollview on some hi-res phones. This is why I'd like to set
<ScrollView
android:layout_height="(Screen_height-banner)-5px"
(of course I didn't even try it in this way, as this is no code, but I think it sums up pretty well what I plan to do)
Thanks a lot for your answers!
You can't query screen size in XML since it doesn't run at runtime, you can do this by using RelativeLayout and use alignments and margins.
in your case if Screen_height-banner represent a screen height and you want to position it at bottom and make a 5dp separator from end your XML would be
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="5dp">
</RelativeLayout >
If you need to scale the scroll view instead of position it you xml would be
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding_bottom="5dp">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</RelativeLayout >
You can user weight parameter. If you set weight of both layouts 0.5, this will share screen width equals for these layouts.
<LinearLayout
android:id="#+id/alt_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/orta_panel" >
<LinearLayout
android:id="#+id/altsol_panel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/altsag_panel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Use a RelativeLayout, set a Margin of 5px and use layout_above
If the following codes don't work for you, you're probably using a newer version of something, and you could try using android:bottom="80dp" instead of android:layout_marginBottom="5dp"
I need to create new activity in my project, and I want to show pictures. The layout that I want to create is like that.
All heights must have "wrap_content" property.
Which layouts I must use?
Try this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<GridView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="66" >
</GridView>
<GridView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="33" >
</GridView>
</LinearLayout>
<GridView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</GridView>
</LinearLayout>
Note: you can use any other View instead of GridView just make sure the attributes are the same.
I think you want to create an StaggeredGridView.
StaggeredGridView is a modified version of Android’s experimental StaggeredGridView. The StaggeredGridView allows the user to create a GridView with uneven rows similar to how Pinterest looks. Includes own OnItemClickListener and OnItemLongClickListener, selector, and fixed position restore.
Use this library.
Hope this helps :)
Why do you insist on using wrap_content?
You have "fixed" width of boxes (66% and 33%). You can use sp/dp units to achieve this, I am sure. With some tweaking, of course.
For more info regarding this topic go to
http://developer.android.com/guide/practices/screens_support.html
and
What is the difference between "px", "dp", "dip" and "sp" on Android?
Consider using a TableLayout or a GridView and use the attribute layout_weight on the children.
Your first child (66%) can have a layout weight of 2, the second (33%) a layout of 1.
Then the first child will occupy 2/3 of the width (66%) and the second child 1/3, that is 33%.
And use the layout_span attribute to expand the last view on the two columns.
For example :
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*" >
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="2" />
</TableRow>
</TableLayout>
Also see that question for an explanation on layout_weight.
What do you think would be the best way to make such layout in android?
It should fill the screen's resolution, while having the left and right part fixed 150px in width, but the center part should stretch out as needed.
Thanks!
Best approach will be to use LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<View
android:layout_width="150px"
android:layout_height="fill_parent"
android:layout_weight="0"/>
<View
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<View
android:layout_width="150px"
android:layout_height="fill_parent"
android:layout_weight="0"/>
</LinearLayout>
use RelativeLayout, and take Three Views init:
LayoutParams of EachView should be as:
View1-- id- #+id/view1
Layout_Width= wrap_content
Layout_Height=wrap_content
view2: id- #+id/view2
Layout_Width= fill_parent
Layout_Height=wrap_content
toLeftOF=#+id/view3
toRightOf=#+id/view1
view3: id- #+id/view2
Layout_Width= wrap_content
Layout_Height=wrap_content
align_parent_right=true
http://developer.android.com/reference/android/widget/TableLayout.html - this is probably what you are looking for, specifically http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns
I'm having trouble figuring out why my image isn't within the bounds of my imageview. Instead, it is floating off to the left and hidden. Is this only because the Graphical Interface doesn't show it?
EDIT:
I edited the original code to more clearly show the issue i'm having and added a picture(i want the image to show in the red box):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/top_container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<View
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="195"
android:background="#00FF00"/>
<ImageView
android:id="#+id/img"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff0000"
android:src="#drawable/img" />
<View
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#0000FF"
/>
</LinearLayout>
</LinearLayout>
Ok I have it working. The height attribute of "block_container" is set to a random 200dp you WILL want to change the height to whatever your needs are, or potentially set it to "wrap_content". I tested this on emulator and device.
I am also assuming that you want all three block to be equally spaced. Notice how the parent "block_container" has a weight_sum of 9? Well the children are equal widths because they have a weight of 3 each (3 blocks * 3 weight each = 9 weight total).
I noticed before it looks like you were trying to use weight as width directly e.g. a weight of 569. Just remember weight != width directly.
EDIT: added the missing id attributes from some of the views
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/block_container"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:weightSum="9"
>
<View
android:id="#+id/left_block"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="3"
android:background="#00FF00"/>
<ImageView
android:id="#+id/img"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#ff0000"
android:src="#drawable/logo" />
<View
android:id="#+id/right_block"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="3"
android:background="#0000FF"
/>
</LinearLayout>
</LinearLayout>
Why are you setting android:layout_height to be 0dp? I can't even get it to display unless I change this to something like fill_parent.
Either way, its because of your layout_weight for the parent LinearLayout. Specify a larger layout_weight for your ImageView and it will come into view.
<ImageView
android:id="#+id/my_img"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="279"
android:src="#drawable/my_img" />
This worked for me.. but math might not be right.
I don't know where to begin with the issues with what you posted, some simple observations before even debugging further:
Your layouts ALL need ids (e.g. android:id="#+id/another_layout")
You have a width on the second linear layout of 0dp
Your first linear layout has a height of 0 dp, combined with the previous I'm surprised it renders at all
The last inner linear layout again has a width of 0dp
What are you trying to achieve? To me there seems to be a lot of layouts unless you have removed some elements to make it easier to understand? You could post a simple image of what you are trying to do then perhaps we can help you refine the markup?
I want to set the width of a LinearLayout to half of the screen width dynamically in my UI initialization. I have a RelativeLayout wrapped around the LinearLayout, the hierarchy is the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/left_linear_layout"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="155dp" <!--want to set this to 1/2 screen width-->
android:orientation="vertical">
...
</LinearLayout>
<LinearLayout
android:id="#+id/right_linear_layout"
android:layout_alignParentRight="true"
android:layout_height="fill_parent"
android:layout_width="385dp"><!--want to set this relative to screen width as well-->
....
</LinearLayout>
</RelativeLayout>
Alternatively, can this problem be solved using View instead of Layout? Any suggestion is appreciated!
You can do this by using layout_weight, but you'll need to add some invisible views for padding. For instance the following would make your top panel half the screen width:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/left_linear_layout"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
...
</LinearLayout>
<!-- need this view to fill the other half of the screen -->
<View
android:id="#+id/spacer"
android:layout_toRightOf="#id/left_linear_layout"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
....
</RelativeLayout>
The amount each view will take up is layout_weight/total_layout_weight. In this case total_layout_weight = 1+1 = 2 and each view has a layout_weight of 1, so each view takes up 1/2 of the screen.
You could simply use a LinearLayout as your top-level layout, and then set the weight of the two child layouts.