How to take a look at everything?

I have activity and many widgets, some of them have animation, and because of the animation, some of the widgets move (translate) on top of each other. For example, a text view moves on some buttons.,.

Now I want the buttons to always be on the front panel. And when the text move is moving, I want to move the buttons.

I can not achieve this. I tried everything I know, and "bringToFront ()" definitelly "does not work.

note I donโ€™t want to control the z-order in the order in which the element is placed in the layout, because I just canโ€™t :), the layout is complicated, and I canโ€™t put all the buttons when begging the layout

+107
android textview button animation z-order
Jul 20
source share
14 answers

You can call the bringToFront () method on the view you want to get in front

That's an example:

yourView.bringToFront(); 
+121
Apr 18 '14 at 12:35
source share

I looked through the stack overflow to find a good answer, and when I could not find it, I looked through the docs.

No one seems to have stumbled upon this simple answer:

 ViewCompat.setTranslationZ(view, translationZ); 

default translation z is 0.0

+32
Dec 21 '15 at 8:03
source share

bringToFront() is the right way, but, NOTE, you must call the bringToFront() and invalidate() methods at the highest level (under the root view), for example:

Your view hierarchy:

 -RelativeLayout |--LinearLayout1 |------Button1 |------Button2 |------Button3 |--ImageView |--LinearLayout2 |------Button4 |------Button5 |------Button6 

So, when you animate your buttons (1-> 6), your buttons will be at the bottom (below) of the ImageView . To cast it (at the top) of the ImageView , you must call the bringToFront() and invalidate() LinearLayout on your LinearLayout s. Then it will work :) ** NOTE. Remember to set android:clipChildren="false" for the root layout or animated view gradparent_layout. Let's take a look at my real code:

.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:hw="http://schemas.android.com/apk/res-auto" android:id="@+id/layout_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/common_theme_color" android:orientation="vertical" > <com.binh.helloworld.customviews.HWActionBar android:id="@+id/action_bar" android:layout_width="match_parent" android:layout_height="@dimen/dimen_actionbar_height" android:layout_alignParentTop="true" hw:titleText="@string/app_name" > </com.binh.helloworld.customviews.HWActionBar> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/action_bar" android:clipChildren="false" > <LinearLayout android:id="@+id/layout_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:gravity="center_horizontal" android:orientation="horizontal" > </LinearLayout> <ImageView android:id="@+id/imgv_main" android:layout_width="@dimen/common_imgv_height" android:layout_height="@dimen/common_imgv_height" android:layout_centerInParent="true" android:contentDescription="@string/app_name" android:src="@drawable/ic_launcher" /> <LinearLayout android:id="@+id/layout_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:orientation="horizontal" > </LinearLayout> </RelativeLayout> </RelativeLayout> 

Some code in .java

 private LinearLayout layoutTop, layoutBottom; ... layoutTop = (LinearLayout) rootView.findViewById(R.id.layout_top); layoutBottom = (LinearLayout) rootView.findViewById(R.id.layout_bottom); ... //when animate back //dragedView is my layoutTop child view (i added programmatically) (like buttons in above example) dragedView.setVisibility(View.GONE); layoutTop.bringToFront(); layoutTop.invalidate(); dragedView.startAnimation(animation); // TranslateAnimation dragedView.setVisibility(View.VISIBLE); 

Glitch!

+19
May 15 '14 at 4:00
source share

An even simpler solution is to edit the XML operation. Use

 android:translationZ="" 
+17
Jun 27. '17 at 1:15
source share

Try FrameLayout , it gives you the ability to place views one above the other. You can create two LinearLayouts : one with a background and one with a foreground view and combine them using FrameLayout . Hope this helps.

+14
Jul 20 '11 at 9:05 a.m.
source share

With this code in XML

  android:translationZ="90dp" 
+5
May 20 '19 at 18:04
source share

There may be another way that saves a day. Just start a new dialog with the desired layout and just show it. I needed this to display a loadView above DialogFragment, and that was the only way to succeed.

 Dialog topDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar); topDialog.setContentView(R.layout.dialog_top); topDialog.show(); 

bringToFront () may not work in some cases, like mine. But the contents of the dialog_top layout should override something at the ui level. But in any case, this is an ugly decision.

+2
Apr 15 '16 at 8:50
source share

I ran into the same problem. The following solution worked for me.

  FrameLayout glFrame=(FrameLayout) findViewById(R.id.animatedView); glFrame.addView(yourView); glFrame.bringToFront(); glFrame.invalidate(); 

2nd solution - using XML add this attribute to the XML view

 android:translationZ="" 
+2
Jul 24. '17 at 13:49
source share

You can try using bringChildToFront , you can check if this documentation is useful in the Android Developers Page .

+2
Nov 21 '17 at 18:47
source share

If you use ConstraintLayout , just place the element after other elements to make it in front than others

+1
Sep 12 '18 at 6:06
source share

You need to use framelayout. And the best way to do this is to make the view invisible if it is not required. You also need to set the position for each view so that they move in accordance with the corresponding position.

0
Jan 04 2018-12-14T00:
source share

If you use LinearLayout , you must call myView.bringToFront() and after calling parentView.requestLayout() and parentView.invalidate() to force the parent to redraw the new descendant order.

0
Aug 11 '17 at 11:35 on
source share

Arrange them in the order you want to show. Suppose you want to show view 1 on top of view 2. Then write code of view 2, and then code of view 1. If you cannot perform this ordering, then call the receiveToFront () method on the root representation of the layout that you want to bring forward.

-one
Apr 09 '19 at 3:23
source share

You can set visibility to false for other views.

 view1.setVisibility(View.GONE); view2.setVisibility(View.GONE); ... 

or

 view1.setVisibility(View.INVISIBLE); view2.setVisibility(View.INVISIBLE); ... 

and install

 viewN.setVisibility(View.VISIBLE); 
-25
Jul 20 2018-11-12T00:
source share



All Articles