user1710911
user1710911

Reputation: 675

TextView set visibility gone if its empty

Hi everyone why this code dose not work? I have TextView get the value from sqlite database I want check if its empty hide TextView.

mTel1 = (TextView) findViewById(R.id.tv_tel1);
    String ed_text = mTel1.getText().toString().trim();

    if(ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
    {
        mTel1.setVisibility(View.VISIBLE);

    }
    else
    {
        mTel1.setVisibility(View.GONE);
    }

XML

  <TextView
        android:id="@+id/tv_tel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/border"
        android:clickable="true"
        android:padding="10dp"
        android:textColor="#0066cc"
        android:textSize="18sp" />

Upvotes: 1

Views: 12263

Answers (5)

Exmile
Exmile

Reputation: 85

Try this:

    if(ed_text.length() == 0 || ed_text.equals(""))
    {
        mTel1.setVisibility(View.GONE);
    } else {
        mTel1.setVisibility(View.VISIBLE);
    }

Also, remember to always set visibility ="gone" in XML:

<TextView
    android:id="@+id/tv_tel1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/border"
    android:clickable="true"
    android:padding="10dp"
    android:textColor="#0066cc"
    android:visibility="gone"
    android:textSize="18sp" />

Upvotes: 5

Gunaseelan
Gunaseelan

Reputation: 15515

Simple , Change your code like this,

if (ed_text == null || ed_text.isEmpty()) {
     mTel1.setVisibility(View.GONE);    
} else {
     mTel1.setVisibility(View.VISIBLE);
}

Upvotes: 0

vasiljevski
vasiljevski

Reputation: 369

Did you try with:

if(ed_text.isEmpty())
{
     mTel1.setVisibility(View.VISIBLE);    
} else {
     mTel1.setVisibility(View.GONE);
}

This should do the trick.

You should also check if you TextView is populated with DB data when you call this.

Upvotes: 3

Vivek Elangovan
Vivek Elangovan

Reputation: 248

According to your question the answer is

if (tv.length() == 0) {
 tv.setVisibility(View.GONE);

}

Or you can use

 if (TextUtils.isEmpty(tv.getText()){

   tv.setVisibility(View.GONE);
} 

Upvotes: 0

Akash Moradiya
Akash Moradiya

Reputation: 3322

try this,

mTel1 = (TextView) findViewById(R.id.tv_tel1);
    String ed_text = mTel1.getText().toString().trim();

    if(ed_text!=null && ed_text.length()> 0)
    {
        mTel1.setVisibility(View.VISIBLE);

    }
    else
    {
        mTel1.setVisibility(View.GONE);
    }

Upvotes: 0

Related Questions