Solved

Issue with OnTouchListener

Posted on 2014-12-15
6
241 Views
Last Modified: 2015-01-05
Ok, I have 3 TextViews - When the user selects the incorrect answer, the value of the textview will disappear.

For testing purposes, I have the 2nd textview with the answer in it, when I click on it it does not go away. Any clue why??

Just a couple notes:

sum is an int
I did load the listener in the savedInstanceState

	public OnTouchListener answerListener = new OnTouchListener() {	
	    String s1 = String.valueOf(sum);
		public boolean onTouch(View v, MotionEvent event) {			
	        if (event.getAction() == MotionEvent.ACTION_DOWN)
	        {
	        	if (answer2TextView.equals(s1))
	        		((TextView)v).setVisibility(TextView.INVISIBLE);
	        	return true;
	        }
			return true;
		}
		};

Open in new window

0
Comment
Question by:Computer Guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 40500562
You need an action_cancel or an action_up event. To quote the developer site

A pointer id remains valid until the pointer eventually goes up (indicated by ACTION_UP or ACTION_POINTER_UP) or when the gesture is canceled (indicated by ACTION_CANCEL).

http://developer.android.com/reference/android/view/MotionEvent.html

Change the if (event.getAction) to a switch

switch (event.getAction())       
{
    case MotionEvent.ACTION_DOWN:
    
    break;
                
    case MotionEvent.ACTION_UP:  
    
    break;
}

Open in new window

0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40500595
Do I have to have an action Down?

Is this the correct Syntax for 3 textViews?

