Example usage for android.widget NumberPicker invalidate

List of usage examples for android.widget NumberPicker invalidate

Introduction

In this page you can find the example usage for android.widget NumberPicker invalidate.

Prototype

public void invalidate() 

Source Link

Document

Invalidate the whole view.

Usage

From source file:com.crcrch.chromatictuner.app.NotePickerFragment.java

@NonNull
@Override/*w  w w  .  ja v  a 2  s.c o m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());

    View dialogContent = LayoutInflater.from(dialogBuilder.getContext()).inflate(R.layout.fragment_note_picker,
            null);

    final NumberPicker notePicker = (NumberPicker) dialogContent.findViewById(R.id.picker_note);
    notePicker.setMinValue(0);
    notePicker.setMaxValue(MiscMusic.CHROMATIC_SCALE.length - 1);
    notePicker.setDisplayedValues(MiscMusic.CHROMATIC_SCALE);
    notePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    final NumberPicker octavePicker = (NumberPicker) dialogContent.findViewById(R.id.picker_octave);
    octavePicker.setMinValue(0);
    octavePicker.setMaxValue(MAX_OCTAVE);
    octavePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    octavePicker.setWrapSelectorWheel(false);

    final TextView frequencyView = (TextView) dialogContent.findViewById(R.id.frequency);
    notePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            frequencyView.setText(selectFrequency(newVal, octavePicker.getValue()));
        }
    });
    octavePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            frequencyView.setText(selectFrequency(notePicker.getValue(), newVal));
        }
    });

    double frequencyToDisplay;
    if (savedInstanceState == null) {
        frequencyToDisplay = initialFrequency;
    } else {
        frequencyToDisplay = savedInstanceState.getDouble(STATE_FREQUENCY);
    }
    frequencyView.setText(selectFrequency(frequencyToDisplay));
    int note = getNoteForFrequency(frequencyToDisplay);
    octavePicker.setValue(getOctaveForNote(note));
    octavePicker.invalidate();
    notePicker.setValue(getNoteIndexForNote(note));
    notePicker.invalidate();

    return dialogBuilder.setTitle(R.string.dialog_title_note_picker).setView(dialogContent)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    listener.onFrequencySelected(frequency);
                    dialog.dismiss();
                }
            }).setNegativeButton(android.R.string.cancel, null).create();
}

From source file:com.bt.heliniumstudentapp.GradesFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
    mainContext = (AppCompatActivity) getActivity();
    gradesLayout = inflater.inflate(R.layout.fragment_grades, viewGroup, false);

    boolean pass = true;

    if (gradesHtml == null) {
        termFocus = Integer.parseInt(
                PreferenceManager.getDefaultSharedPreferences(mainContext).getString("pref_grades_term", "1"));
        yearFocus = 0;//from   w ww. ja v  a  2 s . c o m

        if (Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(mainContext)
                .getString("pref_general_class", "0")) == 0) {
            try { //TODO Improve
                maxYear = Integer.parseInt(((TextView) mainContext.findViewById(R.id.tv_class_hd)).getText()
                        .toString().replaceAll("\\D+", ""));
            } catch (NumberFormatException e) {
                pass = false;

                MainActivity.drawerNV.getMenu().findItem(R.id.i_schedule_md).setChecked(true);
                MainActivity.FM.beginTransaction()
                        .replace(R.id.fl_container_am, new ScheduleFragment(), "SCHEDULE").commit();

                final AlertDialog.Builder classDialogBuilder = new AlertDialog.Builder(
                        new ContextThemeWrapper(mainContext, MainActivity.themeDialog));

                classDialogBuilder.setTitle(R.string.error);
                classDialogBuilder.setMessage(R.string.error_class);

                classDialogBuilder.setCancelable(false);

                classDialogBuilder.setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mainContext.startActivity(new Intent(mainContext, SettingsActivity.class));
                            }
                        });

                classDialogBuilder.setNegativeButton(android.R.string.cancel, null);

                final AlertDialog classDialog = classDialogBuilder.create();

                classDialog.setCanceledOnTouchOutside(false);
                classDialog.show();

                classDialog.getButton(AlertDialog.BUTTON_POSITIVE)
                        .setTextColor(ContextCompat.getColor(mainContext, MainActivity.accentSecondaryColor));
                classDialog.getButton(AlertDialog.BUTTON_NEGATIVE)
                        .setTextColor(ContextCompat.getColor(mainContext, MainActivity.accentSecondaryColor));
            }
        } else {
            maxYear = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(mainContext)
                    .getString("pref_general_class", "1"));
        }
    }

    if (pass) {
        MainActivity.setToolbarTitle(mainContext, getString(R.string.grades), null);

        gradesELV = (ExpandableListView) gradesLayout.findViewById(R.id.lv_course_fg);

        final boolean online = MainActivity.isOnline();

        if (PreferenceManager.getDefaultSharedPreferences(mainContext).getString("html_grades", null) == null) { //TODO Simpler
            if (online) {
                getGrades(termFocus, HeliniumStudentApp.df_date().format(new Date()),
                        HeliniumStudentApp.DIREC_CURRENT, HeliniumStudentApp.ACTION_INIT_IN);
            } else { //TODO Display empty GradesFragment with retry option
                Toast.makeText(mainContext, getString(R.string.database_no), Toast.LENGTH_SHORT).show();

                MainActivity.drawerNV.getMenu().findItem(R.id.i_schedule_md).setChecked(true);
                MainActivity.FM.beginTransaction()
                        .replace(R.id.fl_container_am, new ScheduleFragment(), "SCHEDULE").commit();
            }
        } else if (online && gradesHtml == null && PreferenceManager.getDefaultSharedPreferences(mainContext)
                .getBoolean("pref_grades_init", true)) {
            getGrades(termFocus, HeliniumStudentApp.df_date().format(new Date()),
                    HeliniumStudentApp.DIREC_CURRENT, HeliniumStudentApp.ACTION_INIT_IN);
        } else {
            if (gradesHtml == null)
                gradesHtml = PreferenceManager.getDefaultSharedPreferences(mainContext).getString("html_grades",
                        null);

            if (online)
                parseData(HeliniumStudentApp.ACTION_ONLINE);
            else
                parseData(HeliniumStudentApp.ACTION_OFFLINE);
        }

        ((SwipeRefreshLayout) gradesLayout).setColorSchemeResources(MainActivity.accentSecondaryColor,
                MainActivity.accentPrimaryColor, MainActivity.primaryColor);
        ((SwipeRefreshLayout) gradesLayout).setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                refresh();
            }
        });

        gradesELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            int previousPosition = -1;

            @Override
            public void onGroupExpand(int position) {
                if (position != previousPosition)
                    gradesELV.collapseGroup(previousPosition);
                previousPosition = position;
            }
        });

        gradesELV.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { //Just a little easter egg
            int clickCount = 1;

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int position,
                    long id) {
                if (clickCount >= 80) {
                    Toast.makeText(mainContext, "Is this what you wanted?", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://youtu.be/dQw4w9WgXcQ")));
                } else {
                    switch (clickCount) {
                    case 2:
                        Toast.makeText(mainContext, "Good for you!", Toast.LENGTH_SHORT).show();
                        break;
                    case 10:
                        Toast.makeText(mainContext, "You're really proud of that, aren't you?",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 20:
                        Toast.makeText(mainContext, "It's really not that big of a deal...", Toast.LENGTH_SHORT)
                                .show();
                        break;
                    case 40:
                        Toast.makeText(mainContext, "You can stop now.", Toast.LENGTH_SHORT).show();
                        break;
                    case 50:
                        Toast.makeText(mainContext, "Please...", Toast.LENGTH_SHORT).show();
                    case 60:
                        Toast.makeText(mainContext, "F* OFF!", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }

                clickCount++;
                return false;
            }
        });

        MainActivity.prevIV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (MainActivity.isOnline()) {
                    if (termFocus != 1) {
                        termFocus--;

                        getGrades(termFocus, HeliniumStudentApp.df_grades(yearFocus),
                                HeliniumStudentApp.DIREC_BACK, HeliniumStudentApp.ACTION_REFRESH_IN);
                    } else {
                        MainActivity.setUI(HeliniumStudentApp.VIEW_GRADES, HeliniumStudentApp.ACTION_ONLINE);
                    }
                } else {
                    final int databaseFocus = Integer.parseInt(PreferenceManager
                            .getDefaultSharedPreferences(mainContext).getString("pref_grades_term", "1"));

                    if (PreferenceManager.getDefaultSharedPreferences(mainContext).getString("html_grades",
                            null) != null && yearFocus == 0 && termFocus > databaseFocus) {
                        yearFocus = 0;
                        termFocus = databaseFocus;

                        gradesHtml = PreferenceManager.getDefaultSharedPreferences(mainContext)
                                .getString("html_grades", null);
                        parseData(HeliniumStudentApp.ACTION_OFFLINE);
                    } else {
                        MainActivity.setUI(HeliniumStudentApp.VIEW_GRADES, HeliniumStudentApp.ACTION_OFFLINE);
                    }
                }
            }
        });

        MainActivity.historyIV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (MainActivity.isOnline()) {
                    if (maxYear != 1) {
                        MainActivity.setUI(HeliniumStudentApp.VIEW_GRADES, HeliniumStudentApp.ACTION_ONLINE);

                        final AlertDialog.Builder gradesDialogBuilder = new AlertDialog.Builder(
                                new ContextThemeWrapper(mainContext, MainActivity.themeDialog));
                        final View gradesLayout = View.inflate(mainContext, R.layout.dialog_grades, null);

                        gradesDialogBuilder.setTitle(getString(R.string.year, maxYear));

                        final NumberPicker yearNP = (NumberPicker) gradesLayout.findViewById(R.id.np_year_dg);

                        gradesDialogBuilder.setView(gradesLayout);

                        //TODO Listen for year change.

                        gradesDialogBuilder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (MainActivity.isOnline()) {
                                            final int oldValue = yearFocus;

                                            yearFocus = yearNP.getValue() - maxYear;
                                            getGrades(termFocus, HeliniumStudentApp.df_grades(yearFocus),
                                                    oldValue + HeliniumStudentApp.FOCUS_YEAR,
                                                    HeliniumStudentApp.ACTION_REFRESH_IN);
                                        } else {
                                            Toast.makeText(mainContext, getString(R.string.error_conn_no),
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });

                        yearNP.setMinValue(1);
                        yearNP.setMaxValue(maxYear);

                        yearNP.setValue(maxYear);

                        java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
                        for (java.lang.reflect.Field pf : pickerFields) {
                            if (pf.getName().equals("mSelectionDivider")) {
                                pf.setAccessible(true);

                                try {
                                    pf.set(yearNP, new ColorDrawable(ContextCompat.getColor(mainContext,
                                            MainActivity.accentPrimaryColor)));
                                } catch (IllegalArgumentException | IllegalAccessException ignored) {
                                }
                                break;
                                /*} else if(pf.getName().equals("mSelectorWheelPaint")) {
                                   pf.setAccessible(true);
                                        
                                   try {
                                      ((Paint) pf.get(yearNP))
                                            .setColor(getColor(MainActivity.themePrimaryTextColor));
                                   } catch (IllegalArgumentException |
                                         IllegalAccessException ignored) {}*/ //FIXME Doesn't work... yet
                            } else if (pf.getName().equals("mInputText")) {
                                pf.setAccessible(true);

                                try {
                                    ((EditText) pf.get(yearNP)).setTextColor(ContextCompat.getColor(mainContext,
                                            MainActivity.themePrimaryTextColor));
                                } catch (IllegalArgumentException | IllegalAccessException ignored) {
                                }
                            }
                        }

                        yearNP.invalidate();

                        gradesDialogBuilder.setNegativeButton(android.R.string.cancel, null);

                        AlertDialog gradesDialog = gradesDialogBuilder.create();

                        gradesDialog.setCanceledOnTouchOutside(true);
                        gradesDialog.show();

                        gradesDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(
                                ContextCompat.getColor(mainContext, MainActivity.accentSecondaryColor));
                        gradesDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(
                                ContextCompat.getColor(mainContext, MainActivity.accentSecondaryColor));
                    }
                } else {
                    final int databaseFocus = Integer.parseInt(PreferenceManager
                            .getDefaultSharedPreferences(mainContext).getString("pref_grades_term", "1"));

                    if (PreferenceManager.getDefaultSharedPreferences(mainContext).getString("html_grades",
                            null) != null && yearFocus != 0 || termFocus != databaseFocus) {
                        yearFocus = 0;
                        termFocus = databaseFocus;

                        gradesHtml = PreferenceManager.getDefaultSharedPreferences(mainContext)
                                .getString("html_grades", null);
                        parseData(HeliniumStudentApp.ACTION_OFFLINE);
                    } else {
                        MainActivity.setUI(HeliniumStudentApp.VIEW_GRADES, HeliniumStudentApp.ACTION_OFFLINE);
                    }
                }
            }
        });

        MainActivity.nextIV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (MainActivity.isOnline()) {
                    if (termFocus != 4) {
                        termFocus++;

                        getGrades(termFocus, HeliniumStudentApp.df_grades(yearFocus),
                                HeliniumStudentApp.DIREC_NEXT, HeliniumStudentApp.ACTION_REFRESH_IN);
                    } else {
                        MainActivity.setUI(HeliniumStudentApp.VIEW_GRADES, HeliniumStudentApp.ACTION_ONLINE);
                    }
                } else {
                    final int databaseFocus = Integer.parseInt(PreferenceManager
                            .getDefaultSharedPreferences(mainContext).getString("pref_grades_term", "1"));

                    if (PreferenceManager.getDefaultSharedPreferences(mainContext).getString("html_grades",
                            null) != null && yearFocus == 0 && termFocus < databaseFocus) {
                        yearFocus = 0;
                        termFocus = databaseFocus;

                        gradesHtml = PreferenceManager.getDefaultSharedPreferences(mainContext)
                                .getString("html_grades", null);
                        parseData(HeliniumStudentApp.ACTION_OFFLINE);
                    } else {
                        MainActivity.setUI(HeliniumStudentApp.VIEW_GRADES, HeliniumStudentApp.ACTION_OFFLINE);
                    }
                }
            }
        });
    }

    return gradesLayout;
}