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:
#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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<paths> | |
<external-path | |
name="external" | |
path="." /> | |
<external-files-path | |
name="external_files" | |
path="." /> | |
<cache-path | |
name="cache" | |
path="." /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |