Search code examples
javaandroidonclicklistener

onClickListener does not hide my text


MainActivity

...

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener{
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textID);
        Button b = (Button) findViewById(R.id.button);
        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        tv.setText(R.string.text); 
    }
}

string.xml

<resources>
    <string name="text">This text do not hide!</string>
</resources>

This code is directly showing the text which I will try to show when I click the button.

My question is, where am I making the mistake? Why does this code not work?


Solution

  • to hide a view :

    tv.setVisibility(View.GONE);
    

    for ex.

    @Override
    public void onClick(View v){
        tv.setVisibility(View.GONE);
    }
    

    to empty TextView :

    tv.setText("");
    

    for ex.

    @Override
    public void onClick(View v){
        tv.setText(""); 
    }