Sets visibility of the given view to View.GONE. - Android User Interface

Android examples for User Interface:View Hide Show

Description

Sets visibility of the given view to View.GONE.

Demo Code


//package com.java2s;
import android.app.Activity;

import android.util.Log;

import android.view.View;

public class Main {
    /**//from w  w  w  .j  av  a 2s .c om
     * Sets visibility of the given view to <code>View.GONE</code>.
     *
     * @param context The current Context or Activity that this method is called from
     * @param id      R.id.xxxx value for the view to hide"expected textView to throw a ClassCastException" + textView
     */
    public static void hideView(Activity context, int id) {
        if (context != null) {
            View view = context.findViewById(id);
            if (view != null) {
                view.setVisibility(View.GONE);
            } else {
                Log.e("PercolateAndroidUtils",
                        "View does not exist.  Could not hide it.");
            }
        }
    }

    /**
     * Utility method to make getting a View via findViewById() more safe & simple.
     * <p/>
     * - Casts view to appropriate type based on expected return value
     * - Handles & logs invalid casts
     *
     * @param context The current Context or Activity that this method is called from
     * @param id      R.id value for view
     * @return View object, cast to appropriate type based on expected return value.
     * @throws ClassCastException if cast to the expected type breaks.
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(Activity context, int id) {
        T view = null;
        View genericView = context.findViewById(id);
        try {
            view = (T) (genericView);
        } catch (Exception ex) {
            String message = "Can't cast view (" + id + ") to a "
                    + view.getClass() + ".  Is actually a "
                    + genericView.getClass() + ".";
            Log.e("PercolateAndroidUtils", message);
            throw new ClassCastException(message);
        }

        return view;
    }

    /**
     * Utility method to make getting a View via findViewById() more safe & simple.
     * <p/>
     * - Casts view to appropriate type based on expected return value
     * - Handles & logs invalid casts
     *
     * @param parentView Parent View containing the view we are trying to get
     * @param id         R.id value for view
     * @return View object, cast to appropriate type based on expected return value.
     * @throws ClassCastException if cast to the expected type breaks.
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(View parentView, int id) {
        T view = null;
        View genericView = parentView.findViewById(id);
        try {
            view = (T) (genericView);
        } catch (Exception ex) {
            String message = "Can't cast view (" + id + ") to a "
                    + view.getClass() + ".  Is actually a "
                    + genericView.getClass() + ".";
            Log.e("PercolateAndroidUtils", message);
            throw new ClassCastException(message);
        }

        return view;
    }
}

Related Tutorials