Solved

Issue with OnTouchListener

Posted on 2014-12-15
6
216 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

772 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