android - linear layout bringToFront() - android

I have 4 buttons in my linear layout and i need to bring to front first button.
Normal order is
Button 1 | Button 2 | Button 3 | Button 4
But when I call button1.bringToFront() function , button1 is going to end like
Button 2 | Button 3 | Button 4 | Button 1
How can I solve this problem. Relative layout doesn't causes this problem but I have to use LinearLayout because buttons will order vertically and I'm deleting a button in some conditions.
Thanks

LinearLayout doesn't work with the z-axis, hence, it's name linear. Try using a RelativeLayout and then call bringToFront() to get the desired effect. With a RelativeLayout you can call layout_alignBollow to order the views vertically. Or you can nest views and layouts, for instance, within your LinearLayout nest three RelativeLayout within those you can place your Buttons (be careful with this approach as adding too many views can be a bad thing).

If you have to work with z-axis in a LinearLayout, you may use setTranslationZ function.
Example:
yourView.setTranslationZ(100);

Since bringToFront messes up the LinearLayout order, I decided to use a RelativeLayout and put the "top" view (the view I want on top) last in the XML.
Example:
<RelativeLayout ...>
<ViewBelow
android:layout_below="#+id/view_on_top"
...
/>
<!-- Last view in XML appears above other views on screen -->
<ViewOnTop
android:id="#+id/view_on_top"
...
/>
</RelativeLayout>
In the specific case of this question, the ViewOnTop would be the Button 1, and the ViewBelow would be a LinearLayout containing the other buttons.

make your layout call the forceLayout() to disable re-arrangement of the layout.

Related

Adding views dynamically to LinearLayout not working as expected

I'm inflating views inside a linearlayout dynamically, however once the linear layout reaches the end of the first row, it cuts off the rest and doesn't start on the second row.
for(int a = 0; a < mSkills.get(i).size(); a++){
View singleSkill = LayoutInflater.from(mContext)
.inflate(R.layout.singleskill, holder.mSkillLayout, false);
TextView skillText = singleSkill.findViewById(R.id.singleskilltext);
skillText.setText(mSkills.get(i).get(a));
holder.mSkillLayout.addView(skillText);
}
For the linear layout I have it set to wrap_content for the height:
<LinearLayout
android:id="#+id/ll_skills"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/tv_description"
android:layout_margin="16dp"/>
I've tried setting it to a defined height e.g 300dp however that doesn't work either.
How can I make the layout start on the second row, once the first is full?
Linear Layout can either fill views horizontally or vertically so the 2nd row you are expecting cant to be done with linear layout only. you can try a horizontal scroll view for that to scroll horizontally. For the exact view-like flow that you described, you can use this 3rd party https://github.com/nex3z/FlowLayout
It can manage the flow of your dynamically inflated view such as if there is no space in the first line then it will put the next view in the second line.
also, you can use material design library chips https://material.io/components/chips/#usage
LinearLayout works exactly how it has to be because you specify it as horizontal. For such behavior, you need RecyclerView With GridLayoutManager or create your own layout;).
Actually it's doing exactly as it should be, LinearLayout is Linear!, and place its subviews in a single horizontal or vertical row.
My advice to you is that create dynamic horizontal LinearLayout as you already doing with TextViews. and put every 3 or 4 textviews (depending on screen size) inside it.
and put all LinearLayouts inside one vertical LinearLayout...
Of course in your case, it's not a good idea, the best thing you can do is to use recycler view. but I consider you have problem with that.

Android: How to change Relative Layout to Scroll Layout

Hello i have an Relative Layout in Android, with EditTexts, TextViews, one Spinner and RadioButtons. It's a lot of things for only one screen, so i need to change for a ScrolView. When i try add the line in the first line and on the last Row, I have problems.
How can i add a scroll on my screen without lose all the layout I built so far?
so i need to change for a ScrolView
You do not need to change "for a ScrolView". You need to wrap your RelativeLayout in a ScrollView.
How can i add a scroll on my screen without lose all the layout I built so far?
Put a ScrollView around your RelativeLayout:
<ScrollView>
<RelativeLayout>
<!-- existing stuff here -->
</RelativeLayout>
</ScrollView>
As #CommonsWare has said, you can just wrap your current layout with a ScrollView.
Just keep in mind that a ScrollView must have only one child.

