AndroidPicker

repository·master·Indexed 27 days ago

https://github.com/gzu-liyujiang/androidpicker

A comprehensive library of Android picker components providing bottom-sheet style selection interfaces. It includes specialized pickers for dates, time, addresses (Province/City/District), numbers, colors, files, and images. The library features a CalendarPicker with customizable color schemes, an ImagePicker with integrated camera/gallery access and cropping via ActivityBuilder, and an OptionPicker for custom object lists. Available modules include Common, WheelView, WheelPicker, AddressPicker, FilePicker, ColorPicker, CalendarPicker, and ImagePicker.

Tokens
7.2K
Snippets
11
Records
31
Agent score
91%

What's inside AndroidPicker

  1. Overview of AndroidPicker Wheel Pickers

    master

    AndroidPicker provides a variety of wheel-style picker components for Android applications. Supported picker types include:

    • Single item picker (单项选择器)
    • Number picker (数字选择器)
    • Multi-level linked pickers (二三级联动选择器)
    • Date and time picker (日期时间选择器)
    • Birthday picker (生日选择器)
    • Ethnicity picker (民族选择器)
  2. Overview of AndroidPicker

    master

    AndroidPicker is a comprehensive Android library providing various picker components, including:

    • Date and Time pickers (for birthdays, business hours, etc.)
    • Single-item pickers (for gender, nationality, occupation, etc.)
    • Multi-level linked pickers (for license plates, fund investment dates, etc.)
    • City/Address pickers (Province, City, and District/County levels)
    • Number pickers (for age, height, weight, etc.)
    • Calendar pickers (for hotel or flight bookings)
    • Color pickers
    • File and Directory pickers
  3. Integrate ImagePicker for Camera, Gallery, and Cropping

    master

    AndroidPicker provides a unified interface for selecting images from the camera or gallery, followed by an optional cropping step.

    Important: Permissions Before calling the picker, you must ensure your application has been granted the following permissions by the user:

    • CAMERA
    • READ_EXTERNAL_STORAGE
    • WRITE_EXTERNAL_STORAGE
  4. Add AndroidPicker dependencies

    master

    Add the specific picker modules you need to your module-level build.gradle file. Replace <version> with the desired version number.

    Available Modules:

    • Common: Base window forms for custom popups.
    • WheelView: Wheel controls for custom wheel pickers.
    • WheelPicker: Single-item, number, multi-level linked, and date/time wheel pickers.
    • AddressPicker: Province/City/District address picker.
    • FilePicker: File and directory picker.
    • ColorPicker: Color picker.
    • CalendarPicker: Calendar date picker.
    • ImagePicker: Image picker.
  5. Customize Picker Styles

    master

    There are two ways to customize styles:

    1. Per-picker style (Recommended): Use picker.getWheelView().setStyle(R.style.YourStyle) to apply a specific style from res/values/styles.xml to a single instance.
    2. Global override (Not recommended): Override the WheelDefault style in res/values/styles.xml to affect all pickers in the app.
  6. Integrate Pickers with DialogFragment

    master

    Since all XXXPicker classes inherit from android.app.Dialog, they can be easily integrated into an androidx.fragment.app.DialogFragment by returning the picker instance in onCreateDialog.

    public class OptionPickerFragment extends DialogFragment {
    
        @NonNull
        @Override
        public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
            OptionPicker picker = new OptionPicker(requireActivity());
            picker.setData("土人", "里民子", "羡民", "穿青人", "不在56个民族之内", "未定民族");
            picker.setOnOptionPickedListener(new OnOptionPickedListener() {
                @Override
                public void onOptionPicked(int position, Object item) {
                    Toast.makeText(requireContext(), item.toString(), Toast.LENGTH_SHORT).show();
                }
            });
            picker.getWheelView().setStyle(R.style.WheelStyleDemo);
            return picker;
        }
    
    }
  7. Use ImagePicker to start Camera or Gallery selection

    master

    Use ImagePicker.getInstance() to launch the selection flow. You must pass the current Activity, a boolean for whether to enable cropping, and your PickCallback implementation.

    Required Activity Overrides To ensure the picker functions correctly, you MUST forward the following activity lifecycle methods to the ImagePicker instance:

    1. onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    2. onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    public class ImagePickerActivity extends FragmentActivity {
        private final PickCallback cropCallback = new PickCallback() {
            @Override
            public void onPermissionDenied(String[] permissions, String message) {
                // Handle permission denial
            }
    
            @Override
            public void cropConfig(ActivityBuilder builder) {
                builder.setMultiTouchEnabled(true)
                        .setGuidelines(CropImageView.Guidelines.ON_TOUCH)
                        .setCropShape(CropImageView.CropShape.OVAL)
                        .setRequestedSize(400, 400)
                        .setFixAspectRatio(true)
                        .setAspectRatio(1, 1);
            }
    
            @Override
            public void onCropImage(@Nullable Uri imageUri) {
                // Handle the cropped image Uri
            }
        };
    
        public void onCamera(View view) {
            // Start camera selection with cropping enabled
            ImagePicker.getInstance().startCamera(this, true, cropCallback);
        }
    
        public void onGallery(View view) {
            // Start gallery selection with cropping enabled
            ImagePicker.getInstance().startGallery(this, true, cropCallback);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            ImagePicker.getInstance().onActivityResult(this, requestCode, resultCode, data);
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            ImagePicker.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        }
    }
  8. Configure JitPack repository for AndroidPicker

    master

    To use AndroidPicker, you must add the JitPack repository to your Gradle configuration. The method depends on your Gradle version.

    For Gradle versions below 7.0: Add the repository to your project-level build.gradle file.

    For Gradle versions 7.0 and above: Add the repository to your settings.gradle file within the dependencyResolutionManagement block.

    // For Gradle < 7.0 in project-level build.gradle
    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }
    
    // For Gradle >= 7.0 in settings.gradle
    dependencyResolutionManagement {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }