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

big_f
I am making an app with Nativescript-vue and I would like to have an event where I click on an icon and then a new box appears. For the i...
New
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
ciger
I want to get a clone of Instagram APK But with a difference I want to define in this clone that, for example, it is not possible to acc...
New
DAZ
Is it possible to develop an application that will generate NFC pulses even when the screen is off?
New
mfaamir
Dear Sir, I am a beginner in Android App development and I am currently trying to establish a connection between my Android Studio Java ...
New
cve60069
Hello I am starting the tearning curve to writing an app for my galaxy phone. Me and my friens play a lot of “Texas Hold-Em” and, for t...
New
tunelabguy
I have an expensive app ($160) in the Play Store that I would like to update. I would like to do a beta test of the update with potentia...
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
Devraj5032
I’m using TensorFlow Lite (TFLite) with React Native Expo. When I test the app using the Expo Go app, everything works fine. However, whe...
New
John-xtreme
Hello everyone, I am new to Android development and I need some help. I am running Android Studio Narwhal 3 (version 2025.1.3) on Ubunt...
New

Other popular topics Top

PragmaticBookshelf
Stop developing web apps with yesterday’s tools. Today, developers are increasingly adopting Clojure as a web-development platform. See f...
New
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
AstonJ
Or looking forward to? :nerd_face:
498 14002 274
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
New
hilfordjames
There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New
NewsBot
Node.js v22.14.0 has been released. Link: Release 2025-02-11, Version 22.14.0 'Jod' (LTS), @aduh95 · nodejs/node · GitHub
New
mindriot
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New