This example demonstrates how to work with Camera in an Android App 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.Example Step 3 − Add the following code to src/MainActivity.ktimport android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.graphics.Bitmap import android.os.Bundle import android.provider.MediaStore import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat class MainActivity : AppCompatActivity() { private val cameraRequest = 1888 lateinit var imageView: ImageView override fun onCreate(savedInstanceState: Bundle?) ... Read More
Suppose we have a set of words (all are unique), we have to find all word squares and we can build from them. Here a sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < maximum of numRows and numColumns. So as an example, the word sequence ["ball", "area", "lead", "lady"] will construct a word square because each word reads the same both horizontally and vertically.ballarealeadladySo, if the input is like ["area", "lead", "wall", "lady", "ball"], then the output will be [[ "wall", "area", "lead", "lady"], ... Read More
Suppose we have N axis-aligned rectangles, we have to check whether they all together form an exact cover of a rectangular region or not. Here each rectangle is represented as a bottom-left point and a top-right point. So a unit square is represented as [1, 1, 2, 2]. (bottom-left point is (1, 1) and top-right point is (2, 2)).So, if the input is like rectangles = [[1, 1, 3, 3], [3, 1, 4, 2], [3, 2, 4, 4], [1, 3, 2, 4], [2, 3, 3, 4]], then the output will be true as all 5 rectangles together form an exact ... Read More
Suppose we have a string such as "word" and that contains the following abbreviations: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]. We also have a target string and a set of strings in a dictionary, we have to find an abbreviation of this target string with the smallest possible length such that it does not conflict with abbreviations of the strings in the dictionary. Here each number or letter in the abbreviation is considered as length = 1. So as an example, the abbreviation "a32bc" is of length = 4.So, if ... Read More
This example demonstrates how to create animation using XML files in an Android App 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.Example Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.view.View import android.view.animation.Animation import android.view.animation.AnimationUtils import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity(), Animation.AnimationListener { private lateinit var textView: TextView private lateinit var buttonZoom: Button private lateinit var buttonBlink: Button ... Read More
Suppose we have a non-empty string s and an integer k; we have to rearrange the string such that the same characters are at least distance k from each other. Given strings are in lowercase letters. If there is no way to rearrange the strings, then we will an empty string.So, if the input is like s = "aabbcc", k = 3, then the output will be "abcabc" this is because same letters are at least distance 3 from each other.To solve this, we will follow these steps −ret := an empty stringDefine one map mn := size of sfor ... Read More
This example demonstrates how to create GridView Layout in an Android App 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.Example Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.widget.AdapterView.OnItemClickListener import android.widget.GridView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { lateinit var gridView: GridView private var playerNames = arrayOf("Cristiano Ronaldo", "Joao Felix", "Bernado Silva", "Andre Silve", "Bruno Fernandez", "William Carvalho", "Nelson Semedo", "Pepe", "Rui Patricio") ... Read More
Suppose we have a string; we have to calculate the length of the longest substring T that contains at most k distinct characters.So, if the input is like s = "eceba", k = 2, then the output will be 3 as T is "ece" which its length is 3.To solve this, we will follow these steps −ans := 0Define one map mn := size of sx := 0for initialize j := 0, i := 0, when j < n, update (increase j by 1), do −(increase m[s[j]] by 1)if m[s[j]] is same as 1, then −(increase x by 1)while (x > k and i k && i
This example demonstrates how to switch between different 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.Example 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.btnOpenAct2) button.setOnClickListener ... Read More
This example demonstrates how to create Tab Layout in an Android App 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.Example Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.viewpager.widget.ViewPager import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout.OnTabSelectedListener import com.google.android.material.tabs.TabLayout.TabLayoutOnPageChangeListener class MainActivity : AppCompatActivity() { lateinit var tabLayout: TabLayout lateinit var viewPager: ViewPager override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ... Read More