set color for Lottie animation programatically - android

I am using below code to change the animation color specially for setting the text color but not succeed.
Thanks in advance for the help
//UI xml code
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottie_count_down"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/dp_0"
android:translationZ="#dimen/dp_20"
android:visibility="visible"
app:lottie_rawRes="#raw/countdown_animation" /> // .JSON file
// Kotlin Code
val simpleColor = SimpleColorFilter(Color.parseColor(App.settings!!.theme.textColor))
val callback = LottieValueCallback<ColorFilter>(simpleColor)
callback.setValue(simpleColor)
lottieCountDownAnimation.addValueCallback(
KeyPath("**"),
LottieProperty.COLOR_FILTER,
callback
) // issue in setting text color
lottieCountDownAnimation!!.setBackgroundColor(Color.parseColor(App.settings!!.theme.backgroundColor))
// works for setting the background color
lottieCountDownAnimation.playAnimation()

var mCallback: LottieValueCallback<Int?>
lottieCountDownAnimation.addLottieOnCompositionLoadedListener(
LottieOnCompositionLoadedListener {
mCallback = LottieValueCallback<Int?>()
mCallback.setValue(Color.parseColor(App.settings!!.theme.textColor))
lottieCountDownAnimation.addValueCallback(
KeyPath("**"),
LottieProperty.COLOR,
mCallback
)
})

Related

Change button text color according to ImageSlider

I want to change the color of button text as image change in ImageSlider. I used one library for making ImgeSlider.
Link for ImageSlider is:
https://github.com/denzcoskun/ImageSlideshow
My code is:
//******************************** code for Image Slider**********************
ImageSlider imageSlider = findViewById(R.id.imageSlider);
ArrayList<SlideModel> imageList = new ArrayList<>();
imageList.add(new SlideModel(R.drawable.img1, ScaleTypes.CENTER_CROP));
imageList.add(new SlideModel(R.drawable.img2, ScaleTypes.CENTER_CROP));
imageList.add(new SlideModel(R.drawable.img3, ScaleTypes.CENTER_CROP));
imageSlider.setImageList(imageList,ScaleTypes.CENTER_CROP);
You can either use setItemChangeListener or call the listners below
imageSlider.setItemClickListener(object : ItemClickListener {
override fun onItemSelected(position: Int) {
button.setTextColor(Color.parseColor("#ff0000"));
or
button.setTextColor(getApplication().getResources().getColor(R.color.red));
or
button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));
}
})
inside source of this lib, in ImageSlider class, you can find setOnItemChangeListener - use this method, register own interface and change text color inside called method

Selected Item on RecyclerView Horizontal Layout keeps hidden behind start of Layout on Android

I'm using a RecyclerView with LinearLayout Manager and Horizontal orientation.
It's to be used on a TV app, so it needs to be navigated with the dpad. But whenever I go do the end and come back it get stuck on the last one. See photo:
The selected ItemView is the first, but it won't go to the center. if you go to the right and the come back it will show fully.
the code:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="320dp"
app:layout_constraintBottom_toTopOf="#+id/tv_mini_player_view_placeholder"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/carrier_image_card"
app:layout_constraintVertical_bias="0.10">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/margin_start_tv_home"
android:background="#null"
android:clipToPadding="false"
android:orientation="horizontal"
android:padding="#dimen/margin_medium_big"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:context=".android.screens.auth.SelectCarrierActivity"
tools:listitem="#layout/imagecardview_station" />
</androidx.core.widget.NestedScrollView>
One little thing I observed is the layout World Hits didn't adjust the label size. When selected these layouts show a description which makes the transparent grey box larger to acommodate. It's a "View.GONE" change on the description label. So the last selected layout the description disappeared but the layout didn't readjusted. The code for the View Holder part is below:
inner class StationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val mainImageView: ImageView by lazy { itemView.findViewById<ImageView>(R.id.main_image) }
val titleView: TextView by lazy { itemView.findViewById<TextView>(R.id.title_text) }
val descriptionView: TextView by lazy { itemView.findViewById<TextView>(R.id.description_text) }
init {
val frameLayout = itemView.findViewById<FrameLayout>(R.id.frame_layout)
val cardView = itemView.findViewById<FrameLayout>(R.id.cardview)
cardView.apply {
isFocusable = true
isFocusableInTouchMode = true
onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
AbstractCardPresenter.animateScaleUp(cardView as View)
frameLayout.background = itemView.context.resources.getDrawable(R.drawable.card_border)
descriptionView.visibility = View.VISIBLE
} else {
AbstractCardPresenter.animateScaleDown(cardView as View)
frameLayout.background = null
descriptionView.visibility = View.GONE
}
}
}
itemView.onAttachStateChangeListener {
onViewAttachedToWindow {
if (adapterPosition == 0) {
cardView.requestFocus()
}
}
}
}
}
U need to get rid of NestedScrollView
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/margin_start_tv_home"
android:background="#null"
android:clipToPadding="false"
android:orientation="horizontal"
android:padding="#dimen/margin_medium_big"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:context=".android.screens.auth.SelectCarrierActivity"
tools:listitem="#layout/imagecardview_station" />
I found the offending bit of code!! I was trying to get focus on the first ViewHolder of the RecyclerView upon load. That was causing the problem. Just need to find another way to do it now! This bit of code right there:
itemView.onAttachStateChangeListener {
onViewAttachedToWindow {
if (adapterPosition == 0) {
cardView.requestFocus()
}
}
}

