Android Open Source - RZAndroidBaseUtils View Utils






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raizlabs.widget;
//  w w w  .j a v a 2s.c  om
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

public class ViewUtils {
  
  /**
   * Returns the text for a given {@link textView} or else null if
   * the textView is null or has no text within.
   * @param textView The textView whose text should be returned
   * @return the text within the textView or null
   */
  public static CharSequence getTextOrNull(TextView textView) {
    if (textView == null) return null;
    
      CharSequence text = textView.getText();
      return !TextUtils.isEmpty(text) ? text : null;
  }
  
  /**
   * Returns the a String with the text for a given {@link textView} 
   * or else null if the textView is null or has no text within.
   * <br/><br/>
   * Overload for {@link #getTextOrNull(TextView)} which returns a {@link String}
   * <br/><br/>
   * @param textView The textView whose text should be returned
   * @return the a String of the text within the textView or null
   */
  public static String getStringTextOrNull(TextView textView) {
    CharSequence text = getTextOrNull(textView); 
    return (text != null) ? text.toString() : null; 
  }

  /**
   * Converts the given value in density-independent pixels into raw pixels
   * with respect to the metrics of the given {@link View}
   * @param dips The value in density-independent pixels
   * @param view The {@link View} whose metrics to use to do the conversion
   * @return The value in raw pixels
   */
  public static float dipsToPixels(float dips, View view) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
        dips, view.getResources().getDisplayMetrics());
  }
  
  /**
   * Converts the given value in density-independent pixels into raw pixels
   * with respect to the metrics of the given {@link Resources}
   * @param dips The value in density-independent pixels
   * @param resources The {@link Resources} whose metrics to use to do the
   * conversion
   * @return The value in raw pixels
   */
  public static float dipsToPixels(float dips, Resources resources) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
        dips, resources.getDisplayMetrics()); 
  }
  
  /**
   * Converts the given value in density-independent pixels into raw pixels
   * with respect to the given {@link DisplayMetrics}
   * @param dips The value in density-independent pixels
   * @param metrics The {@link DisplayMetrics} to use to do the conversion
   * @return The value in raw pixels
   */
  public static float dipsToPixels(float dips, DisplayMetrics metrics) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        dips, metrics);
  }
  
  /**
   * Sets the given {@link View}s visibility to {@link View#VISIBLE} or
   * {@link View#GONE} depending on the specified value.
   * @param view The {@link View} to change the visibility of.
   * @param visible True to make the view VISIBLE, false to make it GONE.
   */
  public static void setVisibleOrGone(View view, boolean visible) {
    view.setVisibility(visible ? View.VISIBLE : View.GONE);
  }
  
  /**
   * Sets the given {@link View}s visibility to {@link View#VISIBLE} or
   * {@link View#INVISIBLE} depending on the specified value.
   * @param view The {@link View} to change the visibility of.
   * @param visible True to make the view VISIBLE, false to make it INVISIBLE.
   */
  public static void setVisibleOrInvisible(View view, boolean visible) {
    view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
  }
  
  /**
   * Returns true if the given {@link View}s visibility is set to
   * {@link View#VISIBLE}.
   * @param view The {@link View} to check.
   * @return True if the view is set to VISIBLE.
   */
  public static boolean isVisible(View view) {
    return view.getVisibility() == View.VISIBLE;
  }
  
  /**
   * Returns true if the given {@link View}s visibility is set to
   * {@link View#INVISIBLE}.
   * @param view The {@link View} to check.
   * @return True if the view is set to INVISIBLE.
   */
  public static boolean isInvisible(View view) {
    return view.getVisibility() == View.INVISIBLE;
  }
  
  /**
   * Returns true if the given {@link View}s visibility is set to
   * {@link View#GONE}.
   * @param view The {@link View} to check.
   * @return True if the view is set to GONE.
   */
  public static boolean isGone(View view) {
    return view.getVisibility() == View.GONE;
  }

    /**
     * Returns the height of the actionBar.
     * @param context
     * @return height, in pixels, of the {@link android.app.ActionBar}
     */
    public static int getActionBarHeight(Context context){
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if ((context != null) && (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
        return actionBarHeight;
    }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java