Android GridView Height Autofill the Remaing Space - android

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.

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>

Android LinearLayout with 2 content, one should take 30dp and another the rest

I have vertical linearLayout. First element is a framelayout that has some content and second element is button that should always be at the very bottom.
While button should be at the bottom, the framelayout should take the rest space that is in the linearlayout
<LinearLayout
...>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
...
</FrameLayout>
<Button
.../>
</LinearLayout>
The key to this solution is the combination of 0dp height for your FrameLayout and the layout_weight attribute. This attribute allows a LinearLayout to divide the "extra" space up between its children. Your button takes up a fixed amount of space, and your FrameLayout takes up no space at all... so everything that isn't the button winds up being given to the FrameLayout and now it fills up the whole LinearLayout while leaving enough space for the Button below it.
Use android:layout_height="match_parent" on your FrameLayout
you should use RelativeLayout somthing Like that :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:layout_alignParentBottom="true"
android:id="#+id/btnTest"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:layout_above="#id/btnTest"
>
</FrameLayout>
</RelativeLayout>

Set width of parent linear layout as percentage of the screen

I am building an android application and I have a dialog fragment. The dialog fragment has a set width. However, the issue is that when the app is run on a device with a different screen size, the dialog fragment isn't centered properly.
Initially, what I had was:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="wrap_content">
<-- code goes here -->
</RelativeLayout>
As you can see, I have a relativeLayout with a defined width. Since I know that you can't use layout_weight in a relative layout, what I did was I wrapped that parent relative layout in a linear layout as such:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:weightSum="1">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
However, that doesn't work since the dialog fragment is cut when I run the application on a device with smaller screen size.
How can I set the width a dialog fragment as a percentage of the screen size? Is this possible at all or would I have to resort to setting it programmatically?
This is a correct way, if you want RelativeLayout have 40% width of the screen, but this technique cant apply to the parent layout, because parent layout doesn't have parent layout and android:layout_weight doesn't affect
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="40">
</RelativeLayout>
Since I know that you can't use layout_weight in a relative layout
We can use layout_weight in any view and layout, if it direct child of a LinearLayout
With the new percent support library, you can now use a PercentRelativeLayout.
Check out this link
The code below will make relative layout 1/2 of the parent and will position it horizontally centered:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
Hope it helps.

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>

Child view height is zero inside ScrollView

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.

Categories

Resources