app/src/main/java/openfoodfacts/github/scrachx/openfood/fragments/AddProductNutritionFactsFragment.java
/**
     * Adds a new row in the tableLayout.
     *
     * @param position The index of the additional nutrient to add in the "PARAM_OTHER_NUTRIENTS" array.
     * @param text The hint text to be displayed in the EditText.
     * @param preFillValues true if the created row needs to be filled by a predefined value.
     * @param value This value will be set to the EditText. Required if 'preFillValues' is true.
     * @param unitSelectedIndex This spinner will be set to this position. Required if 'preFillValues' is true.
     */
Adds a new row in the tableLayout.
@param position The index of the additional nutrient to add in the "PARAM_OTHER_NUTRIENTS" array.
@param text The hint text to be displayed in the EditText.
@param preFillValues true if the created row needs to be filled by a predefined value.
@param value This value will be set to the EditText. Required if 'preFillValues' is true.
@param unitSelectedIndex This spinner will be set to this position. Required if 'preFillValues' is true.
Adds a new row in the tableLayout . @param position The index of the additional nutrient to add in the " PARAM_OTHER_NUTRIENTS " array . @param text The hint text to be displayed in the EditText . @param preFillValues true if the created row needs to be filled by a predefined value . @param value This value will be set to the EditText . Required if ' preFillValues ' is true . @param unitSelectedIndex This spinner will be set to this position . Required if ' preFillValues ' is true .
private CustomValidatingEditTextView addNutrientRow(int position, CharSequence text, boolean preFillValues, String value, int unitSelectedIndex) {
        final String nutrientCompleteName = AddProductNutritionFactsData.PARAMS_OTHER_NUTRIENTS.get(position);
        TableRow nutrient = new TableRow(activity);
        nutrient.setPadding(0, dpsToPixels(10), 0, 0);
        CustomValidatingEditTextView editText = new CustomValidatingEditTextView(activity);
        editText.setBackgroundResource(R.drawable.bg_edittext_til);
        editText.setHint(text);
        editText.setId(position);
        final String nutrientShortName = AddProductNutritionFactsData.getShortName(nutrientCompleteName);
        editText.setEntryName(nutrientShortName);
        editText.setKeyListener(keyListener);
        lastEditText.setNextFocusDownId(editText.getId());
        lastEditText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        lastEditText = editText;
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        editText.setSingleLine();
        editText.setGravity(Gravity.CENTER_VERTICAL);
        editText.requestFocus();
        TableRow.LayoutParams lpEditText = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, dpsToPixels(45));
        lpEditText.setMargins(0, dpsToPixels(10), 0, 0);
        editText.setLayoutParams(lpEditText);
        if (preFillValues) {
            editText.setText(value);
        }
        TextInputLayout textInputLayout = new TextInputLayout(activity);
        textInputLayout.addView(editText);
        textInputLayout.setErrorTextAppearance(R.style.errorText);
        nutrient.addView(textInputLayout);
        Spinner spinner = new Spinner(activity);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>
            (activity, android.R.layout.simple_spinner_item, activity.getResources().getStringArray(R.array.weight_all_units));
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setBackgroundResource(R.drawable.spinner_weights_grey);
        spinner.setAdapter(spinnerArrayAdapter);
        spinner.setPadding(dpsToPixels(1), 0, 0, 0);
        final TableRow.LayoutParams spinnerLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, dpsToPixels(35));
        spinnerLayoutParams.setMargins(dpsToPixels(8), dpsToPixels(16), dpsToPixels(8), dpsToPixels(6));
        spinner.setLayoutParams(spinnerLayoutParams);
        nutrient.addView(spinner);
        editText.setAttachedSpinner(spinner);
        editText.setTextInputLayout(textInputLayout);
        if (Nutriments.PH.equals(nutrientShortName)) {
            spinner.setVisibility(View.INVISIBLE);
        } else if (Nutriments.STARCH.equals(nutrientShortName)) {
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>
                (activity, android.R.layout.simple_spinner_item, activity.getResources().getStringArray(R.array.weights_array));
            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(arrayAdapter);
            starchEditText = editText;
        }
        if (preFillValues) {
            spinner.setSelection(unitSelectedIndex);
        }
        tableLayout.addView(nutrient);
        return editText;
    }
