K
K
Kogoth2018-05-06 13:51:23
Android
Kogoth, 2018-05-06 13:51:23

How to get AdapterView from adapter?

There are simple Layout files myspiner.xml offline_game.xml:

spoiler
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="top"
    android:singleLine="true"
    android:includeFontPadding="false"
    android:lineSpacingExtra="0dp" />
spoiler
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.82" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.11" />

    <Spinner
        android:id="@+id/Galactic_size"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

The simplest set of strings.xml :
spoiler
<string-array name="Galactic_Size">
        <item>Малая​|ﺃ</item>
        <item>Средняя|ﺃ</item>
        <item>Большая|ﺃ</item>
    </string-array>

And finally, the simplest Kotlin code that dynamically selects the font size for the size of the spinner:
spoiler
StartGameButton.setOnClickListener {
            setContentView(R.layout.offline_game)
            //val spinner = findViewById<Spinner>(R.id.Galactic_size)
            val adapter = ArrayAdapter.createFromResource(this, R.array.Galactic_Size, R.layout.myspiner)
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            /*spinner*/Galactic_size.adapter = adapter

//Неудачная попытка№ 1
            //Galactic_size.getChildAt(0).findViewById<TextView>(text1.id).setTextSize(TypedValue.COMPLEX_UNIT_PX, 50f)
//Неудачная попытка№ 2
            //Galactic_size.findViewById<TextView>(text1.id).setTextSize(TypedValue.COMPLEX_UNIT_PX, 50f)
//Неудачная попытка№ 3
            //(Galactic_size.adapter.getItem(0) as TextView).setTextSize(TypedValue.COMPLEX_UNIT_PX, 50f)

//Работающий но часто вызываемый лишний раз метод
            val OnCatSpinnerCL = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
                    val TAG = "myLogs"
                    Log.d(TAG, "Начало")
                    var finalTextSize=14f
                    val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
                    paint.style = Paint.Style.FILL
                    //paint.color = color
                    //paint.textAlign = Paint.Align.CENTER
                    paint.textSize = 100f //размер шрифта в **пикселях**
                    val bounds = Rect()
                    var textheight = 0
                    var textwidth = 0
                    for (i in resources.getStringArray(R.array.Galactic_Size).indices) {
                        val text = resources.getStringArray(R.array.Galactic_Size)[i]
                        paint.getTextBounds(text, 0, text.length, bounds)
                        Log.d(TAG, bounds.height().toString() + " : " + bounds.width().toString())
                        if (textheight < bounds.height()) textheight = bounds.height()//высота
                        if (textwidth < bounds.width()) textwidth = bounds.width()//ширина
                    }
                    Log.d(TAG, textheight.toString() + " : " + textwidth.toString())
                    Log.d(TAG, Galactic_size.measuredHeight.toString() + " : " + (parent.getChildAt(0) as TextView).measuredHeight.toString())
                    if (Galactic_size.measuredHeight.toFloat() / (textheight.toFloat() / 95f) < Galactic_size.measuredWidth.toFloat() / (textwidth.toFloat() / 95f)){
                        finalTextSize=Galactic_size.measuredHeight.toFloat() / (textheight.toFloat() / 95f)
                    }else{
                        finalTextSize=Galactic_size.measuredWidth.toFloat() / (textwidth.toFloat() / 95f)
                    }
                    Log.d(TAG, finalTextSize.toString())
                            (parent.getChildAt(0) as TextView).setTextColor(Color.BLUE)
                            (parent.getChildAt(0) as TextView).setTextSize(TypedValue.COMPLEX_UNIT_PX, finalTextSize)
                }
                override fun onNothingSelected(parent: AdapterView<*>) {
                }
            }
            Galactic_size.onItemSelectedListener = OnCatSpinnerCL
        }

Essence of the question: Galactic_size.onItemSelectedListener = OnCatSpinnerCL - makes all the code work every time an item is selected. (parent.getChildAt(0) as TextView).setTextSize(TypedValue.COMPLEX_UNIT_PX, finalTextSize) is the only thing I actually need, but it only works because parent: AdapterView<*> is passed to it. As a result, I want to know how, after assigning Galactic_size.adapter = adapter, to get an instance of the text1 element created for it in order to change its font size one single time where I want and not on every click.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kogoth, 2018-05-10
@Kogoth

In short, I was able to get what I wanted:
Galactic_size.setSelection(0, true)
(Galactic_size.selectedView as TextView).setTextSize(TypedValue.COMPLEX_UNIT_PX, 500f)
But it turned out that the TextView is recreated with each selection, and therefore my original method is more correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question