0

I would like to display either 4 textViews or just one editText depending on the data that I receive from back-end

Here is my view as below

<LinearLayout 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="match_parent"
    android:orientation="vertical">

  <LinearLayout
    android:id="@+id/groupOfFour" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <TextView
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
          />
        <TextView
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
        <TextView
            android:id="@+id/third"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            t/>
        <TextView
            android:id="@+id/fourth"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
   </LinearLayout>

   <EditText
       android:id="@+id/editText"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
</LinearLayout>

In my fragment I am doing like this

class MyFragment : Fragment(R.layout.my_fragment) {

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
   val backedData = getRemoteData()
   if(backedData.hasFour()) {
     editText.visibility = Visibility.GONE
     // set the four text view
   } else {
     groupOfFour.visibility = Visibility.GONE
   }
  }
}

Is there any other way of achieving the same behavior?

Zain
  • 37,492
  • 7
  • 60
  • 84
akram
  • 157
  • 1
  • 15

1 Answers1

0

You can add the Views programmatically using addView

class MyFragment : Fragment(R.layout.my_fragment) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
       val backedData = getRemoteData()

       if(backedData.hasFour()) {
            groupOfFour.addView(addTextView("Textview 1"))
         
        // set the four text view
       } else {
       
            groupOfFour.addView(addTextView("Textview 1"))
            groupOfFour.addView(addTextView("Textview 2"))
            groupOfFour.addView(addTextView("Textview 3"))
            groupOfFour.addView(addTextView("Textview 4"))

       }
    }
    
    fun addTextView(mytext: String):TextView {
        return TextView(activity).apply {
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            text = mytext
        }
    }  
}

And remove the TextViews from the layout

<LinearLayout 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="match_parent"
    android:orientation="vertical">

  <LinearLayout
    android:id="@+id/groupOfFour" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   </LinearLayout>

   <EditText
       android:id="@+id/editText"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
</LinearLayout>

If you want to set static Ids to the TextViews, you can check this answer

Zain
  • 37,492
  • 7
  • 60
  • 84