I am testing a feature where I would like to animate a view from a ScrollView. I think providing a sample layout would be much easier to understand, so here it goes:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/holo_blue_bright"
>
<TextView
android:id="#+id/tv_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="0"
android:textSize="25sp"
android:textStyle="bold"/>
</LinearLayout>
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#android:color/holo_green_light"
android:padding="5dp"
>
<RelativeLayout
android:id="#+id/lay_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#android:color/holo_purple"/>
<ImageView
android:id="#+id/iv_2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/iv_1"
android:layout_marginTop="10dp"
android:background="#android:color/holo_blue_dark"/>
<ImageView
android:id="#+id/iv_3"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/iv_2"
android:layout_marginTop="10dp"
android:background="#android:color/holo_green_dark"/>
<ImageView
android:id="#+id/iv_4"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/iv_3"
android:layout_marginTop="10dp"
android:background="#android:color/holo_red_light"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/iv_4"
android:layout_marginTop="10dp"
android:background="#android:color/white"/>
<FrameLayout
android:id="#+id/bottom_buttons"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame_cart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp">
<Button
android:id="#+id/btn_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#android:color/holo_orange_dark"/>
</FrameLayout>
</FrameLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
Summarizing it: main parent is a LinearLayout, containing a header (another LinearLayout), and a ScrollView. In this ScrollView I got a bunch of Views, and over them, there is another View (a FrameLayout containing a Button in this testing case) which I would like to animate later.
The problem is (or at least what I think of): somehow, the height of the RelativeLayout is computed as wrapped content, and animating that View to the bottom of the screen won't display it, as it goes beyond its parent computed height.
In case it would be relevant, this is the tested animation being fired when I click the button:
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator animToBottom = ObjectAnimator.ofFloat(frameCart, "translationY", 200);
animSet.play(animToBottom);
animSet.setDuration(300);
animSet.start();
Related
I have this layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />
<LinearLayout
android:id="#+id/MainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#drawable/bg"
android:keepScreenOn="true"
tools:context=".MainActivity">
<TextView
style="#style/MyTextStyle"
android:id="#+id/MessageTextView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerHorizontal="true"
android:visibility="gone"/>
<ImageButton
android:src="#drawable/ic_angry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AngryImageButton"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:background="#null"
android:scaleType="fitCenter" />
<LinearLayout
android:id="#+id/BottomLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:layout_below="#+id/FacesHolderFrameLayout"
android:orientation="horizontal"
android:foregroundGravity="bottom">
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:layout_weight="1"
android:src="#drawable/ic_insert_chart_white_48dp"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:id="#+id/StatisticsImageButton"
android:background="#null"
android:layout_marginTop="20dp"
android:scaleType="fitCenter" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Which in general is an imageview that takes the whole screen and acts as a background image, the above it i have a linearlayout (MainLinearLayout) that hold vertically a textview, an imagebutton (AngryImageButton) and another linearlayout (BottomLinearLayout) that is just display a small image (like the logo) horizontally centered.
I want to be able to change programatically the size of the AngryImageButton and the BottomLinearLayout to be displayed in the bottom of the screen and to be always visible. I mean to be on top of the other views.
Right now the BottomLinearLayout appears in the center and below the MainLinearLayout and if i make the AngryImageButton very large, it pushes below the BottomLinearLayout until its not visible in the screen at all, i don't want that. I want it to be at the bottom, and always visible.
How i can accomplish that? Also i want the layout to work in both orientations
To bring View to front, you can use
myView.bringToFront()
or You can make use of ViewCompat and set its translation Z like below.
ViewCompat.setTranslationZ(view, 1)
here's the doc for bringToFront() bringToFront()
I have layount who include one layout, duplicate two time ("include"):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorButtonNiebieski">
<!-- LAYOUT GRACZ (A) -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:scaleY="-1"
android:scaleX="-1"
android:paddingBottom="10dp">
<include
android:id="#+id/lay1"
layout="#layout/layout_pojedynek_duplikat" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.05"
android:gravity="center"
android:scaleY="-1"
android:scaleX="-1">
<TextView
android:id="#+id/txt_gracz_A_liczba_punktow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="30dp"
android:textColor="#color/colorWhite"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.05"
android:gravity="center">
<TextView
android:id="#+id/txt_gracz_B_liczba_punktow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:textSize="30dp"
android:textColor="#color/colorWhite"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:paddingBottom="10dp">
<include
android:id="#+id/lay2"
layout="#layout/layout_pojedynek_duplikat" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
layout_pojedynek_duplikat.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:weightSum="1.0"
android:background="#00ff0000"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:gravity="center">
<TextView
android:id="#+id/txt_gracz_A_i_B_nazwa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POLSKA"
android:textSize="40dp"
android:textColor="#color/colorWhite"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0"
android:layout_weight="0.5"
android:orientation="horizontal">
<ImageView
android:id="#+id/img_pojedynek_wariant_A"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:src="#drawable/argentyna"
android:padding="5dp"
android:adjustViewBounds="true"/>
<ImageView
android:id="#+id/img_pojedynek_wariant_B"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:src="#drawable/kambodza"
android:padding="5dp"
android:adjustViewBounds="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0"
android:layout_weight="0.5"
android:orientation="horizontal">
<ImageView
android:id="#+id/img_pojedynek_wariant_C"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:src="#drawable/watykan"
android:padding="5dp"
android:adjustViewBounds="true"/>
<ImageView
android:id="#+id/img_pojedynek_wariant_D"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:src="#drawable/polska"
android:padding="5dp"
android:adjustViewBounds="true"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
In java file I try set font for txt_gracz_A_i_B_nazwa text field:
TextView txt_gracz_A_i_B_nazwa = (TextView) findViewById(R.id.txt_gracz_A_i_B_nazwa);
txt_gracz_A_i_B_nazwa.setTypeface(myFontBold);
But this set font for only one view:
How can I fix this problem?
DO that for every view. Or create a new View class that's a child of TextView and sets the font, and use that instead of TextView.
Yes, Android's font handling sucks.
When you call Activity.findViewById(), the system will traverse your view hierarchy starting from the root until it finds a view with the given id. In other words, Activity.findViewById() will return the first view with that id in your layout. It will not return all views with that id.
For this reason, it is generally advised to not re-use ids within a single layout. However, this advice is often not practical, and you shouldn't feel bad about your current layout. But it does mean that you will need to call TextView.setTypeface() twice, and that you will need some way to access the second view with the id txt_gracz_A_i_B_nazwa.
You can do this by limiting the scope you search with findViewById(). Instead of using Activity.findViewById(), instead use View.findViewById(). So you could write something like this:
View lay1 = findViewById(R.id.lay1);
TextView txt1 = (TextView) lay1.findViewById(R.id.txt_gracz_A_i_B_nazwa);
txt1.setTypeface(myFontBold);
View lay2 = findViewById(R.id.lay2);
TextView txt2 = (TextView) lay2.findViewById(R.id.txt_gracz_A_i_B_nazwa);
txt2.setTypeface(myFontBold);
Looks like a small logic flaw. The problem with reusing the same layout two times, is that you have the identical IDs although they represent two entirely different UI elements.
lay1.findViewById(your_text_field_id).setFontType
lay2.findViewById(your_text_field_id).setFontType
Should fix you. You can't use generic findViewById as it will grab the first one and use it. Hope that helps.
I have LinearLayout inside of DrawerLayout which renders drawer item and views according to layout_weight. I am trying to hide that view programmatically by setting its height to 0 or with View.Visibility - GONE property.
Tried
By setting its Visibilty as GONE it acts as INVISIBLE
By getting its LayoutParams and setting its height to 0. Though it acts as INVISIBLE
ParentLayout : mother.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/activityMotherDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/activity_mother" />
<include layout="#layout/motherdrawer" />
</android.support.v4.widget.DrawerLayout>
Layout : motherdrawer.xml
<?xml version="1.0" encoding="utf-8"?>
<com.app.ui.view.ScrimInsetsFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerRootLayout"
android:layout_width="#dimen/activity_mother_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/background_application"
android:fitsSystemWindows="false"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/activityMotherDrawerInnerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/activity_mother_drawer_layout_padding">
<LinearLayout
android:id="#+id/activityMotherDrawerInnerInnerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/activityMotherDrawerMyAttendanceLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/activityMotherDrawerMyAttendanceIv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_weight="1.5"
android:padding="#dimen/activity_mother_drawer_image_padding"
android:scaleType="centerInside"
android:src="#drawable/ic_drawer_attendance" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/activityMotherDrawerMyAttendanceIv"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:gravity="top|center"
android:text="#string/activity_mother_drawer_attendance"
android:textColor="#color/colorGray"
android:textSize="#dimen/activity_mother_drawer_text_size" />
</LinearLayout>
<LinearLayout
android:id="#+id/activityMotherDrawerDashboardLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/activityMotherDrawerDashboardIv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_weight="1.5"
android:padding="#dimen/activity_mother_drawer_image_padding"
android:scaleType="centerInside"
android:src="#drawable/ic_drawer_dashboard" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/activityMotherDrawerDashboardIv"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:gravity="top|center"
android:text="#string/activity_mother_drawer_dashboard"
android:textColor="#color/colorGray"
android:textSize="#dimen/activity_mother_drawer_text_size" />
</LinearLayout>
<LinearLayout
android:id="#+id/activityMotherDrawerSettingsLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/activityMotherDrawerSettingsIv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_weight="1.5"
android:padding="#dimen/activity_mother_drawer_image_padding"
android:scaleType="centerInside"
android:src="#drawable/ic_drawer_settings" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/activityMotherDrawerSettingsIv"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:gravity="top|center"
android:text="#string/activity_mother_drawer_setting"
android:textColor="#color/colorGray"
android:textSize="#dimen/activity_mother_drawer_text_size" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</com.app.ui.view.ScrimInsetsFrameLayout>`
Whenever I tried to fetch the layoutParam using getLayoutParams it returns FrameLayout where its ParentView is LinearLayout.
Using Java :
mDashboardLayout = (LinearLayout)mDrawerInnerRootLayout.findViewById(R.id.activityMotherDrawerDashboardLayout);
LinearLayout.LayoutParams mParams = (LinearLayout.LayoutParams) mDashboardLayout.getLayoutParams();//returns FrameLayout instead where it should return LinearLayout
mDashboardLayout.setVisibility(View.GONE); //it acts as INVISIBILITY
Can anyone enlighten me why it returns FrameLayout instead of
LinearLayout Or Why its Visibility changes to INVISIBLE instead of GONE?
Though changing View - Visibility to either GONE or INVISIBLE still View is always INVISIBLE. It does accept onClick event. As per SO, I want to completely remove that view. I am able to achieve it using following method.
I am adding View to ParentView hierarchy accordingly programmatically and refreshing the whole layout.
View inflatedDashboardView;
inflatedDashboardView = mInflater.inflate(R.layout.layout_drawer_dashboard, mDrawerInnerRootLayout, false);
mDashboardLayout = (LinearLayout) inflatedDashboardView.findViewById(R.id.activityMotherDrawerDashboardLayout);
mDrawerInnerRootLayout.addView(inflatedDashboardView);
mDrawerInnerRootLayout.forceLayout();
Still I'm not able to understand why InnerView Layout inside of DrawerLayout always return FrameLayout.
This is not working on my phone but I can see it in the Android Studio Design mode window. I need to place a LinearLayout or any view actually just below the listView. But I can't see it, only the listView fitting the whole height
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/local_detalle_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="android.buendia.cl.pritz.fragments.locales.LocalDetalleFragment">
<view
android:id="#+id/banner"
class="com.android.volley.toolbox.NetworkImageView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:scaleType="centerCrop"
app:srcCompat="?android:attr/textSelectHandleLeft" />
<TextView
android:id="#+id/current_category"
style="#style/current_category"
android:layout_below="#+id/banner"
android:text="TextView" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/current_category">
<ListView
android:id="#+id/list_view_local_detalle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/cart_bar"
style="#style/cart_bar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/cart_status"
style="#style/cart_bar_status"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="TextView" />
<Button
android:id="#+id/cart_next"
style="#style/cart_bar_next"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cart_bar_next_button_text" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Set android:layout_below="#+id/list_view_local_detalle" for LinearLayout
Note : If you'r using weight for vertical linear layout then height must be 0 dp and if using for horizontal linear layout then width must be 0dp.
I have a layout that i am trying to animate. It initially has its visibility set to GONE with alpha 0.
I then use the following to animate it:
final LinearLayout layout = (LinearLayout) findViewById(R.id.main_info);
layout.setVisibility(View.VISIBLE);
layout.bringToFront();
final AlphaAnimation animation = new AlphaAnimation(0F, 0.8F);
animation.setDuration(time);
animation.setFillAfter(true);
layout.startAnimation(animation);
But nothing happends. I have used the same code with a progressBar and i get the result i want. Why isnt it working for my layout?
I am animating it to come in front of another layout (if it isnt already) to act as an information screen hence the bringToFront(). I would like to add that i have since deleted the bringToFront() and there is no change. So the problem is nothing to do with that.
Edit: XML Code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relative_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<ImageView
android:id="#+id/img_custom5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_pic" />
<TextView
android:id="#+id/text_custom5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/img_custom5"
android:layout_alignRight="#id/img_custom5"
android:layout_alignTop="#id/img_custom5"
android:gravity="left"
android:text="#string/text_custom5"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/main_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"
android:background="#color/color_black"
android:orientation="vertical"
android:visibility="gone" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="50dp"
android:minWidth="50dp"
android:src="#drawable/arrow_up" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="#string/pleasewait"/>
</LinearLayout>
</RelativeLayout>