Java Code Examples for android.support.v4.view.MarginLayoutParamsCompat#setMarginStart()

The following examples show how to use android.support.v4.view.MarginLayoutParamsCompat#setMarginStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PercentLayoutHelper.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public void restoreMarginLayoutParams(MarginLayoutParams params) {
    restoreLayoutParams(params);
    params.leftMargin = this.mPreservedParams.leftMargin;
    params.topMargin = this.mPreservedParams.topMargin;
    params.rightMargin = this.mPreservedParams.rightMargin;
    params.bottomMargin = this.mPreservedParams.bottomMargin;
    MarginLayoutParamsCompat.setMarginStart(params, MarginLayoutParamsCompat.getMarginStart(this.mPreservedParams));
    MarginLayoutParamsCompat.setMarginEnd(params, MarginLayoutParamsCompat.getMarginEnd(this.mPreservedParams));
}
 
Example 2
Source File: PercentLayoutHelper.java    From JD-Test with Apache License 2.0 5 votes vote down vote up
/**
 * Restores the original dimensions and margins after they were changed for percentage based
 * values. You should call this method only if you previously called
 * {@link PercentLayoutHelper.PercentLayoutInfo#fillMarginLayoutParams(View, ViewGroup.MarginLayoutParams, int, int)}.
 */
public void restoreMarginLayoutParams(ViewGroup.MarginLayoutParams params) {
    restoreLayoutParams(params);
    params.leftMargin = mPreservedParams.leftMargin;
    params.topMargin = mPreservedParams.topMargin;
    params.rightMargin = mPreservedParams.rightMargin;
    params.bottomMargin = mPreservedParams.bottomMargin;
    MarginLayoutParamsCompat.setMarginStart(params,
            MarginLayoutParamsCompat.getMarginStart(mPreservedParams));
    MarginLayoutParamsCompat.setMarginEnd(params,
            MarginLayoutParamsCompat.getMarginEnd(mPreservedParams));
}
 
Example 3
Source File: PercentLayoutHelper.java    From android-percent-support-extend with Apache License 2.0 5 votes vote down vote up
/**
 * Restores original dimensions and margins after they were changed for percentage based
 * values. Calling this method only makes sense if you previously called
 * {@link PercentLayoutHelper.PercentLayoutInfo#fillMarginLayoutParams}.
 */
public void restoreMarginLayoutParams(ViewGroup.MarginLayoutParams params)
{
    restoreLayoutParams(params);
    params.leftMargin = mPreservedParams.leftMargin;
    params.topMargin = mPreservedParams.topMargin;
    params.rightMargin = mPreservedParams.rightMargin;
    params.bottomMargin = mPreservedParams.bottomMargin;
    MarginLayoutParamsCompat.setMarginStart(params,
            MarginLayoutParamsCompat.getMarginStart(mPreservedParams));
    MarginLayoutParamsCompat.setMarginEnd(params,
            MarginLayoutParamsCompat.getMarginEnd(mPreservedParams));
}
 
Example 4
Source File: PercentLayoutHelper.java    From FimiX8-RE with MIT License 4 votes vote down vote up
public void fillMarginLayoutParams(MarginLayoutParams params, int widthHint, int heightHint) {
    int base;
    fillLayoutParams(params, widthHint, heightHint);
    this.mPreservedParams.leftMargin = params.leftMargin;
    this.mPreservedParams.topMargin = params.topMargin;
    this.mPreservedParams.rightMargin = params.rightMargin;
    this.mPreservedParams.bottomMargin = params.bottomMargin;
    MarginLayoutParamsCompat.setMarginStart(this.mPreservedParams, MarginLayoutParamsCompat.getMarginStart(params));
    MarginLayoutParamsCompat.setMarginEnd(this.mPreservedParams, MarginLayoutParamsCompat.getMarginEnd(params));
    if (this.leftMarginPercent != null) {
        params.leftMargin = (int) (((float) (this.leftMarginPercent.isBaseWidth ? widthHint : heightHint)) * this.leftMarginPercent.percent);
    }
    if (this.topMarginPercent != null) {
        if (this.topMarginPercent.isBaseWidth) {
            base = widthHint;
        } else {
            base = heightHint;
        }
        params.topMargin = (int) (((float) base) * this.topMarginPercent.percent);
    }
    if (this.rightMarginPercent != null) {
        if (this.rightMarginPercent.isBaseWidth) {
            base = widthHint;
        } else {
            base = heightHint;
        }
        params.rightMargin = (int) (((float) base) * this.rightMarginPercent.percent);
    }
    if (this.bottomMarginPercent != null) {
        if (this.bottomMarginPercent.isBaseWidth) {
            base = widthHint;
        } else {
            base = heightHint;
        }
        params.bottomMargin = (int) (((float) base) * this.bottomMarginPercent.percent);
    }
    if (this.startMarginPercent != null) {
        if (this.startMarginPercent.isBaseWidth) {
            base = widthHint;
        } else {
            base = heightHint;
        }
        MarginLayoutParamsCompat.setMarginStart(params, (int) (((float) base) * this.startMarginPercent.percent));
    }
    if (this.endMarginPercent != null) {
        if (this.endMarginPercent.isBaseWidth) {
            base = widthHint;
        } else {
            base = heightHint;
        }
        MarginLayoutParamsCompat.setMarginEnd(params, (int) (((float) base) * this.endMarginPercent.percent));
    }
    if (Log.isLoggable(PercentLayoutHelper.TAG, 3)) {
        Log.d(PercentLayoutHelper.TAG, "after fillMarginLayoutParams: (" + params.width + ", " + params.height + ")");
    }
}
 