How to set two layout one at top and anther at bottom

hi I have 2 layout inside a framelayout and i want one linearlayout at top and another at bottom where i will specify height for both the layouts
and how to set this parameter for the linearLayout "layout_gravity " by code not by XML
Use a surfaceView , the SurfaceView allows you to stack in the Z order.
Docs
make sure your surfaceView is under the views of the views you want to stack.
Hope this helps
first fetch the layout from id.
LinearLayout layout = (LinearLayout)findViewById(R.id.IDOFLAYOUT);
//the set the gravity by this method
layout.setGravity(Gravity.CENTER);

How do I layout a button bar with buttons of different heights?

How do I go about implementing a button bar with buttons of different shapes and heights? As an example (please excuse my poor drawing skills for this quick mockup):
The example bar has 3 buttons, with the middle button (3) a different shape and height than the other 2 buttons (1,2). The button bar will be a view that is included and merged into other views so as to seem to float on top of the parent view.
I was thinking of implementing buttons 1 and 2 into a layout, and then button 3 as another layout that I then merge with the first two button's layout.
like my previous comrades said, you need some kind of layout or container that can have a background (if you wish for button #3 to hoover above it) then use relative layout for mixing the two the disadvantage of this other than complexity is that youcannot relate to the other two buttons since they reside in a different layout.
More elegant solution may be to have a special background drawable that can:
have a method setCurrentHeight() that will specify the height the actual viewable section should have the rest will be filled with transparent color.
override it's own draw so just before it's drawing it will have a callback called, call back you can register yourself to.
then you can register the callback in your activity to take the current position of the #3 button and set the height accordingly, this way you are still with one layout with special drawable as background.
A customized LevelDrawable might do the trick.
I would layout this bar as follows:
A RelativeLayout as a container for the rest, with height set to wrap_content and alignparentbottom = true
An ImageView for the bar
2 Buttons with a transparent background (Button 1 and 2)
Button 3 is a custom Button with a custom Image of a trapezoid as background
So you will have a Layout similar to this:
<RelativeLayout
...>
<ImageView
.../>
<Button
... Button 1 />
<Button
... Button 2 />
<Button
... Button 3 />
</RelativeLayout>
I don't exactly know that this will work, and I can't test it, but you might give something like this a try; I believe it can all be done elegantly through XML.
Have a RelativeLayout (id:mainLayout) that will contain all of your views, wrap_content for both dimensions.
Have a blank View as your first child that will serve as your background bar
Set the View's background color/image to what you want; layout_width to fill_parent; layout_height to wrap_content; layout_alignTop="#id/leftButton"; layout_alignBottom="#id/leftButton".
Add an ImageButton for your center button (id:bigButton), wrap_content for both dimensions; layout_centerInParent="true".
Add an ImageButton for your left button (id:leftButton), wrap_content for both dimensions; layout_toLeftOf="#id/bigButton"; layout_centerInParent="true".
Add an ImageButton for your right button (id:rightButton), wrap_content for both dimensions; layout_toRightOf="#id/bigButton"; layout_centerInParent="true".
In my head, I believe this works, but I could be off. Regardless, something to think about, and I hope it helps you find a solution. :)
Better you can tablelayout with different button styles or relative layout for button "3"

How to make a View to fill the space between two views?

I want to layout my views in the following way: [Button] [SomeView] [Button]. I want to set specific sizes for buttons (in mm), and then have the SomeView fill the remaining space between them.
How to achieve this?
Maybe you could set the layout:weight of the [SomeView] to 1 and put all of these views in a linearlayout. Hope it works!

Categories

Resources