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
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
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
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
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]
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP