ShadowCoder
ShadowCoder

Reputation: 43

Making TextView invisible after few seconds visible again

I'm currently exploring how to use EditText, Button and TextView. My program has an EditText and whenever nothing is typed in the EditText and a button is clicked, a TextView will appear for 3 seconds and become invisible again.
However, when I click on that button a second time and the EditText is still cleared, the TextView will not appear.
How can I make the TextView appear again?

public class NameActivity extends AppCompatActivity{

private EditText editNameText;
private TextView wrongInputTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);
    Log.i(Values.TAG, "NameActivity onCreate");

    editNameText = (EditText) findViewById(R.id.editNameText);
    editNameText.getText().clear();
    wrongInputTV = (TextView) findViewById(R.id.wrongInputTV);
    wrongInputTV.setTextColor(Color.RED);
    ((Button) findViewById(R.id.sendNameBtn)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            goToNextActivity();
        }
    });
}

private void goToNextActivity(){
    if(!TextUtils.isEmpty(editNameText.getText())) {;
        Intent intent = new Intent(this, DateActivity.class);
        intent.putExtra(Values.NAME_KEY, editNameText.getText().toString());
        startActivity(intent);
    }else {
        wrongInputTV.setText("Please enter a valid name");
        wrongInputTV.postDelayed(new Runnable() {
            @Override
            public void run() {
                wrongInputTV.setVisibility(View.INVISIBLE);
            }
        }, 3000);

        }
    }
}

Upvotes: 2

Views: 1853

Answers (3)

AnasAbubacker
AnasAbubacker

Reputation: 3607

You have to make textview VISIBLE again as you are making it INVISIBLE in runnable.

 wrongInputTV.setVisibility(View.VISIBLE);

So, goToNextActivity() should be written as below:-

private void goToNextActivity(){
    if(!TextUtils.isEmpty(editNameText.getText())) {;
        Intent intent = new Intent(this, DateActivity.class);
        intent.putExtra(Values.NAME_KEY, editNameText.getText().toString());
        startActivity(intent);
    }else {
        wrongInputTV.setVisibility(View.VISIBLE);
        wrongInputTV.setText("Please enter a valid name");
        wrongInputTV.postDelayed(new Runnable() {
            @Override
            public void run() {
                wrongInputTV.setVisibility(View.INVISIBLE);
            }
        }, 3000);

   }

Note: Initial VIEW state GONE/INVISIBLE can be set in OnCreate method.

Upvotes: 4

letsCode
letsCode

Reputation: 3046

This is how I would handle it....

public class MainActivity extends AppCompatActivity {

    TextView textView;
    EditText editText;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);

        textView.setVisibility(View.GONE);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(editText.getText().toString().equals("") || editText.getText().toString() == null) {
                    textView.setVisibility(View.VISIBLE);
                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            textView.setVisibility(View.GONE);
                        }
                    }, 3000);
                } else {
                    //go somewhere
                }
            }
        });
    }
}

Simple XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context="net.pixeledstudio.hidetextview.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Type something in the text view"
        android:gravity="center"
        android:id="@+id/textView"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type something"
        android:id="@+id/editText"
        android:layout_marginTop="25dp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/button"
        android:layout_marginTop="25dp"/>

</LinearLayout>

Upvotes: 0

Ben P.
Ben P.

Reputation: 54204

When working with TextView, it's important to understand the distinction between what you can see based on the view's visibility and what you can see based on the view's text.

The reason your original code works once but never again is that your TextView starts out visible but empty (you can "see" it but there's nothing to see). You then call setText(), making it visible and non-empty (now there's something to see). Then, after three seconds, you make it invisible and non-empty (so even though there's something to see, you can't see it).

However, nothing in your code ever changes the view's visiblity back to VISIBLE after you make it INVISIBLE. @lib4's answer covers (one way) how to do just that.

Upvotes: 0

Related Questions