Search code examples
androidkotlinlayoutandroid-background

Kotlin - Finding layout based on it's id


I have the following layout xml:

?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".CombatScreen">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/combatTitleText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Combat"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/combatScreenLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/mountain_combat_background">

            <ImageView
                android:id="@+id/enemyCombatImage"
                android:layout_width="204dp"
                android:layout_height="215dp"
                android:layout_gravity="center"
                android:foregroundGravity="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/yeti_attack" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/attackButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:text="Attack" />

            <Button
                android:id="@+id/defendButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:text="Defend" />

        </LinearLayout>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

What my plan is, that the layout with 'combatScreenLayout' id would serve as the background for my game's battle screen. I would place two imageViews (the player and the enemy) into that layout and the layout's background attribute would serve as the battlefield's image. I want to set the background dynamically and programmatically in the Intent's 'onCreate()' method

I know 'setBackgroundResource()' can achieve this, but for that I need to get that layout into a variable. (Similar to handling a view with 'findViewById()') Is there any way I could do that?


Solution

  • If I understand you correctly, this line of code fix your problem:

            val combatLayout = findViewById<ConstraintLayout>(R.id.combatScreenLayout)
    

    You can refer to layout manager in the same way like to any other view, because it inherits from View class also.