This file contains hidden or 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
| // Creating a Kotlin Class | |
| class KotlinClass(var id: Long, var name: String, var url: String, var mbid: String) { | |
| fun print() { | |
| println("Printing through the function defined in the class") | |
| } | |
| } | |
| // This is our main function - which is pretty similar to the main method of Java | |
| fun main(args: Array<String>) { | |
| // Creating a variable of type KotlinClass (defined above) and assigning null and adding a '?' to tell that this variable can be null |
This file contains hidden or 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
| // Just imagine a closed existing class which you cannot modify, but you want to add some additional functionality to it | |
| class KotlinClass(var id: Long, var name: String, var url: String, var mbid: String) { | |
| fun print() { | |
| println("Printing through the function defined in the class") | |
| } | |
| } | |
| fun main(args: Array<String>) { |
This file contains hidden or 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
| Button button = null; // Just creating a null button | |
| button.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Welcome To Main Activity", Snackbar.LENGTH_LONG); | |
| snackbar.show(); | |
| } | |
| }); |
This file contains hidden or 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
| button.setOnClickListener {MainActivity.showSnackBarMessage("Kotlin is awesome")} |
This file contains hidden or 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
| // Creating a function to add two Int in Kotlin : | |
| fun add(number1: Int, number2: Int): Int { | |
| return number1 + number2 | |
| } | |
| // The above function can be simplified and can be written as | |
| fun add(number1: Int, number2: Int): Int = number1 + number2 |
This file contains hidden or 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
| // Create a function and pass default values | |
| fun addNumbers(num1: Int = 0, num2: Int = 0, num3: Int = 0): Int = num1 + num2 + num3 | |
| // These all are valid calls | |
| addNumbers() | |
| addNumbers(1) | |
| addNumbers(1,2) | |
| addNumbers(1,2,3) |
This file contains hidden or 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
| // Create a function and pass default values | |
| fun addNumbers(num1: Int = 0, num2: Int = 0, num3: Int = 0): Int = num1 + num2 + num3 | |
| // These all are valid calls | |
| addNumbers() | |
| addNumbers(1) | |
| addNumbers(1,2) | |
| addNumbers(1,2,3) | |
| // And if you just want to pass second and third argument, you can do like this : |
This file contains hidden or 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
| class ActivityObserver : LifecycleObserver { | |
| private val TAG = this.javaClass.simpleName | |
| // OnCreate Event | |
| @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) | |
| public fun onCreateEvent() { | |
| Log.d(TAG, "Observer OnCreate") | |
| } | |
| // OnPause Event |
This file contains hidden or 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
| class ProcessImageActivity : AppCompatActivity() { | |
| // Declaring viewModel variable | |
| private lateinit var viewModel : ProcessImageViewModel | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_process_image) | |
| // Attaching the view model | |
| viewModel = ViewModelProviders.of(this).get(ProcessImageViewModel::class.java) | |
This file contains hidden or 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
| class ProcessImageViewModel : ViewModel() { | |
| private val TAG = this.javaClass.simpleName | |
| override fun onCleared() { | |
| super.onCleared() | |
| // This function gets called before view model gets destroyed | |
| } | |
| fun uploadImageAndRecognize(imageBitmap : Bitmap) { |