Skip to main content

Posts

Showing posts from February, 2020

android kotlin - Add a hint to spinner example

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Color import android.graphics.Typeface import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout class MainActivity : AppCompatActivity() { private lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the context context = this // Get the widgets reference from XML layout val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) val spinner = findViewById<Spinner>(R.id.spinner) val textView = findViewById<TextView>(R.id.textView) // list of spinner items val list = mutableListOf( ...

android kotlin - Spinner onItemSelectedListener example

Spinner onItemSelectedListener The Spinner provides an easy way to select an item from a list. The Spinner always shows its currently selected item only. Other items remain invisible and users can see them when they click on the Spinner itself. On Spinner click, it displays a drop-down menu with all other available items. So, the user can select an item from all of the available items. The following android application development tutorial will demonstrate to us how we can set an item selected listener for the Spinner widget. The Spinner AdapterView OnItemSelectedListener registers a callback to invoke when an item in this AdapterView has been selected. So developer gets the Spinner selected value immediately when the user selects it or changes their selection. To populate a Spinner widget with items we have to create an item list. Then we create an instance of ArrayAdapter with this list. After that, we bind the Spinner widget with this ArrayAd...

android kotlin - Spinner selected item background color

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Color import android.graphics.Typeface import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.* import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the context context = this // Get the widgets reference from XML layout val spinner = findViewById<Spinner>(R.id.spinner) // list of spinner items val list = listOf( "Honeydew", "Laurel green", "Light salmon", "Bright maroon", "Cherry blossom pink", ...

android kotlin - Spinner text size programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.Typeface import android.os.Bundle import android.util.TypedValue import android.view.View import android.view.ViewGroup import android.widget.* class MainActivity : Activity() { private lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the context context = this // get the widgets reference from XML layout val spinner = findViewById<Spinner>(R.id.spinner) // list of spinner items val list = listOf( "Alice blue", "Amaranth purple", "Antique brass", "Apple green", "Atomic tangerine", "Bitter lime", "Black chocolate" ) ...

android kotlin - ConstraintLayout center in parent programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet import androidx.constraintlayout.widget.ConstraintSet.* import androidx.transition.TransitionManager class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) val checkbox = findViewById<CheckBox>(R.id.checkbox) val button = findViewById<Button>(R.id.button) button.setOnClickListener { val constraintSet = ConstraintSet() constraintSet.clone(rootLayout) // this block will put it horizontall...

android kotlin - ConstraintLayout set margin programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.content.Context import android.os.Bundle import android.util.TypedValue import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout class MainActivity : AppCompatActivity() { private lateinit var context: Context override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the context context = this; // Get the widgets from XML layout val button = findViewById<Button>(R.id.button); val textView = findViewById<TextView>(R.id.textView); // Text for text view val string = "Lorem Ipsum is simply dummy text of the printing" + " and typesetting industry. Lorem Ipsum has been the" + " industry's standard dummy text ever sin...

android kotlin - ConstraintLayout remove constraint programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet import androidx.constraintlayout.widget.ConstraintSet.* class MainActivity : AppCompatActivity() { private lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the context context = this // Get the widgets reference from XML layout val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) val textView = findViewById<TextView>(R.id.textView) val button = findViewById<Button>(R.id.button) button.setOnClickListener { val constraintSet = ConstraintSet() constraintSet.clone(rootLayo...

android kotlin - ConstraintLayout set constraint programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.content.Context import android.os.Bundle import android.util.TypedValue import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet import androidx.constraintlayout.widget.ConstraintSet.* class MainActivity : AppCompatActivity() { private lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the context context = this // Get the widgets reference from XML layout val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) val textView = findViewById<TextView>(R.id.textView) val button = findViewById<Button>(R.id.button) button.setOnClickListener { val constraint...

android kotlin - Underline TextView text programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Bundle import android.text.SpannableString import android.text.style.UnderlineSpan import android.widget.* import androidx.core.text.HtmlCompat class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val buttonPaint = findViewById<Button>(R.id.buttonPaint) val buttonSpan = findViewById<Button>(R.id.buttonSpan) val buttonHtml = findViewById<Button>(R.id.buttonHtml) val buttonHtmlPart = findViewById<Button>(R.id.buttonHtmlPart) val textView = findViewById<TextView>(R.id.textView) // text to show on text view val string = "Lorem Ipsum is simply dummy text of the printing" + ...

android kotlin - Add view to ConstraintLayout programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.content.Context import android.graphics.Color import android.graphics.Typeface import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.util.TypedValue import android.view.View import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams import androidx.constraintlayout.widget.ConstraintSet import androidx.core.widget.TextViewCompat class MainActivity : AppCompatActivity() { private lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the context context = this // Get the widgets reference from XML layout val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout)...

android kotlin - How to get substring from string

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry." textView.text = string // Returns a substring of this string that starts at the // specified startIndex and continues to the end of the string. val result = string.substring( 50 // ...

android kotlin - String replace range example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry. Lorem Ipsum" + " has been the industry's standard dummy text" + " ever since the 1500s." textView.text = string // The end index of the range is included ...

android kotlin - String replace example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry. Lorem Ipsum" + " has been the industry's standard dummy text" + " ever since the 1500s." textView.text = string /* documentation source kotlin...

android kotlin - String remove surrounding example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry. Lorem Ipsum" textView.text = string /* documentation source kotlinlang.org Removes the given delimiter string from both the start and the end of this string if and o...

android kotlin - String remove range example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry." textView.text = string // Removes the part of a string at the given range. // The end index of the range is included in the removed part. val result = string.removeRange( 10..50 //...

android kotlin - String remove prefix and suffix example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string1 = "This is a sample string to" + " test remove prefix function." textView.text = string1 /* documentation source kotlinlang.org If this string starts with the given prefix, returns a copy of this string with the prefix removed. Otherwise, ret...

android kotlin - How to match two strings region

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string1 = "This is a sample string to test the function." val string2 = "This is another SAMPLE string to test the " + "string region matches method." textView.text = string1 textView.append("\n\n") textView.append(string2) /* documentation so...

android kotlin - Prepend indent to string every line

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the" + " printing and \ntypesetting industry. Lorem Ipsum" + " has been the industry's \nstandard dummy text" + " ever since the 1500s, when \nan unknown printer" + " took a galley of type and \...

android kotlin - How to partition a string

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "This is A SAMple striNg." textView.text = string /* documentation source: kotlinlang.org Splits the original char sequence into pair of char sequences, where first char sequence contains characters for which predicate yielded true, while second char sequen...

android kotlin - String padStart and padEnd example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry." textView.text = string // string is returned as is, when its // length is greater than the specified val afterPadStart10 = string.padStart(10,'*') textView.append("\n\nAfte...

android kotlin - How to split string into lines

MainActivity.kt package com.example.jetpack import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val string = "Lorem Ipsum is simply dummy text \nof the" + " printing and typesetting industry. Lorem" + " Ipsum has been the industry's \nstandard dummy" + " text ever since \nthe 1500s, when an unknown" + " printer took a galley of type \nand scrambled" + " it to make a type specimen book." textView.text = string /* source: kotlinlang.org Splits this char sequence to a list of lines delimited by any of the following...

android kotlin - String isBlank and isEmpty example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = " " textView.text = "This string ($string)" + " length is " + string.length // Returns true if this string is empty // or consists solely of whitespace characters. textView.append("\n\nString is blank? ") textView.append(string.isBlank().toStr...

android kotlin - How to filter string

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the printing" + " and typesetting industry. Lorem Ipsum has been the" + " industry's standard dummy text ever since the 1500s," + " when an unknown printer took a galley of type" + " and scramble...

android kotlin - How to compare two strings

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string1 = "This is a sample string." val string2 = "This is a SAMPLE string." textView.text = "String 1 : \n$string1" textView.append("\n\nString 2 : \n$string2") // Compares two strings lexicographically, // optionally ignoring case differences. if (string1.com...

android kotlin - Get string last index

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "Lorem Ipsum is simply dummy text of the printing" + " and typesetting industry. Lorem Ipsum has been the" + " industry's standard dummy text ever since the" + " 1500s, when an unknown printer took a galley" + " of type and scramble...

android kotlin - String capitalize decapitalize example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val string = "this is a sample string." textView.text = string textView.append("\n\nAfter capitalize it....") // Returns a copy of this string // having its first letter uppercase. textView.append("\n" + string.replaceFirstChar { if (it.isLowerCase()) it.t...