1

I'm using PreferenceFragment with some default preferences categories and one custom layout:

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

    <PreferenceCategory
        android:key="first_category"
        android:title="config" >
        <EditTextPreference
            android:defaultValue="example"
            android:dialogMessage="text"
            android:dialogTitle="Title"
            android:key="mykey"
            android:summary="summary"
            android:title="title" />

        <Preference
            android:key="test_connection"
            android:title="Test connection"
            android:layout="@layout/test_conn"  />
 </PreferenceCategory>

</PreferenceScreen>

My test_conn layout:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="6dip"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="6dip"
    android:layout_marginTop="6dip"
    android:layout_weight="1" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:text="Test connection"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:text="Ko"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ff0000"
        android:textStyle="bold" />

</RelativeLayout>

I'm reading the textview R.id.summary with this code:

        addPreferencesFromResource(R.xml.preferences);
        findPreference(TEST_CONN).setOnPreferenceClickListener(this);    
        Preference custom = findPreference(TEST_CONN);
        customv = custom.getView(null, null);
        res = (TextView) customv.findViewById(R.id.summary);

I can read correctly my TextView, but if I try to change the text, for example inside an asynctask, I can't see the text changed.

This is a part of asynctask:

        @Override
    protected void onPostExecute(Boolean result) {
        if (result == true){
            res.setText("Ok");
            res.setTextColor(Color.GREEN);
        }
        else {
            res.setText("Ko");
            res.setTextColor(Color.RED);
        }
        pdia.dismiss();
    }

When the user click on the preference "test_connection", an asynctask starts and in the funzion onPostExecute I modify the text, but doesn't work. Am I missing something? Should I read the textview with another method?

StarsSky
  • 6,721
  • 6
  • 38
  • 63

1 Answers1

1

Here is a solution: You need to implement a custom Preference class that extends the Android Preference class. Ensure that your custom class is in a separate file -- not part of another class -- otherwise you will get an 'Inflate XML error' like this. Here's an example:

My CustomPreference.java file:

public class CustomPreference extends Preference {
    private TextView txt;

    public CustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setWidgetLayoutResource(R.layout.test_conn);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        txt = (TextView) view.findViewById(R.id.summary);
    }

    public void setText(String text, int color) {
        txt.setText(text);
        txt.setTextColor(color);
    }
}

Your settings.xml file should insert/use the custom Preference class like this:

 <com.myapp.example.widget.CustomPreference
        android:name="com.myapp.example.widget.CustomPreference"
        android:key="test_connection"
        android:title="Test connection"
        android:layout="@layout/test_conn"  />

When you want to modify the TextView or Button on your custom layout, you need to call the public method setText(), providing parameters like text and color:

        @Override
    protected void onPostExecute(Boolean result) {
        if (result == true){
            CustomPreference custom = (CustomPreference) findPreference(TEST_CONN);
            custom.setText("Ok", Color.GREEN);
        }
        else {
            CustomPreference custom = (CustomPreference) findPreference(TEST_CONN);
            custom.setText("Ko", Color.RED);
        }
        pdia.dismiss();
    }

See also: Another answer here

Community
  • 1
  • 1
StarsSky
  • 6,721
  • 6
  • 38
  • 63