• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I am learning Android and have created a TableLayout with two rows in an Activity called PlayActivity. In each row I put TextViews that I want to have borders in the colours with suffix Edge given in colors.xml. Everything works fine except that the YELLOW (text) colour does not lead to yellow border colour in tv.setBackgroundResource(R.drawable.yellow_edge).

I am making a game where the user ends up in the PlayActivity and draws a number of tiles from a "bag". These tiles have a number in the colours in capitals (e.g. YELLOW) and a border in another colour defined in the colors.xml file. I checked whether my yellow_edge colour is opaque and it is. I don't understand why the other colours work while this one does not. Can anyone please help find my mistake? The output I get is this: output

My Tile Class:

import android.graphics.Color.rgb
val BLUE: Int = rgb(0, 0, 255)
val RED: Int = rgb(255, 0, 0)
val YELLOW: Int = rgb(255,235,0)
val BLACK: Int = rgb(0, 0, 0)


class Tile(val col: Int = 0, val num: Int = 0){
    val name = "$col $num"
}

How I instantiate my tiles:

class PlayActivity : AppCompatActivity() {
    val COLOURS = listOf(BLUE, RED, YELLOW, BLACK)
    val NUMBERS = IntRange(1, 13)
    lateinit var nameTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_play)
        val pack = makeTiles()
        nameTv = findViewById(R.id.nameTextView)
        val player = Player("Jack", plankGrid, this)
        nameTv.text = player.name
        player.drawTiles(14, pack)

    }
    private fun makeTiles(): MutableList<Tile>{
        val a = mutableListOf<Tile>()
        var count = 0
        for (value in COLOURS) {
            for (number in NUMBERS) {
                count += 1
                val tile = Tile(value, number)
                a.add(tile)
            }
        }
        a.shuffle()
        return a
    }
}

activity_play.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context=".PlayActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/app_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp" android:id="@+id/textView"/>

    <TableLayout
            android:layout_width="440dp"
            android:layout_height="100dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintVertical_bias="1.0" android:orientation="horizontal" android:gravity="top|start"
            android:id="@+id/plankGrid">

        <TableRow android:id="@+id/row1" android:layout_width="match_parent" android:layout_height="match_parent"
                  android:orientation="horizontal"/>
        <TableRow android:id="@+id/row2" android:layout_width="match_parent" android:layout_height="match_parent"
                  android:orientation="horizontal"/>
    </TableLayout>


</android.support.constraint.ConstraintLayout>

My class where I fill the TableLayout with TextViews:

import android.app.Activity
import android.view.Gravity
import android.view.View
import android.widget.TableLayout
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_play.*
import kotlinx.android.synthetic.main.activity_play.view.*


class Player(val name: String = "Jack",
             private var table: TableLayout,
             private val ct: Activity,
             private var count: Int = 0) {

    fun drawTiles(n: Int, pack: MutableList<Tile>) {
        for (i in IntRange(0, n-1)) {
            addTileToTable(pack[i])
            count += 1
        }
    }

    fun addTileToTable(tile: Tile) {
        val tv = TextView(ct)
        tv.setTextColor(tile.col)
        tv.textSize = 24.toFloat()
        tv.text =  tile.num.toString()
        tv.textAlignment = View.TEXT_ALIGNMENT_GRAVITY
        tv.gravity = Gravity.CENTER
        tv.width = table.layoutParams.width / 15
        tv.height = table.layoutParams.height / 2
        when (tile.col) {  // check colours and then set the border around the textview to that colour
            BLUE -> tv.setBackgroundResource(R.drawable.blue_edge)
            RED -> tv.setBackgroundResource(R.drawable.red_edge)
            YELLOW -> tv.setBackgroundResource(R.drawable.yellow_edge) // this is where it apparently goes wrong
            BLACK -> tv.setBackgroundResource(R.drawable.black_edge)
        }
        if (count <= 15) {
            table.row1.addView(tv)
        }
        if (count in 16..30) {
            table.row2.addView(tv)
        }
    }
}

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="bluebck">#2882D1</color>
    <color name="blueEdge">#004780</color>
    <color name="white">#ffffff</color>
    <color name="redEdge">#E70000</color>
    <color name="yellowEdge">#FFEB3B</color>
    <color name="blackEdge">#000000</color>
</resources>

yellow_edge.xml and for comparison also blue_edge.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="1dp"/>
            <stroke android:width="1dip" android:color="@color/yellowEdge"/>
            <padding android:left="1dp" android:right="1dp"/>
        </shape>
    </item>
</selector>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="1dp"/>
            <stroke android:width="1dip" android:color="@color/blueEdge"/>
            <padding android:left="1dp" android:right="1dp"/>
        </shape>
    </item>
</selector>
      • 1
    • @ReneFerrari thanks for the suggestion. I just checked with debugger and it turns out tile.col is -8448 while YELLOW is -5376. However, they also don't match for the other lines in the when block. I will update the question with yellow_edge.

Okay so after creating a new project using the code you provided I have found out the following:

You define your colors as constants which you than have to import in Activity and Player class.

Now my assumption is that in one of those classes you import YELLOW wrong. You defined this color yourself, but import android.graphics.Color.YELLOW also exists. I believe that you have mistakenly imported this instead of just import YELLOW.

  • 1
Reply Report
    • Thank you so much for taking the time to find that out ! I tried your solution, and in doing so I found the real source of the bug. It turns out that that import was not the problem (I didn't import the colours at all but used global variables), but I had accidentally redefined the colours inside the PlayActivity class and somehow the yellow colour was not recognised in the when statement inside the Player class. It works now at least. Thanks again for your time :)