Often times, there are situation where we don’t know much parameters or arguments would be passed in method. This is done in Java using three dots like this: void anySampleMethod(Int... intList) // Note the three dots after the type { // Now you can access these in for loop. for (Int myInt : intList) { // Do anything with myInt here myInt += 10; } } // When calling this method anySampleMethod(1,2,3,4); // Passed 4 arguments anySampleMethod(2); // Passed 1 argument To do this in Kotlin, we will use vararg which means variable arguments.

Continue reading

While looking through answers on StackOverflow or codes through GitHub about Kotlin, you might have noticed keywords like inline, noinline or crossinline in the methods signatures. Today, I learned about what inline is really about. And will explore other types later. What is inline method, then? Say you have a higher order function in Kotlin, fun higherOrderFunction(callback: () -> Unit) { doSomething() callback() doAnotherThing() } Now, when this is converted to Java, this will look something like this:

Continue reading

When it comes to debugging tools and libraries for Android such as Stetho or Chuck or the Jake Wharton’s great Leak Canary, there’re almost separate modules for debug and release builds. For example, when you add Leak Canary in your app, you will be adding dependencies like this: debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4' releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' Please note that there are two separate modules or AARs for debug and release. Its a common practice to call release variants for debugging and inspection tools as NO OP or No Operations.

Continue reading

We often use View’s visibility in our apps to show and hide them. We use void setVisibility(int visibility) method for that purpose. But have you ever thought that why this method always takes VISIBLE, INVISIBLE and GONE rather than any int value like 0 or 1 etc.? Although method’s parameter type is int, then why it doesn’t accept the direct numbers or any other int variable except those three. In java, enum is known concept, and in many cases you can use it, but for android, enum is something you should avoid to use as it’s processing performance is not efficient, so in Android performance patterns it’s told to avoid enums and to use annotations like @IntDef and @StringDef.

Continue reading

A collection of concise write-ups on the things I learn day to day In this age of busy life with smartphones and laptops, we have almost started taking things we learn on daily basis for granted. Whether its just a minor semicolon missing error or any run-time crash exception, we learn lots of things in just coding for an hour even. These things and tips are so small that we don’t give much value to these and next day or day after that, we almost search for same thing again.

Continue reading

In the app, which I am making at my job, I had a situation where I needed a huge number of tabs with the Fragment View Pager. I used a typical TabLayout and ViewPager with a custom FragmentPagerAdapter class as the adapter for ViewPager. This is TabLayout and ViewPager in XML layout. <android.support.design.widget.CoordinatorLayout> <android.support.design.widget.AppBarLayout> <android.support.v7.widget.Toolbar/> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" app:tabMode="scrollable"/> </<android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> But, as I run it, this is how it looked like:

Continue reading

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 guessed it right. I am talking about Live Templates. For example, to show a simple Toast in android apps, we use something like this:

Continue reading

Author's picture

Wajahat Karim

Android Developer. UI/UX Designer. Blogger. Writer. Wantrepreneur. GitHub Geek. Tea Lover

Senior Android Developer

Karachi, Pakistan