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

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
cajasigwe
Hi, Please I need help to get DHIS2 Capture working in Android Tablet Phone desk ( KT5-3C) Android Version: 10. Build: KT5-3C_LTE05M0_K...
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
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
hasan10242048
There is an Android app. I just want to create two GRAPHS with CPU Runtime and Battery Consumption. It is as simple as this. I tried w...
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
auroralane7754
Hey developers :waving_hand: I’m currently experimenting with various mobile VPN apps to simulate different network conditions for API t...
New
DuendeMartin
My Android Studio emulator is stuck on this loading screen. What’s going on?
New
igarciama
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 t...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1052 22283 402
New
PragmaticBookshelf
Machine learning can be intimidating, with its reliance on math and algorithms that most programmers don't encounter in their regular wor...
New
PragmaticBookshelf
Ruby, Io, Prolog, Scala, Erlang, Clojure, Haskell. With Seven Languages in Seven Weeks, by Bruce A. Tate, you’ll go beyond the syntax—and...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1147 29994 760
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
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
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New