My cheat sheets journey

Ok. This is my cheat sheets.

change textview color programmatically

tv_note_point1?.setTextColor(getColor(R.color.red_600)) 

I make this notes, just in case stackoverflow goes down wkwk.

remove last character

kotlin :

val string = "<<<First Grade>>>"
println(string.drop(6)) // st Grade>>>
println(string.dropLast(6)) // <<<First Gr
println(string.dropWhile { !it.isLetter() }) // First Grade>>>
println(string.dropLastWhile { !it.isLetter() }) // <<<First Grade

java :

public String removeLastCharacter(String str){
       String result = null;
        if ((str != null) && (str.length() > 0)) {
          return str.substring(0, str.length() - 1);
        }
        else{
            return "";
        }

}

create ui programmatically in kotlin

fun createUIProgrammatically() { // Variable for counting text view var counter: Int = 1; // Set a click listener for button widget button.setOnClickListener{ // Create a new TextView instance programmatically val text_view: TextView = TextView(this) // Creating a LinearLayout.LayoutParams object for text view var params : LinearLayout.LayoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, // This will define text view width LinearLayout.LayoutParams.WRAP_CONTENT // This will define text view height ) var params2 : FlexboxLayout.LayoutParams = FlexboxLayout.LayoutParams( FlexboxLayout.LayoutParams.WRAP_CONTENT, // This will define text view width FlexboxLayout.LayoutParams.WRAP_CONTENT // This will define text view height ) // Add margin to the text view params.setMargins(10,10,10,10) params2.setMargins(10,10,10,10) // Now, specify the text view width and height (dimension) text_view.layoutParams = params2 // Display some text on the newly created text view text_view.text = “Lalat : $counter” // Set the text view font/text size text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP,13F) text_view.setTextColor(Color.WHITE) //text_view.setTypeface(text_view.typeface,Typeface.BOLD_ITALIC) //text_view.setTypeface(Typeface.MONOSPACE) text_view.setBackgroundResource(R.drawable.btn_box_rounded_red) // Put some padding on text view text text_view.setPadding(5,5,5,5) // Set a click listener for newly generated text view text_view.setOnClickListener{ Toast.makeText(this,text_view.text,Toast.LENGTH_SHORT).show() } lay_alergi.addView(text_view) // Increment the counter counter++ } }

try other :

fun createUIProgrammatically() { // Variable for counting text view var counter: Int = 1; // Set a click listener for button widget button.setOnClickListener{ // Create a new TextView instance programmatically val text_view: TextView = TextView(this) // Creating a LinearLayout.LayoutParams object for text view var params : LinearLayout.LayoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, // This will define text view width LinearLayout.LayoutParams.WRAP_CONTENT // This will define text view height ) var params2 : FlexboxLayout.LayoutParams = FlexboxLayout.LayoutParams( FlexboxLayout.LayoutParams.WRAP_CONTENT, // This will define text view width FlexboxLayout.LayoutParams.WRAP_CONTENT // This will define text view height ) // Add margin to the text view params.setMargins(10,10,10,10) params2.setMargins(10,10,10,10) // Now, specify the text view width and height (dimension) text_view.layoutParams = params2 // Display some text on the newly created text view text_view.text = "Lalat : $counter" // Set the text view font/text size text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP,13F) text_view.setTextColor(Color.WHITE) //text_view.setTypeface(text_view.typeface,Typeface.BOLD_ITALIC) //text_view.setTypeface(Typeface.MONOSPACE) text_view.setBackgroundResource(R.drawable.btn_box_rounded_red) // Put some padding on text view text text_view.setPadding(5,5,5,5) // Set a click listener for newly generated text view text_view.setOnClickListener{ Toast.makeText(this,text_view.text,Toast.LENGTH_SHORT).show() } lay_alergi.addView(text_view) // Increment the counter counter++ } }

Leave a comment