Saturday, 28 March 2020

How to create Dynamic EditText | set imeOption to EditText | TextWatcher to the Editext - using Kotlin Example

  • Create a new EditText Widget programmatically in Android using Kotlin

var dynamicEditext = EditText(this)
        dynamicEditext.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT)
        dynamicEditext.hint="Dynamic EdiText..."
        linearLayout.addView(dynamicEditext)
  • Set hint to EditText.

dynamicEditext.hint="Dynamic EdiText..."
  • Set inputType.

dynamicEditext.inputType=InputType.TYPE_CLASS_NUMBER
  • Set imeOption.

dynamicEditext.imeOptions = EditorInfo.IME_ACTION_DONE
  • Set isSingleLine.
dynamicEditext.isSingleLine=true
  • TextWather on EditText on text change – Example Kotlin.

dynamicEditext.addTextChangedListener(object :TextWatcher{

            override fun afterTextChanged(s: Editable?) {
                //TODO("Not yet implemented")
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                //TODO("Not yet implemented")
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                //Write your code here
            }

        })
  • Floating Label in EditText using TextInputLayout – Example

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Enter UserId"
            android:inputType="text"
            android:padding="20dp"
            android:singleLine="true" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Password"
            android:padding="20dp" />
    </com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

Friday, 27 March 2020

How to create Dynamic Button | set background to Button | setClickListener to the Button using Kotlin

  • Create a new Button programmatically in Kotlin Android.
  var dynamicButton = Button(this)
dynamicButton.layoutParams=LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
dynamicButton.setPadding(20, 20, 20, 20)
dynamicButton.text = "Dynamic Button"
mainLayout.addView(dynamicButton)
  • Set OnClickListener for Button -Kotlin Android.
dynamicButton.setOnClickListener {    
/* Write your code here */    
Toast.makeText(this, "My Dynamic button onClick", Toast.LENGTH_LONG).show() }
Disable All Caps in Android Button – Kotlin Android.

dynamicButton.isAllCaps = false
  • How to create custom design for Button background in Kotlin Android.(in XML)
dynamicButton.background = getDrawable(R.drawable.btn_edge_color)

btn_edge_color.xml:-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="4dp" />
    <gradient
        android:angle="90"
        android:endColor="#FFFFFF"
        android:startColor="#FFFFFF" />
    <stroke
        android:width="4dp"
        android:color="#238da0" />
</shape>

  • How to set background color of a Button dynamically or pragmatically in android kotlin.
val bgDrawableIds = intArrayOf(
    R.drawable.btn_center_gradient,
    R.drawable.btn_edge_color,
    R.drawable.btn_gradient,
    R.drawable.btn_solid_color)
var button = findViewById(R.id.buttonBg) as Button
var intCol = 0button.setOnClickListener {    
button.background = getDrawable(bgDrawableIds[intCol % bgDrawableIds.size])
    intCol++;

Tuesday, 24 March 2020

How to create Dynamic TextView in Android using Kotlin. | Kotlin Dynamic Textview

MainActivity.kt:-

val dynamicTextView = TextView(this)
dynamicTextView.text = "This is dynamically created textView"
dynamicTextView.textSize = 20f
relLayout.addView(dynamicTextView)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/relLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="This is xml generated textview" />

        <Button
            android:id="@+id/buttonClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:text="Click Me" />
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Find the GitHub Path:-

OUTPUT:-


how to change status bar color in jectpack compose.

 Step1: Go to MainActivity and do following: enableEdgeToEdge ( statusBarStyle = SystemBarStyle .light( Color . Green .h...