I want a horizontal scrollview to take up the full width of the screen and have its child view centered. So far, I'm ending up with it just wrapping the children and everything is left aligned. My view looks like this:
<HorizontalScrollView
android:id="#+id/mainMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#CD000000"
android:fillViewport="true"
android:gravity="center_horizontal"
android:scrollbars="none" >
</HorizontalScrollView>
Inside that we inflate something that looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/mode_panzoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:minWidth="75dip"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/ic_pan"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="#string/mode_panzoom"
android:scaleType="centerInside"/>
</LinearLayout>
<LinearLayout
android:id="#+id/mode_annotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:minWidth="75dip"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/ic_annotate"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="#string/mode_panzoom"/>
</LinearLayout>
<LinearLayout
android:id="#+id/mode_measure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:minWidth="75dip"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/ic_measure"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="#string/mode_panzoom"
android:gravity="center"/>
</LinearLayout>
fillviewport seems to do nothing, and applying gravities seeems to have no effect, probably because its wrapping content even though I set fill_parent as the width.
I figured out my problem, and it ended up being nothing related to the xml. It was the fact that I was inflating the contents of the HorizontalScrollView. After that, if you want your parent to fill_parent correctly you need to force it to layout again via request_layout. Otherwise it won't get the correct size.
Related
I am working on creating a page with a layout with header "Account Information" (ref.. image). Followed by a table and i need to populate the table dynamically after fetching from server but not able to do so. Below is the .xml file and attached is the image:
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_weight="0.20"
android:gravity="center_horizontal"
android:padding="15dp"
android:text="#string/my_account_header_text"
android:textColor="#ffffff"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<RelativeLayout
android:id="#+id/my_activity_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="#+id/my_acc_component"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:text="#string/my_account_component"
android:textColor="#372c24"
android:textSize="#dimen/login_credentials_txt" />
<TextView
android:id="#+id/my_acc_details"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/my_acc_component"
android:text="#string/my_account_details"
android:textColor="#372c24"
android:textSize="#dimen/login_credentials_txt" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
This may not be your issue, since I don't know what the problem is yet, but there is an issue that I see. When using weight in a LinearLayout with a vertical orientation your height should be 0dp. Similarly, if it is a horizontal orientation then width should be 0dp. So for example you would change your layouts to
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff" >
and
<RelativeLayout
android:id="#+id/my_activity_layout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
Also, this is definitely not whatever your problem is but fill_parent is deprecated and match_parent should be used instead.
*Actually, there is a problem with the weight property... if you are working with weight then you better use weight_sum property in the LinearLayout and give your textView or other view an percentage depending on your giver weight_sum value in the parent layout...
However,I think what you are looking for is something like this:*
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|top"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/header"
android:background="#drawable/image" // if you want your title/header to be upon the image. Or just create another ImageView for that image
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:margin_Top="50dp"
android:weight="50"
android:id="#+id/my_acc_component"
android:text="your text"
android:textSize="your Size"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:margin_Top="50dp"
android:weight="50"
android:id="#+id/my_acc_details"
android:text="your text"
android:textSize="your Size"
/>
</LinearLayout>
</RelativeLayout>
I want to fit two Listviews (+two Textviews) and a Button evenly into a LinearLayout. The final result should look something like this:
The Listviews can contain any number of items (from empty to requiring to scroll), however the button must be always visible. A lot of answers here suggest to use layout_weight along with layout_height="0dp" and layout_width="fill_parent", however this results in the Listviews taking up all available space and pushing the button out of the screen. I assume the parameters for the button are wrong, but I can't figure out which of them. At least the TextViews are working, since they are positioned correctly in every case.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="12dp"
android:gravity="left"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
</LinearLayout>
I want to avoid to further nest the layout with additional LinearLayouts, unless there is no other way.
Something like this (not tested):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="12dp"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="wrap_content"/>
</LinearLayout>
Your button needs to have wrap_content height and (I think the reason of your bug) LinearLayout should have fill_parent as the height.
I am using a scroll layout with a layout inflator to fill data from a database. Everything works fine then the screen opens, but when I scroll up, the items scroll past the beginning of where the margin is set at the beginning. I am doing this because I am using a static backgound image that I want to always stay in the same position. Is there a way for me to fix the scroll view?
Here is the XML for the main page.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="#+id/ScrollView01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:background="#drawable/backimage"
>
<LinearLayout
android:id="#+id/mylayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
and this is the XML for each item I inflate to add to the scrollview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"><ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:src="#drawable/nocarpic" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#+id/TextView01" >
</TextView>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:paddingBottom="10dp"
/>
<LinearLayout
android:id="#+id/horizontalLine"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#8B0000"
android:paddingRight="2dp"
/>
</RelativeLayout>
If you mean that you want the scrolling content to clip 70dp below the top of the ScrollView, try removing the margin on the LinearLayout and instead setting android:paddingTop="70dp" on the ScrollView. ViewGroups clip child views to padding by default.
In my LinearLayout the use of weight in Combination with Gravity causes to reverse the effect of weight. The Layout:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView android:id="#+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="8"/>
<TextView android:id="#+id/feedfragmentDate" android:layout_height="wrap_content" android:gravity="right" android:layout_width="fill_parent" android:layout_weight="2"/>
</LinearLayout>
The given LinearLayout contains a Title, which should appromately take 80% of the Layout and an Date+Time with 20%. The Date+Time should have the Gravity of right. The layout i posted doesnt work unfortunately and if i play with the parameters weight, the result is the inversed one.
Is there another way to get the Results without swapping the weights ?
When you use the layout_weight property you need to make the appropriate height or width as 0dip
try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/feedfragmentTitle"
android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="8" android:text="sdfdfsfsd"/>
<TextView android:text="aass" android:id="#+id/feedfragmentDate"
android:layout_height="wrap_content"
android:gravity="right" android:layout_width="0dip"
android:layout_weight="2"/>
</LinearLayout>
Try like this
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left"/>
<TextView android:id="#+id/feedfragmentDate" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_width="wrap_content" />
</LinearLayout>
Good Afternoon,
I have a list being generated gradually on the users screen, (right now the list is sitting in a scrollview but that might not be the final resting place.) Above the scrollview are a few buttons, and below the scrollview are a few buttons. Scrollview takes up whole middle of the screen.
Right now, as the list is generated, it goes under the buttons below the scrollview. I would like it however to stop at the top. Also looking for it to always display the last line, rather than the new information start disappearing at the bottom.
I know this is probably confusing so if you have any questions please let me know.
Right now the XML looks a little something like this, and its layed out in this order on the screen too.
<Button
android:text="Ok"
android:id="#+id/buttonOk"
android:layout_height="wrap_content"
android:layout_width="75sp"
android:layout_below="#+id/textHoleNumber"
android:layout_alignParentRight="true"></Button>
<ScrollView <-- ***would like this to line up under the OK button***
android:layout_height="wrap_content"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_below="#+id/buttonMinus">
<TextView
android:id="#+id/textScoreCard"
android:layout_height="wrap_content"
android:text="Score card info goes here"
android:textSize="20sp"
android:layout_width="fill_parent"></TextView>
</ScrollView> <-- ***would like this to stay above linear layout***
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearLayout1"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:id="#+id/buttonSave"
android:text="Save"></Button>
<Button
android:id="#+id/buttonReset"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_toRightOf="#+id/buttonSave"
android:layout_alignTop="#+id/buttonSave"
android:layout_alignBottom="#+id/buttonSave"
android:text="Reset"></Button>
</LinearLayout>
Always use Scrollview inside linear layout and give android:layout_weight="1". Try following example it will work..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#null">
<LinearLayout android:id ="#+id/layout_above_scrollview" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ScrollView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:id ="#+id/layout_above_scrollview" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
// Your components........
</LinearLayout>
</ScrollView>
<LinearLayout android:id="#+id/add_app_done_layout" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_marginTop="5dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal" android:layout_alignParentBottom="true">
<Button android:id="#+id/add_app_done" android:text="Done"
android:layout_width="100dp"
android:textStyle="bold" android:layout_height="wrap_content" />
<Button android:id="#+id/add_app_revert" android:text="Revert"
android:layout_width="100dp" android:layout_marginLeft="5dp"
android:textStyle="bold" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
If you're talking about what I think you are, try adding a margin to the bottom of the scrollview, and a negative margin to the top of the linear layout. For instance:
<ScrollView android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="60sp">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
<LinearLayout android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-60sp">
</LinearLayout>
As for scrolling to the bottom of the scrollview when a new item is added, look at How to scroll to bottom in a ScrollView on activity startup.
Note: This is assuming you have your ScrollView and the LinearLayout beneath it all contained in a vertical LinearLayout.