Skip to content

Instantly share code, notes, and snippets.

Interview Questions

Kotlin

Q1: What is a primary constructor in Kotlin? ☆☆

Answer: The primary constructor is part of the class header. Unlike Java, you don't need to declare a constructor in the body of the class. Here's an example:

@nknr
nknr / gist:8c143cd85a9abd6f8a4588ce295b6c14
Created August 13, 2021 07:08 — forked from dodyg/gist:5823184
Kotlin Programming Language Cheat Sheet Part 1

#Intro

Kotlin is a new programming language for the JVM. It produces Java bytecode, supports Android and generates JavaScript. The latest version of the language is Kotlin M5.3

Kotlin project website is at kotlin.jetbrains.org.

All the codes here can be copied and run on Kotlin online editor.

Let's get started.

@nknr
nknr / NetworkHelper.kt
Created February 23, 2021 09:54
Coroutine network helper
enum class NetworkStatus {
INITIAL,SUCCESS, ERROR, UNAUTHORIZED
}
suspend fun <T> safeApiCall(dispatcher: CoroutineDispatcher, apiCall: suspend () -> T): Resource<T> {
return withContext(dispatcher) {
try {
Resource.success(apiCall.invoke())
} catch (exception:Exception) {
Timber.e("error $exception")
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import java.io.File;
import java.io.FileInputStream;
private void showDialog(){
AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(this);
builder.setTitle(R.string.exit);
builder.setMessage(R.string.exit_text);
builder.setPositiveButton(getResources().getString(R.string.yes), (dialog, arg1) -> finishAffinity());
builder .setNegativeButton(getResources().getString(R.string.no), (dialog, arg1) -> dialog.dismiss());
androidx.appcompat.app.AlertDialog dialog = builder.create();
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation_2;
dialog.show();
}
@nknr
nknr / CustomAutoCompleteAdapter
Last active October 17, 2019 13:23
CustomAutoCompleteTextView
public class CustomAutoCompleteAdapter extends ArrayAdapter<CustomModel> implements Filterable {
private List<CustomModel> fullList;
private List<CustomModel> mOriginalValues;
private ArrayFilter mFilter;
public CustomAutoCompleteAdapter(Context context, int resource, List<CustomModel> objects) {
super(context, resource, objects);
fullList = objects;
mOriginalValues = new ArrayList<>(fullList);
@nknr
nknr / MainActivity.java
Last active July 18, 2019 12:26
Android Download Manager
String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
private int RC_PERMISSIONS = 1;
private long downloadID;
private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//Checking if the received broadcast is for our enqueued download by matching download id
if (downloadID == id) {