Android - LinearView - Dynamic instantiation of views


fox hunting

I'm trying to create a function that instantiates some views in a linear layout

private lateinit var layout: LinearLayout 
var par = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)

override fun onCreate(savedInstanceState: Bundle?) {
    //The init stuff with binding
    layout = binding.root.findViewById(R.id.gamelayout)
    lpar.setMargins(10,10,10,10)

    test()
}

private fun instanciateText(s:String)
{
    val tv = TextView(this)
    tv.text = s
    tv.layoutParams = par
    layout.addView(tv)
}

fun instanciateImage()
{
    val iv = ImageView(this)
    iv.setImageResource(R.drawable.start1)
    iv.layoutParams = par
    layout.addView(iv)
}

fun test(){
    instanciateText("Text 1 ")
    instanciateImage()
    instanciateText("Text 2 ")
}

It works fine for text, but for images, the views are far from each other and I can't find the reason

screenshot

object

If a stretchable ImageView is added, it will be expanded to the maximum so that the LinearLayout gets 100% fill of the child elements. This is correct behavior. If you want to avoid this, specify ImageView.adjustViewBounds = true .

fun instanciateImage()
{
    val iv = ImageView(this)
    iv.setImageResource(R.drawable.start1)
    iv.layoutParams = par

    iv.adjustViewBounds = true

    layout.addView(iv)
}

Related


Create dynamic views in Android?

Teff In my application, I receive JSON multiple forms like this: "forms":[ {"name": "form1","title": "First Form","order": 1, "fields":[ "name": "firstName","type": "InputText", "maxLength": "10", "restriction

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

gold I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Android save the state of multiple dynamic inflated views

Money I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem. XML of the item I'm inflating in the fragment <LinearLayout

Python: dynamic instantiation

Xendi I need to use object variables to reduce lines of code and complexity. Here is the code I will be using: exchange = ccxt.binance({ 'apiKey': 'YOUR_API_KEY', 'secret': 'YOUR_SECRET', 'enableRateLimit': True, }) I need to make

Python factory dynamic instantiation

time travel penguin I'm a C# programmer and I want to build a simple factory. To comply with the waiver: class ShapeFactory: _registry: Dict[ShapeType, Shape] = { ShapeType.Shape: lambda: Shape(), ShapeType.Circle: lambda: Circle(),

Python factory dynamic instantiation

time travel penguin I'm a C# programmer and I want to build a simple factory. To comply with the waiver: class ShapeFactory: _registry: Dict[ShapeType, Shape] = { ShapeType.Shape: lambda: Shape(), ShapeType.Circle: lambda: Circle(),

Python factory dynamic instantiation

time travel penguin I'm a C# programmer and I want to build a simple factory. To comply with the waiver: class ShapeFactory: _registry: Dict[ShapeType, Shape] = { ShapeType.Shape: lambda: Shape(), ShapeType.Circle: lambda: Circle(),

Dynamic instantiation in Dart

fake peaches I need an object that makes other objects an instance. I want to be able to pass the class of the object to be created, but they all need to be of the same type, it would be nice if they could both start with the same value: class Cloner{ BaseT

Python factory dynamic instantiation

time travel penguin I'm a C# programmer and I want to build a simple factory. To comply with the waiver: class ShapeFactory: _registry: Dict[ShapeType, Shape] = { ShapeType.Shape: lambda: Shape(), ShapeType.Circle: lambda: Circle(),

Python: dynamic instantiation

Xendi I need to use object variables to reduce lines of code and complexity. Here is the code I will be using: exchange = ccxt.binance({ 'apiKey': 'YOUR_API_KEY', 'secret': 'YOUR_SECRET', 'enableRateLimit': True, }) I need to make

Python factory dynamic instantiation

time travel penguin I'm a C# programmer and I want to build a simple factory. To comply with the waiver: class ShapeFactory: _registry: Dict[ShapeType, Shape] = { ShapeType.Shape: lambda: Shape(), ShapeType.Circle: lambda: Circle(),

Python factory dynamic instantiation

time travel penguin I'm a C# programmer and I want to build a simple factory. To comply with the waiver: class ShapeFactory: _registry: Dict[ShapeType, Shape] = { ShapeType.Shape: lambda: Shape(), ShapeType.Circle: lambda: Circle(),

Python factory dynamic instantiation

time travel penguin I'm a C# programmer and I want to build a simple factory. To comply with the waiver: class ShapeFactory: _registry: Dict[ShapeType, Shape] = { ShapeType.Shape: lambda: Shape(), ShapeType.Circle: lambda: Circle(),

Dynamic Type Instantiation C#

beetle I want to instantiate an object at runtime which is of type string but whose value is also string. E.g: string myType = "System.Int32"; string myValue = "3"; I'm looking to create an instance of myType and cast/assign myValue to the just created instan