Purpose of FrameLayout? - android

All examples I find for FrameLayout use the FrameLayout to stack multiple children on top of each other. Even e.g. Stackoverflow answer here says
You use a FrameLayout to stack child views on top of each other,
The official documentation however states
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view,
While adding multiple children to it is possible, it sounds like it's just a side effect.
So, what's the originally intended use case for a FrameLayout with a single item? Anyone has an example for that?

There are different purposes why FrameLayout with only one child can be useful. Imagine having a loading spinner that should be centered but not fill the whole page but also you want the content to be hidden when the loading spinner is shown. Possible solution: FrameLayout with match_parent for width and height and a background color and the ProgressBar (Loading spinner) with android:layout_gravity=center as only child of the FrameLayout. So now you can set the android:visibility of the FrameLayout to show or hide the whole thing.

Related

Is there compulsion of using `FrameLayout` for loading fragments?

I have seen that most of the people use FrameLayout for loading Fragments.My question is Why FrameLayout ? and why not the others like LinearLayout,RelativeLayout or ConstraintLayout.Most of the answers on StackOverflow says FrameLayout is designed to block out an area on the screen to display a single item. But the others can also block the whole screen if the its height and width set to match_parent.What is the difference ?Why most of the people choose FrameLayout if others can do the same job ?
Thanks in advanceCheers
You can use anything (LinearLayout, RelativeLayout or ConstraintLayout).
FrameLayout is just the most basic ViewGroup that provides the least functionality. It is ideal to use if you only need to hold a single child - in this case, a fragment.
FrameLayout To load child one above another, like cards inside a frame, we can place one above another or anywhere inside the frame.
Designed to display a stack of child View controls. Multiple view controls can be added to this layout. This can be used to show multiple controls within the same screen space.
LinearLayout Designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout Designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
For more information, please check this
https://developer.android.com/guide/topics/ui/declaring-layout#CommonLayouts
I hope it's helpful to you!
I'm not so much good at programming but I can give you some reasons.
First, have a look at official docs.
In order to replace one fragment with another, the activity's layout includes an empty FrameLayout that acts as the fragment container. https://developer.android.com/training/basics/fragments/fragment-ui#AddAtRuntime
This means for switching between multiple fragments we have to use FrameLayout. Why Framelayout then? Another look at the official docs.
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
https://developer.android.com/reference/android/widget/FrameLayout.html
It can block out an area of the screen to display a single item. Other layouts could do the same thing too. So Frame?
Because FrameLayout has one characteristic that other layouts don't have.
FrameLayout can hold its child one above another, like a deck of cards. In a deck of cards, one card is placed above other.
FrameLayout does the same job. When you use FrameLayout as a fragment container it holds the child fragment one above other as your code wants. Then it shows one and left others behind it, you switch back to other fragments then it comes above and others go behind again.
That's all I know.

Implementation of two ListViews in a Frame Layout

i have very basic knowledge about the framelayout . i wants to implement two listviews in a One frame Layout plus and the condition is whenever i click any of list out of two list in a frame layout "the clicked list must get zoomed out to fill the screen".
This is not the correct way to do this, if you've read the FrameLayout doc you'd see this:
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
What you want to use is fragments.
I hope this helps.

Use of Frame Layout in a project

there are many question about "FrameLayout". But i need the exact use of this different from LinearLayout, RelativeLayout. And in a project when we have to use this?
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
LinearLayout arranges elements side by side either horizontally or vertically(rows vs columns).
RelativeLayout is a layout manager that helps you arrange your UI elements based on some rule. You can specify thisngs like: align this to parents left edge, place this to the left/right of this elements etc.
Check these links
http://logc.at/2011/10/18/when-to-use-linearlayout-vs-relativelayout/
http://developer.android.com/reference/android/widget/FrameLayout.html

Dimming background around view

Given an activity with multiple views, is there a way to have the background dimmed while one view retains its original state (not dimmed)?
I have an existing layout with views and I am adding a dimmed framelayout over the existing layout, and would like to keep one of the child views in the existing views "lightened up" in its state while the dimmed framelayout view encompasses around it.
The way I know how to do this won't be very elegant for your particular use case. But I'll explain it in case you don't find a better solution.
Wrap your entire layout in a RelativeLayout, and then add a LinearLayout with fill_parent parameters on top of it, and a partially transparent black background. Adjust the alpha and RGB to how you see fit.
Now you can add your undimmed view to the RelativeLayout on top of the dimming. Since you're no longer inside the FrameLayout container, you'd have to adjust the positioning of this layout, and manually remove and re-add these two views for dimming and undimming, hence this is not a very elegant solution.

ANDROID, what layout elements should I use?

I need to make an android layout like this one.
tile background all over the screen.
top menu which, overlays the background ( note the shadow ).
some sort of a table with text options, maybe pictures, which can be scrolled up and down.
bottom menu, which appears by sliding up after a menu button is hit.
What kind of layout elements do you think I should use for that?
Thanks!
I think you should first learn about the Android Layout and XML layout design, then you can easily prepare this layout as well.
Relative Layout will be the better layout as compare to other layouts like Linear Layout and Table Layout.
To display middle part that is showing textual description may contains ListView (ListView because as you have mentioned Text Options should scroll up and down), but it depends on your requirement.
The RelativeLayout is the most flexible, and I think you can make all this with a minimal hierarchy view depth.
My approach would be to use a RelativeLayout. The Top Menu bar could be a custom class which extends a LinearLayout and this can be used in the RelativeLayout (in fact in any screen you have to provide UI consistency). Similarly, the bottom menu would be a custom control containing the appropriate animations. The rest of the screen would be contained in a ScrollView, possibly containing a TableLayout. I have something very similar using a MapView in the main screen and it works fine.

Categories

Resources