Chage Lottie attributes for each keyframe, MotionLayout, Android Animation - android-animation

How I could change Lottie and any other attributes inside MotionLayout?
I have start and end layouts and I one to add middle point(KeyFrame) in which I want to change lottie_rawRes="#raw/lottie_animation(json)" or any other attribute.
My code is here:
motion_scene(xml\activity_app_splash_screen_scene.xml):
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:autoTransition="animateToEnd"
motion:constraintSetEnd="#layout/motion_splash_screen_end"
motion:constraintSetStart="#layout/motion_splash_screen_start"
motion:duration="5000">
<KeyFrameSet>
<KeyAttribute <-------- that one
motion:framePosition="50"
motion:motionTarget="#+id/splashSun" />
<KeyAttribute
android:alpha="0"
motion:framePosition="85"
motion:motionTarget="#+id/splashAppName"
/>
<KeyAttribute
android:alpha="1"
motion:framePosition="95"
motion:motionTarget="#+id/splashAppName" />
</KeyFrameSet>
</Transition>
<ConstraintSet android:id="#layout/motion_splash_screen_end">
<Constraint
android:id="#+id/splashClouds1"
>
<CustomAttribute
motion:attributeName="lottie_rawRes"
motion:customStringValue="#raw/weather_moon_clear" />
</Constraint>
</ConstraintSet>
Part of my start/end layout(layout\motion_splash_screen_start.xml):
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/splash_end"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/splashSun"
android:layout_width="0dp"
android:layout_height="0dp"
android:rotation="0"
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="#+id/splashAppName"
app:layout_constraintDimensionRatio="1.4:1"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline1"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/splash_sun" />

app:lottie_rawRes="#raw/splash_sun"
CustomAttribute calls the name based on bean conventions
so
<CustomAttribute motion:attributeName="lottie_rawRes"
motion:customStringValue="#raw/weather_moon_clear" />
Would look for a method setLottie_rawRes(String str)
And would not find it.
Look in the Lottie api and find the method you want
setAnimation(final String assetName)
Would you use motion:attributeName="animation"
setAnimationFromJson(String jsonString)
would mean you use motion:attributeName="animationFromJson"
Traditionally there is a match between attribute foo and method setFoo

Related

Custom Toast inflater must be initialized

i have this fun
private fun testToast(){
val inflater:LayoutInflater
val view = inflater.inflate(R.layout.custom_toast_message)
val toast = Toast(this.context)
toast.setGravity(Gravity.TOP,0,70)
toast.duration = Toast.LENGTH_LONG
toast.view =view
toast.show()
}
the main idea its to change the background color of the toast without getting a square toast
in this code i get an error in inflater.inflate
(inflater must be initialized)
this is the test xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/background_blue"
android:padding="16dp"
android:weightSum="1"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_toasticon"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:textColor="#color/gauge_red"
android:fontFamily="#font/roboto"
android:text="Prueba prueba PRUEBA"
/>
</LinearLayout>
con someone help me or annother way to do a toast with custom color.
change the color of a toast with rounded corners
You declared the inflater variable but never assigned anything to it (using =). You can’t use a variable’s value if you haven’t assigned anything to it yet.
If this function is in an Activity, you can delete the line that declares val inflater and replace inflater. with layoutInflater. to use the property of the Activity. If it’s in a Fragment, use requireActivity().layoutInflator..
By the way, the latest version of Android has deprecated setting a custom view for your toast. On Android R and later if you set a view on your Toast, it won’t show. They want you to use SnackBars instead. There might be a third party library available that could help create something more similar to Toasts.
you can make a drawable resource to achieve it
toast_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/teal_200" />
<corners android:radius="48dp" />
</shape>
and use this drawable for background of toast view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/toast_background"
android:orientation="horizontal"
android:padding="16dp"
android:weightSum="1">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:text="Prueba prueba PRUEBA"
android:textColor="#color/purple_700"
android:textSize="14sp" />
</LinearLayout>
the result

appbar_scrolling_view_behavior not resolved at SwipeRefreshLayout databinding android

