Alertdialog.Builder setview: calling requires API level 21

I am trying to get the NumberPicker radius running in a class that extends DialogPreference, and I'm having problems with setView (). Let's start with the code:

public class RadiusPickerPreference extends DialogPreference{ public RadiusPickerPreference(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder) { builder.setTitle(R.string.set_radius_dialog_fragment_title); builder.setView(R.layout.dialog_radius_picker); builder.setPositiveButton(android.R.string.ok, null); builder.setNegativeButton(android.R.string.cancel, null); } } 

This gives me an error in builder.setView saying: "API 21 is required to call (current min - 15)." I want to support devices with API 15+, so changing this setting is not an option. Now, if I try to override

protected void onPrepareDialogBuilder(android.support.v7.app.AlertDialog.Builder builder)

instead, he says: "A method does not cancel a method from its superclass."

The question is, how can I set the view? This does not have to be in onPrepareDialogBuilder () if it supports API 15+. Thanks!

PS: Let me know if you need more code. To display it in XML, just add it to <PreferenceScreen> :

 <com.example.project.RadiusPickerPreference android:id="@+id/radPickerPref" android:key="@string/pref_key_default_radius" android:title="@string/pref_title_default_radius"/> 
+6
source share
3 answers

What you are trying to do here is to call a function added in API 21 and not added in API 1. According to the documentation, you want setView(View view) instead of setView(int layoutResId) . To get the View from the layout, you will need a LayoutInflater . To get an instance of LayoutInflater , you need a context object. When you create your dialog, I would recommend storing your Context as a variable in the class for future use. Then in onPrepareDialogBuilder you can use ( as per docs ):

 LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE) 

Now you can use inflater to get the View from your layout and set up the View dialog as follows:

  View v = inflater.inflate(R.layout.dialog_radius_picker, null); 

So your code might look like this:

 @Override protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder) { LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); builder.setTitle(R.string.set_radius_dialog_fragment_title); View v = inflater.inflate(R.layout.dialog_radius_picker, null); builder.setView(v); builder.setPositiveButton(android.R.string.ok, null); builder.setNegativeButton(android.R.string.cancel, null); } 

Hope this helps!

+19
source

Instead of calling setView(int resourceId) , which requires API21 +, just create a View object, start the resource and call setView(View view) , passing this view.

+2
source

I had some not very funny impressions trying to set up alert dialogs, and would recommend just circumventing this idea when you really need to have a detailed popup. Here is the code for the dialog fragment if you want to try this route ...

 public class AboutUs extends DialogFragment { public interface DialogListener { void onDialogFinish(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_about_us, container, false); getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); Display display = getActivity().getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width=size.x; int height=size.y; //change these to make your dialog the size you wish WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes(); wmlp.height=height; wmlp.width=width; getDialog().getWindow().setAttributes(wmlp); WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes(); lp.dimAmount=0.4f; getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getDialog().setCanceledOnTouchOutside(true); return rootView; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NO_FRAME, android.support.v7.appcompat.R.style.Theme_AppCompat_Light); } public AboutUs() { } } \\to call fragment from activity AboutUs aboutUs = new AboutUs(); aboutUs.show(getSupportFragmentManager(), "Dialog Fragment"); 
0
source

Source: https://habr.com/ru/post/989008/


All Articles