public OnTouchListener answerListener = new OnTouchListener() {      
          String s1 = String.valueOf(sum);
            public boolean onTouch(View v, MotionEvent event) {                  
switch (event.getAction())      
{
    case MotionEvent.ACTION_DOWN:
                    if (answer2TextView.equals(s1))
                          ((TextView)v).setVisibility(TextView.INVISIBLE);
else if (answer1TextView.equals(s1))
                          ((TextView)v).setVisibility(TextView.INVISIBLE);
                    return true;
if (answer3TextView.equals(s1))
                          ((TextView)v).setVisibility(TextView.INVISIBLE);
   
    break;
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 40500644
The action_down is to report the movement of the user to the listener class.
An if statment without braces only acts on the following line, so your first if() will always return true.
Also it appears that you will set the View v to invisible regardless of which one is selected, like this.
public boolean onTouch(View v, MotionEvent event) 
{                 
    switch (event.getAction())       
    {
        case MotionEvent.ACTION_DOWN:
         
             if (answer2TextView.equals(s1)
                || answer1TextView.equals(s1)
                || answer3TextView.equals(s1))
             {
                   ((TextView)v).setVisibility(TextView.INVISIBLE);
             }   
         break;
    }
}

Open in new window


 I think you may have to rewrite your class.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 3

Author Comment

by:Computer Guy
ID: 40501287
Rewrite my class like the example you posted?
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40501669
I essentially did this and it is not making the text view invisible on click.

import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	private TextView equasionTextView;
	private TextView answer1TextView;
	private TextView answer2TextView;
	private TextView answer3TextView;
	private TextView selectAnswerTextView;
	private RadioButton addRadio;
	private RadioButton subRadio;
	private RadioGroup radioGroup;
	private Button exitButton;
	
	int sum; // sum

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		radioGroup = (RadioGroup) findViewById(R.id.addSubRadioGroup);
		addRadio = (RadioButton) findViewById(R.id.addRadio);
		subRadio = (RadioButton) findViewById(R.id.subRadio);
		equasionTextView = (TextView) findViewById(R.id.equasionTextView);
		selectAnswerTextView = (TextView) findViewById(R.id.selectAnswerTextView);
		exitButton = (Button) findViewById(R.id.exitB);
		radioGroup.setOnCheckedChangeListener(radioListener);
		
		sum = 0;
		
		// Initialize answer textviews to ""
		answer1TextView = (TextView) findViewById(R.id.answer1TextView);
		answer2TextView = (TextView) findViewById(R.id.answer2TextView);
		answer3TextView = (TextView) findViewById(R.id.answer3TextView);
		
		answer1TextView.setText("");
		answer2TextView.setText("");
		answer3TextView.setText("");
		selectAnswerTextView.setVisibility(View.INVISIBLE);
		equasionTextView.setVisibility(View.INVISIBLE);
		answer1TextView.setOnTouchListener(answerListener);
		answer2TextView.setOnTouchListener(answerListener);
		answer3TextView.setOnTouchListener(answerListener);
		exitButton.setOnClickListener(exitListener);

	}
	
	// Calculate the addition problem
	private void addProblem()
	{
		Random r = new Random();
		int guess1; // random number for guess 1
		int guess2; // random number for guess 2
		int first = r.nextInt(9) + 1; // 1-9
		int second = r.nextInt(9) + 1; // 1-9
		
		//answer
		sum = first + second;
		
		// generate random numbers for other guesses
		guess1 = r.nextInt(9);
		guess2 = r.nextInt(9);
		
		
		
		String problem = Integer.toString(first) + " + " + Integer.toString(second) + " = ? " ;
		equasionTextView.setText(problem);
		
		String g1 = Integer.toString(guess1);
		String g2 = Integer.toString(sum);
		String g3 = Integer.toString(guess2);
		
		answer1TextView.setText(g1);
		answer2TextView.setText(g2);
		answer3TextView.setText(g3);
		
		// Set equasion & answer text view to visible
		equasionTextView.setVisibility(View.VISIBLE);
		selectAnswerTextView.setVisibility(View.VISIBLE);
		
		
	}
	// Calculate the subtraction problem
	private void subtractProblem()
	{
		Random r = new Random();
		int guess1 = r.nextInt(9) + 1; // random number for guess 1
		int guess2 = r.nextInt(9) + 1; // random number for guess 2
		int first = r.nextInt(9) + 1;
		int second = r.nextInt(9) + 1;
		
		// handle negatives
		if (first < second) {
			int temp = first;
			first = second;
			second = temp;
		}
		
		
		// answer
		sum = first - second;
		
		String problem = Integer.toString(first) + " - " + Integer.toString(second) + " = ? " ;
		equasionTextView.setText(problem);
		
		String g1 = Integer.toString(guess1);
		String g2 = Integer.toString(sum);
		String g3 = Integer.toString(guess2);
		
		answer1TextView.setText(g1);
		answer2TextView.setText(g2);
		answer3TextView.setText(g3);
		
		// Set equasion & answer text view to visible
		equasionTextView.setVisibility(View.VISIBLE);
		selectAnswerTextView.setVisibility(View.VISIBLE);
		
		
		
	}
	public OnCheckedChangeListener radioListener = new OnCheckedChangeListener() {

		@Override

		public void onCheckedChanged(RadioGroup group, int checkedId) {


               if(addRadio.isChecked())
            	{
            	   addProblem();
            	   radioGroup.setVisibility(View.INVISIBLE);
            	}
               else
                {
            	   subtractProblem();
            	   radioGroup.setVisibility(View.INVISIBLE);
                }
		


}
	};
	
	// Listeners for answers
	public OnTouchListener answerListener = new OnTouchListener() {
		String s1 = String.valueOf(sum);

		public boolean onTouch(View v, MotionEvent event) 
		{                 
		    switch (event.getAction())       
		    {
		        case MotionEvent.ACTION_DOWN:
		         
		             if (answer2TextView.equals(s1)
		                || answer1TextView.equals(s1)
		                || answer3TextView.equals(s1))
		             {
		                   ((TextView)v).setVisibility(TextView.INVISIBLE);
		             }   
		         break;
		    }
			return true;
		}
	};

	// Listener for exit button
	public OnClickListener exitListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			finish();
			System.exit(0);
		}
	};

}

Open in new window

0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 40502478
No, I was just illustrating what your code was doing. What you want it to do is more complex. The onTouch is processed for each view.  You need a single textView loaded with an object. Here is an explanation of why it is not working.

http://stackoverflow.com/questions/8574622/how-to-make-the-two-textviews-invisible-on-the-touch-in-android


You are going to have to rethink the way you are trying to solve the problem.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question