Display a text or hide view if value is null or empty for TextView - Android User Interface

Android examples for User Interface:TextView

Description

Display a text or hide view if value is null or empty for TextView

Demo Code


//package com.java2s;

import android.text.TextUtils;

import android.view.View;
import android.widget.TextView;

public class Main {
    /**/*ww  w  .  ja v  a 2 s .  c o m*/
     * Display a text or hide view if value is null or empty
     *
     * @param textView TextView which should be filled
     * @param text     the value for the TextView
     */
    public static void showTextOrHide(final TextView textView,
            final String text) {
        showText(textView, text);
        showView(textView, !TextUtils.isEmpty(text));
    }

    public static void showText(final TextView textView, final String text) {
        if (textView != null) {
            textView.setText(text);
        }
    }

    /**
     * hides a view depending on {@param show}.
     * <p/>
     * analog to {@link #showView(android.view.View, boolean)}
     * <p/>
     * If you want the View to be {@link View#GONE} instead of {@link View#INVISIBLE} use {@link
     * #setVisiblity(android.view.View, boolean)}
     *
     * @param view the view to hide
     * @param show true means {@link View#INVISIBLE}, false {@link View#VISIBLE}
     */
    public static void showView(final View view, final boolean show) {
        if (view != null) {
            view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
        }
    }
}

Related Tutorials