0

I've got with my own layout.

I would like to receive the text from the editText after positive button on the layout is clicked. But in onPreferenceChange, I always get only the default value.

It seems that I need to bind my own EditText to the preferences somehow, but I don't know how and where to do this.

Can anybody help me?

Hawk
  • 392
  • 4
  • 24

1 Answers1

2

To answer my own question:

First of all, in PreferenceScreen, you need to state: <full.path.to.your.OwnLayoutClass android:name="whatevever" android:dialogLayout="@layout/your_own_layout" />

your_own_layout can be anything you'd like, linearlayout with of buttons, editTexts, according to your wishes.

Essential is the class representing your own preference Dialog. Here is a simple example of how to do it:

public class YourOwnLayoutClass extends DialogPreference {

    private LinearLayout mView;

    public YourOwnLayoutClass(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(false);
        setDialogLayoutResource(R.layout.your_own_layout);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        mView = (LinearLayout) view;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult) {
            // get value from editFields, do whatever you want here :)
            // you can acces them through mView variable very easily
        }
    }
}

Important references: I need to have a custom dialog in Preferences

Concise way of writing new DialogPreference classes?

Android: launch a custom Preference from a PreferenceActivity

Community
  • 1
  • 1
Hawk
  • 392
  • 4
  • 24