Okay, I'm just trying to handle the second click on a CardView.
For example, Now when I click on the CardView, It will change background color for the CardView. Also, it should change the image in the ImageView.
If I understand your question: you wanna ask how to prevent from multiple click/tap on a view, if yes then you can handle it by adding following method into your Utility class
public static void avoidMultipleTapping(View view) {
view.setEnabled(false);
final Handler handler = new Handler();
handler.postDelayed(() -> view.setEnabled(true), 1000);
}
if you want to say something else you can comment below over my answer.
Related
Goal: To click on an image in an app and get it to fade out to make a different image appear.
My Method: To make the 1st image fade away after 2000ms and AFTER THAT change the image resource of the 1st image to the 2nd image to make the 2nd image appear
I have a correct solution which was provided by my instructor, so I dont want any correct solution for this. What i want to know is why is my solution not working, i.e why is setImageResource() setting the Image1 to Image 2 at the beginning despite calling it at the end
This is the fade function i have created which the image1 goes to when it is clicked
public void fade(View view){
ImageView image1 = findViewById(R.id.image1);
image1.animate().alpha(0f).setDuration(2000);
image1.setImageResource(R.drawable.cat2);
}
Actual Output: Image 1 changes to Image 2 as soon as I click it and then Image 2 fades away
Problem: Inspite of calling setImageResource() at the end of the code, it actually sets the image resource at the beginning
You are never telling image1.setImageResource(R.drawable.cat2); wait for 2000 millisecond and execute, So image2 appears as soon as you click it.
Solution:
Call image1.setImageResource(R.drawable.cat2); After 2000ms
new Handler().postDelayed(() -> {
image1.setImageResource(R.drawable.cat2);
}, 2000);
This may help.
The animation is asynchronous in your code - it doesn't block/wait, but starts the animation (or rather, queues the animation to be started) and then immediately executes the next line, which sets the image. If you want to update your image once the animation is complete, you can use withEndAction and supply a callback.
image1.animate()
.alpha(0f)
.setDuration(2000)
.withEndAction(new Runnable() {
#Override
public void run() {
image1.setImageResource(R.drawable.cat2);
}
})
.start();
I think this will be help to you:
Fade In Fade Out Android Animation in Java
so use in onClickListener like this:
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image1.setVisibility(View.GONE);
//Animation...
image2.setVisibility(View.VISIBLE);
}
});
I am trying to create a calculator. I am working in the latest Android Studio. Like in calculators, all new tokes(numbers, operators) should be shown in the right and if the field is larger than the display, it should scroll to the latest token. I have already browsed and found a way to do the same.
The code for the same is:
private void scrollRight() {
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
ViewTreeObserver viewTreeObserver = horizontalScrollView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
horizontalScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
horizontalScrollView.scrollTo(entry.getWidth(), 0);
}
});
}
scrollRight is called by a onClick event which checks if a token is entered and calls this.
Everything is working perfectly, i.e. on every new token the scroll bar is scrolling to the end. But if I try to scroll to the beginning manually, it no longer works. After this every time a new token is pressed, the scroll bar first moves to the end and then back to the beginning.
The only option that remains is to restart the program. I tried debugging the OnGlobalLayout function but the debugger loses all frames while stepping out from the function, so it is difficult to know what exactly is making the scroll bar go to the beginning.
GIF to show the problem:
Please Help!
Try
horizontalScrollView.post(new Runnable() {
#Override
public void run() {
horizontalScrollView.fullScroll(View.FOCUS_RIGHT);
}
});
And call findViewById only once in onCreate.
I'm trying to programmatically click on an item of a recyclerView. I'm using:
recyclerView.findViewHolderForAdapterPosition(index).itemView.performClick();
This perfectly works when the index is of a visible item. If the item is not visible (at the last position of the recyclerview, for istance), an Exception is thrown.
What can I do?
I just had a similar question with yours.And I had solve it! Here is what I did.
xxx.mRecyclerView.postDelayed(new Runnable() {
#Override
public void run() {
xx.mRecyclerView.scrollToPosition(position);
}
},300);
xxx.mRecyclerView.postDelayed(new Runnable() {
#Override
public void run() {
xxx.mRecyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();
}
},400);
}
You can scroll to the specific item, then perform click.
Because the doc say
If the item at the given position is not laid out, it will not create a new one.
But I know the adapter has the data, so scroll to it first, and findViewHolderForAdapterPositionwill not be null.
One more thing, I do not know how you use the RecyclerView. In my application, I use it in a fragment, and I don not know why we should delay it scroll and perform click. (Maybe it is because of the life circle?).But this really works.
You could call onClick directly, assuming that view manages its own click listener.
View view = recyclerView.findViewHolderForAdapterPosition(index).itemView;
view.onClick(view);
If the click listener is located somewhere else, you just need to get a reference to the object with the onClick method and call it with the correct view as a parameter.
try this for kotlin and viewBinding
viewBinding.catList.post {
val view = viewBinding.catList.findViewHolderForAdapterPosition(0)?.itemView?.findViewById<CheckBox>(R.id.catButton)
view?.callOnClick()
}
I have a scrollable textview with an onClickListener attached to it.
textView.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//DoStuff();
}
});
The problem is if I scroll the text, as soon as I lift my finger off the view it triggers the onClickListener. The only way this doesn't happen is if I scroll and then move off the view before I lift my finger, and I can't exactly tell my users to do that :P
So my question is, is there a way to determine between a click and a scroll or is the only way to do this to use an onTouchListener and decide for myself if it was a scroll or a click?
Use onTouchListener and handle ON_DOWN and ON_MOVE instead of using onClick.
I might be on the wrong track here, and should be thinking events/publish-subscriber, if so, please enlighten me.
I have an android project running, where I have a layout which acts as an on-screen menu. Implemented in several activities/"parent-views" with the use of '< include>'. Working nicely.
Now, some of the functionality is general and global. Like I have an "add"-button, which does something, that it should always do. Then I'd like the possibility to customize what it does in addition to this, based on the activity where the action originated.
I have seperated menulogic in a simple java class, with the constructor taking an activity as a parameter. From here, I can attach clicklisteners to the buttons in the menu fine, and do stuff on click.
What I'd like is something like:
private void addBtn(String text, String path) {
LinearLayout ll = (LinearLayout) parentActivity.findViewById(R.id.dynamicButtonLayout);
Button newButton = new Button(parentActivity);
newButton.setText(text);
newButton.setTag(path);
newButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
//Do Stuff.
fireDoneHandlingButtonClick();
} catch (Exception e) {
}
}
});
}
And then have a way of handling this method in the parent activity. Should I be thinking of events, or should I be thinking of a way to add a method as an argument to the addBtn method from the activity, which can be fired from inside the click-listener?
Look at How To Implement Your Own Listener in Android or Fire and Forget Messages (events) in Android