0

I have a bunch of text views in a linear layout. The user's interaction with them is view-only: If the user guesses correctly (using voice), that guess will show in one of the text views. It looks like this (focus on the green boxes):

enter image description here

The pertinent XML looks like this:

<ScrollView
    android:id="@+id/wof_guesses"
    ...
    >

    <LinearLayout
        android:id="@+id/wof_guesses_list"
        ...
        android:orientation="vertical">

        <TextView
            android:id="@+id/wof_guess0"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess1"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess2"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess3"
            style="@style/WofGuess" />

        ...

    </LinearLayout>
</ScrollView>

And this is the style XML:

<style name="WofGuess" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/mentor_grey_100</item>
    <item name="android:text">Guess?</item>
    <item name="android:fontFamily">@font/roboto</item>
    <item name="android:visibility">invisible</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@android:drawable/editbox_background</item>
    <item name="android:padding">16dp</item>
    <item name="android:textSize">24sp</item>
</style>

In code, when the user guesses something, I try to set the text property of one of the text views (the next one still showing "Guess?") to the text the user just guessed. To get to those text views, I iterate through the children of the linear layout, like this:

for (tv in binding.wofGuessesList.children) {
    tv.setBackgroundColor(Color.parseColor("#90EE90"))
    tv.text = "no such property!"
    tv.setText = "no such property!"

My question: There are dozens, if not hundreds, of properties, methods and callbacks available for those tv's, and I show here setting the background color to that green you see in the screenshot. But not the text??? What am I missing?

Thank you!

2
  • 1
    You need to cast the type of View to TextView. (tv as? TextView)?.setText("no such property!"). The type of tv here is type View. Commented Feb 13 at 17:48
  • Thank you, worked like a charm and I wish I hadn't spent 2 days before posting the question :/ Commented Feb 13 at 18:07

1 Answer 1

1

while you didnt provide a full example of what you are doing this code comes into suspect

for (tv in binding.wofGuessesList.children) {
    tv.setBackgroundColor(Color.parseColor("#90EE90"))
    tv.text = "no such property!"
    tv.setText = "no such property!"

tv here is likely View and not a TextView which has the .setText or .text you are looking for. You need to cast tv as a TextView.

But also why are you looping through the children and not just reference them by their id's? You are creating unnecessary overhead

Sign up to request clarification or add additional context in comments.

1 Comment

Worked, thank you. Re not addressing them by ID - it has to do with some other features of this screen that are "beyond the scope of my question", but I do appreciate your point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.