Example 5
Source File: PercentLayoutHelper.java    From JD-Test with Apache License 2.0 4 votes vote down vote up
/**
 * Fills the margin fields of the passed {@link ViewGroup.MarginLayoutParams} object based
 * on currently set percentage values and the current layout direction of the passed
 * {@link View}.
 */
public void fillMarginLayoutParams(View view, ViewGroup.MarginLayoutParams params,
                                   int widthHint, int heightHint) {
    fillLayoutParams(params, widthHint, heightHint);

    // Preserve the original margins, so we can restore them after the measure step.
    mPreservedParams.leftMargin = params.leftMargin;
    mPreservedParams.topMargin = params.topMargin;
    mPreservedParams.rightMargin = params.rightMargin;
    mPreservedParams.bottomMargin = params.bottomMargin;
    MarginLayoutParamsCompat.setMarginStart(mPreservedParams,
            MarginLayoutParamsCompat.getMarginStart(params));
    MarginLayoutParamsCompat.setMarginEnd(mPreservedParams,
            MarginLayoutParamsCompat.getMarginEnd(params));
    int screenWidth = ScreenUtil.getScreenWidth(view.getContext());

    if (leftMarginPercentX >= 0) {
        params.leftMargin = Math.round(widthHint * leftMarginPercentX);
    }
    else if (leftMarginPercentY >= 0) {
        params.leftMargin = Math.round(heightHint * leftMarginPercentY);
    }
    else if (leftMarginPercentScreenX >= 0) {
        params.leftMargin = Math.round(screenWidth * leftMarginPercentScreenX);
    }


    if (topMarginPercentX >= 0) {
        params.topMargin = Math.round(widthHint * topMarginPercentX);
    }
    else if (topMarginPercentY >= 0) {
        params.topMargin = Math.round(heightHint * topMarginPercentY);
    }
    else if (topMarginPercentScreenX >= 0) {
        params.topMargin = Math.round(screenWidth * topMarginPercentScreenX);
    }


    if (rightMarginPercentX >= 0) {
        params.rightMargin = Math.round(widthHint * rightMarginPercentX);
    }
    else if (rightMarginPercentY >= 0) {
        params.rightMargin = Math.round(heightHint * rightMarginPercentY);
    }
    else if (rightMarginPercentScreenX >= 0) {
        params.rightMargin = Math.round(screenWidth * rightMarginPercentScreenX);
    }


    if (bottomMarginPercentX >= 0) {
        params.bottomMargin = Math.round(widthHint * bottomMarginPercentX);
    }
    else if(bottomMarginPercentY >= 0){
        params.bottomMargin = Math.round(heightHint * bottomMarginPercentY);
    }
    else if(bottomMarginPercentScreenX >= 0){
        params.bottomMargin = Math.round(screenWidth * bottomMarginPercentScreenX);
    }


    boolean shouldResolveLayoutDirection = false;
    if (startMarginPercent >= 0) {
        MarginLayoutParamsCompat.setMarginStart(params,
                Math.round(widthHint * startMarginPercent));
        shouldResolveLayoutDirection = true;
    }
    if (endMarginPercent >= 0) {
        MarginLayoutParamsCompat.setMarginEnd(params,
                Math.round(widthHint * endMarginPercent));
        shouldResolveLayoutDirection = true;
    }
    if (shouldResolveLayoutDirection && (view != null)) {
        // Force the resolve pass so that start / end margins are propagated to the
        // matching left / right fields
        MarginLayoutParamsCompat.resolveLayoutDirection(params,
                ViewCompat.getLayoutDirection(view));
    }
    if (DEBUG) {
        Log.d(TAG, "after fillMarginLayoutParams: (" + params.width + ", " + params.height
                + ")");
    }
}
 
