Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 6 September 2016

Dynamically change layout background color in android

Hi friends

If you want change your component background smoothly then follow below code

actvity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:id="@+id/lin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.samset.user.customtoastsample.Test2">

<LinearLayout
    android:id="@+id/lay1"
    android:layout_width="200dp"
    android:layout_height="150dp">

    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/pause"
        android:text="Pause"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

</LinearLayout>
   
</LinearLayout>


MainActivity.java

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.samset.user.customtoastsample.adapters.TopWinnerstest;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class Test2 extends AppCompatActivity {
   
    LinearLayout lay1;
   

    int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        lay1 = (LinearLayout) findViewById(R.id.lay1);
       


        ArgbEvaluator evaluator = new ArgbEvaluator();
        ValueAnimator animator = new ValueAnimator();
        animator.setIntValues(Color.parseColor("#78CC2E"),Color.parseColor("#901B8E"),      Color.parseColor("#e50000"),Color.parseColor("#FFC807"));
        animator.setEvaluator(evaluator);
        animator.setDuration(6000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.INFINITE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int  color1 = (int) animation.getAnimatedValue();
                //postInvalidate(); if you are animating a canvas
                lay1.setBackgroundColor(color1); //another exampleof where to use

            }
        });
        animator.start();

    }
}

Thanks

Live Sample





No comments:

Post a Comment