Jeffrey Lease
Jeffrey Lease

Reputation: 87

I have a crossword layout in Android - how to add the little numbers?

I am new to Android development, and am trying to create a crossword app. Currently I have a custom Button called CrosswordCell that I am using for each square in the crossword which can handle the styling and interaction as needed. I plan to use the text of the button to represent the letter that the user inputs to the grid.

I am trying to add the little label numbers and don't how to do that. Is it possible to add that in the CrosswordCell class itself or would I need to add a TextView in the layout next to each CrosswordCell?

I know this is a pretty broad question, and may have lots of different approaches. I have not found good examples to help guide me here, so any input is appreciated. Below is an example picture of what I am trying to accomplish and a snippet of XML (just showing 3 cells of the first row for ease of reading) and class I am using :

Crossword Puzzle Example

XML

    <LinearLayout
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/row0"
            style="@style/CrosswordRow">
            <com.example.crosswordconstructor.CrosswordCell
                android:id="@+id/cell00"
                style="@style/CrosswordCell"
                android:text="A" />
            <com.example.crosswordconstructor.CrosswordCell
                android:id="@+id/cell01"
                style="@style/CrosswordCell"
                android:text="A" />
            <com.example.crosswordconstructor.CrosswordCell
                android:id="@+id/cell02"
                style="@style/CrosswordCell"
                android:text="A" />
                .
                .
                .
        </LinearLayout>
        .
        .
        .
    </LinearLayout>

Kotlin class

package com.example.crosswordconstructor
import android.content.Context
import android.util.AttributeSet

class CrosswordCell @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : androidx.appcompat.widget.AppCompatButton(context, attrs, defStyleAttr)
{
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)
        if (width > (height + 0.5).toInt() ) {
            width = (height + 0.5).toInt()
        } else {
            height = width
        }
        super.onMeasure(
            MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
        )
    }
}

Upvotes: 0

Views: 376

Answers (0)

Related Questions