Is there a simple way to get a control when it load from a xml file by using Anko in Android Studio 3.1.2?
Asked Answered
W

0

6

I create a customize Preference layout, and display the UI with UIPreference.kt.

I can handle the button btnClose simply by import kotlinx.android.synthetic.main.layout_preference.*.

Now I hope to handle the CheckBoxPreference chAutoRestore simply, but the layout is loaded from xml\mypreference.xml, how can I do?

At present, I have to use the code val checkboxPref = preferenceManager.findPreference(getString(R.string.IsAutoRestore)) as CheckBoxPreference

UIPreference.kt

import kotlinx.android.synthetic.main.layout_preference.*

class UIPreference : AppCompatPreferenceActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_preference)

        fragmentManager.beginTransaction().replace(R.id.content, MyPreferenceFragment()).commit()


         btnClose.setOnClickListener {
            finish()
        }


    }


    class MyPreferenceFragment : PreferenceFragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            addPreferencesFromResource(R.xml.mypreference)

            val checkboxPref = preferenceManager.findPreference(getString(R.string.IsAutoRestore)) as CheckBoxPreference

            checkboxPref.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
                logError( "Pref " + preference.key + " changed to " + newValue.toString())
                true
            }

        }
    }

}

layout_preference.xml

<?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">

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/ad_unit_id"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toTopOf="@+id/btnClose"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/adView"
        >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />

    </FrameLayout>


    <Button
        android:id="@+id/btnClose"
        style="@style/myTextMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="2dp"
        android:text="@string/BtnClose"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

mypreference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="AppPreference"
    android:summary="@string/PreferenceSummary"
    android:title="@string/PreferenceTitle" >

    <PreferenceCategory android:title="General">
        <EditTextPreference
            android:defaultValue="7000"
            android:inputType="number"
            android:key="WebServerPort"
            android:summary="My PreferencePortSummary"
            android:title="My Title"

            />
    </PreferenceCategory>


    <PreferenceCategory android:title="Auto Restore">
        <CheckBoxPreference
            android:id="@+id/chAutoRestore"
            android:key="@string/IsAutoRestore"
            android:summary="Auto restore every 15"
            android:title="Auto Restore" />

    </PreferenceCategory>


</PreferenceScreen>

To Raymond Arteaga

Thanks!

chAutoRestore is id of CheckBoxPreference android:id="@+id/chAutoRestore"

I hope that I can use the following code

chAutoRestore.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
             ...
}

Just like

btnClose is id of Button android:id="@+id/btnClose"

I can use the code by import kotlinx.android.synthetic.main.layout_preference.*.

btnClose.setOnClickListener {
  ...
}
Well answered 31/5, 2018 at 2:5 Comment(2)
I can't fully understand your question. CheckBoxPreference is not a control (View), but instead a kind of "holder", which is used by the framework to "inflate" a view according to the kind of preference. Maybe I'm a little dumb, but an you please simplify/clarify your question?Rhetorical
Thanks, would you please see my added content in my question?Well

© 2022 - 2024 — McMap. All rights reserved.