How to hide the FloatingActionButton that is part of the Activity in the Fragment in Kotlin? - kotlin

In my 'HomeActivity', I have a FloatingActionButton and four Fragments. I want the FloatingActionButton to be shown only in the 'fragment1' and 'fragment2' make hide it in the 'fragment3' and 'fragment4'. I tried but it didn't work.
Following is what I have in the xml file of the 'HomeActivity'
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_hide_category"
app:layout_anchor="#id/bottom_appbar"/>
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation"
app:layout_anchor="#id/bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I added the below code in the 'HomeActivity'
fun hideFabOne(){
binding.fabOne.hide()
}
and below code in the onCreateView of the fragments where I want to hide the FAB. But, it didn't work.
HomeActivity().hideFabOne()

Looks like you are creating new instance of activity , try
(requireActivity() as HomeActivity).hideFabOne()
*Add for comment
You can modify the hideFabOne() something like
fun hideFabOne(){
binding.fabOne.visiblity = if( binding.fabOne.isVisible ) View.VISIBLE else View.GONE
}

Related

Custom Toast inflater must be initialized

i have this fun
private fun testToast(){
val inflater:LayoutInflater
val view = inflater.inflate(R.layout.custom_toast_message)
val toast = Toast(this.context)
toast.setGravity(Gravity.TOP,0,70)
toast.duration = Toast.LENGTH_LONG
toast.view =view
toast.show()
}
the main idea its to change the background color of the toast without getting a square toast
in this code i get an error in inflater.inflate
(inflater must be initialized)
this is the test xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/background_blue"
android:padding="16dp"
android:weightSum="1"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_toasticon"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:textColor="#color/gauge_red"
android:fontFamily="#font/roboto"
android:text="Prueba prueba PRUEBA"
/>
</LinearLayout>
con someone help me or annother way to do a toast with custom color.
change the color of a toast with rounded corners
You declared the inflater variable but never assigned anything to it (using =). You can’t use a variable’s value if you haven’t assigned anything to it yet.
If this function is in an Activity, you can delete the line that declares val inflater and replace inflater. with layoutInflater. to use the property of the Activity. If it’s in a Fragment, use requireActivity().layoutInflator..
By the way, the latest version of Android has deprecated setting a custom view for your toast. On Android R and later if you set a view on your Toast, it won’t show. They want you to use SnackBars instead. There might be a third party library available that could help create something more similar to Toasts.
you can make a drawable resource to achieve it
toast_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/teal_200" />
<corners android:radius="48dp" />
</shape>
and use this drawable for background of toast view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/toast_background"
android:orientation="horizontal"
android:padding="16dp"
android:weightSum="1">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:text="Prueba prueba PRUEBA"
android:textColor="#color/purple_700"
android:textSize="14sp" />
</LinearLayout>
the result

Kotlin, why the ending of numeric data is truncated & displayed

In my app, the UID is 9 digits numeric data. I pass an ArrayList to the adapter, the onBindViewHolder will log full digits. But the ending digits of each entries will be differently truncated & showed in the card. When displaying 3 entries, the UIDs shown are 7 digits or 8 digits but never 9. The LinearLayout is containing the RecyclerView which display records as CardView. Why the number "truncated" before display? Any comment is really appreciated.
The activity that displaying the data is just responsible for retrieve data from firebase, add them into ArrayList then attach to the adapter. The entries of the list was checked.
MarritalView.xml
<?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="match_parent"
tools:context=".FactModule.smallactivities.FactMarritalView">
//...some views omitted
<LinearLayout
android:id="#+id/linearLayout999"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView15">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/marritalList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
OnBindViewHolder
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentitem = marritalFactList[position]
holder.uid.text = currentitem.uid.toString()
Log.d(ContentValues.TAG, "Value is: ${holder.uid.text}") //display 9 digits
holder.ageGroup.text = currentitem.ageGroup
holder.husbandRace.text = currentitem.husbandRace
holder.divorceAfterYears.text = currentitem.divorceAfterYears.toString()
holder.country.text = currentitem.country
}
MarritalItems.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="6dp"
android:layout_marginStart="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record UID "
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvMarritalUID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="#color/black"
android:textSize="20sp" />
</LinearLayout>
//...more horizontal linear layout ommited
</LinearLayout>
</androidx.cardview.widget.CardView>
MarritalFact.kt
data class MarritalFact(
val uid:Int, val ageGroup:String, val husbandRace:String,
val divorceAfterYears:Int, val country:String, val username:String
){
constructor():this(0,"","",0,"","")
}

