How to make my ImageView responsive to onClickevents while .startAnimation() is running? - android

I'm new to android, and I'm facing a problem where I'm trying to make my ImageView, which is playing an animation through startAnimation(), to become Invisible as the user clicks on it. what happens however is that only after the animation is done the Imageview becomes invisible. my assumptions was it's because they both run on the UI thread. surprisingly however, when I changed my event handling from setVisibility to startAnimation() it actually listened to the click, interrupted the animation and restarted it!
To override my problem, which did turn out nice, I made a new Animation object that opposite to the first, which shows the Imageview, hides the Imageview and it worked perfectly, but I'm still curious as to why my Imageview was selectively responsive to different event handling?
This code change visibility only after the animation is done
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
face.setVisibility(View.INVISIBLE);
}
});
}
this code changes the animation "while" the original is playing
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation hideAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation1);
face.startAnimation(hideAnimation);
}
});
}

You should not call face.setVisibility(...), instaed tt should be:
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.INVISIBLE);
}
});

Related

How to use a Drawable in If/else statement for setImageResource?

I'm new at androidstudio and want to compare a imageView by the following:
I have 2 imageView, both are using a drawable i named "blank" at the start of the app, using if/else I want to chance those images to another drawable i have, i tried the following:
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
if (equipament1.equals(R.drawable.blank)){
equipament1.setImageResource(R.drawable.reactor);
}
else if (equipament2.equals(R.drawable.blank)){
equipament2.setImageResource(R.drawable.reactor);
} else {finish();}
but it doesn't work, the app just replaces the first image and if i click on the button again, nothing happens (this if/else is inside a button).
I want to check if the first image is blank, if it is, the app should replace the blank image with the image "reactor" or, if is not blank, the app should move to the second blank image, and replace it and this go on for more 2 blank spaces.
I'm doing this because I'm building an program similar to LucidChart where you put your equipments in the app.
The problem is that the second time you have already changed the value of the comparator.
If the objective is just to change the images you don't need the if/else.
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}
When the user clicks your button, you want to do 2 things. You want to show some images, or you want to call finish().
I would suggest using a boolean as a flag the the state and compare that instead of comparing the ImageView itself. This'll be easier, and make your code easier to read.
I created a flag called firstClick that is set to true by default. When the user clicks your button (button1 in this example), we check against that and show the images. Then we set it to false,
so the next click will call finish().
private ImageView equipament1;
private ImageView equipament2;
// The current state of the Activity
private boolean firstClick = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
// Setting your OnClickListener
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if( firstClick ) {
firstClick = false;
sentImg();
} else {
finish();
}
}
});
}
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}

An Issue animating a new TextView added to the activity on Nexus4 vs Other devices

Im very new to java and android programming.
I am trying to create a button that when pressed, a text will appear at the location of the click and immediately fade out.
At the moment, the problem is that when clicked - the text does appear, but the animation doesn't play, and the text disappears after a few seconds (just like I want).
I have edited this question a few times down bellow, following some progress on solving this problem with great help from the guys replying. Thanks!
The code I tried is this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClickMe = (Button) findViewById(R.id.button1);
mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
mClickMe.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int xClickPos = (int) event.getRawX();
int yClickPos = (int) event.getRawY();
final TextView scorePop = new TextView(getApplicationContext());
scorePop.setText("+10");
scorePop.setX(xClickPos);
scorePop.setY(yClickPos);
scorePop.setVisibility(TextView.INVISIBLE);
// Create the score pop animation
Animation scoreAnimation = new AlphaAnimation(1, 0);
scoreAnimation.setDuration(1000);
scoreAnimation.setAnimationListener (new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
scorePop.setVisibility(TextView.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
mRelativeLayout.removeView(scorePop);
}
});
mRelativeLayout.addView(scorePop);
scorePop.setAnimation(scoreAnimation);
scoreAnimation.start();
}
return false;
}
});
}
EDIT: I have also noticed that if the button is clicked quickly on different spots, the text appears at those spots and disappears with a "choppy" fade. Yet still, if clicked once - the text appears and disappears but no animation.
Thanks for the help!
EDIT 2: With the help I got here it seems the problem has something to do with the device. I am using Nexus 4, and running an emulator to test my apps as Nexus 4 as well. Oddly, running emulator for another device such as Galaxy Nexus seemed to solve the issue.
EDIT:
I was able to recreate your original bug and it had something to do with setX(),setY(). After executing either of those methods the TextView gets "numb" and won't animate. The workarround I figured was to set the layout's margins not the TextView's coordinates:
// Set the margins instead of the actual position
ViewGroup.MarginLayoutParams params=(ViewGroup.MarginLayoutParams)scorePop.getLayoutParams();
params.leftMargin=xClickPos;
params.topMargin=yClickPos;
On my device Galaxy Nexus it is showing animation fine on first time also. May be your device takes time here "mRelativeLayout.addView(scorePop);" Add a little delay after that and then start animation It should run fine.
try it...
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
// set animation listener
animFadein.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animFadein);
}
});

Start an animation automatically without the need of click on screen

i know this question has been asked many times before but i could never quite understand as the other codes were quite a bit more complex or different than mine. so like the question says ... how to start animation automatically instead of on click .. ill leave the code below ... thanks in advance.. !!
final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
a.reset();
final ImageView rImage = (ImageView) findViewById(R.id.title);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
rImage.startAnimation(a);
func(); //A call to the function.
}
});
Something like this should work... This will be triggered everytime the activity is "displayed".
#Override
public void onResume() {
super.onResume();
// layout should have bee initialized in onCreate
layout.post(new Runnable() {
#Override
public void run() {
rImage.startAnimation(a);
}
});
}
you can put your code in onResume, or onCreate depends of your needs, also you can set a timer to active this code, there are many ways...
How to start Animation immediately after onCreate?
http://damianflannery.wordpress.com/2011/06/08/start-animation-in-oncreate-or-onresume-on-android/

How to provide visual feedback when user is tapping ImageView?

I have a view in the UI that is technically an extended ImageView inside a RelativeLayout that I'd rather not change, if possible. I want to show a Drawable from resources (a small PNG with a gradient or a frame) at specific coordinates as a visual feedback when user is tapping the screen (there even is a onShowPress callback). What is the simplest way to do this? I don't want to modify the layout.xml from which UI is inflated, and I'm not too enthusiastic about overriding onDraw to do extra job there.
This is one easy way to do it using view animation class:
final ImageView aboutButton = (ImageView) findViewById(R.id.AboutButton);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation myAnim2 = AnimationUtils.loadAnimation(LaunchActivity.this, R.anim.buttons);
aboutButton.startAnimation(myAnim2);
myAnim2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//do on click after animation ends
Intent intent = new Intent(LaunchActivity.this, MyDialog.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
//change the backround of the button etc on click etc...
}
});
}});

Image Change On Button Click In Android

I'm new to android and am making a simple app. I'm trying to change the image (in an imageview) on a button click.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frown = (ImageView)findViewById(R.id.imageView1);
}
public void action(View view)
{
Toast.makeText(getApplicationContext(), buttontest, Toast.LENGTH_SHORT).show();
frown.setImageResource(R.drawable.chimpy);
}
"action" is being called via XML with the "android:onClick"[insert method here]" for my button
The button works fine and I get my toast, but the image stays the same.
Try changing the drawable to something standard e.g. android.R.drawable.btn_default.
Does it chnage Now?
I am sure you are having some issues with R.drawable.chimpy.
You must use .png image and you can go with the following code snippet:
frown.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
frown.setBackgroundDrawable(R.id.chimpy);
}
});
If it is not working, just tell me...!

Categories

Resources