The best I could explain this is by the image above. I have 4 views(1,2,3,4) which are linear and relative layouts and I have 5 which is a linear layout inside of 1, but 5 has to overlap 2.
If I contain 1,2,3,4 inside a linear layout I can arrange them but 5 wouldn't then be able to overlap 2 it would be cut off by it, I tried view5.bringToFront(); view5.invalidate(); but it didn't work.
So I guess 1,2,3,4 must be contained within a framelayout, but how then would I be able to align them vertically the way a linearlayout would do(1 then 2 then 3 then 4 above each other like the image provided)
Or is there a better way to do this?
It's simple hierarchy in this case. The view that is written first will be drawn first. SO if you want your 5th view to come on top of your linear layout make your xml like this
<FrameLayout
.........
.........
<LinearLayout
............
............
/>
<View
......5th view...
.................
/>
</FrameLayout>
Related
In the following link: FrameLayout it is written:
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.
From the way i understand this quote, it means that it is recommended to add only one child to FrameLayout (although it possible to use more than one in order to make each view to overlap his "brother"). But, i can't understand the use of the FrameLayout only with one child, Let's say a LinearLayout. What is the difference between starting a layout file with a LinearLayout as the root of the xml file (see code 1), or wrapping it with a FrameLayout as the root, and then placing a LinearLayout has is son that will be the father of many other ViewGroup and View (see code 2)?
Code 2:
<FrameLayout
...>
<LinearLayout
...>
<ImageView .... />
<TextView .../>
</LinearLayout>
</FrameLayout>
code 1:
<LinearLayout...>
<ImageView.../>
<TextView.../>
</LinearLayout>
I'm new at Android programming and couldn't solve how to do a design problem.
I have an activity with constraint layout, and it is full of textviews and buttons.
I want to put 6 imageview tool top of to the this activity. And there should be 3 of them should be seen on the screen, other ones should be scrolls to the right side. Like the screenshot I'm uploading.
Only these 6 imageviews should be scrolling horizontally.
I don't know what to do, which tools should I use. How can I do that design?
Thanks.
For the 6 ImageViews scrolling horizontally, I would advise you make use of a RecyclerView which is very powerful in dealing with showing data in a list especially for very large data sets. You can check out how to work with a RecyclerView here.
Since you only have 6 ImageViews, you can make use of a HorizontalScrollView, a LinearLayout with orientation set to horizontal i.e android:orientation="horizontal" and then place your ImageViews inside the LinearLayout.
<HorizontalScrollView ...........>
<LinearLayout ......>
<ImageView......./>
<ImageView......./>
<ImageView......./>
<ImageView......./>
<ImageView......./>
<ImageView......./>
</LinearLayout>
</HorizontalScrollView>
The easiest solution here is a HorizontalScrollView.
As HorizontalScrollView only accepts one child, wrap your ImageViews in another layout, e.g. LinearLayout or ConstraintLayout, then put that in a HorizontalScrollView.
You can use horizontal recyclerview for imageviews and below horizontal recyclerview, you can use linear layout for textviews inside scroll view.
<ScrollView>
<LinearLayout>
<RecyclerView>
</RecyclerView>
<LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
I am working on linear layout for my simple android application. I wanna make the portion of two views dynamically change based on the size ( I want to have, for a row for left to right, the first 20% is empty, and all the content is inside the rest of 80%) . For this approach, i chosen the weight for different view. I created an nested linear layout for this approach. For example, the layout hierarchy is something like this.
<linearLayout> //parent layout
<linearLayout //child 1 layout
android:layout_weight="1">
//so that this view occupy 20% of the space regardless the width of device. I intensionally wanna keep this view empty.
</linearLayout>
<linearLayout //child 2 layout
android:layout_weight="4">
//so that this view occupy 80% of the space regardless the width of device. and
//inside this view I have whatever view I wanna add on it.
<EditText>
<ImageView>
</linearLayout>
</linearLayout>
With this approach, the Lint in Android Studio tell me the following warnings:
This is a Nested Layout. Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
the child 1 layout is useless: This LinearLayout view is useless (no children, no background, no id, no style)
Can anyone address me the right layout to use in order to have the layout dynamically change based on the size of devices? How should I correctly set up the empty space for a linear layout case?
This is a possible solution using weights:
<LinearLayout
android:layout_width="match_parent"
android:gravity="end"
android:weightSum="1">
<!-- Your content here: -->
<View
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_gravity="end" />
</LinearLayout>
Have a look at PercentRelativeLayout.
Note: You need the Percent library to use it.
So I have this ScrollView inside which I have a RelativeLayout on one of my views on my Android app. Inside the RelativeLayout I already have a TableLayout that I'm using, and above it I want; 2 different text views (one header and one longer text, and I want the header to be placed on top of the longer bit of text) aswell as an ImageView that I want to be placed to the right of the 2 TextViews, and I want all 3 views to be placed on a differently colored background than the other stuff on the ScrollView such as the TableLayout for example.
I tried putting another RelativeLayout inside the ScrollView but it tells me ScrollView can only host one direct child, so that didn't really pan out. What would the most fitting way to accomplish this? Because I want this 3-view-background-thingie to scroll with the TableLayout and all the other stuff on the view.
As always, thankful for any answers or tips!
(My design kinda looks like this at the moment, schematically;)
<Container (that doesn't scroll)/>
<ScrollView
<RelativeLayout
*Alot of stuff here, such as a TableLayout for example
</RelativeLayout>
</ScrollView>
Put everything in your RelativeLayout
<ScrollView
<RelativeLayout
<LinearLayout>
<TextView>
</TextView>
<TextView>
</TextView>
<ImageView>
</ImageView>
</LinearLayout>
<TableLayout>
</TableLayout>
</RelativeLayout>
</ScrollView>
This way, the only child is the RelativeLayout, the others being children of the RelativeLayout.
Hope this helps!
I was wondering if it's possible to set an automatic/dynamic margin (padding?) between elements in an Android layout without having to do it programmatically?
For example let's say there is a horizontal LinearLayout which is set to android:layout_width="fill_parent" and that contains five elements. Is there a setting that evenly shares the remaining empty space of the LinearLayout to the margins of the child elements?
See image at http://img63.imageshack.us/img63/8/margin.png
Thanks for any help!
You could use view spacers as your margin, with the layout weight set on them.
<LinearLayout ...>
<View id=marginLeft android:layout_weight="1"/>
<Element1/>
<Element2/>
<Element3/>
<Element4/>
<Element5/>
<View id=marginRight android:layout_weight="1"/>
</LinearLayout>
This should make the two views use up any remaining space in your row. Note, the above XML will not parse :)
-- Edit
Just saw the picture. To add equal spacing between each of the elements too would just be a case of adding more spacer elements between your content elements (all with the same layout weight)
Yup. Only LinearLayouts support it. Tis called layout weight
Look for LinearLayout at http://developer.android.com/guide/topics/ui/layout-objects.html
For a good intro, look here