androiduser-interfaceshimmer

How do I set the shimmer layout duration programmatically in java?


I want to set the shimmer layout duration programmatically in Java.

I have done it in xml, it works, but I want to do it in Java.

Here is the xml code:

<com.facebook.shimmer.ShimmerFrameLayout
    android:id="@+id/shimmerFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:shimmer_shape="radial"
    app:shimmer_auto_start="true"
    app:shimmer_duration="800"
    app:shimmer_repeat_mode="restart">

    <include layout="@layout/content_home_activity"/>
</com.facebook.shimmer.ShimmerFrameLayout>

Here is my Java code:

public class HomeActivity extends Fragment{


    private ShimmerFrameLayout shimmerCarousel;
    private static final int shimmerDuration = 1500;

    public HomeActivity() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState){

    View view = inflater.inflate(R.layout.activity_home, container, false);

    context = getActivity().getApplicationContext();

    shimmerCarousel = view.findViewById(R.id.shimmerFrameLayout);
    }
}    

I already know how to do it in xml but I want to assign it using Java.

I am looking for a Java or better Kotlin solution.


Solution

  • In this example I'm programmatically creating a Shimmer effect in a TextView, setting the duration and a few other things:

        //Create shimmer builder
        Shimmer.ColorHighlightBuilder shimmerBuilder = new Shimmer.ColorHighlightBuilder()
                                        .setBaseColor(ContextCompat.getColor(context, R.color.White))
                                        .setHighlightColor(ContextCompat.getColor(context, R.color.White))
                                        .setDuration(1200)
                                        .setIntensity(0.9f)
                                        .setDropoff(0.9f)
                                        .setBaseAlpha(0.6f)
                                        .setHighlightAlpha(1f);
        //Create shimmer
        Shimmer shimmer = shimmerBuilder.build();
        
        //Creating some view to apply the shimmer     
        TextView myTextView = new TextView(context);
    
        //Creating the shimmer frame layout
        ShimmerFrameLayout shimmerContainer = new ShimmerFrameLayout(context);
        shimmerContainer.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        shimmerContainer.addView(myTextView);
        shimmerContainer.setShimmer(shimmer);
        shimmerContainer.showShimmer(true);
    
        ...
    
        //Adding the shimmerContainer to myView
        myView.addView(shimmerContainer);
    

    You can also use Shimmer.AlphaHighlightBuilder.
    To stop the Shimmer use shimmerContainer.hideShimmer();
    Check the docs here for more info.