How to use reflection using AlertDialog.Builder?

I am creating an Alert dialog using the following:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 

In Android 3.0, alert dialogs inherit the activity topic that created them. You can override this action by creating a warning dialog box:

 AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_DARK); 

(More on this here )

Unfortunately, this force closes on previous versions of Android. I believe that using reflection is the answer, but I cannot understand the syntax, no matter how hard I read. Can anyone provide an example?

+4
source share
1 answer

I assume that using reflection is the answer, but I can't understand the syntax, no matter how hard I read.

Perhaps you can use reflection. I would not.

I would go with HoneycombHelper .

This sample project also has a situation where it needs to do different things for version 3.0 versus no - in this case, work with the custom View in the action bar. You cannot call getActionView() on MenuItem pre-3.0.

So where I need a custom View , I do this:

  EditText add=null; if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) { View v=HoneycombHelper.getAddActionView(menu); if (v!=null) { add=(EditText)v.findViewById(R.id.title); } } if (add!=null) { add.setOnEditorActionListener(onSearch); } 

Here I buried the call to getActionView() in the static method of the HoneycombHelper class:

 class HoneycombHelper { static View getAddActionView(Menu menu) { return(menu.findItem(R.id.add).getActionView()); } } 

I only download HoneycombHelper in version 3.0 or higher, so even though it contains invalid method calls for older versions of Android, this is not a problem.

In your case, your HoneycombHelper will have a gimmeMyBuilderDammit() method or some that uses the level 11 API constructor.

+6
source

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


All Articles