Pass Drawables Between Activities in Android Using Kotlin

Azhar
Updated on 18-Aug-2020 09:03:20

362 Views

This example demonstrates how to pass drawables between activities in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.content.Intent import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp"       val button: Button = findViewById(R.id.button) ... Read More

Send Email with File Attachment in Android using Kotlin

Azhar
Updated on 18-Aug-2020 09:00:11

2K+ Views

This example demonstrates how to send an email with a file attachment in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.                                 Step 3 − Add the following code to src/MainActivity.ktimport android.content.Intent import android.net.Uri import android.os.Bundle import android.view.View import android.widget.Button import android.widget.EditText import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var ... Read More

Change Android Overflow Menu Icon Programmatically Using Kotlin

Azhar
Updated on 18-Aug-2020 08:55:55

870 Views

This example demonstrates how to change the Android Overflow menu icon programmatically.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.ktimport android.graphics.Color import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.Toast import androidx.appcompat.widget.Toolbar import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var toolBar: Toolbar    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp" ... Read More

JavaScript Array Includes in Nested Array Returning False

AmitDiwan
Updated on 18-Aug-2020 07:43:39

1K+ Views

It is a well-known dilemma that when we use includes() inside nested arrays i.e., multidimensional array, it does not work, there exists a Array.prototype.flat() function which flats the array and then searches through but it’s browser support is not very good yet.So our job is to create a includesMultiDimension() function that takes in an array and a string and returns a boolean based on the presence/absence of that string in the array.There exist many solutions to this problem, most of them includes recursion, heavy array function, loops and what not.What we are going to discuss here is by far the ... Read More

Cap Values When Summing Two Arrays in JavaScript

AmitDiwan
Updated on 18-Aug-2020 07:42:25

112 Views

Suppose, we have two arrays both containing three elements each, the corresponding values of red, green, blue color in integer.Our job is to add the corresponding value to form an array for new rgb color and also making sure if any value adds up to more than 255, we that value to 255.Therefore, let’s define a function addColors() that takes in two arguments, both arrays and returns a new array based on the input.The code for this will be −Exampleconst color1 = [45, 125, 216]; const color2 = [89, 180, 78]; const addColors = (color1, color2) => {    const newColor = color1.map((val, index) => {       return val + color2[index]

Title Case a Sentence in JavaScript

AmitDiwan
Updated on 18-Aug-2020 07:41:22

298 Views

Let’s say we are required to write a function that accepts a string and capitalizes the first letter of every word in that string and changes the case of all the remaining letters to lowercase.For example, if the input string is −hello world coding is very interestingThe output should be −Hello World Coding Is Very InterestingLet’s define a function capitaliseTitle() that takes in a string and capitalises the first letter of each word and returns the string −Examplelet str = 'hello world coding is very interesting'; const capitaliseTitle = (str) => {    const string = str    .toLowerCase()   ... Read More

Find Integer That Appears Odd Number of Times in JavaScript Array

AmitDiwan
Updated on 18-Aug-2020 07:40:26

198 Views

We are given an array of integers and told that all the elements appear for an even number of times except a single element. Our job is to find that element in single iteration.Let this be the sample array −[1, 4, 3, 4, 2, 3, 2, 7, 8, 8, 9, 7, 9]Before attempting this problem, we need to understand a little about the bitwise XOR (^) operator.The XOR operator returns TRUE if both the operands are complementary to each other and returns FALSE if both the operands are the same.TRUTH TABLE OF XOR () operator −0 ^ 0 → 0 ... Read More

Add Two JavaScript Arrays of Objects and Replace Repeated Members

AmitDiwan
Updated on 18-Aug-2020 07:39:37

154 Views

We have the following arrays of objects, we need to merge them into one removing the objects that have redundant value for the property name −const first = [{    name: 'Rahul',    age: 23 }, {    name: 'Ramesh',    age: 27 }, {    name: 'Vikram',    age: 35 }, {    name: 'Harsh',    age: 34 }, {    name: 'Vijay',    age: 21 }]; const second = [{    name: 'Vijay',    age: 21 }, {    name: 'Vikky',    age: 20 }, {    name: 'Joy',    age: 26 }, {    name: 'Vijay',   ... Read More

Merge Object Properties Through Unique Field and Print Data in JavaScript

AmitDiwan
Updated on 18-Aug-2020 07:36:56

152 Views

Let’s say we have a students object containing two properties names and marks. The names is an array of object with each object having two properties name and roll, similarly marks is an array of object with each object having properties mark and roll. Our task is to combine the marks and names properties according to the appropriate roll property of each object.The students object is given here −const students = {    marks: [{       roll: 123,       mark: 89    }, {       roll: 143,       mark: 69    }, ... Read More

Print Star Pattern in JavaScript

AmitDiwan
Updated on 18-Aug-2020 07:32:48

1K+ Views

Here is a simple star pattern that we are required to print inside the JavaScript console. Note that it has to be printed inside the console and not in the output or HTML window −* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Here’s the code for doing so in JavaScript −Exampleconst star = "* "; //where length is no of stars in longest streak const length = 6; for(let i = 1; i

Advertisements