Hopefully a simple Kotlin regarding looping through a recycler

I have a recycler in my activity for acrostic poetry. The recycler displays an image of the letters of the word (Stone Age, in this case) and an EditText to enter the acrostic words. What I want to do is read the words that have been entered, so for "Stone Age" I'm creating 8 items in the recycler.
In order to get the words, I have tried to replicate the steps in this question How to iterate over RecyclerView items. I think I'm close, but I can't quite get over the line.
I have this function where I am trying to read the values (at this stage just into the logcat)
I figured that getChildCount returned the items, but this is logging 2 (or sometimes, oddly, 3) rather than the 8 I would expect, so I'm struggling to understand what I am looping.
private fun saveTask() {
var p: String = ""
Log.d(HWG.TAG, "Children: ${acrosticRecycler.getChildCount().toString()}")
for (i in 0 until acrosticRecycler.getChildCount()) {
var holder: AcrosticAdapter.ViewHolder
if(acrosticRecycler.findViewHolderForAdapterPosition(i) != null) {
holder = acrosticRecycler.findViewHolderForAdapterPosition(i) as AcrosticAdapter.ViewHolder
p = holder.txtWord.text.toString()
Log.d(HWG.TAG, "Word: ${p}")
}
}
}
This is the layout of the recycler
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/llTaskList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/imgLetter"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
app:srcCompat="#drawable/icons8_s" />
<EditText
android:id="#+id/txtWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:hint="Stone Age"
android:textAllCaps="true"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
This is the activity in which the recycler sits
<?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="match_parent"
android:background="#color/background"
tools:context=".TextualTask">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:background="#color/background"
app:cardCornerRadius="4dp"
app:cardElevation="6dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/subjectImage"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
app:srcCompat="#drawable/icons8_knowledge_sharing_80" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:layout_marginLeft="15dp"
android:text="Acrostic Poem"
android:textAllCaps="true"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_margin="6dp"
android:background="?android:attr/listDivider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="#+id/txtPreamble"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="15dp"
android:text="Preamble text"
android:textAlignment="center"
android:textSize="14sp"
android:textStyle="bold|italic" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<androidx.cardview.widget.CardView
android:id="#+id/crdWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="24dp"
app:cardBackgroundColor="#color/cardback"
app:cardCornerRadius="4dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<Button
android:id="#+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:backgroundTint="#color/start"
android:text="Submit" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/acrosticRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="5"
tools:listitem="#layout/acrostic_task" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Oddly enough, sometime the for loop gets the values from items 0-2, but sometimes it doesn't. The call to saveTask is made from btnSubmit as I don't need to get the values at the point the user enters them, just when they're happy to submit.
Could someone point me in the right direction please?
You can't get the correct count from acrosticRecycler.getChildCount().
because acrosticRecycler.getChildCount() will return the count of currently showed item in the RecyclerView. So even if your data is 8, but the showed data in the screen is only 3 items. So you will always get 3 not 8.
It's not good practice to get the data from the ViewHolder in recycler view directly. You should get the data from the list you have inserted to recycler view adapter.
for example, you can create adapter like this :
class AcrosticAdapter(var listData: ArrayList<AcrosticPoetry>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun getItemCount(): Int = listData.size
}
//assuming this is your model class
class AcrosticPoetry() {
var textWord = ""
}
and you can change your code to
private fun saveTask() {
val adapter = acrosticRecycler.adapter as AcrosticAdapter
val data = adapter.listData
for (i in 0 until data.size) {
val word = data[i].txtWord
Log.d(HWG.TAG, "Word: ${word}")
}
}

