I'm using sdk 21 and added buttons to a gridview. However, somehow, only the first button is showing ripple effect when clicked and others not showing...
Edit: I found if I didn't add onClick listener to the buttons, the ripple effect is showing, but after I add the listener, only the first one is showing ripple effect...
Edit: Well, after further investigation, I found the effect is gone after I call setCompoundDrawablesWithIntrinsicBounds()
I got the resourceId by following:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.hashtag_button);
mSelectedResourceId = a.getResourceId(R.styleable.hashtag_button_selected_image, R.drawable.star_selected);
mUnselectedResourceId = a.getResourceId(R.styleable.hashtag_button_unselected_image, R.drawable.star_unselected);
a.recycle();
I change image by following method:
public void setFavorite(boolean isFavorite) {
mIsFavorite = isFavorite;
if (mIsFavorite) { //Favorite
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, mSelectedResourceId, 0);
} else { //Not Favorite
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, mUnselectedResourceId, 0);
}
}
So this caused only first button has ripple effect... Not sure how to solve this...
Anyone has same issue?
Thanks
Related
Context of the Problem
I was currently trying to build a functionality to fast forward videos in ExoPlayer exactly like how Youtube does it. I thought that I could add a double click listener and I was able to do that using the answer given here.
Problem
The problem I am facing is that I was getting the ripple animation even when I do a single click which is not what I want. I want to be able to get a ripple only when a double click is triggered. So, for this, I tried to get the number of clicks and then accordingly set the ripple visibility on the view but it did not seem to work for me and instead giving a weird behaviour. The issue can be seen in the gif given below* :
It can be clearly seen that on single click, it shows ripple. On double click, it shows one ripple right but the other one comes out from nowhere!
Code
Kotlin
var doubleClickLastTime = 0L
var clicksDone = 0
binding.forward.setOnClickListener {
if((System.currentTimeMillis() - doubleClickLastTimeF) < 350){
doubleClickLastTime = 0
clicksDone = 0
exoPlayer.seekTo(exoPlayer.contentPosition + 10000)
}else{
if (clicksDoneF == 0){
binding.forward.foreground = null
}else{
binding.forward.foreground = with(TypedValue()) {
this#VideoPlayer.theme.resolveAttribute(
android.R.attr.selectableItemBackground, this, true)
ContextCompat.getDrawable(this#VideoPlayerFromUrl, resourceId)
}
}
clicksDone++
doubleClickLastTime = System.currentTimeMillis()
}
}
XML
<androidx.cardview.widget.CardView
android:id="#+id/forward"
android:layout_width="550dp"
android:layout_height="match_parent"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginVertical="50dp"
android:foreground="?selectableItemBackgroundBorderless"
app:cardCornerRadius="16px"
app:cardElevation="0dp"
android:backgroundTint="#android:color/transparent"/>
*Sorry for the bad gif quality
If you convert it in kotlin you can use this
GestureDetector gestureDetector=new GestureDetector(getApplicationContext(),new GestureDetector.SimpleOnGestureListener(){
#Override
public boolean onDoubleTap(MotionEvent e) {
// you can put your code here
return super.onDoubleTap(e);
}
});
I need to find the red buttons on Activity and set orange background color to these buttons.
When I click on the button I set it to red:
view.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
When I click on another button, the red buttons should turn orange.
public void Active(View view){
for (View but : buttons) {
but.setClickable(true);
but.setBackgroundTintList();
}
}
I do not know how to get the id of the colors
For me is unclear your question but I'll try to answer it.
Supposing buttons is a List<Button>, so what you can do is.
for(View but : buttons){
int color = ((ColorDrawable)but.getBackground()).getColor();
if(color == Color.Red){
//This button is red change it to orange
but.setBackgroundColor(R.colors.orange);
}
}
And when you are clicking the button use
button.setBackgroundResource(Color.Red);
but.setBackgroundColor(Color.RED);
use that
I am using this tool to get a toggle button behavior, this is the library:
https://github.com/ceryle/SegmentedButton
I want the left and right buttons to have different colors when selected. When I change the color programmatically, it works fine except from inside the listener, any hin on why this would happen?
SegmentedButtonGroup toggle = dialogView.findViewById(R.id.my_toggle);
toggle.setSelectorColor(activity.getResources().getColor(R.color.green));
toggle.setRippleColor(activity.getResources().getColor(R.color.green));
toggle.setOnPositionChanged(new SegmentedButtonGroup.OnPositionChanged() {
#Override
public void onPositionChanged(int position) {
SegmentedButtonGroup toggle = dialogView.findViewById(R.id.my_toggle);
toggle.setSelectorColor(activity.getResources().getColor(R.color.red));
toggle.setRippleColor(activity.getResources().getColor(R.color.red));
}
});
I am also open to other implementations of this functionality, I am having a hard time with this...
in my app i need to change the image of a toolbar button programmatically, and for so I´m using:
public boolean onCreateOptionsMenu( Menu menu ) {
getMenuInflater().inflate(R.menu.main_activity_menu, menu);
toolbarButton = menu.findItem(R.id.action_button);
return true;
}
toolbarButton.setIcon(R.drawable.ic_settings);
And it works perfectly, the problem is that it happens too fast, so i´d need to add an animation to the transition, but I don´t know how to add an animation to a toolbar button.
Could somebody help me please?
Thanks a lot!
Okay, so finally thanks to Victorldavila i managed to get it working, this is my final code:
View button = toolbar.findViewById(R.id.action_button);
//Remove icon animation
if (button != null) {
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_fade_out);
animation.setStartOffset(0);
button.startAnimation(animation);
}
//Add icon animation
if (button != null) {
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_slide_in_up);
animation.setStartOffset(0);
button.startAnimation(animation);
}
Maybe these answers can help you:
https://stackoverflow.com/a/39182291/2759449
https://stackoverflow.com/a/30787535/2759449
If you see the Google Play app , when swiping the menu the menu button convert itself to back button with the finger gradually and nicely, it seems not an animation. can any one help me to achieve this thing ?
Feel free to move/close the post but please be nice to redirect me in any helpful direction.
Icon transitions can be implemented using AnimatedStateListDrawable (There’s also support for animated vector icons.)
e.g.
public void setOrAnimatePlusCheckIcon(final ImageView imageView, boolean isCheck,
boolean allowAnimate) {
if (!hasL()) {
compatSetOrAnimatePlusCheckIcon(imageView, isCheck, allowAnimate);
return;
}
Drawable drawable = imageView.getDrawable();
if (!(drawable instanceof AnimatedStateListDrawable)) {
drawable = mActivity.getResources().getDrawable(R.drawable.add_schedule_fab_icon_anim);
imageView.setImageDrawable(drawable);
}
imageView.setColorFilter(isCheck ?
mActivity.getResources().getColor(R.color.theme_accent_1) : Color.WHITE);
if (allowAnimate) {
imageView.setImageState(isCheck ? STATE_UNCHECKED : STATE_CHECKED, false);
drawable.jumpToCurrentState();
imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
} else {
imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
drawable.jumpToCurrentState();
}
}
code from https://github.com/google/iosched