Example usage for android.view Gravity CENTER_HORIZONTAL

List of usage examples for android.view Gravity CENTER_HORIZONTAL

Introduction

In this page you can find the example usage for android.view Gravity CENTER_HORIZONTAL.

Prototype

int CENTER_HORIZONTAL

To view the source code for android.view Gravity CENTER_HORIZONTAL.

Click Source Link

Document

Place object in the horizontal center of its container, not changing its size.

Usage

From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java

private void calculateBaseOffsets() {
    final float currentTextSize = mCurrentTextSize;
    // We then calculate the collapsed text size, using the same logic
    calculateUsingTextSize(mCollapsedTextSize);

    // BEGIN MODIFICATION: set mTextToDrawCollapsed and calculate width using it
    mTextToDrawCollapsed = mTextToDraw;//from   w ww . j av a  2 s  .  c o m
    float width = mTextToDrawCollapsed != null
            ? mTextPaint.measureText(mTextToDrawCollapsed, 0, mTextToDrawCollapsed.length())
            : 0;
    // END MODIFICATION

    final int collapsedAbsGravity = GravityCompat.getAbsoluteGravity(mCollapsedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);

    // BEGIN MODIFICATION: calculate height and Y position using mTextLayout
    float textHeight = mTextLayout != null ? mTextLayout.getHeight() : 0;

    switch (collapsedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mCollapsedDrawY = mCollapsedBounds.bottom - textHeight;
        break;
    case Gravity.TOP:
        mCollapsedDrawY = mCollapsedBounds.top;
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textOffset = (textHeight / 2);
        mCollapsedDrawY = mCollapsedBounds.centerY() - textOffset;
        break;
    }
    // END MODIFICATION

    switch (collapsedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mCollapsedDrawX = mCollapsedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mCollapsedDrawX = mCollapsedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mCollapsedDrawX = mCollapsedBounds.left;
        break;
    }

    calculateUsingTextSize(mExpandedTextSize);

    // BEGIN MODIFICATION: calculate width using mTextLayout based on first line and store that padding
    width = mTextLayout != null ? mTextLayout.getLineWidth(0) : 0;
    mExpandedFirstLineDrawX = mTextLayout != null ? mTextLayout.getLineLeft(0) : 0;
    // END MODIFICATION

    final int expandedAbsGravity = GravityCompat.getAbsoluteGravity(mExpandedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);

    // BEGIN MODIFICATION: calculate height and Y position using mTextLayout
    textHeight = mTextLayout != null ? mTextLayout.getHeight() : 0;
    switch (expandedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mExpandedDrawY = mExpandedBounds.bottom - textHeight;
        break;
    case Gravity.TOP:
        mExpandedDrawY = mExpandedBounds.top;
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textOffset = (textHeight / 2);
        mExpandedDrawY = mExpandedBounds.centerY() - textOffset;
        break;
    }
    // END MODIFICATION

    switch (expandedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mExpandedDrawX = mExpandedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mExpandedDrawX = mExpandedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mExpandedDrawX = mExpandedBounds.left;
        break;
    }

    // The bounds have changed so we need to clear the texture
    clearTexture();
    // Now reset the text size back to the original
    setInterpolatedTextSize(currentTextSize);
}

From source file:com.facebook.react.views.textinput.ReactTextInputManager.java

@ReactProp(name = ViewProps.TEXT_ALIGN)
public void setTextAlign(ReactEditText view, @Nullable String textAlign) {
    if (textAlign == null || "auto".equals(textAlign)) {
        view.setGravityHorizontal(Gravity.NO_GRAVITY);
    } else if ("left".equals(textAlign)) {
        view.setGravityHorizontal(Gravity.LEFT);
    } else if ("right".equals(textAlign)) {
        view.setGravityHorizontal(Gravity.RIGHT);
    } else if ("center".equals(textAlign)) {
        view.setGravityHorizontal(Gravity.CENTER_HORIZONTAL);
    } else if ("justify".equals(textAlign)) {
        // Fallback gracefully for cross-platform compat instead of error
        view.setGravityHorizontal(Gravity.LEFT);
    } else {//  w  w  w  . j a va 2s  .com
        throw new JSApplicationIllegalArgumentException("Invalid textAlign: " + textAlign);
    }
}

