CommonUtils SDK – android (Part 1)

Android CommonUtils

As an android developer, in how many apps you have checked a field is empty or not? Entered email is correct or not? Data connectivity is available or not? What is the current location of user? Forms, data connectivity, validations, images, preferences, downloading files, user’s locations, notifications, map, dialogs, toasts, etc have now become common requirements in the apps.

CommonUtils library is developed to reduce efforts to achieve common features of the android apps. While developing the apps, we realized that we’re coding for many common features in all the apps. For e.g. check the network’s availability, using shared preferences, parsing, etc. And like us, many other android developers might be doing the same. So we thought that it needs to be reduced for all to save the development time with ease. This is how an idea popped in our mind, and we decided to develop an SDK which can reduce developers’ time and efforts.

You can get the code from GitHub. You can add your own methods that can reduce developers’ time and efforts. Any improvements, changes and suggestions are welcomed.

Here is the demo apk file that you can install and check in your devices. You can download it from Google Drive.

This SDK includes 64 common methods as of now. You can go through them below.

 

1. Check empty EditText

This method is used to check whether data is available or not in EditText.

Parameter:

edt_name (Pass EditText name)

Returns:

Returns true if data is available, else false.

How to use:

Common.isEmptyEditText (edt_name);

2. Check network availability

This method is used to check network availability in device.

Parameter:

mContext (Pass application context)

Returns:

Returns true if network is available, else false.

How to use:

Boolean isNetworkAvailable = Common.isNetworkAvailable (mContext);

3. Check Email

This method is used to check whether email is valid or not.

Parameter:

strEmail (Pass email address)

Returns:

Returns true if email is valid, else false.

How to use:

Boolean isEmailValid = Common.isEmailIdValid (strEmail);

4. Get current Date in String format

This method is used to get current date in string format.

Parameter:

strDate (Pass date format as a string).

Examples:

yyyy-MM-dd
yyyy-MM-dd HH:mm
yyyy-MM-dd HH:mmZ
yyyy-MM-dd HH:mm:ss.SSSZ
yyyy-MM-dd ‘T’ HH:mm:ss.SSSZ

Returns:

Current Date in string format, otherwise returns null.

How to use:

String strDate = Common.getCurrentDate (strDate);

5. Get Device id from device

This method is used to get device Id.
Note: You need to add following permission in manifest file READ_PHONE_STATE.
<uses-permission android: name=”android.permission.READ_PHONE_STATE” />

Parameter:

mContext (Pass application context).

Returns:

Device Id in string, otherwise returns null.

How to use:

String strDeviceId = Common.getDeviceId (mContext);

6. Set Preferences and get Preferences (String, int, long, Boolean, Float)

Setting preferences using setStringPrefrences

This method is used to set string preference.

Parameters:

mContext (Pass application context)

prefName (Name of Preference)

Value (Value of Preference)

PREFS_FILE_NAME (file name of preferences)

Returns:

This method does not return any value.

How to use:

Common.setStringPrefrences(context, prefName, Value, PREFS_FILE_NAME);

Getting preferences using getStringPrefrences

This method is used to get string from preference.

Parameters:

mContext (Pass application context)

prefName (Name of Preference)

PREFS_FILE_NAME (file name of preferences)

Returns:

This method does not return any value.

How to use:

Common.setStringPrefrences(context, prefName, PREFS_FILE_NAME);

7. Clear Preferences

This method is used to clear all the preferences from device.

Parameters:

mContext (Pass application context)

PREFS_FILE_NAME (file name of preferences)

Returns:

This method does not return any value.

How to use:

Common.removeAllPrefrences (context, PREFS_FILE_NAME);

8. Get Current Location

This method used to get current location.

Parameter:

mContext (Pass application context)

Returns:

Returns location object.

How to use:

Location location = Common.getCurrentLocation (mContext);

9. Apply Pinch Zoom on image

This method is used to apply pinch zoom functionality on image.

Parameter:

imgPinchZoom (Pass Imageview object)

Returns:

This method does not return any value. It just applies Pinch Zoom on ImageView.

How to use:

Common.applyPinchZoomOnImage (imgPinchZoom);

Note:

Use this ImageView in xml file.

<ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:scaleType="matrix" // Do not forget to add this line
 android:src="@drawable/airdroid" />

10. Get application Launcher Icon

This method is used to get application launcher icon.

Parameter:

mContext(Pass you application context).

Returns:

drawable of the application launcher icon.

How to use:

Drawable drawable = Common.getAppIcon (mContext);

11. Local Notification

This method is used to send local notification in application.

Parameters:

mContext (Pass application context)

strTitle (Title of notification)

strMessage (Content of notification)

Intent (Pass intent to open application or null to clear notification on touch)

How to use:

Common.sendLocalNotification (mContext, title, message, mIntent);

12. Get random Character

This method is used to get random character.

Returns:

Returns any random character.

How to use:

char character = Common.getRandomCharacter();

13. Disable Sleep Mode while using application

This method is used to disable sleep mode while using application.

Parameter:

mContext (Pass application context)

Returns:

This method does not return any value.

How to use:

Common.disableSleepMode (mContext);

14. Enable Sleep Mode while using application

This method is used to enable sleep mode while using application.

Parameter:

mContext (Pass application context)

Returns:

This method does not return any value.

How to use:

