Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Issue with OnTouchListener

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
Computer Guy
Asked:
Computer Guy
  • 3
  • 3
1 Solution
 
Chris HarteThaumaturgeCommented:
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
 
Computer GuyAuthor Commented:
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
 
Chris HarteThaumaturgeCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Computer GuyAuthor Commented:
Rewrite my class like the example you posted?
0
 
Computer GuyAuthor Commented:
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
 
Chris HarteThaumaturgeCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now