Solved

Issue with OnTouchListener

Posted on 2014-12-15
6
198 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 16

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 16

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 16

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This is an explanation of a simple data model to help parse a JSON feed
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now