1

I have created a custom EditTextPreference:

<EditTextPreference
                android:key="alert_planed_edittext_preference"
                android:title="@string/alert_planed_edittext_preference"
                android:summary="@string/alert_planed_summary_edittext_preference"
                android:dialogTitle="@string/alert_planed_dialog_title_edittext_preference"
                android:numeric="integer"
                android:layout="@layout/edit_text_preference_layout" />

the custom layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/alert_planed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dip"
        android:text="5" >
    </TextView>


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="6dip" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="2"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>

</RelativeLayout>

I just added the text view with id alert_planed.

In my preference activity i am trying to get the new EditTextPreference value and put it in my text view alert_planed. I am getting the EditTextPreference value, that's ok. But when i set the text i got a null exception telling me that the TextView is null.

Here is my Preference code:

addPreferencesFromResource(R.xml.preferences);

        mTextViewAlertPlanned = (TextView) findViewById(R.id.alert_planed);

        findPreference("alert_planed_edittext_preference").setOnPreferenceChangeListener(new OnPreferenceChangeListener()
        {

            @Override
            public boolean onPreferenceChange(
                    android.preference.Preference arg0, Object value) {

                if(value != null)
                {

                    Log.i("Preference","String.valueOf(value) ="+String.valueOf(value));

                    Log.i("Preference","mTextViewAlertPlanned ="+mTextViewAlertPlanned);

                    int myVal = Integer.parseInt(String.valueOf(value));

                    mTextViewAlertPlanned.setText(myVal);
                }
                return true;
            }
        });

How can i get my TextView correctly (the TextView is a part of the custom EditTextPreference layout)?

haythem souissi
  • 3,263
  • 7
  • 50
  • 77

2 Answers2

0

Simple Inflate you xml

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.custome_layout, null);

mTextViewAlertPlanned = (TextView) view.findViewById(R.id.alert_planed);
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • android.content.res.Resources$NotFoundException: String resource ID #0x6 – haythem souissi Jan 22 '13 at 09:33
  • check your strings.xml in values folder of your project and make sure all the string resource are there. – Sunny Jan 22 '13 at 09:57
  • i am sure i have all my strings, i think the problem here that we create a new layout which doesn't exist on the screen – haythem souissi Jan 22 '13 at 10:09
  • 01-22 11:00:37.371: E/AndroidRuntime(10124): android.content.res.Resources$NotFoundException: String resource ID #0x6 01-22 11:00:37.371: E/AndroidRuntime(10124): at android.content.res.Resources.getText(Resources.java:229) 01-22 11:00:37.371: E/AndroidRuntime(10124): at android.widget.TextView.setText(TextView.java:3620) 01-22 11:00:37.371: E/AndroidRuntime(10124): at fr.haploid.preferenceprotoptvo.Preference$1.onPreferenceChange(Preference.java:59) – haythem souissi Jan 22 '13 at 10:10
0

I had the same issue has you, and what worked for me was to create a class extending from EditTextPreference:

class CustomEditTextPreference constructor(
    context: Context,
    attrs: AttributeSet) : EditTextPreference(context, attrs) {

    lateinit var tvTitle: TextView
    lateinit var summary: TextView

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
        with(holder.itemView) {
            // Here would go your ids
            tvTitle = findViewById(R.id.tv_title)
            summary = findViewById(R.id.tv_summary)
        }
    }

    // Whenever you want to change the 'summary' you call this function and pass the value as parameter
    fun changeSummary(value: String){
        summary.text = value
    }
}

In the preferenceFragment, instead of doing:

preference.summary = newValue.toString()

you just call the function: changeSummary(newValue.toString())

Hope that works!