Last Updated: October 30, 2019

Tips and Tricks android developer should know - part 5

Description: Hello coders! In this post I'm gonna show some interesting tips and tricks you can start using in your android project.

Also check out Part 1,Part 2Part 3 and Part 4

So let's get started.

1. How to remove boiler plate code of  Parcelable interface?

Let's consider this class with Parcelable Interface implementation. This class contains overriden methods which are boiler plate at larger extends

class User(val id: String, val name: String, val age: Long, val address: String, val isPrime: Boolean, val contact: String) : Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readString(),
            parcel.readLong(),
            parcel.readString(),
            parcel.readByte() != 0.toByte(),
            parcel.readString())

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(id)
        parcel.writeString(name)
        parcel.writeLong(age)
        parcel.writeString(address)
        parcel.writeByte(if (isPrime) 1 else 0)
        parcel.writeString(contact)
    }

    override fun describeContents(): Int = 0

    companion object CREATOR : Parcelable.Creator {
        override fun createFromParcel(parcel: Parcel): User {
            return User(parcel)
        }

        override fun newArray(size: Int): Array {
            return arrayOfNulls(size)
        }
    }

}

So here comes @Parcelize annotation into picture. Lets modify our User class using @Parcelize
@Parcelize
class User(val id: String, val name: String, val age: Long, val address: String, 
           val isPrime: Boolean, val contact: String) : Parcelable

@Parcelize is the part of androidExtensions we have to enable it by adding below snippets within module's build.gradle section.
android {
    ...
    androidExtensions {
        experimental true
    }
    ...
}

2. Using Plurals

Although plurals where introduced long time back, but have seen developers adding logics for plural strings. Let's take an example of adding plural, when any post is liked.
    <plurals> name="like_count">
        <item quantity="one">%d second ago</item>
        <item quantity="other">%d seconds ago</item>
    </plurals>


Access within the code
mContext.resources.getQuantityString(R.plurals.like_count, 1, 2)

#Output:
2 second ago

#Note: First argument matches the plural and second is the argument for %d 

3. How to Change Default layout of activity?


Its interesting because it will help to add some constant template(toolbar in all the activity etc ) which we normally add after the activity is created, henceforth saving time. Simply Go to File -> New -> Edit File Templates


Edit Templates
Edit Templates

Change Default Tag of any layout.xml
Changing Root Tag for layouts

In the above example we can simply replace  ROOT_TAG with <LinearLayout> and next time when we create new layout it will start with <LinearLayout>


Bingo we're done.

For more updates follow us on -  Twitter

#codingIsAnArt
#coderconsole

15 comments :

  1. Enroll in the best Autocad institute in Delhi and Noida that is High technologies solutions. Web at HTS provides Real life case studies, live projects, Assignments, free demo class and placement assistance. Call us for more details at 9311002620 or visit our website.
    autocad training institute in Delhi
    autocad training institute in Noida

    ReplyDelete
  2. Enroll in the leading Autocad institute in Delhi and Noida.High Technologies Solutions believes in quality training and provide innovative friendly environment.We also provide live projects, Assignments, free demo class and placement assistance. Call at 9311002620.
    autocad training institute in Delhi
    autocad training institute in Noida

    ReplyDelete
  3. The blog and data is excellent and informative as well
    data science course
    360DigiTMG

    ReplyDelete
  4. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
    data analytics course in Bangalore

    ReplyDelete
  5. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data analytics course
    data science course
    big data course
    360DigiTMG

    ReplyDelete
  6. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
    data science course in malaysia
    data science certification
    data science course malaysia
    data science malaysia
    data scientist course malaysia

    ReplyDelete
  7. It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article. I want to read more things about it!

    PMP Course in Malaysia

    ReplyDelete
  8. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data analytics course
    data science course
    big data course
    big data course
    360DigiTMG

    ReplyDelete
  9. I think I have never seen such blogs ever before that has complete things with all details which I want. So kindly update this ever for us.
    data science course in malaysia

    ReplyDelete
  10. This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again. 360DigiTMG PMP Certification
    PMP Certification in Malaysia
    PMP Course in Malaysia
    PMP Course
    PMP training

    ReplyDelete
  11. I really enjoy reading and also appreciate your work.
    data science course

    ReplyDelete
  12. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data analytics course
    big data analytics malaysia
    big data course

    ReplyDelete
  13. It's very useful article with inforamtive and insightful content and i had good experience with this information.Enroll today to get free access to our live demo session which is a great opportunity to interact with the trainer directly which is a placement based Salesforce training India with job placement and certification . I strongly recommend my friends to join this Salesforce training institutes in hyderabad practical course, great curriculum Salesforce training institutes in Bangalore with real time experienced faculty Salesforce training institutes in Chennai. Never delay to enroll for a free demo at Salesforce training institutes in Mumbai who are popular for Salesforce training institutes in Pune.

    ReplyDelete
  14. Most cell phone and tablet producers give certain inbuilt applications in the gadgets. One of the most significant segments that come worked in is the UI.mx player

    ReplyDelete

Your comments are valuable for us !!!