0

You may think this is a duplicate question but i looked up almost every existing answer and i still did not get it right. Here is my question:

  1. I want to create a default YesNoDialogPreference by extending the DialogPreferenceclass
  2. Creating a preference using YesNoDialogPreference in prefs.xml
  3. In the MainActivity i want to set an onClickListener for Yes and No options

I have tried doing this using AlertDialog.Builder but it didn't work, i've also tried to use com.android.internal.preference.YesNoPreference and it did work cause of R.attr error Can somebody please give me a full answer...PLEASE!!, i have been struggling with this for weeks now.

Here's my code: YesNoDialogPreference.java

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;

public class YesNoDialogPreference extends DialogPreference {

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

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

}

The preference from prefs.xml

<com.me.myapp.YesNoDialogPreference
            android:key="KEY"
            android:dialogMessage="You sure ?"
            android:title="Do something"
            android:summary="will do something"
            />

I do not know how to link them in the MainActivity.

Bisho
  • 5
  • 4
  • Updated my question..please help – Bisho Oct 21 '14 at 12:54
  • Any error corrupted? Try to define both constructors: public YesNoDialogPreference(Context context, AttributeSet attrs) { super(context, attrs); } public YesNoDialogPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } – Govtart Oct 21 '14 at 13:02
  • Done it...This will create the DiaolgPreference ok..But i want to make some thing like dialog.setPoasativeButton(......etc); in my MainActivity...just like the AlertDialog – Bisho Oct 21 '14 at 13:07

2 Answers2

1

What you are trying to achieve does not make sense. The MainActivity is not active so can't be the target of the dialog. You need an onClick handler in your YesNoDialogPreference which then does what you want. You typically safe the value in your settings and read that value in all other places - like your MainActivity. Here is a code sample: How to get the DialogPreference POSITIVE_BUTTON to work on OnClick?

Community
  • 1
  • 1
Carsten
  • 806
  • 5
  • 6
  • Okay..so i have like 6 or more DialogPrferences..do i have to do this for each one of them? – Bisho Oct 21 '14 at 13:13
  • Yes but then you can create a common base class - given that you have common steps. – Carsten Oct 21 '14 at 13:43
  • I actually thought about it..but i dont know how to exactly implement it.. Can you please tell me how? – Bisho Oct 21 '14 at 14:32
  • Create an abstract class which extends DialogPreference and covers your common steps. Then create your 6 different classes which extend your abstract base class. That is just one possible way. – Carsten Oct 21 '14 at 15:28
  • Well...This is gonna take both time and space... isn't there a way to implement this just like androids built in YesNoPreference? where you set the dialog vars and set an onClickListener based on the key of the preference? That would be grgeat – Bisho Oct 21 '14 at 16:39
0

Just use onClick method and implement listener to provide handle actions where you want

@Override
public void onClick(DialogInterface dialog, int which){

    if(which == DialogInterface.BUTTON_POSITIVE) {
        // do your stuff to handle positive button

    }else if(which == DialogInterface.BUTTON_NEGATIVE){
        // do your stuff to handle negative button
    }
 }
Govtart
  • 332
  • 1
  • 9
  • This is what i needed, But i have 5 YesNo preferences, how can i identify which one was clicked in my YesNoDialog abstract class using android:key?? cause i want one class to handle all the YesNoPreferences – Bisho Nov 06 '14 at 13:36
  • Try to String key = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "key", 100); – Govtart Nov 06 '14 at 14:32
  • Okay that is just great @Govtart, but i want to use that key in a switch statement, can you show me how? – Bisho Nov 11 '14 at 12:25