Tuesday 5 July 2016

Android Basic Utils Function

Which is the major issue or difficulty developer face during development ? just ask your self and you will find that answer is Utils function which we are using in daily basis. We are doing coding whole day most of the conman things are repeated in all project or module , only some part of the unique code , view and logic which we added. Developer are so lazzy to write same code again and again and make create utils class to make reusable code.

Here i am sharing list of function which we are using in daily basis in android application development which will save your time to find from Google or Stack overflow and write in your utils class or java class. The main objective of this task is Reuse ability of code , Time saving , Easy & Less coding. Let's start.

  • You have two ways to use this Utils function :

    1. JAVA File
    2. Live Template
  • Direct form JAVA file :

    You just need to add my created Utils JAVA file in your <package> and access using class object. You can find 120+ utils function in java file but that is not mandatory to add all in your application you can remove list of function which is not useful for your application so that will not save unnecessary code. see below example to see "How to use this class in your activity class?"

     
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    public class MainActivity extends Activity {
        private CKAndroidUtils objCKUtils;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            objCKUtils = new CKAndroidUtils(this);
            if (objCKUtils.isNetworkAvailable()) {
              ….. //Do your task
            }
        }
     
     
    
    public class MainActivity extends Activity {
        private CKAndroidUtils objCKUtils;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            objCKUtils = new CKAndroidUtils(this);
            if (objCKUtils.isNetworkAvailable()) {
              ….. //Do your task
            }
        }
    
    
  • Live Template : 

    Code completion can improve your productivity by reducing how much you have to type, but there are situations when a more powerful tool is needed. Thanks to Android Studio and IntelliJ, live templates make it much easier to focus on just the things you care about. You can read one of the grate article regarding Live Template . You have to create CKAndroidUtils snippet file (XML)  which contains your code in XML formate and that will be use to access the function using short cut key : Ctrl + Alt + Shift + J  FUNCTION NAME .

  • How to create your share with others :  

    1. Path : File > Settings > Editor > Live Template > .. You can see Add or remove snippet code or template group from “+” or “-“ from right side button.

    2. Now choose Android group or own created template to add snippet code. That will ask you Abbreviation = Function name , Description = What function do, Template text = your code, Type = java , xml ,other. Apply > Ok Done!   Now search it using give short cut key and access function any where in your application.

    3. Now you can share added snippet code to other developer and setup same file to get access.

  • Check out below steps to configure the XML :

    1. Download CKAndroidUtils snippet file 

    2. Go to “/.Android Studio/config /templetes folder (by default is hide folder at user /dir)
      i.e /home/sotsys073/.AndroidStudio/config/templetes

    3. Paste “CKAndroidUtils” file in above /dir.

    4. Restart android studio and check CKAndroidUtils is available in File >> Settings >> Live template >> CKAndroidUtils

    5. Short cut to access function : Ctrl + Alt + Shift + J  FUNCTION NAMENow you  are able to add function in template and know how to use it . Download CKAndroidUtils.java file and create your snippet or use directly. Start it now!!! Hope this will relay helpful for all of you. 

      Note : Please add function which you are using in project and not listed in below list so that will be helpful for other

  • List of Utils Function

    Index

    Function Name
    1
    askToExit()
    2
    showYesNoDialog()
    3
    showOkDialog()
    4
    isNetworkEnable()
    5
    isLocationEnable()
    6
    showProgressDialog()
    7
    hideProgressDialog()
    8
    showToast()
    9
    hideSoftKeyboard()
    11
    openSoftKeyboard()
    12
    dpToPixel()
    13
    pixelToDp()
    14
    getDeviceDensity()
    15
    getScreenHeight()
    16
    getScreenWidth()
    17
    getScreenSize()
    18
    openDialer()
    19
    isPlayServiceExist()
    20
    setDynamicListViewHeight()
    21
    switchActivity()
    22
    callActivityForResult()
    23
    adjustImageOrientation()
    24
    getRotateBitmap()
    25
    getCropBitmapToSquare()
    26
    getRoundedCornerBitmap()
    27
    getResizedBitmap()
    28
    getBitmapFromDrawable()
    29
    getDrawableFromUrl()
    30
    getMaskedBitmap()
    31
    getMaskedImageByColor()
    32
    getStorageDirectory()
    33
    downloadImageFromUrl()
    34
    moveFile()
    35
    copyFile()
    36
    deleteFile()
    37
    createDirectory()
    38
    shareDefaultIntent()
    39
    shareImageviaIntent()
    40
    calculateInSampleSize()
    41
    loadBase64toImageView()
    42
    getFileFromPath()
    43
    getBitmapFromPath()
    44
    getFileFromBitmap()
    45
    sendMailFromGmail()
    46
    showFBUserProfile()
    47
    showTwitterUserProfile()
    48
    showInstagramUserProfile()
    49
    getImagePathFromUri()
    50
    getUriFromBitmap()
    51
    getBitmapFromFile()
    52
    drawableToBitmap()
    53
    saveBmpToJPG()
    54
    resizeImageScale()
    55
    isRemoteFileExists()
    56
    cropCenter()
    57
    getBlurImagFromBitmap()
    58
    getBlurImagUsingRenderScript()
    59
    getThumbnail()
    60
    getBlurImageUsingGaussianBlur()
    61
    isCameraExist()
    62
    getDistanceBetweenLocation()
    63
    getAddressFromLocation()
    64
    getLocationFromAddress()
    65
    getLatitudeFromAddress()
    66
    getLongitudeFromAddress()
    67
    getAddressFromLocationByAPI()
    68
    setDirectionURL()
    69
    getZoomLevelByMarkers()
    70
    getZoomLevelByPolyline()
    71
    getZoomLevelByRadius()
    72
    getRadiusFromMeter()
    73
    getZoomLevelByMeters()
    74
    animateMarker()
    75
    initFragment()
    76
    addFragment()
    77
    replaceFragment()
    78
    removeFragment()
    79
    isFragmentExist()
    80
    showAdmobInterstitial()
    81
    setAdmobBanner()
    82
    setupGA()
    83
    SendDataToGA()
    84
    UncaughtExceptionHandler()
    85
    getCountryZipCode()
    86
    createFackBookHashKey()
    87
    isvalidString()
    88
    isvalidDouble()
    89
    isValidText()
    90
    isValidPassword()
    91
    isValidNumber()
    92
    isValidEmail()
    93
    rateTheApp()
    94
    shareTheApp()
    95
    getCountry()
    96
    callToPerson()
    97
    cancelNotification()
    98
    loadWebpage()
    99
    SetLocale()
    100
    getNetworkType()
    101
    IsServiceRunning()
    102
    getDeviceId()
    103
    getAppVersion()
    104
    getIPAddress()
    105
    getWIFIAddress()
    106
    getIMEI()
    107
    getBluetoothAddress()
    108
    getAccount()
    109
    isAppInstalled()
    110
    getCapcha()
    111
    getRandomInt()
    112
    getUniqueDeviceId()
    113
    printLog()
    114
    encryptToMD5()
    115
    compareDate()
    116
    isValidDate()
    117
    getMonthName()
    118
    getCurrentTime()
    119
    openDatePicker()
    120
    setCurrentDate()
    121
    openTimePicker()
    122
    getTimeStampFromDate()
    123
    getUTCDate()
    124
    getDaysCount()
    125
    getDateDifference()
    126
    showSnackBar()
    127
    NFCUtilsClass