Attempt to invoke virtual method on a null object reference -> resolve?

I try to do a button for switch from a main activity to an other, but when I try to run the app, it close itself immediatly after click on the app. I find in the command that message of error :
Unable to start activity ComponentInfo{fr.amseu.mystretching/fr.amseu.mystretching.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
My code in the MainActivity is this one :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<ImageButton>(R.id.imageButton)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent);}
That is my Main Activity :
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/item_main"
android:background="#color/light_red">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="100dp"
android:layout_height="120dp"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/lower_legs"
android:contentDescription="#string/home_page_first_button" />
</androidx.cardview.widget.CardView>
<View
android:id="#+id/view_separation"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/name_item"
style="#style/SubTitleTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/default_margin"
android:text="#string/home_page_first_button"
app:layout_constraintBottom_toTopOf="#+id/view_separation"
app:layout_constraintStart_toEndOf="#+id/cardView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/description_item"
style="#style/DefaultTextStyle"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/home_page_description_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/name_item"
app:layout_constraintTop_toBottomOf="#+id/view_separation" />
</androidx.constraintlayout.widget.ConstraintLayout>
And for my recycler view, I take this model in my page and the following item are siblings but I change the image and texts, with an adapter, HomeFragment...
Can someone help me please ?
(It is my first app)
Thanks a lot !

MaterialCardView not showing when added programmatically

I am not able to see MaterialViewCard at my Activity when adding programmatically. If I add directly on XML, it shows. But When adding via Kotling, it doesn't.
XML sample:
<?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="match_parent"
tools:context=".SecondFragment">
<LinearLayout
android:id="#+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<LinearLayout
android:id="#+id/layout_first_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/surface"
android:layout_margin="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textColor="#color/on_surface"/>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/surface"
android:layout_margin="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="01"
android:textColor="#color/on_surface"/>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/surface"
android:layout_margin="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="03"
android:textColor="#color/on_surface"/>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<Button
android:id="#+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/previous"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Result:
Cards added via XML
But when I try this (below) won't work.
XML:
<?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="match_parent"
tools:context=".SecondFragment">
<LinearLayout
android:id="#+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<LinearLayout
android:id="#+id/layout_first_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<!-- Will add here-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<Button
android:id="#+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/previous"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_second).setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
}
val layoutFirstBlock: LinearLayout = view.findViewById<LinearLayout>(R.id.layout_first_block)
for (r in 1..10){
val row: LinearLayout = LinearLayout(context)
row.orientation = LinearLayout.HORIZONTAL
row.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
for(c in 1..10){
val card: MaterialCardView = MaterialCardView(context)
val marginParams: ViewGroup.MarginLayoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
marginParams.setMargins(R.dimen.small_padding, R.dimen.small_padding, R.dimen.small_padding, R.dimen.small_padding)
card.layoutParams = marginParams
card.setBackgroundColor(resources.getColor(R.color.surface, context?.theme))
val num: TextView = TextView(context)
num.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
val value = ((r-1)*10 + c).toString()
num.text = value
num.setTextColor(resources.getColor(R.color.on_surface, context?.theme))
Log.d("i", "i$r$c = $value")
card.addView(num)
row.addView(card)
}
layoutFirstBlock.addView(row, 0)
}
}
Result: Cards missing when added by Kotlin
Any ideas?
Thanks!
In my case the issue was i have a component above the cardview and i have set its height to match_parent,when i gave the height to wrap_content the issue has been solved.
If it doesn't work also check in build gradle for below line.
implementation 'com.google.android.material:material:1.0.0'
Firstly,
You need to generate id's of each view as well.
val card: MaterialCardView = MaterialCardView(context)
card.id = View.generateViewId()
and
val num: TextView = TextView(context)
num.id = View.generateViewId()
Secondly,
I think, you should need to update index of
layoutFirstBlock.addView(row, 0)
to
layoutFirstBlock.addView(row, r)