Programmatically create a MaterialButton with Outline style

I would programmatically like to create a button as defined in the design guidelines here: https://material.io/design/components/buttons.html#outlined-button, looking like this:
In XML I'm able to do this, using this piece of layout xml:
<com.google.android.material.button.MaterialButton
android:id="#+id/buttonGetStarted"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="#string/title_short_intro" />
What I'm looking for is an example that shows how to do this using Java code? I have tried the following:
MaterialButton testSignIn = new MaterialButton( new ContextThemeWrapper( this, R.style.Widget_MaterialComponents_Button_OutlinedButton));
String buttonText = "Sign-in & empty test account";
testSignIn.setText( buttonText );
But this does not result in the outline variant:
You can use below:
MaterialButton testSignIn = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
String buttonText = "Sign-in & empty test account";
testSignIn.setText(buttonText);
If you want to apply a Outlined button you can use the R.attr.materialButtonOutlinedStyle attribute style in the constructor:
MaterialButton outlinedButton = new MaterialButton(context,null, R.attr.materialButtonOutlinedStyle);
outlinedButton.setText("....");
MaterialButton has strokeColor and strokeWidth which is used to set the outline.
val _strokeColor = getColorStateList(R.styleable.xxx_strokeColor)
val _strokeWidth = getDimensionPixelSize(R.styleable.xxx_strokeWidth, 0)
button = MaterialButton(context).apply {
layoutParams = LayoutParams(MATCH_PARENT, WRAP_PARENT)
strokeColor = _strokeColor
strokeWidth = _strokeWidth
}
Create outlined button layout outlined_button.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.material.button.MaterialButton>
Then inflate outlined button in runtime
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MaterialButton button = (MaterialButton)inflater.inflate(R.layout.outlined_button, vg, false);

Controlling view visibility via databinding

