android - make textView invisible

2.4k Views Asked by At

When my app starts, the user needs to touch on the screen before the real action starts. I have a textView which gives the hint to touch the screen.

After the screen is touched, I want the text to get invisible. Right now the textView never disappears and always stays in the front.

public class MainActivity extends Activity implements OnGestureListener
{       
    public boolean touched = false;
    TextView mMyView;

    public void onTouch() 
    {
        mMyView.setVisibility(View.GONE);  
        touched = true;                 
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {   
        super.onCreate(savedInstanceState);                       
        setContentView(R.layout.activity_game);
        mMyView = (TextView)findViewById(R.id.textView6);

        if(touched == true) 
        {

        }

    }
}
6

There are 6 best solutions below

0
On BEST ANSWER

1.Always use if(something) if you want to see if it's true/false instead of writing if(something == true) [something is a boolian assigned with value true.]

2.If you point your views xml to a method using android:onClick like below,

<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="onTouch" />

. What's the point of implementing OnGestureListener?

If i do this onCreate i initialize my view

View myView = findViewById(R.id.my_view);

3.If i really want a touch i will do this

myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // ... Respond to touch events --> tv.setVisibility(View.INVISIBLE);
        return true; // if you return false with this then the listener will not be called for the subsequent ACTION_MOVE and ACTION_UP string of events.
    }
});
  1. Now you can see in the 3rd ones parameter there is a MotionEvent, you can identify the motion ACTION_DOWN , ACTION_MOVE and ACTION_UP

Now think have you ever used them. You got an idea in your head about a touch so tried to use touch events .. But you don't use them. So it does the same as what onClickListner does in your case. If you want motions use that 3rd one i gave.

But simply you can use

 // view is the background layout
    myView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Do something here --> Hide your text  tv.setVisibility(View.INVISIBLE);
        }
    });

Those view onClickListner or setOnTouchListener you can directly use them inside onCreate or keep them inside a method and you can call that method from onCreate. Why to keep a boolean? It's nothing major

Note i considered myView as the background layout not your textView , background is the one you click / touch

1
On

Use the code below on the onCreate method and yes set the visibility as GONE instead of invisible. Also state the current visibilty of the TextView in the onTouch then set it to

  tv.setVisibility(View.GONE);
0
On

You are checking if screen was touched in onCreate() which is called only once at the start of the activity. Initialize TextView globally and set its visibility inside onTouch(View v, MotionEvent event) Also your onTouch() isn;t correct. You should override public boolean onTouch(View v, MotionEvent event)

0
On
public class MainActivity extends Activity
{       
    public boolean touched = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {   
        super.onCreate(savedInstanceState);                       
        setContentView(R.layout.activity_game);
        TextView tv = (TextView) findViewById(R.id.textView6);
        tv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                touched = true; 
                tv.setVisibility(View.INVISIBLE);  

                return true;
            }
        });
    }
}
6
On

So now you changed the questions code several times and I hope it´s the final change. Only than my answer could help.

You have done this in your onCreate():

 if(touched == true) 
        {
           tv.setVisibility(View.INVISIBLE);           
        }

But this is executed directly and has nothing to do with you onTouch() method. Let´s assume your onTouch() works correctly. Make the TextView global:

TextView mMyView;

initialize it in onCreate():

mMyView = (TextView)findViewById(R.id.textView6);

and then hide it in onTouch():

onTouch(View view){

mMyView.setVisibility(View.GONE);
}

But you have to be sure that your method onTouch() works. You can make a Toast or a Log to check. You have to be sure that:

-The TextView is inside your layout xml that you set with setContentView(R.layout.activity_game);

-The onTouch() method is declared in your TextView's xml attribute

android:onClick="onTouch"

and set clickable of your TextView to true:

android:clickable="true";

EDIT

If you implement onGestureListener() I guess the touch event is consumed by the listener and your TextView did not recognize onTouch(). If you don´t do any gesture detection in your activity, then remove this implementation.

0
On

Instead of implementing OnGestureListener add a setOnTouchListener in your root view of your activity layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlTestView"/>

For example rlTestView is your activity's root layout id, then use below code in your oncreate method

((RelativeLayout)findViewById(R.id.rlTestView)).setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            tv.setVisibility(View.GONE); 

            return true;
        }
    });`