Class ViewUtils


  • public class ViewUtils
    extends java.lang.Object
    Class containing a collection of utility methods for View.
    • Constructor Summary

      Constructors 
      Constructor Description
      ViewUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends android.widget.TextView>
      T
      appendText​(android.content.Context context, android.view.View view, java.lang.String text, int id)
      Appends a string inside textview.
      static void formatUrlTextToUpperCase​(android.widget.TextView textView, android.text.Spanned formattedText)
      Added to support underline in url format for Androidx
      static java.lang.CharSequence getText​(android.app.Activity activity, int id)
      Fetches the text input inside textview.
      static java.lang.CharSequence getText​(android.view.View view, int id)
      Fetches the text input inside textview.
      static void hideKeyboard​(android.app.Activity activity)
      Hide the keyboard binded on current view
      static void highlightText​(android.widget.TextView textView, java.lang.String textString, int color, int start, int end)
      Adds a highlight to text.
      static boolean isActivityDestroyed​(android.app.Activity activity)
      Checks if activity is Destroyed.
      static void openUrlInBrowser​(android.content.Context context, java.lang.String url)
      Calls an intent to open a browser externally
      static <T extends android.view.View>
      void
      setBackgroundResource​(android.app.Activity activity, int background, int... ids)
      Set the background to a given resource.
      static <T extends android.view.View>
      void
      setBackgroundResource​(android.view.View view, int background, int... ids)
      Set the background to a given resource.
      static <T extends android.view.View>
      void
      setEnabled​(android.app.Activity activity, boolean enabled, int... ids)
      Sets the enabled state of view.
      static <T extends android.view.View>
      void
      setEnabled​(android.view.View view, boolean enabled, int... ids)
      Sets the enabled state of view.
      static <T extends android.view.View>
      void
      setImageResource​(android.app.Activity activity, int drawable, int... ids)
      Sets a drawable as the content of this ImageView.
      static <T extends android.view.View>
      void
      setImageResource​(android.view.View view, int drawable, int... ids)
      Sets a drawable as the content of this ImageView.
      static <T extends android.view.View>
      void
      setOnClickListener​(android.app.Activity activity, android.view.View.OnClickListener listener, int... ids)
      Callback when this view is clicked.
      static <T extends android.view.View>
      void
      setOnClickListener​(android.view.View view, android.view.View.OnClickListener listener, int... ids)
      Callback when this view is clicked.
      static <T extends android.view.View>
      void
      setTag​(android.view.View view, int key, java.lang.Object data, int... ids)
      Sets a tag associated with this view and a key.
      static <T extends android.widget.TextView>
      void
      setText​(android.app.Activity activity, java.lang.String text, int... ids)
      Populates the textview.
      static <T extends android.widget.TextView>
      T
      setText​(android.view.View view, android.text.Spanned text, int id)
      Populates the textview.
      static <T extends android.widget.TextView>
      void
      setText​(android.view.View view, java.lang.String text, int... ids)
      Populates the textview.
      static <T extends android.widget.TextView>
      void
      setText​(android.widget.TextView textView, android.text.Spanned span)
      Populates the textview.
      static <T extends android.widget.TextView>
      void
      setText​(android.widget.TextView textView, java.lang.String text)
      Populates the textview.
      static <T extends android.widget.TextView>
      void
      setTextColor​(android.app.Activity activity, int color, int... ids)
      Sets the text color.
      static <T extends android.widget.TextView>
      void
      setTextColor​(android.view.View view, int color, int... ids)
      Sets the text color.
      static <T extends android.widget.TextView>
      void
      setTextFromHtml​(android.widget.TextView textView, java.lang.String text)
      Populates the textview with the text from html.
      static <T extends android.widget.TextView>
      void
      setTextFromHtmlToUpperCase​(android.widget.TextView textView, java.lang.String text)
      Populates the textview with the uppercase text from html.
      static <T extends android.widget.TextView>
      void
      setTextFromHtmlWithVisibility​(android.widget.TextView textView, java.lang.String text)
      Shows the textview and sets the text from html when text is not empty.
      static <T extends android.widget.TextView>
      void
      setTextWithVisibility​(android.widget.TextView view, java.lang.String text)
      Shows the textview and sets its text when text is not empty.
      static void setViewVisibility​(android.view.View view, boolean visibility)
      Show/Hide view
      static void setViewVisibility​(android.view.View view, java.lang.String text)
      Show/Hide view if text is not empty
      static <T extends android.view.View>
      void
      setVisibility​(android.app.Activity activity, int visibility, int... ids)
      Shows/Hides the view.
      static <T extends android.view.View>
      void
      setVisibility​(android.view.View view, int visibility, int... ids)
      Shows/Hides the view.
      static void showError​(java.lang.Exception e, android.content.Context context)
      Shows an error message.
      static void startActivity​(android.content.Context context, android.content.Intent intent)
      Launches an activity.
      static void startActivity​(android.content.Context context, android.content.Intent intent, android.os.Bundle bundle)
      Launches an activity.
      static void startActivity​(android.content.Context context, android.content.Intent intent, ViewUtils.ActivityExceptionCallback callback)
      Launches an activity.
      static void startActivity​(androidx.fragment.app.Fragment fragment, android.content.Intent intent)
      Launches an activity.
      static void startActivityForResult​(android.app.Activity activity, android.content.Intent intent, int requestCode)
      Launches new activity and returns the input from there.
      static void startActivityForResult​(android.app.Activity activity, android.content.Intent intent, int requestCode, android.os.Bundle bundle)
      Launches new activity and returns the input from there.
      static void startActivityForResult​(androidx.fragment.app.Fragment fragment, android.content.Intent intent, int requestCode)
      Launches new activity and returns the input from there.
      static void startActivityForResult​(androidx.fragment.app.Fragment fragment, android.content.Intent intent, int requestCode, android.os.Bundle bundle)
      Launches new activity and returns the input from there.
      static android.graphics.Bitmap takeScreenshot​(android.view.View v)
      Method for taking screenshot.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ViewUtils

        public ViewUtils()
    • Method Detail

      • setTag

        public static <T extends android.view.View> void setTag​(android.view.View view,
                                                                int key,
                                                                java.lang.Object data,
                                                                int... ids)
        Sets a tag associated with this view and a key.
        Type Parameters:
        T - instance of View
        Parameters:
        view - container where lookup will start
        key - key for the tag
        data - value to be stored
        ids - list of ids to find
      • setOnClickListener

        public static <T extends android.view.View> void setOnClickListener​(android.view.View view,
                                                                            android.view.View.OnClickListener listener,
                                                                            int... ids)
        Callback when this view is clicked.
        Type Parameters:
        T - instance of View
        Parameters:
        view - container where lookup will start
        listener - callback used for click action
        ids - list of ids to find
      • setOnClickListener

        public static <T extends android.view.View> void setOnClickListener​(android.app.Activity activity,
                                                                            android.view.View.OnClickListener listener,
                                                                            int... ids)
        Callback when this view is clicked.
        Type Parameters:
        T - instance of View
        Parameters:
        activity - container where lookup will start
        listener - callback used for click action
        ids - list of ids to find
      • setBackgroundResource

        public static <T extends android.view.View> void setBackgroundResource​(android.app.Activity activity,
                                                                               int background,
                                                                               int... ids)
        Set the background to a given resource.
        Type Parameters:
        T - instance of View
        Parameters:
        activity - container where lookup will start
        background - resource id of the background
        ids - list of ids to find
      • setBackgroundResource

        public static <T extends android.view.View> void setBackgroundResource​(android.view.View view,
                                                                               int background,
                                                                               int... ids)
        Set the background to a given resource.
        Type Parameters:
        T - instance of View
        Parameters:
        view - container where lookup will start
        background - resource id of the background
        ids - list of ids to find
      • setVisibility

        public static <T extends android.view.View> void setVisibility​(android.app.Activity activity,
                                                                       int visibility,
                                                                       int... ids)
        Shows/Hides the view.
        Type Parameters:
        T - instance of View
        Parameters:
        activity - container where lookup will start
        visibility - can be View.VISIBLE, View.INVISIBLE, View.GONE
        ids - list of ids to find
      • setVisibility

        public static <T extends android.view.View> void setVisibility​(android.view.View view,
                                                                       int visibility,
                                                                       int... ids)
        Shows/Hides the view.
        Type Parameters:
        T - instance of View
        Parameters:
        view - container where lookup will start
        visibility - can be View.VISIBLE, View.INVISIBLE, View.GONE
        ids - list of ids to find
      • setImageResource

        public static <T extends android.view.View> void setImageResource​(android.app.Activity activity,
                                                                          int drawable,
                                                                          int... ids)
        Sets a drawable as the content of this ImageView.
        Type Parameters:
        T - instance of View
        Parameters:
        activity - container where lookup will start
        drawable - resource id of the image
        ids - list of ids to find
      • setImageResource

        public static <T extends android.view.View> void setImageResource​(android.view.View view,
                                                                          int drawable,
                                                                          int... ids)
        Sets a drawable as the content of this ImageView.
        Type Parameters:
        T - instance of View
        Parameters:
        view - container where lookup will start
        drawable - resource id of the image
        ids - list of ids to find
      • setTextColor

        public static <T extends android.widget.TextView> void setTextColor​(android.app.Activity activity,
                                                                            int color,
                                                                            int... ids)
        Sets the text color.
        Type Parameters:
        T - instance of TextView
        Parameters:
        activity - container where lookup will start
        color - resource id of the color
        ids - list of ids to find
      • setTextColor

        public static <T extends android.widget.TextView> void setTextColor​(android.view.View view,
                                                                            int color,
                                                                            int... ids)
        Sets the text color.
        Type Parameters:
        T - instance of TextView
        Parameters:
        view - container where lookup will start
        color - resource id of the color
        ids - list of ids to find
      • setText

        public static <T extends android.widget.TextView> void setText​(android.app.Activity activity,
                                                                       java.lang.String text,
                                                                       int... ids)
        Populates the textview.
        Type Parameters:
        T - instance of TextView
        Parameters:
        activity - container where lookup will start
        text - message to set
        ids - list of ids to find
      • setText

        public static <T extends android.widget.TextView> void setText​(android.view.View view,
                                                                       java.lang.String text,
                                                                       int... ids)
        Populates the textview.
        Type Parameters:
        T - instance of TextView
        Parameters:
        view - container where lookup will start
        text - message to set
        ids - list of ids to find
      • setText

        public static <T extends android.widget.TextView> T setText​(android.view.View view,
                                                                    android.text.Spanned text,
                                                                    int id)
        Populates the textview.
        Type Parameters:
        T - instance of TextView
        Parameters:
        view - container where lookup will start
        text - message to set
        id - resource id to find
        Returns:
        view found using the id
      • setText

        public static <T extends android.widget.TextView> void setText​(android.widget.TextView textView,
                                                                       java.lang.String text)
        Populates the textview.
        Type Parameters:
        T - instance of TextView
        Parameters:
        textView - instance of TextView where text is set
        text - message to set in TextView
      • setText

        public static <T extends android.widget.TextView> void setText​(android.widget.TextView textView,
                                                                       android.text.Spanned span)
        Populates the textview.
        Type Parameters:
        T - instance of TextView
        Parameters:
        textView - instance of TextView where text is set
        span - message to set in TextView
      • setTextFromHtml

        public static <T extends android.widget.TextView> void setTextFromHtml​(android.widget.TextView textView,
                                                                               java.lang.String text)
        Populates the textview with the text from html.
        Type Parameters:
        T - instance of TextView
        Parameters:
        textView - instance of TextView where text is set
        text - message to set in TextView
      • setTextFromHtmlToUpperCase

        public static <T extends android.widget.TextView> void setTextFromHtmlToUpperCase​(android.widget.TextView textView,
                                                                                          java.lang.String text)
        Populates the textview with the uppercase text from html.
        Type Parameters:
        T - instance of TextView
        Parameters:
        textView - instance of TextView where text is set
        text - message to set in TextView
      • setTextFromHtmlWithVisibility

        public static <T extends android.widget.TextView> void setTextFromHtmlWithVisibility​(android.widget.TextView textView,
                                                                                             java.lang.String text)
        Shows the textview and sets the text from html when text is not empty.
        Type Parameters:
        T - instance of TextView
        Parameters:
        textView - instance of TextView where text is set
        text - message to set in TextView
      • setTextWithVisibility

        public static <T extends android.widget.TextView> void setTextWithVisibility​(android.widget.TextView view,
                                                                                     java.lang.String text)
        Shows the textview and sets its text when text is not empty.
        Type Parameters:
        T - instance of TextView
        Parameters:
        view - instance of TextView where text is set
        text - message to set in TextView
      • setViewVisibility

        public static void setViewVisibility​(android.view.View view,
                                             java.lang.String text)
        Show/Hide view if text is not empty
        Parameters:
        view - view to show/hide
        text - text to check for view visibility
      • setViewVisibility

        public static void setViewVisibility​(android.view.View view,
                                             boolean visibility)
        Show/Hide view
        Parameters:
        view - view to show/hide
        visibility - true to show, else false
      • setEnabled

        public static <T extends android.view.View> void setEnabled​(android.app.Activity activity,
                                                                    boolean enabled,
                                                                    int... ids)
        Sets the enabled state of view.
        Type Parameters:
        T - instance of View
        Parameters:
        activity - container where lookup will start
        enabled - true to enable interaction with the view, else false
        ids - list of ids to find
      • setEnabled

        public static <T extends android.view.View> void setEnabled​(android.view.View view,
                                                                    boolean enabled,
                                                                    int... ids)
        Sets the enabled state of view.
        Type Parameters:
        T - instance of View
        Parameters:
        view - container where lookup will start
        enabled - true to enable interaction with the view, else false
        ids - list of ids to find
      • getText

        public static java.lang.CharSequence getText​(android.app.Activity activity,
                                                     int id)
        Fetches the text input inside textview.
        Parameters:
        activity - container where lookup will start
        id - resource id to find
        Returns:
        current text of the TextView
      • getText

        public static java.lang.CharSequence getText​(android.view.View view,
                                                     int id)
        Fetches the text input inside textview.
        Parameters:
        view - container where lookup will start
        id - resource id to find
        Returns:
        current text of the TextView
      • appendText

        public static <T extends android.widget.TextView> T appendText​(android.content.Context context,
                                                                       android.view.View view,
                                                                       java.lang.String text,
                                                                       int id)
        Appends a string inside textview.
        Type Parameters:
        T - instance of TextView
        Parameters:
        context - used to get resources
        view - container where lookup will start
        text - message to append
        id - resource id to find
        Returns:
        TextView reference found using the id
      • takeScreenshot

        public static android.graphics.Bitmap takeScreenshot​(android.view.View v)
        Method for taking screenshot.
        Parameters:
        v - View source of the screenshot
        Returns:
        screenshot in Bitmap
      • highlightText

        public static void highlightText​(android.widget.TextView textView,
                                         java.lang.String textString,
                                         int color,
                                         int start,
                                         int end)
        Adds a highlight to text.
        Parameters:
        textView - instance of TextView where to highlight text
        textString - message to display
        color - id of the color
        start - start index of the highlight
        end - end index of the highlight
      • startActivity

        public static void startActivity​(android.content.Context context,
                                         android.content.Intent intent)
        Launches an activity.
        Parameters:
        context - used for starting Intent
        intent - Intent to be executed
      • startActivity

        public static void startActivity​(androidx.fragment.app.Fragment fragment,
                                         android.content.Intent intent)
        Launches an activity.
        Parameters:
        fragment - used for starting Intent
        intent - Intent to be executed
      • startActivity

        public static void startActivity​(android.content.Context context,
                                         android.content.Intent intent,
                                         android.os.Bundle bundle)
        Launches an activity.
        Parameters:
        context - used for starting Intent
        intent - Intent to be executed
        bundle - data stored as Bundle with the Intent
      • startActivity

        public static void startActivity​(android.content.Context context,
                                         android.content.Intent intent,
                                         ViewUtils.ActivityExceptionCallback callback)
        Launches an activity.
        Parameters:
        context - used for starting Intent
        intent - Intent to be executed
        callback - callback to handle ActivityNotFoundException
      • startActivityForResult

        public static void startActivityForResult​(android.app.Activity activity,
                                                  android.content.Intent intent,
                                                  int requestCode)
        Launches new activity and returns the input from there.
        Parameters:
        activity - used for starting Intent
        intent - Intent to be executed
        requestCode - request code for activity result
      • startActivityForResult

        public static void startActivityForResult​(android.app.Activity activity,
                                                  android.content.Intent intent,
                                                  int requestCode,
                                                  android.os.Bundle bundle)
        Launches new activity and returns the input from there.
        Parameters:
        activity - used for starting Intent
        intent - Intent to be executed
        requestCode - request code for activity result
        bundle - data stored as Bundle with the Intent
      • startActivityForResult

        public static void startActivityForResult​(androidx.fragment.app.Fragment fragment,
                                                  android.content.Intent intent,
                                                  int requestCode)
        Launches new activity and returns the input from there.
        Parameters:
        fragment - used for starting Intent
        intent - Intent to be executed
        requestCode - request code for activity result
      • startActivityForResult

        public static void startActivityForResult​(androidx.fragment.app.Fragment fragment,
                                                  android.content.Intent intent,
                                                  int requestCode,
                                                  android.os.Bundle bundle)
        Launches new activity and returns the input from there.
        Parameters:
        fragment - used for starting Intent
        intent - Intent to be executed
        requestCode - request code for activity result
        bundle - data stored as Bundle with the Intent
      • isActivityDestroyed

        public static boolean isActivityDestroyed​(android.app.Activity activity)
        Checks if activity is Destroyed.
        Parameters:
        activity - Activity to check for state
        Returns:
        true if activity instance is dead, else false.
      • hideKeyboard

        public static void hideKeyboard​(android.app.Activity activity)
        Hide the keyboard binded on current view
        Parameters:
        activity - Activity reference of the input method service
      • showError

        public static void showError​(java.lang.Exception e,
                                     android.content.Context context)
        Shows an error message.
        Parameters:
        e - Exception to identify the error shown
        context - used to get resources
      • formatUrlTextToUpperCase

        public static void formatUrlTextToUpperCase​(android.widget.TextView textView,
                                                    android.text.Spanned formattedText)
        Added to support underline in url format for Androidx
        Parameters:
        textView - view to include content
        formattedText - content of the view
      • openUrlInBrowser

        public static void openUrlInBrowser​(android.content.Context context,
                                            java.lang.String url)
        Calls an intent to open a browser externally
        Parameters:
        context - used to get resources
        url - url to be opened