From source file:com.cloverstudio.spika.CameraCropActivity.java

public void scaleView() {
    // instantiate the views
    View top_view = findViewById(R.id.topView);
    View bottom_view = findViewById(R.id.bottomView);
    LinearLayout footer = (LinearLayout) findViewById(R.id.llFooter);
    LinearLayout crop_frame = (LinearLayout) findViewById(R.id.llCropFrame);
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

    // 90% of width
    crop_container_size = (int) ((float) width * (1f - (10f / 100f)));

    // 10% margins
    float margin = ((float) width * (1f - (90f / 100f)));

    // Parameters for white crop border
    LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(crop_container_size, crop_container_size);
    par.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    par.setMargins((int) (margin / 2f), 0, (int) (margin / 2f), 0);
    crop_frame.setLayoutParams(par);/*from w  w  w . j a va2s .  c  o  m*/

    // Margins for other transparent views
    float top_view_height = ((float) (height - crop_container_size - footer.getHeight())) / (float) 2;
    top_view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) top_view_height));
    bottom_view
            .setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) top_view_height));

    // Image container
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(crop_container_size,
            crop_container_size);
    params.setMargins((int) (margin / 2f), (int) top_view_height, (int) (margin / 2f), 0);
    mImageView.setLayoutParams(params);
    mImageView.setImageBitmap(mBitmap);
    mImageView.setMaxZoom(4f);
}

From source file:uk.ac.horizon.artcodes.scanner.ScannerActivity.java

private void setFocusTextVisible(final boolean visible) {
    final TextView focusTextView = (TextView) findViewById(R.id.focusText);
    runOnUiThread(new Runnable() {
        @Override//from  www. j a v a 2 s . c om
        public void run() {
            if (focusTextView != null) {
                if (focusTextAlreadyShown) {
                    focusTextView.setText(R.string.tap_to_refocus);
                    focusTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
                    focusTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                }
                if (visible) {
                    focusTextView.setVisibility(View.VISIBLE);
                } else {
                    focusTextView.setVisibility(View.INVISIBLE);
                    focusTextAlreadyShown = true;
                }
            }
        }
    });
}

From source file:com.mk4droid.IMC_Activities.FActivity_TabHost.java

private LinearLayout ActivateColorize(LinearLayout ll, String text, Drawable dr) {

    // text/*from   ww w.  ja  v  a2  s  . c  om*/
    TextView v = (TextView) ll.findViewWithTag("tv");
    v.setText(text);
    v.setTextSize(10);
    v.setTextColor(Color.BLACK); //v.setTextColor(Color.WHITE);
    v.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

    //dr.setColorFilter(0xFF888888, android.graphics.PorterDuff.Mode.MULTIPLY);
    dr.setColorFilter(resources.getColor(R.color.orange), android.graphics.PorterDuff.Mode.SRC_ATOP);

    v.setCompoundDrawablesWithIntrinsicBounds(null, dr, null, null);
    v.setPadding(0, 5, 0, 2);

    v.setBackgroundDrawable(resources.getDrawable(R.drawable.gradient_tabs_focused));

    // hbar
    View hbar = ll.findViewWithTag("hbar");
    //hbar.setBackgroundColor(resources.getColor(R.color.orange));
    hbar.setBackgroundDrawable(resources.getDrawable(R.drawable.gradient_orange));

    return ll;
}

From source file:com.eugene.fithealthmaingit.UI.SaveSearchAddItemActivityMain.java

