Child view height is zero inside ScrollView - android

I want to have a transparent view behind the LinearLayout which can consume click events, but only those events that were not consumed by LinearLayout children, so I use the following layout xml for this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/test_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
....
</LinearLayout>
</FrameLayout>
</ScrollView>
The problem (when content is long enough to not fit in a screen):
on android 2.3 view (id="test_view") has a zero height but on android 4.2 it has height as big as its parent FrameLayout.
Tried to add dynamically this view through addView in code but not working, replaced FrameLayout with RelativeLayout - the same problem.
It works only with concrete height value (for example, 20dp), but I want this view to be as big as its parent. Please, help to resolve this problem.

Related

Unable to add TextView into main xml Layout

I've been working on an app and am looking to adding a text view below the Camera view so that I could display some text there.
But, for some reason when trying to drag a text view into the layout it doesn't show up on the final screen.
This is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<com.google.android.CameraSourcePreview
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.GraphicOverlay
android:id="#+id/faceOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.CameraSourcePreview>
</LinearLayout>
Layout view com.google.android.CameraSourcePreview height is set as 'match_parent' , so its eating up all the space on the view-port.
Try giving particular height and you should be able to see textview added below CameraSourcePreview.
Hope it helps.
Most likely, you're not able to add the TextView due to there not being enough space on the screen.
You're using a LinearLayout which displays all of it's views one after the other in either a vertical or horizontal manner.
Your CameraSourcePreview has it's height and width set to match_parent, which means it'll stretch completely on the screen. However, in a LinearLayout, this also means that there's no space to place the next View because it'll be placed off the screen.
You can add android:layout_weight="1" to your CameraSourcePreview. This will allow your TextView to fit into the LinearLayout because it's basically your CameraSourcePreview telling others it'll resize itself to allow for other components to fit on the screen.
But if you don't want your CameraSourcePreview to resize itself based on other Views, then you should look into using some other Layouts instead of LinearLayout. Perhaps one such as ConstraintLayout or RelativeLayout will work better, since they allow overlapping Views on top of each other.
This is happening because the height of the camera view is taking the whole space on the display you can use layout_weight for LinearLayout to make some necessary space for your TextView.
Just make height of CameraSourcePreview equals to 0dp and add a property
android:layout_weight="1"
So this it would look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:orientation="vertical">
<com.google.android.CameraSourcePreview
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.google.android.GraphicOverlay
android:id="#+id/faceOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.CameraSourcePreview>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text here" />
</LinearLayout>
Instead of using LinearLayout I suggest to use FrameLayout which is easy to control for your case. It is also possible to display textView on CameraSourcePreview by using following code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<com.google.android.CameraSourcePreview
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity ="center">
<com.google.android.GraphicOverlay
android:id="#+id/faceOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.CameraSourcePreview>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text here"
android:layout_gravity ="center|bottom" />
</FrameLayout>

Custom view with horizontalScrollview in RecyclerView match_parent not working

I have a RecyclerView that includes my custom view.
My custom view not get all the width even so I configure it as match_parent.
what i want is that the horizontal scroll view will take all screen, and also the card will take screen width (so the yelow part will stay outside at beginning)
I've tried to use LayoutInflater.from(getContext()).inflate(R.layout.view_home_screen_card, (ViewGroup) this, false), but didn't work
Also I've tried to set width programmatically, but I'm seeing the wrong size for few milliseconds before it's change and its look ugly.
The weird thing is that when i'm taking out the horizontal scroll view, the view take all place.
attached the layout
<?xml version="1.0" encoding="utf-8"?> <merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/view_home_card_and_button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#color/amber_200" />
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/half_unit"
app:cardCornerRadius="#dimen/8dp">
</android.support.v7.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
</merge>
any more ideas?
Is your recyclerview have match_parent width? If no then make it.

Android GridView Height Autofill the Remaing Space

Is it possible for a GridView to auto adjust to occupy the remaining empty space of its parent layout? If yes how ?
Yeah. It is possible.
Let's consider following xml,
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//First View or layout
<RelativeLayout
android:id="#+id/first_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000">
</RelativeLayout>
//Second View or layout
<RelativeLayout
android:id="#+id/second_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/first_layout"
android:background="#8b3737">
</RelativeLayout>
</RelativeLayout>
In your Activity,
RelativeLayout layout_one=(RelativeLayout)findViewById(R.id.first_layout);
RelativeLayout layout_two=(RelativeLayout)findViewById(R.id.second_layout);
when you set,
layout_one.setVisibility(View.GONE);
then the second Layout/View will occupy the whole parent space.
NOTE: I took relative layouts for examples. It can be any view. But the parent should be a RelativeLayout.
If you want second layout to be hidden and first layout should fill parent,
Then the XML will be,
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//First View or layout
<RelativeLayout
android:id="#+id/first_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/second_layout"
android:background="#000">
</RelativeLayout>
//Second View or layout
<RelativeLayout
android:id="#+id/second_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#8b3737">
</RelativeLayout>
</RelativeLayout>
when you set,
layout_two.setVisibility(View.GONE);
then the first Layout/View will occupy the whole parent space.
NOTE: At least one Layout/View should have static height(in this case).
I hope it will help you.
All the best.

How to Prevent Scrolling within FrameLayouts and Instead Have the Parent LinearLayout Handle all Scrolling?

I have a scrollable LinearLayout parent with a few FrameLayouts nested inside it as follows:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical"
android:orientation="vertical">
<FrameLayout
android:id="#+id/hb_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/gn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/yt_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I would like FrameLayouts to take up the space they need, allowing for scrolling of the full view through the LinearLayout.
Issue is: the LinearLayout will only take up the screen, it won't scroll, instead the FrameLayout's will scroll if the content overflows the bottom of the screen.
To be clear, I just want the FrameLayouts to fill whatever space they need, and the LinearLayout can scroll it like one long view. Must I fix the FrameLayout heights to achieve this? If so, is there risk of my layout breaking on different screen sizes/densities (or does DP really work in all cases?).
Thank you immensely!
EDIT: I have confirmed that setting fixed heights in the FrameLayout does exactly what I want this to do: scroll as one. However, why doesn't wrap_content measure the height and then go from there? That is what I expected the case was... I'm not certain how to judge the right heights for each element.
Try adding a scrollview to the layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<FrameLayout
android:id="#+id/hb_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/gn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/yt_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>

Overlay for RelativeLayout inside ListView with match_parent

I am dealing with a weird problem.
I am using the following layout for ListView rows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:jn="http://schemas.android.com/apk/res/com.appeaser.justnoteproject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<BUNCH OF VIEWS />
<RelativeLayout
android:id="#+id/rlCon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/margin_note_bottom"
android:layout_marginLeft="#dimen/margin_note_left"
android:layout_marginRight="#dimen/margin_note_right"
android:layout_marginTop="#dimen/margin_note_bottom" >
<BUNCH OF VIEWS />
<View
android:id="#+id/vOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blue_overlay"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
In my list adapter, I toggle vOverlay's visibility based on a certain condition. But, even after toggling the visibility, vOverlay is not displayed.
To verify that the code works as intended, I changed the height and width of vOverlay to 50dp. When I did this, vOverlay's visibility worked as intended - except that the size I need is match_parent.
Could someone explain what I'm doing wrong? How can I get intended results?
replace outer RelativeLayout with FrameLayout and move your overlay to be a second child of that FrameLayout

Categories

Resources