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

20 comments :

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. What an extremely wonderful post this is. Genuinely, perhaps the best post I've at any point seen to find in as long as I can remember. Goodness, simply keep it up.
    360DigiTMG

    ReplyDelete
  8. I feel extremely glad to have seen your site page and anticipate such a large number of additionally engaging occasions perusing here. Much obliged again for all the subtleties.
    360DigiTG supply chain analytics beginner's guide

    ReplyDelete
  9. I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
    data science malaysia

    ReplyDelete
  10. incredible article!! sharing these kind of articles is the decent one and I trust you will share an article on information science.By giving an organization like 360DigiTMG.it is one the best foundation for doing guaranteed courses
    data science course noida

    ReplyDelete
  11. You delivered such an impressive piece to read, giving every subject enlightenment for us to gain information. Thanks for sharing such information with us due to which my several concepts have been cleared. basic knowledge hub

    ReplyDelete
  12. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. spacedesk

    ReplyDelete
  13. I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. palm hills alexandria

    ReplyDelete
  14. JavaScript is among the most powerful and flexible programming languages of the web. It powers the dynamic behavior on most websites, including this one. You will learn programming fundamentals and basic object-oriented concepts using the latest JavaScript syntax. The concepts covered in these lessons lay the foundation for using JavaScript in any environment. free udemy courses to learn kotlin

    ReplyDelete
  15. Hey mate, .This was a great post for such a complicated subject to discuss. I look forward to seeing many more excellent posts like this one. Thanks Tutoriales de java en espanol

    ReplyDelete
  16. สล็อตเว็บตรง เป็นเกมส์สล็อตออนไลน์ที่มีคนนิยมและเล่นหรือใช้บริการเป็นตจำนวนมากและ PG Slotเป็นตัวเกมส์ที่ค่อยหารายได้เสริมให้กับตัวเองและเป็นตัวเกมส์สล็อตออนไลน์ชั่นนำที่มีคนเล่นและใช้บริการมากกว่าในทุกๆปี PG Slot

    ReplyDelete
  17. It's great to return to your blog; I haven't visited in a very long time.
    Thankfully, this piece has been on my mind for a very long time. I will need this post to supplement my schoolwork, which also contains a similar topic and your review. Thank you for the excellent offer.power bi course malaysia

    ReplyDelete
  18. Individuals in Malaysia who complete an information examination course will have the information and capacities expected to dissect and comprehend information in various areas appropriately. The center thoughts of informationexamination, factual examination, information perception, and information mining are normally remembered for an information examination course's educational program in Malaysia. In Malaysia, colleges, schools, preparingoffices, and web based learning stages all give information examination courses.
    Data analytics course malaysia

    ReplyDelete

Your comments are valuable for us !!!