I have class "myButton" that inherits from class "Button". Inside implemented method "onTouch", which works if you press the button. And I need to perform actions when you click wherever outside the button.
Is there any way to verify that the touch was made outside the button?
I came up with the idea to add a touch check to the View (full screen). But in this case, if you click on the button, two events "onTouch" will be activated: inside my class "myButton" and clicking on View.
If you want to handle the push down and release on a button you can just do
Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
break; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL
// RELEASED
break; // if you want to handle the touch event
}
return false;
}
});
this will handle if a button is pushed down and if a button is released and if you just want pushed you can change accordingly
You have kotlin and java both tagged this is java so if you want it in kotlin just let me know
ps im returning false because if you dont you will circumvent the button's regular touch processing. Which means you will loose the visual effects of pressing the button down and the touch ripple. Also, Button#isPressed() will return false while the button is actually pressed.
Related
I am doing a calculator app with swing. I have changed the background color of JButton but when clicked its color changes to a blueish color. Can I set a color manually on mouse over and on click. like the windows 10 default calculator?
Try using this:
buttonName.setFocusPainted(false);
This will get rid of the default border when you click on a button.
To make the button do stuff ie change it's background you can implement the MouseListener interface for it, but I think the best thing for you would be to read about event handling in swing :)
Here's a link
You need to first detect mouse motion over the button using an Mouse listener .You can use an implemented version of it called the MouseAdapter
button.addMouseMotionListener(new MouseAdapter()//To
detect mouse motion
{
#override
public void mouseEntered(MouseEvent m)//called when mouse
enters first time into the button
{
button.setBackground(Color.blue);
}
#override
public void mouseClicked(MouseEvent m)//called when mouse is clicked[pressed and then released]
{
button.setBackground(//whatever you want);
}
});
Note for mouse clicks you can just change the color inside your action listener
I have an activity and 10 fragments which I draw on activity. All fragments on activity need to have "drag to refresh". I add Swipe Refresh Layout to create it. Now when I click on the switch, all is fine; but when I try to drag it from one stage to another, swipe to refresh appears, and the switch is just staying in the same position. So how can I block the refreshing of the swipe refresh layout on time when the user drags the switch from one position to another?
I tried to make it like this:
onOffSwitchSettings.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
((MainActivity) getActivity()).swipeContainer.setRefreshing(false);
return false;
}
});
But how can I turn it on after?
According to SwipeRefreshLayout reference:
The SwipeRefreshLayout will notify the listener each and every time
the gesture is completed again; the listener is responsible for
correctly determining when to actually initiate a refresh of its
content. If the listener determines there should not be a refresh, it
must call setRefreshing(false) to cancel any visual indication of a
refresh. If an activity wishes to show just the progress animation, it
should call setRefreshing(true). To disable the gesture and progress
animation, call setEnabled(false) on the view.
You've forgotten to use ((MainActivity)getActivity()).swipeContainer.setEnabled(false) method.
Another problem is that using above you're going to disable SRL permanently, so you need to enable it again when the action is right, I mean there's no more dragging.
Hope it will help
I have a ProgressMonitorDialog object which contains a cancel button. I want to disable the initial focus of the cancel button. The reason I want to remove the initial focus from the button is that a user may accidentally hit a key on the keyboard while a batch operation is in progress and cancel the whole operation. If a user wants to cancel an operation I would prefer that they press the tab key and manually set the focus, or click on the cancel button with a mouse.
From what I can tell, there is no easy way to do this. I can create another button on the ProgressMonitorDialog that does nothing and have that take focus, but that's an ugly workaround. Especially since the button has to be visible or the focus will shift to the cancel button. I have also tried overriding the method that creates the cancel button and bypassing the shell.setDefaultButton() method but no luck.
Any clues/suggestions?
If you are extending Dialog class, then you can do the following, override createButtonsForButtonBar(Composite) method, and pass in false as default button argument on the button which you don't want the initial focus on.
#Override
protected void createButtonsForButtonBar(Composite parent) {
// create OK and Cancel buttons by default
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
Right now I have:
panel.getZoomButton().addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ACtionEvent e)
{
zoom();
}
}
This is called every time the zoom button is pressed. How can I change it so that zoom() will be continuously called if the zoom button is being held down?
You will need to use a MouseListener and override the mousePressed() method. There you can use a Timer or something similar to measure the time, the button has been pressed, in order to calculate your zoom.
Perhaps this question helps you with that: Java MouseEvent, check if pressed down
use Swing Action (most scallable abstraction) instead of ActionListener, there you can to set isEnabled, to switch to false value until all events are done
or add Swing Timer for reset Boolean to true value
there is possible to setMultiClickThreshhold(long threshhold), but is aplicable only for MouseEvents, this methods doesn't react to KeyBindings (ENTER and TAB) firing from Keyboard
I would like to capture all motion events from the screen using a listener for example if i do a swipe on a screen from top to bottom there will be a touch down, touch move and a touch up. Since this touch will be over multiple views example linear view, buttons and text fields i tried to attach on touch listeners to all views but i would get bad data for example i would get touch up without touch downs and so on. Please advise a way that will achieve this.
A good way to handle this is to attach the OnTouchListener to the parent ViewGroup (layout) of all your views.
For example a RelativeLayout that have multiple views (linear view, buttons and text fields) as you mention in your question. You can do like that:
RelativeLayout currentView = (RelativeLayout) findViewById( R.id.MyRelativeLayout );
currentView.addView(aSubView);
currentView.addView(aSubButton);
currentView.addView(aSubTextView);
currentView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Here you will receive all the motion event.
return false;
}
});
You can override the dispatchTouchEvent(MotionEvent ev) method in you activity.
It's called before the MotionEvent is forwarded to the different Activity Views.
You can then do what you want with it, forward them, consume them...