Code example for ScrollView

Methods: scrollTo, getParent, setBackgroundColor

0
 
    /** Saves relevant views to instance variables and initializes their settings. */ 
    private void initInternalViews(View dialogView) {
        mScrollingContainer = (ScrollView) dialogView.findViewById(R.id.scrolling_container);
        // Background cannot be set in XML (see bug 783597 - TODO: Change this to XML when bug is fixed). 
        mScrollingContainer.setBackgroundColor(Color.WHITE);
        mPreviewFontView = (TextView) dialogView.findViewById(R.id.preview);
 
        mDecreaseFontButton = (Button) dialogView.findViewById(R.id.decrease_preview_font_button);
        mIncreaseFontButton = (Button) dialogView.findViewById(R.id.increase_preview_font_button);
        setButtonState(mPreviewFontIndex);
        mDecreaseFontButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                updatePreviewFontSize(mFontTwipValues[--mPreviewFontIndex]);
                mIncreaseFontButton.setEnabled(true);
                // If we reached the minimum index, disable the button. 
                if (mPreviewFontIndex == 0) {
                    mDecreaseFontButton.setEnabled(false);
                } 
            } 
        }); 
        mIncreaseFontButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                updatePreviewFontSize(mFontTwipValues[++mPreviewFontIndex]);
         
                mDecreaseFontButton.setEnabled(true);
                // If we reached the maximum index, disable the button. 
                if (mPreviewFontIndex == mFontTwipValues.length - 1) {
                    mIncreaseFontButton.setEnabled(false);
                } 
            } 
        }); 
    } 
 
    @Override 
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        if (!positiveResult) {
            mPreviewFontIndex = mSavedFontIndex;
            return; 
        } 
        mSavedFontIndex = mPreviewFontIndex;
        final String twipVal = mFontTwipValues[mSavedFontIndex];
        final OnPreferenceChangeListener prefChangeListener = getOnPreferenceChangeListener();
        if (prefChangeListener == null) {
            Log.e(LOGTAG, "PreferenceChangeListener is null. FontSizePreference will not be saved to Gecko.");
            return; 
        } 
        prefChangeListener.onPreferenceChange(this, twipVal);
    } 
 
    /** 
     * Finds the index of the given twip value and sets it as the saved preference value. Also the 
     * current preview text size to the given value. Does not update the mPreviewFontView text size. 
     */ 
    protected void setSavedFontSize(String twip) {
        final Integer index = mFontTwipToIndexMap.get(twip);
        if (index != null) {
            mSavedFontIndex = index;
            mPreviewFontIndex = mSavedFontIndex;
            return; 
        } 
        resetSavedFontSizeToDefault(); 
        Log.e(LOGTAG, "setSavedFontSize: Given font size does not exist in twip values map. Reverted to default font size.");
    } 
 
    /** 
     * Updates the mPreviewFontView to the given text size, resets the container's scroll to the top 
     * left, and invalidates the view. Does not update the font indices. 
     */ 
    private void updatePreviewFontSize(String twip) {
        float pt = convertTwipStrToPT(twip);
        // Android will not render a font size of 0 pt but for Gecko, 0 twip turns off font 
        // inflation. Thus we special case 0 twip to display a renderable font size. 
        if (pt == 0) {
            // Android adds an inexplicable extra margin on the smallest font size so to get around 
            // this, we reinflate the view. 
            ViewGroup parentView = (ViewGroup) mScrollingContainer.getParent();
            parentView.removeAllViews();
            final LayoutInflater inflater =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View dialogView = inflater.inflate(R.layout.font_size_preference, parentView);
            initInternalViews(dialogView);
            mPreviewFontView.setTextSize(PREVIEW_FONT_SIZE_UNIT, 1);
        } else { 
            mPreviewFontView.setTextSize(PREVIEW_FONT_SIZE_UNIT, pt);
        } 
        mScrollingContainer.scrollTo(0, 0);
    } 
 
    /** 
     * Resets the font indices to the default value. Does not update the mPreviewFontView text size. 
     */ 
Contextual code suggestions in your IDE  Get Codota for Java