Common.enabelSleepMode (mContext);

15. Open image from selected directory path

This method is used to open image from given directory path.

Parameters:

mContext (Pass application context)

strPath (Directory path of the image)

For e.g. “ /mnt/sdcard/Picture/imagename.png”

Returns:

This method does not return any value.

How to use:

Common.openImage (mContext, strPath);

16. Open video from selected directory path

This method is used to open video from given directory path.

Parameters:

mContext (Pass application context)

strPath (Directory path of the video)

For e.g. “ /mnt/sdcard/Picture//mnt/Video/1.mp4”

Returns:

This method does not return any value.

How to use:

Common.openImage (mContext, strPath);

17. Open URL in browser

This method is used to open URL in mobile browser.

Parameters:

mContext (Pass application context)

strUrl – String URL to open in browser, like https://www.google.com

Returns:

This method does not return any value. It just redirects to browser.

How to use:

Common.openURL (mContext, strUrl );

18. Shows address location on map

This method is used for getting location on Google map using specified address.

Parameters:

mContext (Pass application context)

strAddress (Pass address to locate on map)

Returns:

This method does not return any value.

How to use:

Common.showAddressOnMap (mContext, strAddress);

19. Create directory in sdcard specified location

This method is used to create directory on specified location.

Parameters:

mContext (Pass application context)

strPath (Pass you sdcard path), for e.g. ” /mnt/sdcard/specifiedFolder/”

strDirectoryname (Pass directory name)

Returns:

Returns true if folder created else false.

How to use:

Common.createFolder (mContext,strPath,strDirectoryname);

20. Download image from URL

This method is used to download image from specified URL.

Parameters:

strImgURL (image URL in string)

imgView (imageView in which image is to be set)

How to use:

Common.downloadImageFromURL (imageURL, imageview);

21. Show Date Picker

This method is used to open DatePickerDialog and get selected date in TextView.

Parameters:

mContext (Pass application context)

strFormat (format of date)

txtTextView (Pass TextView in which date is to be set)

Returns:

This method does not return any value.

How to use:

Common.showDatePickerDialog (mContext, strFormat, txtTextView) ;

22. Show time picker

This method is used to open TimePickerDialog and get selected date in textview.

Parameters:

mContext (Pass application context)

mTextView (textView in which selected date has to be set)

Returns:

This method does not return any value.

How to use:

Common.showTimePickerDialog (mContext, mTextView);

23. Get number of file counts in sdcard directory

This method is used to get number of files available in specified sdcarddirectory.

Parameters:

strFormat (Pass file format. For e.g. .jpg, .mp3, .png)

strDirectoryPath (Pass path to get files count in directory. For e.g. “/mnt/sdcard/Pictures/Screenshots/”)

Returns:

It returns number of count in int format.

How to use:

int intCount = Common.getFileCounts (strFormat, strDirectoryPath);

24. Calculate date difference between two dates

This method is used to calculate number of days between two dates. Note that, both the dates must be in same format.

Parameters:

date1 (Pass date1)

date2 (Pass date2)

Returns:

It returns long value of date difference. Positive value for future days and negative value for passed days from current.

How to use:

long longDayCount = Common.calculateDays (date1, date2);

25. Convert Stringdate to Date format

This method is used to convert String Date to Date format.

Parameters:

strDate (Pass string Date)

strDateFormat (Pass string format)

Returns:

It returns Date in date format.

How to use:

Date date = Common.stringToDate(strDate, strDateFormat);

26. Get device height

This method is used to get device height.

Parameter:

mContext (Pass application context)

Returns:

Returns device height.

How to use:

int intHeight = Common.getDeviceHeight(mContext);

27. Get device width

This method is used to get device width.

Parameter:

mContext (Pass application context)

Returns:

It returns device width.

How to use:

int intWidth = Common.getDeviceWidth(mContext);

28. Get random number

This method is used to get random number.

Parameter:

number (Maximum number)
For e.g. if you want to get random number up to 999.

Returns:

It returns any random picked number.

How to use:

int number = Common.getRandom(number);

29. Add postfix to numbers

This method is used to set postfix to a number.

Parameter:

intNumber (Pass integer number to add postfix)
For e.g. pass 10, it will return 10th.

Returns:

It returns number in string format with added postfix in it.

How to use:

String strNumber = Common.getPostFixForNumber(number);

30. Convert comma separated String to ArrayList

This method is used to convert comma separated string to array list.

Parameter:

strCommaSeparatedString (Pass comma separated string)

For e.g. “test1, test2,test3,test4,test5”.

Returns:

It returns ArrayList of comma separated string.

How to use:

ArrayList arrList = Common.stringToArrayList(strCommaSeparatedString);

For more methods of this SDK, look for the part-2 of this post. However, you can always refer the PDF doc from GitHub.

Avatar for Multidots
Author : Multidots
Multidots is full service enterprise software development company with services revolving around consulting, enterprise-grade custom application development, custom mobile and cloud applications to automated and manual testing. We are proud to have served mid-sized to large enterprises including Fortune 500 Companies in our clientele.

Comments

Leave a Reply

How can we help you?

Submit your Business Enquiry here and we will get in touch shortly.

Contact Us

Looking for a First-Class Technology Consultant?

X

Quick Contact

I'm interested in receiving periodic updates and mailers from Multidots