i managed to do that by adding "actionDownChecker" on every RadioButton.
private View.OnTouchListener actionDownChecker = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
mTabbar.check(view.getId());
}
return true;
}
};
public void add(int radioButtonResourceId, Class<? extends Fragment> contentClass) {
mContent.put(radioButtonResourceId, new TabInfo(radioButtonResourceId, contentClass));
//hack to check radio button on ACTION_DOWN, not UP!
mTabbar.findViewById(radioButtonResourceId).setOnTouchListener(actionDownChecker);
}
Is there another more elegant way to do that?
The only other way I would know how to do it, is to physically change the code in the Listener. I'm not the smartest guy when it comes to developing for android, but the default constructor sets the boxes attached to RadioButtons to check on release.
Unless someone else knows something I don't, you either have to modify the listener class, or modify the entry after the listener has been evoked (by adding your actionDownCheker). Personally if it was me, i would just copy and paste your actionDownChecker, that way your listener class isn't messed up for your next project.
I'm sure there are some applications for it, but I'm not sure why you would want to do that. If someone presses down on the wrong button, they can just move off the button before it is released to cancel the press.
Related
I want to drag my PopUpWindows. So I tried to implement the demo App to drag a simple PopUp Window from here:
https://questdot.com/android-popup-floating-window-tutorial/
The problem arises when I add the listener to the view
popVideospeedView.setOnTouchListener(new View.OnTouchListener():
then View.OnTouchListener() appears with a red underline and warns:
Class 'Anonymous class derived from OnTouchListener' must either be declared abstract or implement abstract method 'onTouch(View, MotionEvent)' in 'OnTouchListener'
it also rejects the #Override saying it cannot be overriden, and also says it cannot Resolve MotionEvent
Also, the library android.view.View.OnTouchListener from:
import android.view.View.OnTouchListener; is unused
My code is very similar to the code that runs from the link quoted, and their mini App compiles and works fine in the Android Studio SDK. But my code rejects
the implementation of the touch listener. What could I be doing wrong?
The code for my PopUp Window implementation is:
public void functionVideospeedPopupWindow() {
try {
LayoutInflater popVideospeedLayoutInflater = (LayoutInflater) LMMoviesMainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popVideospeedView = popVideospeedLayoutInflater.inflate(R.layout.popvideospeed, null);
final PopupWindow popVideospeedPopupWindow = new PopupWindow(popVideospeedView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
popVideospeedPopupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorStone)));
popVideospeedPopupWindow.setOutsideTouchable(false);
popVideospeedPopupWindow.setFocusable(false);
Button videospeedCloseButton = (Button) popVideospeedView.findViewById(R.id.popvideospeed_closeButton);
popVideospeedPopupWindow.showAtLocation(popVideospeedView, Gravity.CENTER, 0, 0); // 8:Margin to the left?. 18 comienza el texto utube
popVideospeedView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// event code...
return true;
}
});
videospeedCloseButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
popVideospeedPopupWindow.dismiss();
}
});
} catch (Exception e){}
} // End of functionVideospeedPopupWindow()
The only difference that I notice with the example that runs is that their PopUp code is declared within the onCreate sector of the Class, while my PopUp Window code is located within a method that is started by the user later in the App. Is that a relevant difference? if so, then how or why?
Finally, in case that I add: OnTouchListener (or is it View.OnTouchListener?) in: implements of the Class, as in:
public Class ...etc. ... implements OnTouchListener, it underlines in red and warns:
Class 'MyMainActivity' must either be declared abstract or implement abstract method 'onTouch(View, MotionEvent)' in 'OnTouchListener'
I know that to implement the OnTouchListener interface, I should add a structure like:
public boolean onTouch(View v, MotionEvent event) {...
but how to proceed with a PopUp Window with an onTouch declared outside of the PopUp method?
Thanks a lot for any ideas and for your interest.
Solved! It turns out that Android Studio wasn't adding the library for the MotionEvent when I tried with Alt+Enter. So I manually added the library: import android.view.MotionEvent; and now View.OnTouchListener doesn't generate any warning. Thanks a lot for your help, and I hope this case can help somebody else to overcome this silly bug. Cheers
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.
This is what I'm trying to do. I have 3 text boxes, a submit button and image on the screen. I want the image to go away if any of the text boxes are touched to input data for the login credentials. The reason is that the soft keyboard pushes the image up into the text boxes when the user tries to input anything. I tried the setOnClickListener but it appears that takes two clicks into the field using the AVD. So I'm trying to use OnTouchListener and it's not going well.
It is asking me to remove the qualifier because of an 'expected Class or package'. It wants to remove the login from this line of code.
login.OnTouchListener(new View.OnTouchListener(){
It happens with the other two text boxes which are name 'phone' and 'password'.
Next, if I remove the qualifier then it complains about a Method call expected and wants to do an insert which changes the code to this.
new View.OnTouchListener(new View.OnTouchListener() {
Then it complains about 'OnTouchListener' is abstract; cannot be instantiated' and wants to implement a method, which gives an error at the end about an expected ). When this is added the whole cycle starts over with the same error messages.
Here's the pieces that I believe are important.
The import statement
import android.view.View.OnClickListener;
The variables that I'm using for the OnTouchListner.
final EditText phone = (EditText) findViewById(R.id.phone1);
final EditText login = (EditText) findViewById(R.id.uname);
final EditText pass = (EditText) findViewById(R.id.password);
The onTouchListener that I need so I can make the image invisible and not overlap the text box input.
login.OnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
image.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
The method signature you're looking for is login.setOnTouchListener(new View.OnTouchListener() { ... }
The reason this is happening is because OnTouchListener is a class, not a method.
The correct way to add an OnTouchListener is to call
setOnTouchListener( listener )
So, for your code,
login.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
image.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
I am pretty new to android and am trying to build an app where the user can draw a letter, press a button that connects a service that reads it, and then the letter is displayed back to them.
This is my main layout:
!(http://i58.photobucket.com/albums/g271/billmoney3/layout_zps71eb45ca.jpg)
I want the user to be able to draw in the blue area. I made the blue area a custom view called InnerDrawingView. I need help on how to organize the views and the OnTouchListener.
Right now I have this java code:
public class DoodleActivity extends Activity {
Button confirmButton;
EditText drawingResult;
InnerDrawingView innerView;
// on create
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doodle); // main layout
// where the drawing happens
innerView = (InnerDrawingView) findViewById(R.id.innerDrawingView1);
innerView.setOnTouchListener(handleTouch);
...
...
// handle the touching of the inner view
private OnTouchListener handleTouch = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
drawingResult.setText("O RLY?!"); // just a test
return true;
}
}; // end of class
Is this the correct way to go about it? What kind of touch listener do I put in the InnerDrawingView class? Can I just call: innerView.onTouch() from inside my main activity's onTouch()? Or the onDraw() method? If someone could direct me to a good paint tutorial also that would help me a lot.
Thanks for the input.
You need to pull x and y from the "event" object. Then the code depends on your needs. You can connect a new point with the previous one (to make a segment) or just put it in the list. Pseudo-code:
public boolean onTouch(View v, MotionEvent event) {
innerView.submitNewPoint(event.getX()), event.getY());
return true;
}
If you want to handle multi-touch events you need to retrieve points count ( event.getPointerCount() ) and do something with coords from event.getX(i)/event.getY(i) (i - index of multi-touch event point).
Of cause you need to implement drawing of the points/segments/? list in the InnerDrawingView.
P.S. do not forget to make you fields private ;)
I want my activity (a normal activity with a digital clock at the center of it) to close when I tap it anywhere. Is there a sort of Activity.setOnClickListener or a way to do it?
Yes on an Activity you override the existing handlers, on controls you add touch listeners, and [activityinstance].finish() gracefully closes your app.
#Override
public boolean onTouchEvent(MotionEvent event) {
this.finish();
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Write Code Goes Here means >> this.finish();
return false;
}
This will help you.
use onUserInteraction() for finishing your activity this method called Called whenever a key, touch, or trackball event is dispatched to the activity. as doc says