Example 6
Source File: FloatingSearchView.java    From FloatingSearchView with Apache License 2.0 4 votes vote down vote up
private void applyXmlAttributes(AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
    final TypedArray a = getContext().obtainStyledAttributes(attrs,
            R.styleable.FloatingSearchView, defStyleAttr, defStyleRes);

    // Search bar width
    View suggestionsContainer = findViewById(R.id.fsv_suggestions_container);
    int searchBarWidth = a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_searchBarWidth,
            mSearchContainer.getLayoutParams().width);
    mSearchContainer.getLayoutParams().width = searchBarWidth;
    suggestionsContainer.getLayoutParams().width = searchBarWidth;

    // Divider
    mDivider.setBackgroundDrawable(a.getDrawable(R.styleable.FloatingSearchView_android_divider));
    int dividerHeight = a.getDimensionPixelSize(R.styleable.FloatingSearchView_android_dividerHeight, -1);

    MarginLayoutParams dividerLP = (MarginLayoutParams) mDivider.getLayoutParams();

    if(mDivider.getBackground() != null && dividerHeight != -1)
        dividerLP.height = dividerHeight;

    float maxShadowSize = mSearchBackground.getMaxShadowSize();
    float cornerRadius = mSearchBackground.getCornerRadius();
    int horizontalPadding = (int) (RoundRectDrawableWithShadow.calculateHorizontalPadding(
            maxShadowSize, cornerRadius, false) + .5f);

    dividerLP.setMargins(horizontalPadding, dividerLP.topMargin, horizontalPadding, dividerLP.bottomMargin);
    mDivider.setLayoutParams(dividerLP);

    // Content inset
    MarginLayoutParams searchParams = (MarginLayoutParams) mSearchInput.getLayoutParams();

    int contentInsetStart = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetStart,
            MarginLayoutParamsCompat.getMarginStart(searchParams));
    int contentInsetEnd = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetEnd,
            MarginLayoutParamsCompat.getMarginEnd(searchParams));

    MarginLayoutParamsCompat.setMarginStart(searchParams, contentInsetStart);
    MarginLayoutParamsCompat.setMarginEnd(searchParams, contentInsetEnd);

    // anything else
    setLogo(a.getDrawable(R.styleable.FloatingSearchView_logo));
    setContentBackgroundColor(a.getColor(R.styleable.FloatingSearchView_fsv_contentBackgroundColor, DEFAULT_CONTENT_COLOR));
    setRadius(a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_cornerRadius, ViewUtils.dpToPx(DEFAULT_RADIUS)));
    inflateMenu(a.getResourceId(R.styleable.FloatingSearchView_fsv_menu, 0));
    setPopupTheme(a.getResourceId(R.styleable.FloatingSearchView_popupTheme, 0));
    setHint(a.getString(R.styleable.FloatingSearchView_android_hint));
    setIcon(a.getDrawable(R.styleable.FloatingSearchView_fsv_icon));

    a.recycle();
}
 
Example 7
Source File: FloatingSearchView.java    From FloatingSearchView with Apache License 2.0 4 votes vote down vote up
private void applyXmlAttributes(AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
    final TypedArray a = getContext().obtainStyledAttributes(attrs,
            R.styleable.FloatingSearchView, defStyleAttr, defStyleRes);

    // Search bar width
    View suggestionsContainer = findViewById(R.id.fsv_suggestions_container);
    int searchBarWidth = a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_searchBarWidth,
            mSearchContainer.getLayoutParams().width);
    mSearchContainer.getLayoutParams().width = searchBarWidth;
    suggestionsContainer.getLayoutParams().width = searchBarWidth;

    // Divider
    mDivider.setBackgroundDrawable(a.getDrawable(R.styleable.FloatingSearchView_android_divider));
    int dividerHeight = a.getDimensionPixelSize(R.styleable.FloatingSearchView_android_dividerHeight, -1);

    MarginLayoutParams dividerLP = (MarginLayoutParams) mDivider.getLayoutParams();

    if(mDivider.getBackground() != null && dividerHeight != -1)
        dividerLP.height = dividerHeight;

    float maxShadowSize = mSearchBackground.getMaxShadowSize();
    float cornerRadius = mSearchBackground.getCornerRadius();
    int horizontalPadding = (int) (RoundRectDrawableWithShadow.calculateHorizontalPadding(
            maxShadowSize, cornerRadius, false) + .5f);

    dividerLP.setMargins(horizontalPadding, dividerLP.topMargin, horizontalPadding, dividerLP.bottomMargin);
    mDivider.setLayoutParams(dividerLP);

    // Content inset
    MarginLayoutParams searchParams = (MarginLayoutParams) mSearchInput.getLayoutParams();

    int contentInsetStart = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetStart,
            MarginLayoutParamsCompat.getMarginStart(searchParams));
    int contentInsetEnd = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetEnd,
            MarginLayoutParamsCompat.getMarginEnd(searchParams));

    MarginLayoutParamsCompat.setMarginStart(searchParams, contentInsetStart);
    MarginLayoutParamsCompat.setMarginEnd(searchParams, contentInsetEnd);

    // anything else
    setLogo(a.getDrawable(R.styleable.FloatingSearchView_logo));
    setContentBackgroundColor(a.getColor(R.styleable.FloatingSearchView_fsv_contentBackgroundColor, DEFAULT_CONTENT_COLOR));
    setRadius(a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_cornerRadius, ViewUtils.dpToPx(DEFAULT_RADIUS)));
    inflateMenu(a.getResourceId(R.styleable.FloatingSearchView_fsv_menu, 0));
    setPopupTheme(a.getResourceId(R.styleable.FloatingSearchView_popupTheme, 0));
    setHint(a.getString(R.styleable.FloatingSearchView_android_hint));
    setIcon(a.getDrawable(R.styleable.FloatingSearchView_fsv_icon));

    a.recycle();
}
 
