I'm a little new to Android, but pretty fluent in VB.net. I have two questions regarding Splash Screens:
I am trying to create a Splash Screen that launches on Application start. I can do it with Frame-Animations but would like to use the TransitionDrawable class because of the effect it has (fadeIn) that I would like to use. I used the same code for the Frame-Animation, after changing the definitions, but can't get it to work. What am I doing wrong?
This logo I am loading consists of 16 images. How can I use the TransitionDrawable class to go from logo1 to logo2 to logo3... to logo16? I tried using a loop and the array of "imageIds" to create my own Frame Animation, but can't it to work for the Transition. Help would be greatly appreciated.
Here is my code:
public class SplashScreenActivity extends Activity {
TransitionDrawable animation;
ImageView transImage;
Integer[] imageIds = { R.drawable.logo1, R.drawable.logo2,
R.drawable.logo3, R.drawable.logo4, R.drawable.logo5,
R.drawable.logo6, R.drawable.logo7, R.drawable.logo8,
R.drawable.logo9, R.drawable.logo10, R.drawable.logo11,
R.drawable.logo12, R.drawable.logo13, R.drawable.logo14,
R.drawable.logo15, R.drawable.logo16 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
transImage = (ImageView) findViewById(R.id.splashImageView);
animation = (TransitionDrawable) getResources().getDrawable(R.anim.transition_list);
transImage.setBackgroundDrawable(animation);
transImage.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
finish();
startActivity(new Intent("com.V1.V1LogoSplash.V1LogoMainActivity"));
}
return false;
}; // END ONTOUCH
}); // END ONLISTSENER
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
animation.startTransition(3000);
finish();
}
}
you try this type code
public class SplashActivity extends Activity {
Thread mSplashThread;
private int SPLASH_DISPLAY_LENGTH = 3800;
ImageView image;
AnimationDrawable frameAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashactivity);
image = (ImageView) findViewById(R.id.splashImg);
image.setBackgroundResource(R.drawable.splash_animation);
frameAnimation = (AnimationDrawable) image.getBackground();
Thread splashTread = new Thread() {
public void run() {
try {
sleep(SPLASH_DISPLAY_LENGTH);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent;
intent = new Intent(SplashActivity.this,
ProductListActivity.class);
startActivity(intent);
finish();
}
}
};
splashTread.start();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
frameAnimation.start();
}
}
and other one
drawable in xml (your img put in xml)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="#drawable/survey11"
android:duration="500"/>
<item
android:drawable="#drawable/survey6"
android:duration="300"/>
<item
android:drawable="#drawable/survey5"
android:duration="300"/>
<item
android:drawable="#drawable/survey3"
android:duration="300"/>
<item
android:drawable="#drawable/survey2"
android:duration="300"/>
<item
android:drawable="#drawable/survey1"
android:duration="800"/>
</animation-list>
Related
I have a DialogFragment and I set animation for enter/exit in the onActivityCreated method as below:
#Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}
My DialogAnimation style files is as follows:
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
This works for me now...
Now my problem is i want to have two different exit animation one for when the "OK" button is clicked and one for the cancel button. So what I did was I tried changing the transition just before dismissing but it didn't work.
Any solution on how it can be achieved?
This is what I tried:
#Override
public void onClick(View v) {
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation2;
this.dismiss();
}
You can do it inside your DialogFragment, without changing
getDialog().getWindow()
.getAttributes().windowAnimations
You should animate "decor view" in onStart and onClick.
This is the code snipped :
Create Dialog first
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Hello from animated dialog :)")
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//we have to add button here and then override it's click in onStart
}
}
)
.setCancelable(false)
.create();
}
Then override onStart method
#Override
public void onStart() {
super.onStart();
AlertDialog dialog = (AlertDialog)getDialog();
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("scaleY", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("alpha", 0.0f, 1.0f));
scaleDown.setDuration(2000);
scaleDown.start();
Button positiveButton = dialog.getButton(Dialog.BUTTON_NEGATIVE);
positiveButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
scaleDown.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationEnd(Animator animation) {
dismiss();
}
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
scaleDown.setDuration(2000);
scaleDown.start();
}
});
}
Here is the result animation
And if you remove scale properties from my code you will get only alpha animation. Exactly as you wanted.
Remove this:
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),
You can set an Up & Down animations for dialog fragment. In 'res/anim' add two files:
// Slide up animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="0" />
</set>
// Slide dowm animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="0%p"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%p" />
</set>
// Style
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_down</item>
</style>
// Inside Dialog Fragment
#Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}
I think the best approach is to call the different animations on Button click. Hence you would have something similar to the below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button OkButton = (Button) findViewById(R.id.btnOk);
Button CancelButton = (Button) findViewById(R.id.btnCancel);
OkButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
}
});
return true;
CancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation2;
}
});
return true;
}
If I were you, I would also use correct naming conventions for future reference. For example setting DialogAnimation to OkAnimation and DialogAnimation2 to CancelAnimation.
Home this helps :)
One simple way to do what you want is to use animation listeners like.
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
// dismiss your dialog in here and it will work
}
});
Start animation on your onclick method and dismiss dialog on onAnimationEnd() method.You may have to make snimation objects and start them manually with View's startAnimation(animation) method.
You should set a theme for your base dialog
let say :-
public class CustomDialogFragment extends DialogFragment implements OnEditorActionListener
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Set a theme on the dialog builder constructor!
AlertDialog.Builder builder =
new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );
builder
.setTitle( "Your title" )
.setMessage( "Your message" )
.setPositiveButton( "OK" , new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
}
Then you just need to define the theme that will include your desired animation. In styles.xml add your custom theme:
<style name="MyCustomTheme" parent="#android:style/Theme.Panel">
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
</style>
<style name="MyAnimation.Window" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/anim_in</item>
<item name="android:windowExitAnimation">#anim/anim_out</item>
</style>
refer this
If any one ends up using the onCreateDialog answer in many dialogs, then you can use this Kotlin extension function.
//ExtensionFunctions.kt file
fun Dialog.setWindowAnimations(#StyleRes id: Int): Dialog {
this.window?.attributes?.windowAnimations = id
return this
}
and in your onCreateDialog function
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return super.onCreateDialog(savedInstanceState)
.setWindowAnimations(R.style.MyDialogAnimation)
}
I use animation to display my buttons, i have 9 buttons so i used duration for display, but it is run animation for all buttons at same time and just with diffrent duration, of course it is correct, but i want to run animation for button1 first and when it is done, do it again for button2 and when it is done too, do it for next buttons....
I mean run same animation with same speed for different buttons one after another,not same time. now my apk is this:
https://drive.google.com/file/d/0B9Q0pN8FVwEORGRQVE1kQmtvS28/view?usp=sharing
it must be like this:
http://up.vbiran.ir/uploads/4517142605716735676_animationn.gif
my full code:
public class MainActivity extends ActionBarActivity {
public MediaPlayer player;
public MediaPlayer playerp;
private final ButtonSupport[] buttonSupports = new ButtonSupport[]{
new ButtonSupport(R.id.Button06, 1000l, YourClassActivity.class),
new ButtonSupport(R.id.Button03, 2000l,YourClassActivity2.class),
new ButtonSupport(R.id.button1, 3000l,YourClassActivity3.class),
new ButtonSupport(R.id.Button08, 5000l,YourClassActivity4.class),
new ButtonSupport(R.id.Button04, 6000l,YourClassActivity5.class),
new ButtonSupport(R.id.Button01, 7000l,YourClassActivity6.class),
new ButtonSupport(R.id.Button07, 9000l,YourClassActivity7.class),
new ButtonSupport(R.id.Button05, 10000l,YourClassActivity8.class),
new ButtonSupport(R.id.Button02, 11000l,YourClassActivity9.class),
};
private static class ButtonSupport{
final int buttonId;
final long duration;
final Class<? extends Activity> clazz;
ButtonSupport(int buttonId, long duration, Class<? extends Activity> clazz) {
this.buttonId = buttonId;
this.duration = duration;
this.clazz = clazz;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (ButtonSupport buttonSupport : buttonSupports) {
animButton(buttonSupport);
}
}
private void animButton(final ButtonSupport buttonSupport) {
final Button button = (Button) findViewById(buttonSupport.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startButtonAnimation(v, buttonSupport.clazz);
}
});
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
anim.setDuration(buttonSupport.duration);
button.startAnimation(anim);
}
public void startButtonAnimation(View btn, final Class<? extends Activity> clazz) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
player = MediaPlayer.create(MainActivity.this, R.raw.music);
player.setLooping(true); // Set looping
player.setVolume(1,1);
player.start();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
player.stop();
startActivity(new Intent(getApplicationContext(), clazz));
overridePendingTransition(R.anim.animation, R.anim.animation2);
playerp = MediaPlayer.create(MainActivity.this, R.raw.musicp);
playerp.setLooping(false); // Set looping
playerp.setVolume(1,1);
playerp.start();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
animation xml code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="1000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
</set>
I guess the easiest change to make it work looks like this:
int ANIM_OFFSET=100;
....
for (int i=0;i<buttonSupports.length;i++) {
ButtonSupport buttonSupport =buttonSupports[i];
animButton(buttonSupport,i);
}
....
anim.setStartOffset(i*ANIM_OFFSET);
I have function that starts thread for blinking view if accepted true, but how do I stop it if false received?
private void blinkText(boolean b){
final Handler handler = new Handler();
if(b)
{
new Thread(new Runnable() {
#Override
public void run() {
int timeToBlink = 1000; //in ms
try{
Thread.sleep(timeToBlink);
}catch (Exception e) {
}
handler.post(new Runnable() {
#Override
public void run() {
TextView txt = (TextView) findViewById(R.id.tv);
if(txt.getVisibility() == View.VISIBLE){
txt.setVisibility(View.INVISIBLE);
}else{
txt.setVisibility(View.VISIBLE);
}
blinkText(true);
}
});
}}).start();
}
else
{
// stop blinking
}
}
You can define a blinking animation in xml and add it to your textview after initialising it:
res/anim/blink.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="200"
android:repeatMode="reverse"
android:repeatCount="infinite"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
Add animation to view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
TextView textView = (TextView) this.findViewById(R.id.textview);
Animation blink = AnimationUtils.loadAnimation(this, R.anim.blink);
textView.setAnimation(blink);
}
To start:
textView.getAnimation().start()
To stop:
textView.getAnimation().cancel()
textView.getAnimation().reset()
you need to create a boolean field as class member ( boolean mStopBlink = false; ) and track of whether the TextView is currently blink:
txt.setVisibility(View.VISIBLE);
if(!mStopBlink){
txt.setVisibility(View.INVISIBLE);
I have three scroll views that overlap. For some reason, when I set the other two to View.Gone and the one scroll view I wanted to View.Visible, then start an animation, it doesn't get triggered. These scroll views are within a fragment -- I know some features don't work fully within a fragment. Animation seems pretty basic though.
Here is my button listener's method;
sv2.setVisibility(View.GONE);
sv3.setVisibility(View.GONE);
sv1.setVisibility(View.VISIBLE);
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_scollview);
//set your animation
sv1.startAnimation(fadeInAnimation);
also tried to set invisible, load animation, then make it visible;
sv1.setVisibility(View.INVISIBLE);
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_scollview);
//set your animation
sv1.startAnimation(fadeInAnimation);
sv1.setVisibility(View.VISIBLE);
And here is my animation xml;
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500"
android:repeatCount="infinite"/>
</set>
For use animation in Fragment try below code
This is my layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_banner"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
This is my fragment java class
public class Fragment_Home extends Fragment {
public int currentimageindex = 0;
Handler mHandler = new Handler();
Runnable mUpdateResults;
//Array of drawable images
private int[] IMAGE_IDS = {
R.drawable.home_slider_stemer, R.drawable.home_slider_plane
};
//image view
private ImageView iv_banner;
private View rootView;
public Fragment_Home() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_home, container, false);
LoadUIElements();
return rootView;
}
private void LoadUIElements() {
iv_banner = (ImageView) rootView.findViewById(R.id.iv_banner);
int delay = 1000; // delay for 1 sec.
int period = 2000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
mHandler.post(mUpdateResults);
}
}, delay, period);
mUpdateResults = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
AnimateandSlideShow();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
/**
* Helper method to start the animation on the splash screen
*/
protected void AnimateandSlideShow() {
// TODO Auto-generated method stub
try {
iv_banner.setImageResource(IMAGE_IDS[currentimageindex
% IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(getActivity()
.getBaseContext().getApplicationContext(), R.anim.fade_in);
iv_banner.startAnimation(rotateimage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Don't forget to put images in your Drawable folder in res.
I got around the problem by setting an animation listener and managing all the visibility stuff inside there.
sv1.setVisibility(View.INVISIBLE);
//grab animation from anim folder in res/
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_anim);
fadeInAnimation.setAnimationListener(new AnimationListener() {
//set other scroll views to invisible once done
public void onAnimationEnd(Animation animation) {
sv2.setVisibility(View.INVISIBLE);
sv3.setVisibility(View.INVISIBLE);
}
public void onAnimationRepeat(Animation animation) {
}
//once our animation starts, we set our view to visible
public void onAnimationStart(Animation animation) {
sv1.setVisibility(View.VISIBLE);
}
});
scrollViewAnimationActive = true;
//start our animations for views that need to be removed.
//We know one of these views were showing by checking if it was "visible".
if (sv2.getVisibility() == View.VISIBLE)
sv2.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
else if (sv3.getVisibility() == View.VISIBLE) {
sv3.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
}else if (wikiParentLL.getChildCount() > 1) {
wikiParentLL.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
}
//finally, start our "animation"
sv1.startAnimation(fadeInAnimation);
Hope this helps.
I had exactly problem, I solved it by using onWindoFocusChangeListener and Handler.
mview.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
#Override
public void onWindowFocusChanged(final boolean hasFocus) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startSeedAnimation();
}
}, 600);
}
});
Where you can get the view object in onViewCreated or simply call view? / getView() from everywhere.
I want to change the button background color for a short period of time after the button is pressed. The button should regain it's previous condition after that period.
Probably a handler is the correct decision for this problem, unfortunately i didn't found a working example for doing a similar thing.
If anyone can give me a short example of how doing a such thing i would appreciate it.
Do this :
public class LaunchActivity extends Activity implements OnTouchListener{
private Button yourButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yourButton= (Button)findViewById(R.id.yourButton);
yourButton.setOnTouchListener(this);
}
#Override
public boolean onTouch(final View view, MotionEvent event) {
final int action = event.getAction();
if(view.getId()==R.id.yourButton){
if(action == MotionEvent.ACTION_DOWN)
yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
if(action == MotionEvent.ACTION_UP){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
yourButton.setBackgroundResource(R.drawable.ic_button_normal);
}
}, 2000);
}
}
}
}
Or with an onClick listener :
#Override
public void onClick(View v) {
yourButton.setBackgroundResource(R.drawable.first_icon);
// SLEEP 2 SECONDS HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
yourButton.setBackgroundResource(R.drawable.second_icon);
}
}, 2000);
}
you can define an XML background for your button under res/drawable/button_background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_background_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/button_background_notpressed"/>
</selector>
and for use an ImageButton
<ImageButton
...
android:background="#drawable/button_background"
... />