<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable
name="notificationResponse"
type="myms.models.NotificationResponse"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
...........
<TextView
android:id="#+id/tv_empty_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_12dp"
android:layout_marginRight="#dimen/_12dp"
android:minHeight="#dimen/_60dp"
android:gravity="center"
android:textSize="#dimen/_18sp"
android:textStyle="bold"
android:text="No Message"
android:background="#color/white"
android:visibility="#{notificationResponse.payloads.size() > 0 ? View.GONE : View.VISIBLE}"/>
......
</FrameLayout>
</layout>
What i want to achieve is that by default the view should be GONE and after the async call in my code where i actually bind the notificationResponse object it should decide whether to show or hide the view.
The Interpretation of the code you have written android:visibility="#{notificationResponse.payloads.size() > 0 ? View.GONE : View.VISIBLE}" is
When your list size is having more than one data you want to hide that TextView and in other case you want to show it.
Not when you are calling your API, your list size will definitely less than or equal 0 so it will not be visible.
Solution :
Pass some variable which indicates API is still calling in background and when API call is done, set that variable to false.
android:visibility="#{notificationResponse.payloads.size() > 0 || !loading ? View.GONE : View.VISIBLE}"
It means if your list size is more than one and API call is done, TextView should hide.
By default value of loading should be false, when you calling API change that value to true and when API call is done again set it to false.
In fragment/activity you could create field int itemsCount = 0 and after you got a response set the itemsCount = response.payloads.size() and in the xml set itemCount instead of NotificationResponse.
Actually you have to set your new variable in the binding to make an effect on view. It's mean that if you want to achive you result without changing you xml just set new List yo your variable and after get a response set list from response.
EDIT
first way (according to comments below) is like this:
public void setLoading(boolean loading) {
isLoading = loading;
notifyPropertyChanged(BR._all);
}
and
notifyPropertyChanged(BR.loading);
ref
But, there is an easier way of doing this, I would have done it the following way:
first change this line in your view
android:visibility="#{notificationResponse.payloads.size() > 0 ? View.GONE : View.VISIBLE}"/>
to
android:visibility="gone"/>
And then in your AsyncTask add something like this:
protected void onPostExecute(Boolean toBeShown) {
if(toBeShown){
tvEmptyView.setVisibility(View.VISIBLE);
}else{
tvEmptyView.setVisibility(View.GONE);
}
}
Another option would be to use a binding adapter.
#BindingAdapter("viewVisibility")
fun bindViewVisibility(view: View, shouldShow: Boolean) {
view.let {
if (shouldShow) {
it.visibility = View.VISIBLE
} else {
it.visibility = View.GONE
}
}
}
And in the layout:
<androidx.constraintlayout.widget.ConstraintLayout
...
viewVisibility="#{viewModel.showError}"
...>
</androidx.constraintlayout.widget.ConstraintLayout>

Android : Crouton lib and custom font

I use custom fonts in my app so i want a custom font for Crouton. I 've tried to do it with setTextAppearance, it doesn't work.
<?xml version="1.0" encoding="utf-8"?>
<com.ecab.ui.custom.TextViewCustomFont
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.crouton"
android:id="#+id/crouton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ban_confirmation"
android:gravity="center"
android:text="TEST"
android:textColor="#android:color/white"
custom:typeface="gothamBold" />
In Style class :
INFOCUSTOM = new Builder().setDuration(3000).setTextAppearance(R.id.crouton).build();
Then, I've tried to do it by changing setTypeface() with my font, it doesn't work.
In Crouton class :
private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
text.setId(TEXT_ID);
text.setText(this.text);
text.setTypeface(MyFonts.getGothamBookBold(this.activity));
Log.d(Constants.D_TAG, "chaneg the typeFace");
text.setGravity(this.style.gravity);
// set the text color if set
if (this.style.textColorResourceId != 0) {
text.setTextColor(resources.getColor(this.style.textColorResourceId));
}
// Set the text size. If the user has set a text size and text
// appearance, the text size in the text appearance
// will override this.
if (this.style.textSize != 0) {
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, this.style.textSize);
}
// Setup the shadow if requested
if (this.style.textShadowColorResId != 0) {
initializeTextViewShadow(resources, text);
}
// Set the text appearance
if (this.style.textAppearanceResId != 0) {
text.setTextAppearance(this.activity, this.style.textAppearanceResId);
}
return text;
}
What can i do to have a custom Font ?
ps : library version ==> 1.7
Okay, I found the problem !
It works with the second solution by changing the Typeface. I had just forget to remove the
setTextAppearance(R.id.crouton)
in the Style class. So my custom style is like this :
INFOCUSTOM = new Builder().setDuration(3000).setBackgroundDrawable(R.drawable.ban_confirmation).setHeight(LayoutParams.WRAP_CONTENT)
.build();
One problem resolves, another arrives :) ! With the background drawable, the text is not vertically center
You can a custom Style that uses the resourceId of your text
appearance via Style.Builder.setTextAppearance(...).
This takes a reference from your styles.xml and uses it within the
internal TextView of the Crouton.
Then you can call Crouton.makeText or Crouton.showText with your
custom Style.
Source
How does MyFonts.getGothamBookBold() look like?
This however should work:
private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
text.setId(TEXT_ID);
text.setText(this.text);
Typeface myTypeFace = Typeface.createFromAsset(this.activity.getAssets(), "gothamBold.ttf");
text.setTypeface(myTypeFace);
text.setGravity(this.style.gravity);
// set the text color if set
if (this.style.textColorResourceId != 0) {
text.setTextColor(resources.getColor(this.style.textColorResourceId));
}

Categories

Resources