When I'd ran this code, it did nothing, I'm sure that this event is called when I touch the button. but It doesn't change the opacity of imageView.
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f);
fadeAltAnim.start();
}
};
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
Is there anything wrong with my code?
Try this code, its using ViewPropertyAnimator :
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.animate().alpha(0.2f).setDuration(1000);
}
};
Its always good to set a duration, so the Animation knows how long its supposed to run.
EDIT : You might want to attach it to a MotionEvent if you are using onTouchListener , something like :
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent == MotionEvent.ACTION_DOWN)
view.animate().alpha(0.2f).setDuration(1000);
}
};
EDIT 2 :
If you want to use a Button its best to use an OnClickListener instead of onTouchListener, if you want to attach an Image you have to initiate it(for example in an ImageView) :
Button button = (Button) findViewById(R.id.your_button);
ImageView imageView = (ImageView) findViewById(R.id.imgTest);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.animate().alpha(0.2f).setDuration(1000);
}
};
It's because you're passing in an id when the method requires the View. Try passing in the View.
So basically change ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f); to ObjectAnimator.ofFloat(view, "alpha", 0.2f);
Here's an example
findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(v, "alpha", 0.2f);
fadeAltAnim.start();
}
});
Is there any reason you're using ObjectAnimator? Can you do:
AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);
**try this**
public class Animationtest extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
imageView = (ImageView)findViewById(R.id.text);
setAnimation();
}
private void setAnimation(){
imageView.setOnTouchListener(new OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "touch", Toast.LENGTH_LONG).show();
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(imageView, "alpha", 0.2f);
fadeAltAnim.start();
return false;
}
});
}
}
Related
I use a GestureListener on an image to detect a Double Tap.
holder.image.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return holder.myDoubleTapDetector.onTouchEvent(event);
}
});
And I use the library Zoomy to make the images zoomable.
Zoomy.Builder builder = new Zoomy.Builder(mActivity).target(holder.image);
builder.register();
Both work perfectly fine for them alone.
But adding both at the same time does not work! Only the one that was added last works.
How can I have both of them working?
Zoomy provides a solution for that:
Because Zoomy works by attaching a View.OnTouchListener to the registered View, View.OnClickListener can not be set along with Zoomy, so a TapListener, LongPressListener and DoubleTapListener are provided to ensure the View still can listen for gestures. A ZoomListener is also provided if you are interested in zoom events.
In Code:
Zoomy.Builder builder = new Zoomy.Builder(this)
.target(mZoomableView)
.tapListener(new TapListener() {
#Override
public void onTap(View v) {
//View tapped, do stuff
}
})
.longPressListener(new LongPressListener() {
#Override
public void onLongPress(View v) {
//View long pressed, do stuff
}
}).doubleTapListener(new DoubleTapListener() {
#Override
public void onDoubleTap(View v) {
//View double tapped, do stuff
}
})
.zoomListener(new ZoomListener() {
#Override
public void onViewStartedZooming(View view) {
//View started zooming
}
#Override
public void onViewEndedZooming(View view) {
//View ended zooming
}
});
thanks for helping. I was trying to implement an add to favorite function in my recycler view, everything is working fine except for one thing. I am unable to commit the changed image button in my recycler view, every time I press the button, the post is added to the favorites and the image button turns yellow, but as soon as I move to some other fragment, the yellow button goes back to its initial stage. Can anyone of you help me on how can I make my button commit to the changes in recycler view. Below is my relevant code.
Initialization of buttons in holder class:
class UsersViewHolder1 extends RecyclerView.ViewHolder {
View mView;
private ImageButton mFavouritesBlack, mFavouritesYellow;
private GestureDetector mGestureDetector;
private Heart mHeart;
public UsersViewHolder1(View itemView) {
super(itemView);
mView = itemView;
mFavouritesBlack = mView.findViewById(R.id.ad_start_fav);
mFavouritesYellow = mView.findViewById(R.id.ad_start_fav1);
}
}
OnBindViewHOlder class(I omitted the irrelevant code):
protected void onBindViewHolder(#NonNull final UsersViewHolder1 Holder, final int position, #NonNull
Ad ad) {
Holder.setTitle(ad.getTitle());
Holder.setPrice(ad.getPrice());
Holder.setCategory(ad.getCategory());
Holder.setImage(ad.getImage(), getContext());
Holder.setTime(ad.getTime());
String user_id = getRef(position).getKey();
final String kk = user_id.toString();
Holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mew = new Intent(getActivity(), ExpandActivity.class);
mew.putExtra("user_id", kk);
startActivity(mew);
}
});
Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(),"Added to Fav",Toast.LENGTH_SHORT).show();
if (Holder.mFavouritesBlack.isShown())
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}
});
return true;
}
});
Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(),"Removed from Fav",Toast.LENGTH_SHORT).show();
if (Holder.mFavouritesYellow.isShown())
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
});
return true;
}
});
}
What you are trying to implement is selection inside a recyclerview which requires saving state, that is you will have to save the favourite items say in a SparseBooleanArray which can be used to save favourite adapter positions, and inside onBindViewHolder check if the favourites array contains the adapter position and toggle imageview (There is no need to use two images instead you can change the color of drawable), please refer this artcle
That is because you are saving the flags when a click happened,but when the views are redrawn you are not resetting the images accordingly. Inside the onBindViewHolder add the following code.
//Replace the if condition statement with the state value check(Abstract code shown below)
if(mFav.child(puid).child(kk).child("fav_status").getValue(),equals("Added as fav")){
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}else{
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
After 2 days of messed up searches and work I finally removed my the problem was that recycler view isn't able to hold the state by itself after change, that's why we need to provide a way to store the state (or the image in my case) so that it can remember it everytime it recycles the view, but instead I added the direct statement through which it checks whether the favourite node is present in database or not and based on that the buttons behaved. Below is the code.
mFav.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(puid).hasChild(kk)){
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}else{
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Added to Fav", Toast.LENGTH_SHORT).show();
if (!itemStateArray.get(adapterPosition, false))
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}
});
return true;
}
});
Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Removed from Fav", Toast.LENGTH_SHORT).show();
if (itemStateArray.get(adapterPosition, false))
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
});
return true;
}
});
But huge thanks to everyone who tried to help.
I'm using the AppCompat ActionBar (Theme.AppCompat.Light.DarkActionBar) for my Android app.
After starting the app, the ActionBar fades out after 6 seconds (onCreate method):
h.postDelayed(new Runnable() {
#Override
public void run() {
getSupportActionBar().hide();
}
}, 6000);
So, how can I fade in the ActionBar with a touch gesture from the top of the screen to the middle ... and fade out it again after 10 seconds?
I think the problem is, that the ActionBar has no OnTouchListener.
Try something like this.
In the class you want to use the gesture:
private GestureDetectorCompat gestureDetectorCompat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetectorCompat = new GestureDetectorCompat(this, new MyGestureListener());
//Stuff in onCreate()
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetectorCompat.onTouchEvent(event);
return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
//Swipe from the top to the bottom
if(event2.getY() > event1.getY()){
getSupportActionBar().show();
h.postDelayed(new Runnable() {
#Override
public void run() {
getSupportActionBar().hide();
}
}, 6000);
}
return true;
}
}
So my question is how can I make an animation Starts when touch and Once it's not Touched it plays another animation ..
(sorry for my English..)
main.java :
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView is = (ImageView) findViewById(R.id.img1);
is.setBackgroundResource(R.animator.animup);
final AnimationDrawable animup = (AnimationDrawable) is.getBackground();
final ImageView iv = (ImageView) findViewById(R.id.img1);
iv.setBackgroundResource(R.animator.anim);
final AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
is.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true, true);
anim.start();
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.setVisible(true, true);
animup.start();
//perform your animation when button is released
}
return false;
}
});
};
;
}
+Another Problem......
EDITED
I reckon this link will help you.
Edit
For implementing touch and release you have to use onTouchListener rather than onClickListener.
Below is the code-
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//perform your animation when button is released
}
}
};
Edit 2
In the else if block you are not setting the visibility of anim to false. I feel that is the problem. Rewrite your code as this-
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true,true);
anim.start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.stop(); //perform your animation when button is released
animup.setVisible(true,true);
animup.start();
}
}
};
If I implement the OnClickListener, the sound effect will work on clicking the button. While on implementing the OnTouchListener the sound effect doesn't work on touching it. So how to enable the sound effect on implementing the OnTouchListener?
EDIT:
Here is the code if I use clicking approach:
public class CalculatorActivity extends Activity implements OnClickListener
{
//...
private Button btn1;
private EditText edLCD;
//...
public void onCreate(Bundle savedInstanceState)
{
//...
btn1 = (Button) findViewById(R.id.d1);
btn1.setOnClickListener(this);
edLCD = (EditText) findViewById(R.id.edLCD);
}
//...
public void onClick(View v)
{
edLCD.setText(edLCD.getText().toString() + "1");
}
}
And here is the code if I use touching approach:
public class CalculatorActivity extends Activity implements OnTouchListener
{
//...
private Button btn1;
private EditText edLCD;
//...
public void onCreate(Bundle savedInstanceState)
{
//...
btn1 = (Button) findViewById(R.id.d1);
btn1.setOnTouchListener(this);
edLCD = (EditText) findViewById(R.id.edLCD);
}
//...
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
edLCD.setText(edLCD.getText().toString() + "1");
}
return true;
}
}
Try...
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
v.playSoundEffect(SoundEffectConstants.CLICK);
edLCD.setText(edLCD.getText().toString() + "1");
}
return true;
}