private CustomValidatingEditTextView addNutrientRow ( int position , CharSequence text , boolean preFillValues , String value , int unitSelectedIndex ) { final String nutrientCompleteName = AddProductNutritionFactsData . PARAMS_OTHER_NUTRIENTS . get ( position ) ; TableRow nutrient = new TableRow ( activity ) ; nutrient . setPadding ( 0 , dpsToPixels ( 10 ) , 0 , 0 ) ; CustomValidatingEditTextView editText = new CustomValidatingEditTextView ( activity ) ; editText . setBackgroundResource ( R . drawable . bg_edittext_til ) ; editText . setHint ( text ) ; editText . setId ( position ) ; final String nutrientShortName = AddProductNutritionFactsData . getShortName ( nutrientCompleteName ) ; editText . setEntryName ( nutrientShortName ) ; editText . setKeyListener ( keyListener ) ; lastEditText . setNextFocusDownId ( editText . getId ( ) ) ; lastEditText . setImeOptions ( EditorInfo . IME_ACTION_NEXT ) ; lastEditText = editText ; editText . setImeOptions ( EditorInfo . IME_ACTION_DONE ) ; editText . setSingleLine ( ) ; editText . setGravity ( Gravity . CENTER_VERTICAL ) ; editText . requestFocus ( ) ; TableRow . LayoutParams lpEditText = new TableRow . LayoutParams ( TableRow . LayoutParams . MATCH_PARENT , dpsToPixels ( 45 ) ) ; lpEditText . setMargins ( 0 , dpsToPixels ( 10 ) , 0 , 0 ) ; editText . setLayoutParams ( lpEditText ) ; if ( preFillValues ) { editText . setText ( value ) ; } TextInputLayout textInputLayout = new TextInputLayout ( activity ) ; textInputLayout . addView ( editText ) ; textInputLayout . setErrorTextAppearance ( R . style . errorText ) ; nutrient . addView ( textInputLayout ) ; Spinner spinner = new Spinner ( activity ) ; ArrayAdapter < String > spinnerArrayAdapter = new ArrayAdapter < > ( activity , android . R . layout . simple_spinner_item , activity . getResources ( ) . getStringArray ( R . array . weight_all_units ) ) ; spinnerArrayAdapter . setDropDownViewResource ( android . R . layout . simple_spinner_dropdown_item ) ; spinner . setBackgroundResource ( R . drawable . spinner_weights_grey ) ; spinner . setAdapter ( spinnerArrayAdapter ) ; spinner . setPadding ( dpsToPixels ( 1 ) , 0 , 0 , 0 ) ; final TableRow . LayoutParams spinnerLayoutParams = new TableRow . LayoutParams ( TableRow . LayoutParams . MATCH_PARENT , dpsToPixels ( 35 ) ) ; spinnerLayoutParams . setMargins ( dpsToPixels ( 8 ) , dpsToPixels ( 16 ) , dpsToPixels ( 8 ) , dpsToPixels ( 6 ) ) ; spinner . setLayoutParams ( spinnerLayoutParams ) ; nutrient . addView ( spinner ) ; editText . setAttachedSpinner ( spinner ) ; editText . setTextInputLayout ( textInputLayout ) ; if ( Nutriments . PH . equals ( nutrientShortName ) ) { spinner . setVisibility ( View . INVISIBLE ) ; } else if ( Nutriments . STARCH . equals ( nutrientShortName ) ) { ArrayAdapter < String > arrayAdapter = new ArrayAdapter < > ( activity , android . R . layout . simple_spinner_item , activity . getResources ( ) . getStringArray ( R . array . weights_array ) ) ; arrayAdapter . setDropDownViewResource ( android . R . layout . simple_spinner_dropdown_item ) ; spinner . setAdapter ( arrayAdapter ) ; starchEditText = editText ; } if ( preFillValues ) { spinner . setSelection ( unitSelectedIndex ) ; } tableLayout . addView ( nutrient ) ; return editText ; }
Adds a new row in the tableLayout.
Adds a new row in the tableLayout .