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.
Related
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.
Do i necessairly need to change my recyclerView to expandableRecyclerView for doing animation of expand + animation of arrow drop down -> arrow up? Is there any way to implement simple slide down animation?
On button drop down arrow click i change visibility of textView and imageView from GONE to VISIBLE (also changing src of arrow button in code)
From:
To:
Here is some code just in case
taskViewHolder.showDesc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(taskViewHolder.description.getVisibility()==View.GONE){
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_up_arrow));
taskViewHolder.imgDesc.setVisibility(View.VISIBLE);
taskViewHolder.description.setText(task.getDescription());
taskViewHolder.description.setVisibility(View.VISIBLE);
}
else {
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_down_arrow));
taskViewHolder.description.setVisibility(View.GONE);
taskViewHolder.imgDesc.setVisibility(View.GONE);
}
}
});
Have a Look on my old project, I had to do something like you have told:
https://bitbucket.org/MauzerTheCat/music-example-app/src/master/
I am creating an application that uses scroll view, and can not figure out how to implement a button that when pressed would take me from the bottom of a page after scrolling, to the top of the page.
![bottom of the page]https://imgur.com/a/YLCJNAs
![top of the page after button press]https://imgur.com/a/XXJSS2d
it's done like this considering that your ScrollView is SV and your button is BTN :
BTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SV.fullScroll(ScrollView.FOCUS_UP);
}
});
Try putting the following line in the onClick for that bottom button
yourScrollView.fullScroll(ScrollView.FOCUS_UP);
I am new to android programming and trying to develop TIC TAC TOE game. I have created gameLogic() method and the problem is this that it is not working as it is expected to do, means on click of ImageView none of the images is getting displayed. Any help will be highly appreciated.
This is my code:
public class MainActivity extends AppCompatActivity {
public void gameLogic(View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public void onClick (View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500)
Try changing the class name to onClick and go to XML the page where you designs the button and declare onClick method in the image view button.
All you have to do is let the design page know where the code is by providing a common name to look for which is on click.
Also you have to add an onClick listener which listens for the button to click as the code should know which button it is linked to.
So declare a setonclicklistener method on the button Id and you're set to go.
Also write the code within the override after the setting of the layout.
Hope you understood :)
If you're new try head first Android it'll be really helpful I know how struggling it is during the initial days.
TL;DR I have a RecyclerView of EditTexts. When the user is focused on EditText #1 and taps on EditText #2, I want EditText #2 to get focus but I don't want the ReyclerView to scroll. How can I achieve this?
I'm trying to work with a RecyclerView populated with a bunch of EditTexts. When I'm focused on one EditText and I click on another, the RecyclerView scrolls so that the second is at the top of the screen. I want to disable this auto-scrolling from the RecyclerView, but I still want the user to be able to scroll, and I still want the second EditText to be focused on so the user can start typing. How can I achieve this?
I've already tried the following solutions:
https://stackoverflow.com/a/8101614/4077294, but with a RecyclerView.OnItemTouchListener. I called recyclerView.requestFocusFromTouch in onInterceptTouchEvent.
Behavior: Scrolled to the top of the tapped EditText all the time.
Clearing the focus from any EditText whenever it was focused on, via
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, bool hasFocus) {
if (hasFocus) {
v.clearFocus();
}
}
});
Behavior: The keyboard never showed up, and the RecyclerView still scrolled to the top.
Disabling scrolling altogether as in this question is not acceptable because I still want the user to be able to scroll.
I ended up with this solution from #pskink:
recylerView.setLayoutManager(new LinearLayoutManager(this) {
#Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
return false;
}
});
It seems to work perfectly, but #pskink has mentioned that this could have problems when using arrow keys. He's posted another solution here: https://pastebin.com/8JLSMkF7. If you have problems with the above solution, you may try the alternative solution at the link. For now, I'm sticking with the one I just posted here.
UPDATE
Since support-library v25.3.0 you should also override another requestChildRectangleOnScreen method in LayoutManager:
#Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
return false;
}
RecyclerView will scroll to focused item.
Try recyclerView.setFocusable(false).
This worked for me.