I am about to write a little quiz app. I have 4 Buttons which represent the possibilities of answers. When one button is depressed It should blink red or green to indicate right or wrong. So far no problem.
But the next step should be to slide out the old question and to slide in the new one. This should initiate by a touch of the user anywhere on the screen.
How can I achieve this? Which methods I have to use?
Code:
#Override
public void onClick(View v) {
Button button = (Button)findViewById(v.getId());
if(button.getText().equals(quizWorld.getCurrentQuestion().getRightAnswer())){
//right
}else{
//wrong
}
}
#Override
public boolean onTouchEvent(MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_UP:
//Game Over?
if(quizWorld.swapQuestion()==false){
viewFlipper.setDisplayedChild(1);
}
break;
}
return true;
}
Just for Explanation: In the onClick the Programm compares the text on the button with the saved answer and initiate the blinking (not coded yet). And on the onTouchEvent it changes the question.
And now: onTouchEvent should be only "activated" when the Programm has just finished the checking process. After that the Programm shouldn't react on OnEventTouch.
If you don't need to handle the touch event, just return false onTouchEvent and let the touch pass through.
#Override
public boolean onTouchEvent(MotionEvent event){
if (I don't need this touch)
{
return false;
}
switch(event.getAction()){
case MotionEvent.ACTION_UP:
//Game Over?
if(quizWorld.swapQuestion()==false){
viewFlipper.setDisplayedChild(1);
}
break;
}
return true;
}
Related
I'm banging my head with this problem which probably is simple but since I'm new to this topic I somehow haven't been able to figure it out yet.
I've successfully implemented dragging a view with onTouch method. I've also successfully implemented onLongClick and onClick methods. But both of these functionalities were implemented separately.
The problem, like the title says is when I want to join these functionalities. I want the onTouch method to be called when a user long clicks a view and I want a new activity to start when a user clicks a view.
Here is the pseudo code:
public class Website extends AppCompatActivity implements View.OnTouchListener{
TextView longpress;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_website);
longpress = (TextView) findViewById(R.id.longpress);
longpress.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view){
//I don't really know how to do this part
onTouch(View view, Motion Event event);
return true;
}
});
longpress.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Code for new activity comes here (I know how to do this part)
}
});
}
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction(){
case MotionEvent.ACTION_DOWN:
//Save initial coordinates of view <-- view.getX(), view.getY()
break;
case MotionEvent.ACTION_MOVE:
//Calculate dX and dY and setX and Y of the view (move view)
break;
case MotionEvent.ACTION_UP:
//If view is certain distance away from initial points do something
break;
}
}
}
Like I said, onTouch works on itself if I don't try to call it from onLongClick method. If I try to call onTouch(View view, MotionEvent event) from onLongClick method the problem occurs because onLongClick only receives one out of two arguments onTouch method should receive (onLongClick only receives view argument but it should also receive event argument).
Maybe I'm trying to do this in a totally wrong way but I have been looking at some documentation e.g. https://developer.android.com/training/gestures/ but still won't get an idea what to do.
(I would like to have a similar functionality to notifications on android phones)
So I've come to a solution which might or might not be a good one but for now it serves my functionality. If someone has a better solution and thinks mine is bad in some way please say so.
Here is the code:
boolean flag;
public boolean onTouch(View view, MotionEvent event){
int action = event.getAction() & MotionEvent.ACTION_MASK;
if(action == MotionEvent.ACTION_DOWN){
//do something on a down press
flag = true;
return true;
}
if(action == MotionEvent.ACTION_UP && flag == true){
//do something if we move finger away from screen we
//didn't move the view first
return true;
}
if(action == MotionEvent.ACTION_UP && flag == false){
//do something if we move finger away from screen and we moved
//the view before we moved the finger away from screen
}
if(action == MotionEvent.ACTION_MOVE){
//do something when moving the view
flag = false;
}
I am creating a custom UI component where user can upload an image of a map and then user can specify certain locations and Display a pin. First upon image of the map loaded user will be able to pan and zoom to any position. for this I used following example which works like a charm.
TouchImageView
Next part was to add a pin when it is long pressed on the image. For this I used an Absolute Layout over the map image and added an OnTouchListener and Returned false at the end for the touch listener on ImageView to work as well. I implemented the OnTouchListener As below
absoluteLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
count=-1;// reset count. Thredd will start as 0
released = false;// Touch long press is started
final Handler handler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
while(!released){//Stop count if touch released
try {
count++;// Count every 200 ms
Thread.sleep(200);
if(count>10){ //if pressed for 200*10= 2 seconds
released=true;
handler.post(new Runnable() {
#Override
public void run() {
addPin((int) event.getX(), (int) event.getY());//Calling the method to add the pin
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
break;
case MotionEvent.ACTION_UP:
released=true;// touch is released
break;
case MotionEvent.ACTION_MOVE:
released=true; // touch is a movement therefore release long press
break;
}
return false;
}
});
But the issue here is With case :"MotionEvent.ACTION_MOVE" it always make the long press released. But without it if user is trying to pan or zoom the imageView it will add a pin.
How can i detect if there is a movement in the touch and ignore when user is zooming or panning the map?
Thanks in advance.
You can specify a minimum amount of movement to be sure if it's an accidental movement or not, like it's explained here
I've implemented a grid of buttons using a grid layout. I'm trying to allow for a single swipe to activate multiple buttons. When the user touches any one of the buttons, I call a function with the specific button pressed to take the according action. But, currently, I can only activate one button per touch. Multi-touch works, but not a single swipe. The problem is that while the onTouch function is called continuously the view object that I use to determine the button pressed is only updated on the initial touch. What I need to do is get the id's of all of the buttons that were swiped across.
Thanks.
#Override
public boolean onTouch(View v, MotionEvent event)
{
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
switch (v.getId())
{
case R.id.padZeroZero:
padTouch(0,0);
break;
case R.id.padZeroOne:
padTouch(0,1);
break;
case R.id.padZeroTwo:
padTouch(0,2);
break;
//There's a lot more cases (it's a 9x8 grid), but they all do the same thing.
}
}
return false;
}
I'm very new to android development and I just started to study. So sorry for this simple question.
When I long press button it will pass string successfully but when I release button click it does not pass second string... please let me know where is the problem.
Hii you can use what #pervez other wise you can use ToggleButton for example you can use like this.
ToggleButton myButton=(ToggleButton)findViewById(R.id.myToggle);
myButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
if(myButton.isChecked()){
doYourMethod1();
}else{
doYourMethod2();
}
});
Long press only fires once use onTouchListener if you want two events to be fired one at ACTION_DOWN and other at ACTION_UP.
EDIT: Use this only if you want two events to be fired one when the user touch the view and other when user lifts the finger from the view.The code can be like this...
textView.setOnTouchListener(new onTouchListener)
{
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d("DOWN","DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d("MOVE","MOVE");
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
How can I make the screen as a button?
I want to make a game where you click on the hole screen not just a button in Xml.
I want this code to be in Java not Xml.
Anyone with a tip out there?
Your question is very generic, perhaps this will help you to get started. If you tell us what you're trying to accomplish with this 'Screen Button' perhaps it would be easier for us to point you in the right direction.
You need to implement the following method in your View :
public boolean onTouchEvent(MotionEvent event) {}
Then you'll be able to grab the coordinates were the screen was touched with :
event.getX() and event.getY()
EDIT : You can detect wether the action is a click or release by checking the MotionEvent like so :
#Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
// Do click work here ...
break;
case MotionEvent.ACTION_UP:
// Do release work here ...
break;
}
return super.onTouchEvent(event);
}
Other type of actions can also be checked via other MotionEvent :
MotionEvent.ACTION_CANCEL
MotionEvent.ACTION_MOVE