-2

I have searched and found a lot of different answers for this question, but nothing quite settles it for me. Total android n00b, and I ask for your patience in advance.

I'm having trouble dealing with a FileChooser problem with Android KitKat. As per [here][1], and according to [Steve N][2] i get the impression that this file chooser problem is caused by my android version (4.4.2)

Given that filechooser isn't working, I want to implement a basic dialog. I'm going to check the android version number for the device, and then display a message, citing the current lack of support, if the version number comes back with a 4.4 in front.

At present, i'm just using toast

public boolean checkVersionSupport(){
    if (Build.VERSION.RELEASE.equals("4.4.2") {
        Toast toast = Toast.makeText(context, androidOS, duration);
        toast.show();
    }
}

Instead of toast, I'd like a simple, one button dialog box to open, with an OK button, which I will use to redirect the user out of the native app and off to chrome, where the whole file chooser thing seems to be working.

Couple of things that I have found difficult after reading through the android developer materials.

  1. Do I need a layout XML file for the dialog box?

  2. Where do I put the class file for the MyAlertDialogFragment class I am creating? Can it be anywhere in the java folder or does it have to be in a sub folder java/com.myproject... And what impact does that have for importing the class into my java activity file?

  3. Can someone please explain where FragmentAlertDialog comes from in the Android Developer Materials.

    public static class MyAlertDialogFragment extends DialogFragment {
    
    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");
    
        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((**FragmentAlertDialog**)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((**FragmentAlertDialog**)getActivity()).doNegativeClick();
                        }
                    }
                )
                .create();
      }
    }
    
  4. What's best practice for icons? I see a debate going on about this, but for the simple purpose of having an icon at the top of a dialog, for one single basic usage, what would be the quickest way of getting it done? I'm just copying files into the res folder and referencing them... 48dp? This might be a whole different question.

Hems
  • 1
  • 8
John Owens
  • 61
  • 6
  • if you want a file picker, you should look at the source code for some already existing libraries, they're useful - this one looks particularly interesting to me: https://github.com/spacecowboy/NoNonsense-FilePicker – EpicPandaForce May 11 '15 at 10:40
  • I'm not overly pushed about getting the file picker piece to work. I'm happy enough to just not support that version of android for now. If the lollipop release continues for my mobile provider, then I'll revisit, but i'm expecting an update to be rolled out pretty soon. But Thanks! I appreciate the effort to solve what is clearly the bigger problem. Unfortunately, it's the simpler issue of the dialog implementation that I'm stuck with at the mo. – John Owens May 11 '15 at 12:24

1 Answers1

0

Do I need a layout XML file for the dialog box?

That honestly depends on which type of Dialog you use.

  • The AlertDialog does not require a specified layout, all you need is to use AlertDialog.Builder() and set the positive/negative buttons, and how it handles that.

  • The DialogFragment onCreateDialog() method is the preferred way of using an AlertDialog. It also doesn't require an XML layout, but as such, its layout is still restricted to essentially a Yes/No option: look here for complete example

  • The DialogFragment onCreateView() method allows you to create a dialog from a specified layout XML. So if you wish to customize the view of the dialog beyond "title, description, yes/no", then yes, you need to specify the XML file for the dialog fragment, look here for an example, although the advice says you should look into ButterKnife and Otto libraries to make it even better.

Where do I put the class file for the MyAlertDialogFragment class I am creating? Can it be anywhere in the java folder or does it have to be in a sub folder java/com.myproject... And what impact does that have for importing the class into my java activity file?

Anywhere inside the project. Although I prefer to put it in something like <projectroot>/presentation/fragments/dialog, package-wise.

Can someone please explain where FragmentAlertDialog comes from in the Android Developer Materials.

FragmentAlertDialog is an assumed Activity from which the MyAlertDialogFragment dialog fragment is shown, and is assumed to have the methods doPositiveClick() and doNegativeClick(). The title int that is provided is most likely a string resource defined in /res/values/strings.xml which is used for localization. For example, R.string.fancy_title_name.

So it's something like this

public class FragmentAlertDialog extends AppCompatActivity {
    @Override
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_fragment_alert_dialog);
        //stuff
    }

    @Override
    public void onPostResume() {
        MyAlertDialogFragment madf = MyAlertDialogFragment.newInstance(R.string.something);
        madf.show(getSupportFragmentManager(), "alert-dialog-fragment");
    }
}

Otherwise, MyAlertDialogFragment just extends from DialogFragment and overrides onCreateDialog to create an AlertDialog inside this DialogFragment.

In case you'd ask, the newInstance() method is so that you would NOT use a parametrized constructor. Fragments should not have parametrized constructors, they ought to receive their data in the setArguments(Bundle) method.

What is the best practice for icons?

Refer to the material design guidelines.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428