private void updateItems() {

    mServingSizeUpdated.setText("1");

    LinearLayout changeServing = (LinearLayout) findViewById(R.id.changeServing);
    changeServing.setOnClickListener(new View.OnClickListener() {
        @Override/*from  w ww .  jav a2s.  c  o m*/
        public void onClick(View v) {
            AlertDialog.Builder alert = new AlertDialog.Builder(SaveSearchAddItemActivityMain.this);

            alert.setTitle("Update Serving Size: ");
            alert.setMessage("Servings Consumed");

            final EditText input = new EditText(SaveSearchAddItemActivityMain.this);
            input.setText(mServingg.getText().toString());
            input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            input.selectAll();
            input.setGravity(Gravity.CENTER_HORIZONTAL);
            alert.setView(input, 64, 0, 64, 0);
            alert.setPositiveButton("Update", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    double values = Double.valueOf(input.getText().toString());
                    mServingg.setText(df.format(values));
                    mServingSizeUpdated.setText(df.format(values));
                    mCalUpdate.setText(dfW.format(Double.valueOf(mCalories) * values));
                    mCalorieProgress = Double.valueOf(mCalories) * values;
                    mFattieUpdate.setText(df.format(Double.valueOf(mFat) * values));
                    mFatProgress = Double.valueOf(mFat) * values;
                    mSaturatedFatUpdate.setText(df.format(Double.valueOf(mSaturatedFat) * values));
                    mCholesterolUpdate.setText(df.format(Double.valueOf(mCholesterol) * values));
                    mSodiumUpdate.setText(df.format(Double.valueOf(mSodium) * values));
                    mCarbUpdate.setText(df.format(Double.valueOf(mCarbohydrates) * values));
                    mCarbProgress = Double.valueOf(mCarbohydrates) * values;
                    mFiberUpdate.setText(df.format(Double.valueOf(mFiber) * values));
                    mSugarUpdate.setText(df.format(Double.valueOf(mSugar) * values));
                    mProUpdate.setText(df.format(Double.valueOf(mProtein) * values));
                    mProteinProgress = Double.valueOf(mProtein) * values;
                    mVitAUpdate.setText(df.format(Double.valueOf(mVitA) * values));
                    mVitCUpdate.setText(df.format(Double.valueOf(mVitC) * values));
                    mCalciumUpdate.setText(df.format(Double.valueOf(mCalcium) * values));
                    mIronUpdate.setText(df.format(Double.valueOf(mIron) * values));
                    progressBars();
                    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(input.getWindowToken(), 0);
                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(input.getWindowToken(), 0);
                }
            });
            alert.setCancelable(false);
            alert.show();
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    });

}

From source file:net.grobas.widget.AutoLinearLayout.java

private void updateLeftPositionByGravity(ViewPosition pos, int size, int gravity) {
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case GravityCompat.END:
        pos.left += (size > 0) ? size : 0;
        break;/*ww  w  .  j a  v  a  2 s  . c o m*/

    case Gravity.CENTER_HORIZONTAL:
        pos.left += ((size > 0) ? size : 0) / 2;
        break;
    }
}

From source file:org.otempo.view.StationActivity.java

/**
 * Devuelve un TextView con el nombre de un da para poner sobre las predicciones
 *///from  www  . j a va 2 s.c o m
private TextView getDayName(Calendar date) {
    TextView dayName = new TextView(this);
    if (date != null) {
        dayName.setText(DateUtils.weekDayFormat.format(date.getTime()));

    } else {
        dayName.setText("???");
    }
    dayName.setGravity(Gravity.CENTER_HORIZONTAL);
    if (_background.equals("black")) {
        dayName.setTextColor(Color.rgb(255, 255, 255));
    } else {
        dayName.setTextColor(Color.rgb(0, 0, 0));

    }
    return dayName;
}

From source file:cm.aptoide.pt.webservices.login.Login.java

