2

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;
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Ranjitha
  • 39
  • 4

3 Answers3

2

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);
    }
}
nc2000
  • 161
  • 9
1

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.

SMBiggs
  • 11,034
  • 6
  • 68
  • 83
0

you can override the onCreateDialog method and add

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

return dialog;

inside it

Catalina
  • 1,954
  • 1
  • 17
  • 25