TextUtils Importance In Android

in #android6 years ago

 Android Utility Library.

TextUtils is simply a set of utility functions to do operations on String objects. 

                                                                                              .   .   . 

I  usually have gone through many projects & I have seen many times in  the code there is no use of TextUtils, I am not saying that it’s  necessary to use it but if we can reduce some extra work so then why  not?

                                                                                              .   .   .

 Let’s take an example which I usually see in the projects, it’s a simple  example like we all do, the string null check and empty check. You can  all see the code below: 

  private void onLocationReceived() {    String location = getLocationName(mLocationModel.getLat(), mLocationModel.getLng());    if(location!=null && !location.isEmpty())    //your work           }        private String getLocationName(double lattitude, double longitude) {    String locationStr = "";    Geocoder gcd = new Geocoder(getActivity(), Locale.getDefault());    try {    List<Address> addresses = gcd.getFromLocation(lattitude, longitude, 5);    for (Address adrs : addresses) {    if (adrs != null) {                       city = (adrs.getLocality() == null) ? "" : adrs.getLocality();                       country = adrs.getCountryName();                       state = (adrs.getAdminArea() == null) ? "" : adrs.getAdminArea();                       postalcode = adrs.getPostalCode();        StringBuilder sb = new StringBuilder();                       sb.append(city);        if (!city.isEmpty() && !state.isEmpty())                           sb.append(", ");                           sb.append(state);                       locationStr = sb.toString();        if (!city.isEmpty() && !state.isEmpty()) {    break;                       }                   }               }           } catch (IOException e) {               e.printStackTrace();           }    return locationStr;       } 


                                                                                               .   .   .


 Like In the above code we checked in the onLocationReceived() method the location is not null and not empty then do our work there, so if we replace with TextUtils utility function isEmpty() so how it will look, let’s check the code below. 


 private void onLocationReceived() {    String location = getLocationName(mLocationModel.getLat(), mLocationModel.getLng());    if(!TextUtils.isEmpty(location))    //your work           }        private String getLocationName(double lattitude, double longitude) {    String locationStr = "";    Geocoder gcd = new Geocoder(getActivity(), Locale.getDefault());    try {    List<Address> addresses = gcd.getFromLocation(lattitude, longitude, 5);    for (Address adrs : addresses) {    if (adrs != null) {                       city = (adrs.getLocality() == null) ? "" : adrs.getLocality();                       country = adrs.getCountryName();                       state = (adrs.getAdminArea() == null) ? "" : adrs.getAdminArea();                       postalcode = adrs.getPostalCode();        StringBuilder sb = new StringBuilder();                       sb.append(city);        if (!city.isEmpty() && !state.isEmpty())                           sb.append(", ");                           sb.append(state);                       locationStr = sb.toString();        if (!city.isEmpty() && !state.isEmpty()) {    break;                       }                   }               }           } catch (IOException e) {               e.printStackTrace();           }    return locationStr;       } 


                                                                                               .   .   .

 So, we easily replaced the null & empty check with TextUtils.IsEmpty() in the above code, there is no need to check whether the value is null or not, as TextUtils.IsEmpty() function check both the values, see the below screenshot. 


                                                                                               .   .   .

 This is  the simple example I explained there are many functions in this class,  you can check the list from the official site below: 

https://developer.android.com/reference/android/text/TextUtils


                                                                                               .   .   .

Thanks for reading this article. Be sure to Upvote this article if you found it helpful. It means a lot to me. 


Sort:  

Congratulations @lvlasuod! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!