How to reduce the click area of an Button? - java

I have a gridview that is filled with buttons that looks like this:
right now if I click anywhere on the button, it will be pressed. But I want to the button to only be pressed when it's clicked in the light gray area, not the dark gray or white area on the top and left sides. I tried using setPadding(int,int,int,int) and setPaddingRelative(int,int,int,int) but neither of them makes any effect. How do I fix this?
Edit: the button is 50x50 and is already extended from the Button Class

Make a custom Button view inherit from Button class and override onDraw method with custom code to do that.
You can override touch methods to detect when button is pressed and get the x and y coords to remove cases when button must not be pressed.

Override onTouchEvent in your custom button:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (isInGrayArea(event.getX(), event.getY())) {
return super.onTouchEvent(event);
}
return false;
}
event.getX() and event.getY() is pixels coordinates, you will need to convert them into dp, I suppose.

Related

Getting a mouse position with JavaFX and Event

I'm trying to get the position of a mouse click in my JavaFX application. I have an EventHandler as so:
EventHandler<Event> mouseHandler = new EventHandler<Event>() {
#Override
public void handle(Event event) {
String eType = event.getEventType().toString();
if(eType.equals("MOUSE_PRESSED")){
//mouse position
}
}
};
which is called on mouse press, release and drag. I'm unable to get the position of the mouse from Event and when I try and change EventHandler<Event> to EventHandler<MouseEvent> I get the message Type paremeter 'java.awt.event.MouseEvent' is not within it's bounds; should extend to 'javafx.event.Event'.
How can I move this over to MouseEvents?
You need to use the JavaFx MouseEvent.
Currently you are trying to use the Java.awt MouseEvent
Try importing only Java libraries from Javafx and not awt so you can avoid having the wrong type.
Once you fix that, the methods getX() and getY() should give you the position. (Or depending on what you want that position relative to, getScreenX() or getSceneX() might be what you want.)

Making "toggle buttons" with LibGDX

I'm trying to make sort of toggle buttons with LibGDX, so I searched how I could do them, and I found the ToggleButton class, but I suppose it's old since I don't have it in the last build...
So I'm trying to do them this way:
final TextButton button = new TextButton(weapon.getName(), skin2, "buy");
button.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
if(button.isChecked()){
button.setChecked(false);
System.out.println("unchecked");
} else {
button.setChecked(true);
System.out.println("checked");
}
}
});
Actually, it keeps telling me unchecked, as if my button was always unchecked, so the setChecked method doesn't seems to word ...
I tried the toggle method, and it doesn't help at all, and I didn't found any other solution ...
So i was wondering if you had any idea of how I should do this !
Thanks for your help ! :)
The button is toggled automatically when clicked, you don't have to add another listener to do it manually.
So the reason it only prints "unchecked" is because the Button checks itself when clicked, and then your listener is called, which just unchecks it immediately.

Why does Activity not have a onClick event handler?

I'm trying to have my activity respond to a single click but I cannot find a way to implement this.
I can use onTouchEvent override that method and have my logic run there but onClick is not possible?
What is the reason behind this and what could I do to get the desired effect that I want?
As requested: what I currently have
#Override
public void onClick(View event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
pixelColor = Color.RED;
PixelParticle pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
setContentView(pp);
}
this isn't working - I implement OnClickListener and override the above method, the canvas never draws what I want it to draw.
Touch down + touch up of the same point in a short time is a onClick(Actually onClick event in the android source code is really made in this way).
Because the onTouchEvent(in activity) is made for you to check touch event of the whole screen.But onClick is designed to check the click event on a view(not the whole screen).Touch event is the original event,click isnĀ“t.
If you want to check whether your screen is clicked ,just use the onClick method of your layout(give it an id myLayout).
LinearLayout layout = findViewById(R.id.myLayout);
layout.setOnClickListener(xxx);

How to change SWT Button background color or make it transparent

I have a transparent image on a Button (no text), which is placed on a Composite. Since the Composite is white (created with FormToolkit#createComposite(parent, SWT.NONE)), I'd like the Button background to be the same color. How do I do it?
The Label does the trick, but doesn't have the shadows like Button does when I'm clicking on it..
The background color of a Button is determined by the OS. In fact, the documentation for Control.setBackground() states that:
Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.
That said, one possible way to circumvent this is to override the paint event as shown here: Changing org.eclipse.swt.widgets background color in Windows. When I tried this out the results were a bit wonky.
The safest and most consistent approach would be to use a label like in your second image, but have different images to display on various mouse events to emulate how a button behaves.
Those images can emulate the shadow just by adding whatever shape of shadow you want to the image itself. That shadow can also change for each image to give the impression that the button is being pressed or not.
For example, I'm thinking something along the lines of:
public class MyButton {
private final Label buttonLabel;
public MyButton(final Composite parent, final Theme theme) {
buttonLabel = new Label(parent, SWT.NONE);
buttonLabel.setImage(theme.getUpImage());
buttonLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonPressedImage());
}
#Override
public void mouseUp(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonUpImage());
}
});
buttonLabel.addMouseTrackListener(new MouseTrackAdapter() {
#Override
public void mouseEnter(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonHoverImage());
}
#Override
public void mouseExit(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonUpImage());
}
});
}
}
Where the Theme just has all of the images already conveniently loaded.
You'll also need to make sure that the parent Composite has the background mode set to force its background color:
parent.setBackgroundMode(SWT.INHERIT_FORCE);
Obviously the drawback to this approach is that you have to handle the mouse click logic yourself (ie. mouseDown isn't really clicked until the mouse is released, so you'll have to handle the state of the button in each listener method).

Move image using keyboard - Java

I wanted to move my image using keyboard arrow keys. when I pressed the arrow keys it moves accordingly to the direction. However, I need to click on the image before able to move it. May I know how to edit the code such that I do not need to click on the image before able to move it? I would also like to know how to make the image appear from the left once it reaches right and vice versa.
My codes are :
Collect.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_LEFT)
{
Collect.setLocation(Collect.getX()-8,Collect.getY());
repaint();
}
if(ke.getKeyCode() == KeyEvent.VK_RIGHT)
{
Collect.setLocation(Collect.getX()+8,Collect.getY());
repaint();
}
}
});
Collect.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
if(me.getClickCount() == 1)
{
boolean dd = Collect.isOptimizedDrawingEnabled();
boolean ff = Collect.requestFocusInWindow();
repaint();
}
}
});
You have look at KeyBindings, otherwise you have to JComponent#setFocusable() that nest the Image, example for Moving Image
Collect.requestFocusInWindow();
requestFocusInWindow()..
Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window.
Make sure to call that only after the main window is visible and has the focus.
KeyListeners only work when the component which has the listener has focus. You are giving focus to what appears to be Collect by clicking on it. Then the listener works. You can add the listener to other things or force focus to remain on something like the outer frame by using a focus listener to regain focus whenever it is lost.

Categories

Resources