Package com.cheetahdigital.uikit.helper
Class ViewUtils
- java.lang.Object
-
- com.cheetahdigital.uikit.helper.ViewUtils
-
public class ViewUtils extends java.lang.Object
Class containing a collection of utility methods for View.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ViewUtils.ActivityExceptionCallback
Interface for unhandled activity exceptions.
-
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>
TappendText(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 Androidxstatic 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 viewstatic 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 externallystatic <T extends android.view.View>
voidsetBackgroundResource(android.app.Activity activity, int background, int... ids)
Set the background to a given resource.static <T extends android.view.View>
voidsetBackgroundResource(android.view.View view, int background, int... ids)
Set the background to a given resource.static <T extends android.view.View>
voidsetEnabled(android.app.Activity activity, boolean enabled, int... ids)
Sets the enabled state of view.static <T extends android.view.View>
voidsetEnabled(android.view.View view, boolean enabled, int... ids)
Sets the enabled state of view.static <T extends android.view.View>
voidsetImageResource(android.app.Activity activity, int drawable, int... ids)
Sets a drawable as the content of this ImageView.static <T extends android.view.View>
voidsetImageResource(android.view.View view, int drawable, int... ids)
Sets a drawable as the content of this ImageView.static <T extends android.view.View>
voidsetOnClickListener(android.app.Activity activity, android.view.View.OnClickListener listener, int... ids)
Callback when this view is clicked.static <T extends android.view.View>
voidsetOnClickListener(android.view.View view, android.view.View.OnClickListener listener, int... ids)
Callback when this view is clicked.static <T extends android.view.View>
voidsetTag(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>
voidsetText(android.app.Activity activity, java.lang.String text, int... ids)
Populates the textview.static <T extends android.widget.TextView>
TsetText(android.view.View view, android.text.Spanned text, int id)
Populates the textview.static <T extends android.widget.TextView>
voidsetText(android.view.View view, java.lang.String text, int... ids)
Populates the textview.static <T extends android.widget.TextView>
voidsetText(android.widget.TextView textView, android.text.Spanned span)
Populates the textview.static <T extends android.widget.TextView>
voidsetText(android.widget.TextView textView, java.lang.String text)
Populates the textview.static <T extends android.widget.TextView>
voidsetTextColor(android.app.Activity activity, int color, int... ids)
Sets the text color.static <T extends android.widget.TextView>
voidsetTextColor(android.view.View view, int color, int... ids)
Sets the text color.static <T extends android.widget.TextView>
voidsetTextFromHtml(android.widget.TextView textView, java.lang.String text)
Populates the textview with the text from html.static <T extends android.widget.TextView>
voidsetTextFromHtmlToUpperCase(android.widget.TextView textView, java.lang.String text)
Populates the textview with the uppercase text from html.static <T extends android.widget.TextView>
voidsetTextFromHtmlWithVisibility(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>
voidsetTextWithVisibility(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 viewstatic void
setViewVisibility(android.view.View view, java.lang.String text)
Show/Hide view if text is not emptystatic <T extends android.view.View>
voidsetVisibility(android.app.Activity activity, int visibility, int... ids)
Shows/Hides the view.static <T extends android.view.View>
voidsetVisibility(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.
-
-
-
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 ofView
- Parameters:
view
- container where lookup will startkey
- key for the tagdata
- value to be storedids
- 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 ofView
- Parameters:
view
- container where lookup will startlistener
- callback used for click actionids
- 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 ofView
- Parameters:
activity
- container where lookup will startlistener
- callback used for click actionids
- 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 ofView
- Parameters:
activity
- container where lookup will startbackground
- resource id of the backgroundids
- 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 ofView
- Parameters:
view
- container where lookup will startbackground
- resource id of the backgroundids
- 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 ofView
- Parameters:
activity
- container where lookup will startvisibility
- can beView.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 ofView
- Parameters:
view
- container where lookup will startvisibility
- can beView.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 ofView
- Parameters:
activity
- container where lookup will startdrawable
- resource id of the imageids
- 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 ofView
- Parameters:
view
- container where lookup will startdrawable
- resource id of the imageids
- 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 ofTextView
- Parameters:
activity
- container where lookup will startcolor
- resource id of the colorids
- 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 ofTextView
- Parameters:
view
- container where lookup will startcolor
- resource id of the colorids
- 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 ofTextView
- Parameters:
activity
- container where lookup will starttext
- message to setids
- 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 ofTextView
- Parameters:
view
- container where lookup will starttext
- message to setids
- 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 ofTextView
- Parameters:
view
- container where lookup will starttext
- message to setid
- 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 ofTextView
- Parameters:
textView
- instance ofTextView
where text is settext
- message to set inTextView
-
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 ofTextView
- Parameters:
textView
- instance ofTextView
where text is setspan
- message to set inTextView
-
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 ofTextView
- Parameters:
textView
- instance ofTextView
where text is settext
- message to set inTextView
-
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 ofTextView
- Parameters:
textView
- instance ofTextView
where text is settext
- message to set inTextView
-
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 ofTextView
- Parameters:
textView
- instance ofTextView
where text is settext
- message to set inTextView
-
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 ofTextView
- Parameters:
view
- instance ofTextView
where text is settext
- message to set inTextView
-
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/hidetext
- 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/hidevisibility
-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 ofView
- Parameters:
activity
- container where lookup will startenabled
- true to enable interaction with the view, else falseids
- 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 ofView
- Parameters:
view
- container where lookup will startenabled
- true to enable interaction with the view, else falseids
- 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 startid
- 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 startid
- 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 ofTextView
- Parameters:
context
- used to get resourcesview
- container where lookup will starttext
- message to appendid
- 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 ofTextView
where to highlight texttextString
- message to displaycolor
- id of the colorstart
- start index of the highlightend
- 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 startingIntent
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 startingIntent
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 startingIntent
intent
-Intent
to be executedbundle
- data stored asBundle
with theIntent
-
startActivity
public static void startActivity(android.content.Context context, android.content.Intent intent, ViewUtils.ActivityExceptionCallback callback)
Launches an activity.- Parameters:
context
- used for startingIntent
intent
-Intent
to be executedcallback
- callback to handleActivityNotFoundException
-
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 startingIntent
intent
-Intent
to be executedrequestCode
- 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 startingIntent
intent
-Intent
to be executedrequestCode
- request code for activity resultbundle
- data stored asBundle
with theIntent
-
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 startingIntent
intent
-Intent
to be executedrequestCode
- 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 startingIntent
intent
-Intent
to be executedrequestCode
- request code for activity resultbundle
- data stored asBundle
with theIntent
-
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 showncontext
- 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 contentformattedText
- 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 resourcesurl
- url to be opened
-
-