I try to add the app:layout_behavior="#string/appbar_scrolling_view_behavior" to the SwipeRefreshLayout but I don't know why i isn't resolved at XML layout although It works properly after running the codes. Does this matter with data binding and MVVM?
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="viewModel"
type="ir.basamadazmanovin.heartrate.ui.main.home.HomeViewModel" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:onRefreshListener="#{()-> viewModel.onRefresehing()}"
app:refreshing="#{viewModel.isLoading}">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/fragment_home_recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="3" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/fragment_home_toolbar"
style="#style/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:subtitleTextColor="#color/material_white"
app:title="#string/app_name"
app:titleTextAppearance="#style/Toolbar.TitleText"
app:titleTextColor="#color/material_white" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
implementation "com.google.android.material:material:1.0.0"
app:layout_behavior="#string/appbar_scrolling_view_behavior" is red and the IDE mentions "Unresolved class '#string/appbar_scrolling_view_behavior' less... (Ctrl+F1) Inspection info: Validates resource references inside Android XML files."
Use this instead:
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

Data binding: visible depend on list's size (via XML)?

Android Studio 3.1, java 1.8, Gradle 4.5.
Here my activity:
public class OrdersActivity extends AppCompatActivity {
ObservableArrayList<OrderEntry> orderList = new ObservableArrayList<>();
#Override
public void setOrderList(List<OrderEntry> list) {
this.orderList.clear();
this.orderList.addAll(list);
}
}
I want to show list's size in layout. So here layout's xml.
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="android.view.View" />
<variable
name="handler"
type="com.myproject.ui.OrdersActivity" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="#{handler.orderList.size > 0 ? View.VISIBLE : View.GONE}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</layout>
But I get error:
What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.myproject.ui.OrdersActivity.orderList
file:\app\src\main\res\layout\orders.xml
loc:118:38 - 118:54
****\ data binding error ****
Did you try making it public or wrapping it with a public getOrderList()?

Is it possible to pass value to include layout without create variable only by xml?

Android Studio 3.1, Java 1.8.
I use data binding from Google.
Here my profile.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="handler"
type="com.myproject.ui.ProfileActivity" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/common_color_bg">
<include
android:id="#+id/prfoileToolbar"
layout="#layout/tool_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="#{#string/profile}" />
</android.support.constraint.ConstraintLayout>
</layout>
Here tool_bar.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="title"
type="String" />
</data>
<android.support.constraint.ConstraintLayout
android:id="#+id/toolBarConstraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="0dp"
android:layout_height="#dimen/tool_bar_height"
android:background="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/toolbaTitleTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:textAllCaps="true"
android:textColor="#android:color/black"
android:textSize="13sp"
android:textStyle="bold"
android:text="#{title}"
app:layout_constraintBottom_toBottomOf="#+id/toolBar"
app:layout_constraintEnd_toEndOf="#+id/toolBar"
app:layout_constraintStart_toStartOf="#+id/toolBar"
app:layout_constraintTop_toTopOf="#+id/toolBar" />
</android.support.constraint.ConstraintLayout>
</layout>
And here my activity:
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProfileBinding binding = DataBindingUtil.setContentView(this, R.layout.profile);
binding.setHandler(this);
}
}
And it's work fine. In included xml (tool_bar) success show text "Profile". Nice.
I do this by create variabel "title" in tool_bar.xml
The question is:
Is it possible to pass string value to tool_bar.xml and set it to "toolbaTitleTextView" without create variable "title" ONLY by xml?

Using android DataBinding library how to pass parameters to binding events

I followed examples from Android Developers Binding Events and implementing step-by-step. That working fine. But I want to send parameters from adapter to handlers, how can achieve this using data binding handlers
I got the answer.
In xml for onclick use lambda expression
layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="embitel.com.databindingexample.helper.Movie" />
<variable
name="handler"
type="embitel.com.databindingexample.helper.MyHandlers" />
</data>
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:onClick="#{(view)->handler.onItemClicked(view,movie)}"
app:cardBackgroundColor="#android:color/white"
app:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="128dp"
android:scaleType="centerCrop"
app:error="#{#drawable/ic_launcher}"
app:imageUrl="#{movie.imageUrl}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{movie.title}" />
</LinearLayout>
</android.support.v7.widget.CardView>
then create handler class as,
public class MyHandlers {
public void onItemClicked(View v, Movie movie) {
Context context = v.getContext();
context.startActivity(DetailActivity.buildIntent(context, movie));
}
}
then you need to set handler where that xml is iflated as,
binding.setHandler(new MyHandlers());
you can also put handler method in any class. In that case you have to set that class name as handler.