Ranjitha
Ranjitha

Reputation: 39

how to override onCreateDialogView() and add custom view inside dialogpreference

I am trying to add custom view inside Dialog preference, but I don't know how to override oncreatedialogview. Please help me.

@Override
protected View onCreateDialogView() {
    // TODO Auto-generated method stub
    LayoutInflater inflt = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflt.inflate(R.layout.numberpicker,null);
    if(view!=null){
        System.out.println("view error");
    }else{
        System.out.println("view ");
    }
    /*plus=(Button)view.findViewById(R.id.buttonplus1);
    minus=(Button)view.findViewById(R.id.buttonminus1);
    d   isplay=(TextView)view.findViewById(R.id.textView1);
    */return view;
}

Upvotes: 2

Views: 1453

Answers (3)

SMBiggs
SMBiggs

Reputation: 11686

The problem is that you're overriding the wrong class.

Here's the code we all want to make a new DialogPreference:

public class MyCustomPreference extends DialogPreference {

and like most of the time, we use the auto-complete to make sure we don't have any typing mistakes and to be nice and speedy programmers.

But Android Studio creates this import statement:

import androidx.preference.DialogPreference;

That's nice and all, but the AndroidX class does not have an onCreateDialogView() method.

Fortunately the fix is easy. Just delete the 'x' from the import statement, giving you:

import android.preference.DialogPreference;

Because of the many different libraries, this sort of issue is common, and will get more and more common as things keep changing.

Upvotes: 1

nc2000
nc2000

Reputation: 171

You should not have to override the onCreateDialogView() method if you specify the resource in your DialogPreference class by calling setDialogLayoutResource(). For example:

public class NumberPickerPreference extends DialogPreference
{
    public NumberPickerPreference(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setDialogLayoutResource(R.layout.numberpicker);
    }
}

Upvotes: 2

Catalina
Catalina

Reputation: 2098

you can override the onCreateDialog method and add

Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.layout);

return dialog;

inside it

Upvotes: 0

Related Questions