igarciama

igarciama

Is there any way to create a custom element with XML that can contain a child in a specific place?

I’ve been trying to create a custom CardView with a title and below the title any type of content, but when I try overriding addView in the Kotlin class of the custom element to redirect the child it doesn’t allow it. Does anyone have any solution? Here is the code:

XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/title"
        style="@style/TextView.Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Título de ejemplo" />

    <FrameLayout
        android:id="@+id/childrenContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

KOTLIN:
class TarjetaTitle @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : CardView(context, attrs, defStyleAttr) {

private var title: TextView
private var childrenContainer: FrameLayout

init {
    LayoutInflater.from(context).inflate(R.layout.element_tarjeta_title, this, true)

    title = findViewById(R.id.title)
    childrenContainer = findViewById(R.id.childrenContainer)

    context.obtainStyledAttributes(attrs, R.styleable.TarjetaTitle, defStyleAttr, 0).apply {
        try {
            title.text = getString(R.styleable.TarjetaTitle_title)
            val titleStyle = getResourceId(R.styleable.TarjetaTitle_titleStyle, -1)

            if (titleStyle != -1)
                title.setTextAppearance(titleStyle)

        } finally {
            recycle()
        }
    }
}

override fun addView(child: View?, params: LayoutParams?) {
    childrenContainer.addView(child, params)
}

}

Where Next?

Popular Android topics Top

AndroidMan
I’ve tried signing up for Airnow as I used to do very well when they were Airpush, but i haven’t used them in years. I went through the s...
New
Nevio
Hi, I made an android app and created a signature with keytool. I then bundled the signature in the apk. So, I was wondering if, wanting...
New
Joakins
Android studio displayed access is denied during gradle build C:\Program Files\Java\jdk1.8.0_251\native\68d5fa5c4cc2d200863cafc0d521ce42...
New
AndroidDog123
I’m a developer working as a contractor for a company. I’m finding it confusing on how I should answer these questions? Has Google just b...
New
freerefills
I have an android app whereby notifications will fire and immediately leave the tray. This doesn’t always happen, it’s unpredictable they...
New
vsiPr
I am trying to develop an app which will make your smartphone act as a “keyboard”. I need it to send different symbols to another devices...
New
Rodion
Hello everyone, I recently started learning Kotlin and downloaded Android Studio, but after writing my first code and trying to run it, I...
New
skysnap
Helo, I’m using doubleviewpager in my project: Is there something similar that works with android x?
New
Garrett
I’m stuck trying to understand how to get a variable initialised, and later updated, in kotlin to update in Jetpack Compose and cause a c...
New
paulo500
Can anyone help? I have an android projector with Google-licensed Android system loaded. It has the developer options passworded and th...
New

Other popular topics Top

PragmaticBookshelf
Brace yourself for a fun challenge: build a photorealistic 3D renderer from scratch! In just a couple of weeks, build a ray tracer that r...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
First poster: AstonJ
Jan | Rethink the Computer. Jan turns your computer into an AI machine by running LLMs locally on your computer. It’s a privacy-focus, l...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New