Adrian Olar
Adrian Olar

Reputation: 2903

TextView setVisibility(View.GONE) does not remove my CustomTextView

I am using a custom textview and i am trying to hide it under some circumstances... However i get a strange behaviour on some devices(Nexus 4) when i am using the method setVisibility(View.GONE), the view is not hidden as it should be but instead it's loaded with data from another previous textview.

Here's my code:

<ro.gebs.captoom.utils.fonts.CustomFontTextView
                        android:id="@+id/delete_btn"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/layout_padding"
                        android:background="@drawable/rounded"
                        android:drawablePadding="10dp"
                        android:drawableRight="@drawable/input_delete_red"
                        android:padding="@dimen/layout_padding"
                        android:paddingLeft="10dp"
                        android:text="@string/delete"
                        android:textColor="@color/redish"
                        android:textSize="18sp"
                        custom:fontName="SemiBold"
                        android:visibility="gone"/>

The CustomFontTextView class:

package ro.gebs.captoom.utils.fonts;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import ro.gebs.captoom.R;

public class CustomFontTextView extends TextView {

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode()) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView,
                    defStyle, 0);

            assert a != null;
            int fontId = a.getInteger(R.styleable.CustomFontTextView_fontName, -1);
            if (fontId == -1) {
                throw new IllegalArgumentException("The font_name attribute is required and must refer "
                        + "to a valid child.");
            }
            a.recycle();
            initialize(fontId);
        }

    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomFontTextView(Context context) {
        super(context);
    }

    @SuppressWarnings("ConstantConditions")
    public void initialize(int fontId) {

        Typeface tf = null;
        switch (fontId) {
            case -1:
            case 0:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Regular.ttf");
                break;
            case 1:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Bold.ttf");
                break;
            case 2:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Semibold.ttf");
                break;
            case 3:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-ExtraBold.ttf");
                break;

        }

        setTypeface(tf);
    }
}

My Activity code:

@Override
    protected void onResume() {
        super.onResume();

        if (deleteBtnVisible) {
            delete_btn.setVisibility(View.VISIBLE);
        } else {
            delete_btn.setVisibility(View.GONE);
        }


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            final int statusFolder = extras.getInt(Constants.STATUS, Constants.STATUS_NEW);
            if (statusFolder == Constants.STATUS_EDIT) {
                if (folder.getTitle().equals("Inbox")) {
                    delete_btn.setVisibility(View.GONE);
                } else {
                    delete_btn.setVisibility(View.VISIBLE);
                    delete_btn.setEnabled(true);
                }
            }
        }
    }

Any hints that could solve this strange issue?

Upvotes: 1

Views: 1038

Answers (1)

Santosh Kathait
Santosh Kathait

Reputation: 1444

Try this : In place of View.GONE use View.INVISIBLE

Upvotes: 1

Related Questions