Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Issue with OnTouchListener

Posted on 2014-12-15
6
Medium Priority
?
250 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…
Suggested Courses

721 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