I may be asking a basic question, but to be honest, I have no real developement or code knowledge. I've been requested to make a prototybe of some basic app, that is supposed mainly to be buttons on screens, activable or disactivable.
I've been coding this on Android Studio 3.0, I (hardly) managed to place PNGs files on the screen, making it looking like a button. When I was pressing it, nothing happened of course, so I searched there and there how to make it change when pressed This worked
casedanger1.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
casedanger1.setImageResource(R.drawable.casedanger1slct);
break;
case MotionEvent.ACTION_UP :
casedanger1.setImageResource(R.drawable.casedanger1);
break;
}
return false;
}
});
But when I try to disable again the button, it doesn't revert to the standard image (casedanger1)
How should I proceed ? I've been searching for days without a real solution. I have tried to make it a toggle button, which works, but makes the image bigger, and thus cropped.
Any hints that could help ? I'm really desperated, this is not something I'm familiar with.
Thank you
-Pliskin
I think you were close. Try this
casedanger1.setOnTouchListener(new View.OnTouchListener(){
// track if the image is selected or not
boolean isSelected = false;
public boolean onTouch(View v, MotionEvent event) {
if(isSelected) {
casedanger1.setImageResource(R.drawable.casedanger1slct);
} else {
casedanger1.setImageResource(R.drawable.casedanger1);
}
// toggle the boolean
isSelected = !isSelected;
return false;
}
});
Related
As the title says, is there a method in libGDX which returns the touch pressure (similar to MotionEvent.getPressure() in Android) ?
Or is there any other way I can get touch pressure in my game which works on both android and iOS(using RoboVM)?
I can't help you with the iOS part
but for android there is a work around
you can do something like this to get the pressure
View view = initializeForView(game, config);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
getInput().onTouch(v, event);
game.setPressure(event.getPressure());
Log.v("log", "hooked " + event.getPressure());
return true;
}
});
and still have the complete event handling of libgdx
I am having this problem when I try to change the source/resource of a image(a button) when is pressed and released.
Btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
/*if(event.getAction() == MotionEvent.ACTION_DOWN) {
ImageBtn.setImageResource(R.drawable.btn1);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ImageBtn.setImageResource(R.drawable.btn0);
}*/
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
}
return false;
}
});
This is the code, I tried what is in the comment too, but no result. I do have to mention that the button changes its resource just when touching it, but after releasing it, it doesn't change.
The button listener is on a relative layout(also defined as a relative layout in the xml code), in java however is defined as a button.
The image of the button doesn't change from when you touch it to when you release it because you are setting the same background resource.
Both cases contain:
ImageBtn.setBackgroundResource(R.drawable.btn1);
Set one of them to a different image.
so pretty much i made a game that if you miss the target on screen the game is over, it works on my computer because it is a single click in a location but doesn't register on android because i feel like my finger is dragging by accident. i want the game to be over at the end of the drag in libgdx and am not sure how to go about it.
#Override
public boolean touchDragged(int screenX, int screenY, int pointer)
{
game.state = (GameState.GAMEOVER);
return false;
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
// This is what happens on the click down...
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
// this is what happens on the click release
return true;
}
return false;
}});
This would process the click if any of their motion is on the object and is based on release to vague first-engagement will be refined to further accuracy where they choose to remove their finger.
You just can't, because on android the touch drag method is called almost everytime, because your finger is still moving a little bit.. with the mouse you can keep it in the same place without problem..
But why you game should stop when the touch dragg is called?
To see if you miss your target on screen use a collision detection..
I'm trying to create a view programmatically, and change its background according to its click state.
I know I can do this with xml drawables, but I want to learn to program it too.
But if I press on my view, then slide my finger, the view gets stuck in pressed state. (white = 0xaaffffff background)
My code is this :
Edit:
I solved the problem. Here is the working code :
mainContainer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action==MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(0xaaffffff);
return false;
}
if(action==MotionEvent.ACTION_MOVE)
{
}
if(action==MotionEvent.ACTION_CANCEL)
{
v.setBackgroundColor(0x00ffffff);
}
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}
return false;
}
});
As you see, I tried returning true wherever I want onTouch() to be called again. But apparently there is something I do not understand here.
What am I doing wrong ?
Thanks for any help.
What about returning true in your ACTION_UP aswell?
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}
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.