I have a LinearLayout with 3 children with weights to set their proportions: 1/7, 3/7, 3/7. The last section contains text that could extend outside the space allowed, so I added a scrollview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical"
android:background="#drawable/package_status_border"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<TextView
android:id="#+id/PackageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
This layout is working fine if the PackageContent text is small:
expected formatting
But, if the PackageContent text is large, rather than maintaining the expected height and just scrolling the extra text it is filling the entire screen:
bad formatting, with scroll view filling entire screen
How to I make sure that the last section is still only 3/7 of the screen, with content scrolling, regardless of how much content the textView contains?
I have also tried putting the ScrollView within its own LinearLayout but that did not work either.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical"
android:background="#drawable/package_status_border"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/PackageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</FrameLayout>
</LinearLayout>
Related
I am trying to add many RecyclerViews dynamically to a LinearLayout depending upon the x coordinate of a 2-D array (Set). All the children of each view are not visible (it appears on scrolling the view).
In short, making the RecyclerView's height to WRAP_CONTENT is not stretching the height of RecyclerView.
I want all the children of all the RecyclerViews to be visible and not let any RecyclerView to be scrollable.
Following is the table_layout which I add dynamically:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Engine"
android:padding="6dp"
android:gravity="center"
android:id="#+id/tv_label"
android:layout_marginTop="16dp"
android:background="#color/light_gray"
android:textSize="#dimen/bid_report_label_text_size"
android:textColor="#color/white"
android:textStyle="bold"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view"/>
</LinearLayout>
Following is the activity_main layout of activity:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button_bid_now"
android:id="#+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linear_layout_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="210dp"
android:id="#+id/bike_image"
android:onClick="startImageSlideshow"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout_details"
android:orientation="vertical"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I add table_layout to activity_main dynamically.
Please tell me how to make the RecyclerView WRAP_CONTENT?
The problem was with ScrollView. Changing it to NestedScrollView solved the problem.
I have a Relative View which has two children an scroll view and a button below it now i want to do this: i want this view to wrap it's content and does not make empty space and also not to exceed screen size.
how can i do that? can any one help?
here is my current layout file which is not working.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</ScrollView>
<Button
android:id="#+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/content"
... />
</RelativeLayout>
RelativeLayout does not have such possibility. However LinearLayout does. Just set LinearLayout's android:orientation to vertical, set ScrollView's android:layout_weight to 1, and it should be working:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ScrollView>
<Button
android:id="#+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
You should use LinearLayout and adjust both the views according to a weight.In that way it will not exceed in any resolution and will adjust acording to the size of screen.
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:fillViewPort="true"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
</ScrollView>
<Button
android:id="#+id/action"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="#id/content"
... />
</LinearLayout>
You should make action button align at the bottom and that scroll view should be above that button. In this way scroll view will always be in screen and also button will be visible.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
... />
<ScrollView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_above="#id/action"
android:layout_height="match_parent">
....
</ScrollView>
</RelativeLayout>
I want to place 3 separate LinearLayout controls inside a RelativeLayout, and divide the available height between the 3 LinearLayout controls, so that they all have an even height.
I have tried, weightSum, as well as gravity, but none of these work as I wrongly assumed it would. I have read about layout_weight, but this is not available in LinearLayout.
I can give each of them a static height, but how would I know the available space for each and every device the app may run on?
Here is my current code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_image">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:weightSum="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:weightSum="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:weightSum="1"></LinearLayout>
</RelativeLayout>
I am still learning Android development, so any help/advice would be appreciated.
The following layout will have 3 LinearLayouts evenly divided vertically
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="vertical"
android:background="#drawable/bg_image">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
I have read about layout_weight, but this is not available in LinearLayout
No you are wrong LinearLayout supports assigning a weight to individual children with the android:layout_weight attribute.
I want to place 3 separate LinearLayout controls inside a RelativeLayout, and divide the available height between the 3 LinearLayout controls, so that they all have an even height.
Better adding 3 separate RelativeLayout inside LinearLayout and then giving android:weightSum="3" to the parent LinearLayout and android:layout_weight="1" to each child RelativeLayout.
Is that you are looking for
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
</LinearLayout>
I want two set Listviews vertically.
I want them to be 40% and 60% length of vertical screen respectively.
Here is the code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_weight="0.4"
android:layout_width="match_parent"
android:layout_height="0dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="0dp">
<ExpandableListView
android:id="#+id/listEvents"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</LinearLayout>
But this doesn't work.
The first ListView takes all the screen.
I have also tried removing the LinearLayout that embeds the two ListViews, but it didn't work either.
I also tried with android:layout_height="wrap_content" in both Listviews and didn't work.
If I rotate screen to landscape and after back to port again, it works.
It fails the first instance creation.
Any idea on how to solve it?
This layout will work:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="#+id/lvwView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
/>
<ExpandableListView
android:id="#+id/elvEvents"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
/>
</LinearLayout>
Just don't inherit from ListActivity or ListFragment!
So I have a two "sections" to my screen. I have the top section which is a ScrollView and a bottom section which is random text. The random text at the bottom will always be changing sizes, so sometimes it can take 40dp, sometimes 20dp, etc.
My question is, is there a way to make the bottom part dynamic (without loss of text) and make the top scrollview adapt to the new size and restricts its own size to "fill the remaining space" based on what portion the bottom part is using?
Like this:
As you can see, the scroll view just fills the remaining space available.
I'm searching for a solution using XML only
Need HELP!
You can try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomTextView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#android:color/holo_red_dark"
android:text="Test" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/bottomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray"/>
Or
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#android:color/holo_red_dark"
android:text="Test" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/bottomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"/>
Wrap both Views in a vertical LinearLayout;
The view which is at the top: height = "0dp", weight = 1
And the view which is in the bottom: height = "wrap_content"