Reputation: 33
I have created a piece of code that is meant to dynamically create a linearLayout and populate it with textViews at the push of a button - AND apply a style.
quickTest is the name of the style I am trying to apply.
TableManners is a layout in which I am trying to force this whole ordeal.
fab.setOnClickListener {
val linearshell = LinearLayout(this, null, R.style.quickTest)
linearshell.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
linearshell.orientation = LinearLayout.VERTICAL
linearshell.textAlignment = LinearLayout.TEXT_ALIGNMENT_CENTER
val Name = TextView(this)
val CurrentPlace = TextView(this)
val Date = TextView(this)
Name.text = "WowwoW"
CurrentPlace.text = "Here"
Date.text = "11/04/2020"
TableManners.addView(linearshell)
linearshell.addView(Name)
linearshell.addView(CurrentPlace)
linearshell.addView(Date)
}
The style in question:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="quickTest">
<item name="android:backgroundTint">@color/Redmen</item>
<item name="android:background">@drawable/roundstyle</item>
</style>
</resources>
The roadblock I am facing is the style is not applying.
Upvotes: 1
Views: 66
Reputation: 4321
Try this
fab.setOnClickListener {
val themeContext = ContextThemeWrapper(this, R.style.quickTest)
val linearshell = LinearLayout(themeContext, null, 0)
linearshell.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
linearshell.orientation = LinearLayout.VERTICAL
linearshell.textAlignment = LinearLayout.TEXT_ALIGNMENT_CENTER
val Name = TextView(this)
val CurrentPlace = TextView(this)
val Date = TextView(this)
Name.text = "WowwoW"
CurrentPlace.text = "Here"
Date.text = "11/04/2020"
TableManners.addView(linearshell)
linearshell.addView(Name)
linearshell.addView(CurrentPlace)
linearshell.addView(Date)
}
Upvotes: 1