I need to create custom viewgroup that contains children which are proceeded with the attributes of the outer viewgroup for my custom.
For example,
<ConstraintLayout...>
<CustomViewGroup...>
<RecyclerView.../>
<TextView.../>
<TextView.../>
<CustomView.../>
<RecyclerView.../>
<Button.../>
<Button.../>
</CustomViewGroup>
</ConstraintLayout>
So I need for all of these RecyclerView, TextView, TextView, CustomView, RecyclerView, Button and Button to use the attributes of ConstraintLayout like
app:layout_constraintWidth_percent
app:layout_constraintHeight_percent
app:layout_constraintTop_toBottomOf
app:layout_constraintLeft_toLeftOf
app:layout_constraintRight_toRightOf
app:layout_constraintBottom_toTopOf
etc.
And the one sense of CustomViewGroup is to be used, for example, like FrameLayout, to be the container of several fragments but without many other unnecessary extra properties (all views of these fragments should be depended only on ConstraintLayout constraints).
The main point of my own don't use the nested layouts (so that the CustomViewGroup needs to be like a fake layout).
UPDATE By virtue of Mike M. comment I need to clarify my request.
I need to have some Activity with three buttons in the top and the fragments area below. Inside this fragment area three fragments should be displayed. Two fragments are almost similar and the third is different from others. First button changes the fragment area with first fragment, second button changes the fragment area with second fragment and the third button changes the fragment area with third fragment. The first and second buttons may be proceeded with one fragment just changing some parameters of general views and the third button should displays the third fragment. In any case I don't want to have nested viewgroups to arrange views inside fragments or these viewgroup's attributes should not be used by the views inside. So that I need the solutions to make these views depend on positioning attributes of the root ViewGroup ignoring the attributes of nearest viewgroups.
Related
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.
I understand the advantage of a FrameLayout when you need to add multiple children to it. But what is the point of adding a FrameLayout as your root element when you only have one child (lets say a TextView)? I mean why wouldn't you simply set that single child (e.g. the TextView) as the root of your layout?
A FrameLayout is - as the name implies - a Layout. It inherits from ViewGroup which is used for positioning and aligning child views.
Of course you would be able to set a TextView alone in your Layout xml. But what are you going to do if you want it to be centered?
Then it would make sense to use a FrameLayout as a sort of "Wrapper" around your TextView which lets you position it the way you want.
You have an Activity A --> display a fragment B (scrollView), on next button you have fragment C replaced with relative Layout. But the view is not replaced.
ScrollView Defines as follows :
"A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout."
-ScrollView is not a base view hence wrapping the scrolling in base views like (Linear Layout,RelativeLayout Can solve your problem
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.
how do you place a view inside of a view in android xml? I have come from a web development background so the closest example I can think of is placing div's inside of div's, something like this for example
You can't place Views inside Views (i.e. ImageView inside an ImageView).
You can place Views inside ViewGroups (i.e. an ImageView inside a LinearLayout).
Please check here for more info: http://developer.android.com/guide/topics/ui/index.html and here: http://developer.android.com/resources/tutorials/views/index.html
Hope this helps!
You can't place views inside another view.
A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties
some viewgroups are
AbsoluteLayout, AdapterView, FragmentBreadCrumbs, FrameLayout, GridLayout, LinearLayout, RelativeLayout, SlidingDrawer, Toolbar, TvView, CardView, etc