3

I want put a button on android edittextpreference. I create a custom editextpreference:

public class EditTextPreferenceWithButton extends EditTextPreference {

    private Context context;

    public EditTextPreferenceWithButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context=context;
      }

      public EditTextPreferenceWithButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
      }

      public EditTextPreferenceWithButton(Context context) {
        super(context);
        this.context=context;
      }



      @Override
        protected void onBindDialogView(View view) {
            super.onBindDialogView(view);



           view.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));



            final EditText editText = (EditText)view.findViewById(android.R.id.edit);
            ViewGroup vg = (ViewGroup)editText.getParent();

            Button button = new Button(context);


            vg.addView(button,ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);


        }
}

In this way the button is show below the edit text, but I want it is next the edittext like this:

|EditText| |Button|

Please help me! Thank you

user1437101
  • 117
  • 1
  • 5
  • 13

2 Answers2

5

I would create a subclass of DialogPreference.

class EditTextDialogPreference extends DialogPreference {

    //Layout Fields
    private final LinearLayout layout = new LinearLayout(this.getContext());
    private final EditText editText = new EditText(this.getContext());
    private final Button button = new Button(this.getContext());


    //Called when addPreferencesFromResource() is called. Initializes basic paramaters
    public EditTextDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(true);
        button.setText("Button");
        layout.setOrientation(LinearLayout.HORIZONTAL);
    }

    //Create the Dialog view
    @Override
    protected View onCreateDialogView() {
        layout.addView(editText);
        layout.addView(button);
        return parentLayout;
    }

    //Attach persisted values to Dialog
    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        editText.setText(getPersistedString("EditText"), TextView.BufferType.NORMAL);
    }

    //persist values and disassemble views
    @Override
    protected void onDialogClosed(boolean positiveresult) {
        super.onDialogClosed(positiveresult);
        if (positiveresult && shouldPersist()) {
            persistString(editText.getText().toString());
        }

        ((ViewGroup) editText.getParent()).removeView(editText);
        ((ViewGroup) button.getParent()).removeView(button);
        ((ViewGroup) layout.getParent()).removeView(layout);

        notifyChanged();
    }
}

I'm assuming you're going to persist the value in the EditText and I'm leaving the action of the button up to you. See this post for more information on the in's and out's of extending DialogPreference.

In order to get the key into the SharedPreferences, put the following in your XML:

<com.yourpackage.EditTextDialogPreference
    android:key="Your Key"
    android:persistent="true"/>
Community
  • 1
  • 1
gobernador
  • 5,659
  • 3
  • 32
  • 51
  • @user As K_Anas said, when your problem is solved, remember to mark an answer as accepted. This really helps other people who come along later with a similar problem. – gobernador Jul 02 '12 at 02:37
  • Thank you so much, all work fine but when I call addPreferencesFromResource(R.xml.preferences) it dont persit the value. – user1437101 Jul 02 '12 at 18:34
  • What are you getting back? Is the preference key valid, and there's just a blank string? Or is there no key at all? – gobernador Jul 02 '12 at 22:46
  • how can i get click event on that button in my preferencesActivity ? – Uniruddh Jan 08 '14 at 08:09
0

Here's is a more concrete class based on @gobernador answer but using RelativeLayout:

class EditTextDialogPreference extends DialogPreference {

    //Layout Fields
    private final RelativeLayout layout = new RelativeLayout(this.getContext());
    private final EditText editText = new EditText(this.getContext());
    private final Button button = new Button(this.getContext());


    //Called when addPreferencesFromResource() is called. Initializes basic paramaters
    public EditTextDialogPreference(final Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(true);
        button.setText("Button");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //button action
            }
        });
        editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

    //Create the Dialog view
    @Override
    protected View onCreateDialogView() {
        layout.addView(editText);
        layout.addView(button);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        return layout;
    }

    //Attach persisted values to Dialog
    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        editText.setText(getPersistedString("EditText"), TextView.BufferType.NORMAL);
    }

    //persist values and disassemble views
    @Override
    protected void onDialogClosed(boolean positiveresult) {
        super.onDialogClosed(positiveresult);
        if (positiveresult && shouldPersist()) {
            String value = editText.getText().toString();
            if (callChangeListener(value))
                persistString(value);
        }

        ((ViewGroup) editText.getParent()).removeView(editText);
        ((ViewGroup) button.getParent()).removeView(button);
        ((ViewGroup) layout.getParent()).removeView(layout);

        notifyChanged();
    }

    public void setValue(String value) {
        editText.setText(value);
    }
}
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90