private void checkCredentials(String username, String password) {
    if (username.trim().length() > 0 && password.trim().length() > 0) {
        new CheckUserCredentials().execute(username, password, "false");
    } else {//w  w w  . j  av a2s . c om
        Toast toast = Toast.makeText(context, context.getString(R.string.check_your_credentials),
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 30);
        toast.show();
    }
}

From source file:app.sunstreak.yourpisd.LoginActivity.java

/**
 * Shows the progress UI and hides the login form.
 */// w ww  .  ja  va 2s . c o  m
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
    // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
    // for very easy animations. If available, use these APIs to fade-in
    // the progress spinner.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

        int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

        mLoginFormView.setVisibility(View.VISIBLE);
        mLoginFormView.animate().setDuration(shortAnimTime)
                //.translationY(-200)
                .alpha(show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
                    }
                });
        mLoginStatusView.setVisibility(View.VISIBLE);
        mLoginStatusView.animate().setDuration(shortAnimTime).alpha(show ? 1 : 0)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
                    }
                });

        //            mLoginFormView.setVisibility(View.VISIBLE);
        //            mLoginFormView.animate().setDuration(500).setInterpolator(new DecelerateInterpolator())
        //                    .translationY(height*(show? -1 : 1)).setListener(new AnimatorListenerAdapter() {
        //            @Override
        //            public void onAnimationEnd(Animator animation) {
        //                    mLoginFormView.setVisibility(show ? View.INVISIBLE
        //                            : View.VISIBLE);
        //            }
        //         });
        //            mLoginStatusView.setVisibility(View.VISIBLE);
        //            mLoginStatusView.animate().setDuration(shortAnimTime).translationY(0)
        //         .setListener(new AnimatorListenerAdapter() {
        //            @Override
        //            public void onAnimationEnd(Animator animation) {
        //               mLoginStatusView.setVisibility(show ? View.VISIBLE
        //                     : View.INVISIBLE);
        //                    System.out.println("show loading: " + show);
        //            }
        //         });

        if (DateHelper.isAprilFools()) {
            mLoginStatusView.removeAllViews();

            try {
                ImageView img = new ImageView(this);
                //noinspection ResourceType
                img.setId(1337);
                InputStream is = getAssets().open("nyan.png");
                img.setImageBitmap(BitmapFactory.decodeStream(is));
                is.close();
                TextView april = new TextView(this);
                april.setText(
                        "Today and tomorrow, we shall pay \"homage\" to the numerous poor designs of the internet");
                april.setGravity(Gravity.CENTER_HORIZONTAL);
                mLoginStatusView.addView(img);
                mLoginStatusView.addView(april);

                RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                rotateAnimation1.setInterpolator(new LinearInterpolator());
                rotateAnimation1.setDuration(500);
                rotateAnimation1.setRepeatCount(Animation.INFINITE);
                img.startAnimation(rotateAnimation1);

            } catch (Exception e) {
                e.printStackTrace();
                return;
            }

        }

        //         mLoginStatusView.animate().setDuration(shortAnimTime)
        //               .alpha(show ? 1 : 0)
        //               .setListener(new AnimatorListenerAdapter() {
        //                  @Override
        //                  public void onAnimationEnd(Animator animation) {
        //                     mLoginStatusView.setVisibility(show ? View.VISIBLE
        //                           : View.GONE);
        //                  }
        //               });

        //         mLoginFormView.setVisibility(View.VISIBLE);
        //         mLoginFormView.animate().setDuration(shortAnimTime)
        //               .alpha(show ? 0 : 1)
        //               .setListener(new AnimatorListenerAdapter() {
        //                  @Override
        //                  public void onAnimationEnd(Animator animation) {
        //                     mLoginFormView.setVisibility(show ? View.GONE
        //                           : View.VISIBLE);
        //                  }
        //               });

    } /* else if(getIntent().getExtras().getBoolean("Refresh")){
        // The ViewPropertyAnimator APIs are not available, so simply show
        // and hide the relevant UI components.
        mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
        mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
      }*/

}