Expandable for text view in kotlin

Viewed 83

I have a lot of text view that under them I have 4 or 5 buttons and want now make text view Expandable to show buttons with tap on text view and hide them with again tap

i should say this buttons should send sms and send sms code are in a fragment

sorry for bad English

and item are in fragment layout

i will put a example of codes to better explane thanks all


    class OrderFragment: Fragment() {

        override fun onCreateView(inflater: LayoutInflater,
                                  container: ViewGroup?, savedInstanceState: Bundle?): View?
                = inflater.inflate(R.layout.fragment_order, container, false).apply {

            // Variable and constants declaration
            val btn1 = findViewById<Button>(R.id.bokhari1Reset)
            val number = findViewById<EditText>(R.id.idEdtPhone)

            btn1.setOnClickListener {

                val phoneNumber = number.text.toString()
                val message = "#9903011"
                try {
                    val smsManager: SmsManager = SmsManager.getDefault()

                    smsManager.sendTextMessage(phoneNumber, null, message, null, null)
                    Toast.makeText(context, "send", Toast.LENGTH_SHORT).show()

                } catch (e: Exception) {
                    Toast.makeText(context, "please enter phone number ", Toast.LENGTH_SHORT).show()
                } }
                bokhari1Auto.setOnClickListener {

                    val phoneNumber = number.text.toString()
                    val message = "#9903012"
                    try {
                        val smsManager: SmsManager = SmsManager.getDefault()

                        smsManager.sendTextMessage(phoneNumber, null, message, null, null)
                        Toast.makeText(context, "بخاری 1 = اتوماتیک کردن", Toast.LENGTH_SHORT).show()

                    } catch (e: Exception) {
                        Toast.makeText(context, "تلفن را صحیح وارد کنید", Toast.LENGTH_SHORT).show()
                    } }
        }
     }


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity">
        
        
        
        
           <TextView
            android:id="@+id/bokhari2"
            android:layout_width="153dp"
            android:layout_height="56dp"
            android:layout_marginTop="36dp"
            android:text="بخاری 2"
            android:textSize="30sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.941"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bokhari1Auto" />

        <Button
            android:id="@+id/bokhari1On"
            android:layout_width="169dp"
            android:layout_height="52dp"
            android:layout_marginStart="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="15dp"
            android:backgroundTint="#8BC34A"
            android:padding="5dp"
            android:text="دستی همه روشن"
            android:textAllCaps="false"
            android:textSize="20sp"
            app:layout_constraintEnd_toStartOf="@+id/bokhari2Reset"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bokhari2" />

        <Button
            android:id="@+id/bkhari2Off"
            android:layout_width="169dp"
            android:layout_height="52dp"
            android:layout_marginStart="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="15dp"
            android:text="دست همه خاموش "
            android:backgroundTint="#FF1100"
            android:textSize="17sp"
            app:layout_constraintEnd_toStartOf="@+id/bokhari2Auto"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bokhari2On" />

        <Button
            android:id="@+id/bokhari2Auto"
            android:layout_width="169dp"
            android:layout_height="52dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="15dp"
            android:backgroundTint="#3F51B5"
            android:padding="5dp"
            android:text="اتوماتیک کردن"
            android:textAllCaps="false"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bokhari2Reset" />

        <Button
            android:id="@+id/bokhari2Reset"
            android:layout_width="169dp"
            android:layout_height="52dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="15dp"
            android:backgroundTint="#FF9800"
            android:text="ریست نمودن"
            android:textAllCaps="false"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bokhari2" />
        
        
        
    </androidx.constraintlayout.widget.ConstraintLayout>


2 Answers

Here is extension function to achieve expand/collapse .

 fun View.expand(duration: Long) {
        val initialHeight = this.measuredHeight
        val matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec((this.parent as View).width, View.MeasureSpec.EXACTLY)
        val wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        this.measure(matchParentMeasureSpec, wrapContentMeasureSpec)
        val targetHeight = this.measuredHeight

        this.layoutParams.height = initialHeight
        this.visibility = View.VISIBLE
        val a: Animation = object : Animation() {
            override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
                this@expand.layoutParams.height = if (interpolatedTime == 1.0f) ViewGroup.LayoutParams.WRAP_CONTENT else (initialHeight + ((targetHeight - initialHeight) * interpolatedTime)).toInt()
                this@expand.requestLayout()
            }

            override fun willChangeBounds(): Boolean {
                return true
            }
        }


        a.duration = duration
        this.startAnimation(a)
    }

    fun View.collapse(duration: Long) {
        val initialHeight = this.measuredHeight
        val a: Animation = object : Animation() {
            override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
                if (interpolatedTime == 1.0f) {
                    this@collapse.visibility = View.GONE
                } else {
                    this@collapse.layoutParams.height = initialHeight - (initialHeight * interpolatedTime).toInt()
                    this@collapse.requestLayout()
                }
            }

            override fun willChangeBounds(): Boolean {
                return true
            }
        }

        a.duration = duration
        this@collapse.startAnimation(a)
    }

Maintain a global variable in activity/fragment if you need some thing like Learn More/Show Less.

 var isCollapsed = false
    if(isCollapsed) 
       view.expand(300)
    else 
       view.collapse(300)

Increase/decrease duration based on requirement.

Related