Example 8
Source File: QuestionHelpFragment.java    From android-galaxyzoo with GNU General Public License v3.0 4 votes vote down vote up
private void addRowForAnswer(final Context context, final TableLayout tableLayout, final DecisionTree.Question question, final DecisionTree.BaseButton answer) {
    final TableRow row = new TableRow(context);
    final TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, UiUtils.getPxForDpResource(context, R.dimen.standard_large_margin), 0, 0);
    tableLayout.addView(row, params);


    final LinearLayout layoutVertical = new LinearLayout(context);
    layoutVertical.setOrientation(LinearLayout.VERTICAL);

    final TextView textViewAnswer = new AppCompatTextView(context);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        textViewAnswer.setTextAppearance(R.style.TextAppearance_AppCompat_Subhead);
    } else {
        //noinspection deprecation
        textViewAnswer.setTextAppearance(context, R.style.TextAppearance_AppCompat_Subhead);
    }
    textViewAnswer.setText(answer.getText());
    layoutVertical.addView(textViewAnswer,
            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    final LinearLayout layoutHorizontal = new LinearLayout(context);
    layoutHorizontal.setOrientation(LinearLayout.HORIZONTAL);
    final LinearLayout.LayoutParams paramsHorizontal = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    paramsHorizontal.setMargins(0, UiUtils.getPxForDpResource(context, R.dimen.standard_margin), 0, 0);
    layoutVertical.addView(layoutHorizontal, paramsHorizontal);

    final BitmapDrawable icon = getIcon(context, answer);
    final ImageView imageIcon = new ImageView(context);
    imageIcon.setImageDrawable(icon);
    layoutHorizontal.addView(imageIcon);

    final LinearLayout.LayoutParams paramsImage = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    // TODO: Use a custom FlowTable class to avoid items going off the right edge of the screen
    // when there are too many.
    final Singleton singleton = getSingleton();
    for (int i = 0; i < answer.getExamplesCount(); i++) {

        final String iconName = answer.getExampleIconName(question.getId(), i);
        final BitmapDrawable iconExample = singleton.getIconDrawable(context, iconName);
        final ImageButton imageExample = new ImageButton(context);
        //Remove the space between the image and the outside of the button:
        imageExample.setPadding(0, 0, 0, 0);
        imageExample.setImageDrawable(iconExample);

        //Needed to make the image expand as a transition into the SubjectViewerActivity,
        //which uses the same name in fragment_subject.xml
        ViewCompat.setTransitionName(imageExample, getString(R.string.transition_subject_image));

        //This requires API level 17: paramsImage.setMarginStart(getPxForDp(activity, MARGIN_MEDIUM_DP));
        //imageExample.setLayoutParams(paramsImage);
        MarginLayoutParamsCompat.setMarginStart(paramsImage, UiUtils.getPxForDpResource(context, R.dimen.standard_large_margin));

        final int answerIndex = i;
        imageExample.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View v) {
                // Perform action on click
                onExampleImageClicked(v, answer, answerIndex);
            }
        });

        layoutHorizontal.addView(imageExample, paramsImage);
    }

    row.addView(layoutVertical,
            new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
}