aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/settings/TextPreference.java
blob: eecf4ccf3f6a9a9f8ce862a31dae239d65017ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cgeo.geocaching.settings;

import cgeo.geocaching.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Preference to simply show a text message. Links are not shown.
 * <p>
 * Usage: The displayed text is taken from the "android:text" attribute of the preference definition. Example:
 *
 * <pre>
 * <cgeo.geocaching.settings.TextPreference
 *      android:text="@string/legal_note"
 *      android:layout="@string/text_preference_default_layout"
 * />
 * </pre>
 *
 * </p>
 */
public class TextPreference extends AbstractAttributeBasedPrefence {

    private String text;
    private TextView summaryView;
    private CharSequence summaryText;

    public TextPreference(Context context) {
        super(context);
    }

    public TextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected int[] getAttributeNames() {
        return new int[] { android.R.attr.text };
    }

    @Override
    protected void processAttributeValues(TypedArray values) {
        this.text = values.getString(0);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        this.setSelectable(false);

        View v = super.onCreateView(parent);

        TextView text = (TextView) v.findViewById(R.id.textPreferenceText);
        text.setText(this.text);

        summaryView = (TextView) v.findViewById(R.id.textPreferenceSummary);
        setSummary(null); // show saved summary text

        return v;
    }

    @Override
    public void setSummary(CharSequence summaryText) {
        // the layout hasn't been inflated yet, save the summaryText for later use
        if (summaryView == null) {
            this.summaryText = summaryText;
            return;
        }

        // if summaryText is null, take it from the previously saved summary
        if (summaryText == null) {
            if (this.summaryText == null) {
                return;
            }
            summaryView.setText(this.summaryText);
        } else {
            summaryView.setText(summaryText);
        }
        this.summaryView.setVisibility(View.VISIBLE);
    }

}