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:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Layout.Alignment getLayoutAlignment(TextView textView) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return Layout.Alignment.ALIGN_NORMAL;
    }/*from  w  ww  .j av  a 2s .co  m*/

    Layout.Alignment alignment;
    switch (textView.getTextAlignment()) {
    case TextView.TEXT_ALIGNMENT_GRAVITY:
        switch (textView.getGravity() & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
        case Gravity.START:
            alignment = Layout.Alignment.ALIGN_NORMAL;
            break;
        case Gravity.END:
            alignment = Layout.Alignment.ALIGN_OPPOSITE;
            break;
        case Gravity.LEFT:
            alignment = (textView.getLayoutDirection() == TextView.LAYOUT_DIRECTION_RTL)
                    ? Layout.Alignment.ALIGN_OPPOSITE
                    : Layout.Alignment.ALIGN_NORMAL;
            break;
        case Gravity.RIGHT:
            alignment = (textView.getLayoutDirection() == TextView.LAYOUT_DIRECTION_RTL)
                    ? Layout.Alignment.ALIGN_NORMAL
                    : Layout.Alignment.ALIGN_OPPOSITE;
            break;
        case Gravity.CENTER_HORIZONTAL:
            alignment = Layout.Alignment.ALIGN_CENTER;
            break;
        default:
            alignment = Layout.Alignment.ALIGN_NORMAL;
            break;
        }
        break;
    case TextView.TEXT_ALIGNMENT_TEXT_START:
        alignment = Layout.Alignment.ALIGN_NORMAL;
        break;
    case TextView.TEXT_ALIGNMENT_TEXT_END:
        alignment = Layout.Alignment.ALIGN_OPPOSITE;
        break;
    case TextView.TEXT_ALIGNMENT_CENTER:
        alignment = Layout.Alignment.ALIGN_CENTER;
        break;
    case TextView.TEXT_ALIGNMENT_VIEW_START:
        alignment = Layout.Alignment.ALIGN_NORMAL;
        break;
    case TextView.TEXT_ALIGNMENT_VIEW_END:
        alignment = Layout.Alignment.ALIGN_OPPOSITE;
        break;
    case TextView.TEXT_ALIGNMENT_INHERIT:
        //
    default:
        alignment = Layout.Alignment.ALIGN_NORMAL;
        break;
    }
    return alignment;
}

From source file:Main.java

/**
 * Display a {@link Toast} letting the user know what an item does when long
 * pressed.//from   ww  w. ja v  a2  s. c om
 * 
 * @param view The {@link View} to copy the content description from.
 */
public static void showCheatSheet(final View view) {

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g.
                                          // status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (48 * context.getResources().getDisplayMetrics().density);

    final Toast cheatSheet = Toast.makeText(context, view.getContentDescription(), Toast.LENGTH_SHORT);
    final boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                displayFrame.bottom - screenPos[1]);
    }
    cheatSheet.show();
}

From source file:Main.java

/**
 * Internal helper method to show the cheat sheet toast.
 *//*w w w .  j a  va2 s.  c om*/
private static boolean show(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g. status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (ESTIMATED_TOAST_HEIGHT_DIPS
            * context.getResources().getDisplayMetrics().density);

    Toast cheatSheet = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        // NOTE: We can't use Gravity.BOTTOM because when the keyboard is up
        // its height isn't factored in.
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - estimatedToastHeight);
    }

    cheatSheet.show();
    return true;
}

From source file:org.telegram.ui.Cells.JoinSheetUserCell.java

public JoinSheetUserCell(Context context) {
    super(context);

    imageView = new BackupImageView(context);
    imageView.setRoundRadius(AndroidUtilities.dp(27));
    addView(imageView, LayoutHelper.createFrame(54, 54, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 7, 0, 0));

    nameTextView = new TextView(context);
    nameTextView.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
    nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    nameTextView.setMaxLines(1);//from   ww  w  .  ja va2s  . c om
    nameTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    nameTextView.setLines(1);
    nameTextView.setSingleLine(true);
    nameTextView.setEllipsize(TextUtils.TruncateAt.END);
    addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT,
            Gravity.LEFT | Gravity.TOP, 6, 64, 6, 0));
}

From source file:org.telegram.ui.Cells.ShareDialogCell.java

public ShareDialogCell(Context context) {
    super(context);
    setBackgroundResource(R.drawable.list_selector);

    imageView = new BackupImageView(context);
    imageView.setRoundRadius(AndroidUtilities.dp(27));
    addView(imageView, LayoutHelper.createFrame(54, 54, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 7, 0, 0));

    nameTextView = new TextView(context);
    nameTextView.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
    nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    nameTextView.setMaxLines(2);/*  w w w .jav a 2s . c  o  m*/
    nameTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    nameTextView.setLines(2);
    nameTextView.setEllipsize(TextUtils.TruncateAt.END);
    addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT,
            Gravity.LEFT | Gravity.TOP, 6, 64, 6, 0));

    checkBox = new CheckBox(context, R.drawable.round_check2);
    checkBox.setSize(24);
    checkBox.setCheckOffset(AndroidUtilities.dp(1));
    checkBox.setVisibility(VISIBLE);
    checkBox.setColor(0xff3ec1f9);
    addView(checkBox, LayoutHelper.createFrame(24, 24, Gravity.CENTER_HORIZONTAL | Gravity.TOP, 17, 39, 0, 0));
}

From source file:com.android.settings.widget.SlidingTabLayout.java

public SlidingTabLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    mLayoutInflater = LayoutInflater.from(context);
    mTitleView = new LinearLayout(context);
    mTitleView.setGravity(Gravity.CENTER_HORIZONTAL);
    mIndicatorView = mLayoutInflater.inflate(R.layout.sliding_tab_indicator_view, this, false);

    addView(mTitleView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    addView(mIndicatorView, mIndicatorView.getLayoutParams());
}

From source file:org.michaelbel.bottomsheet.BottomSheetGrid.java

public BottomSheetGrid(Context context) {
    super(context);

    setOrientation(VERTICAL);//w  ww.  ja  v  a  2s.c  o  m
    setPadding(Utils.dp(getContext(), 24), Utils.dp(getContext(), 16), Utils.dp(getContext(), 24),
            Utils.dp(getContext(), 16));

    iconView = new ImageView(context);
    iconView.setScaleType(ImageView.ScaleType.CENTER);

    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(Utils.dp(getContext(), 48),
            Utils.dp(getContext(), 48));
    params1.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

    iconView.setLayoutParams(params1);
    addView(iconView);

    textView = new TextView(context);
    textView.setLines(1);
    textView.setMaxLines(1);
    textView.setSingleLine(true);
    textView.setEllipsize(TextUtils.TruncateAt.END);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    params2.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

    textView.setLayoutParams(params2);
    addView(textView);
}

From source file:org.telegram.ui.Cells.HintDialogCell.java

public HintDialogCell(Context context) {
    super(context);
    setBackgroundResource(R.drawable.list_selector);

    imageView = new BackupImageView(context);
    imageView.setRoundRadius(AndroidUtilities.dp(27));
    addView(imageView, LayoutHelper.createFrame(54, 54, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 7, 0, 0));

    nameTextView = new TextView(context);
    nameTextView.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
    nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    nameTextView.setMaxLines(2);/*  w w  w  .  j  av  a 2 s  .  c om*/
    nameTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    nameTextView.setLines(2);
    nameTextView.setEllipsize(TextUtils.TruncateAt.END);
    addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT,
            Gravity.LEFT | Gravity.TOP, 6, 64, 6, 0));

    if (countDrawable == null) {
        countDrawable = getResources().getDrawable(R.drawable.dialogs_badge);
        countDrawableGrey = getResources().getDrawable(R.drawable.dialogs_badge2);

        countPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        countPaint.setColor(0xffffffff);
        countPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    }
    countPaint.setTextSize(AndroidUtilities.dp(13));
}

From source file:dk.cafeanalog.IsOpenFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mVisible = true;//from  w  w w . ja v  a 2  s.c  o m
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_is_open, container, false);

    mOpenSwitcher = (TextSwitcher) v.findViewById(R.id.text_view);

    mOpenSwitcher.setFactory(new TextSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            AppCompatTextView textView = new AppCompatTextView(getActivity());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
            return textView;
        }
    });
    mOpenSwitcher.setCurrentText(getResources().getText(R.string.is_open_analog));
    mOpenSwitcher.setInAnimation(v.getContext(), android.R.anim.slide_in_left);
    mOpenSwitcher.setOutAnimation(v.getContext(), android.R.anim.slide_out_right);

    mNamesSwitcher = (TextSwitcher) v.findViewById(R.id.name_view);
    mNamesSwitcher.setFactory(new TextSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            AppCompatTextView textView = new AppCompatTextView(getActivity());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
            return textView;
        }
    });
    mNamesSwitcher.setInAnimation(v.getContext(), android.R.anim.slide_in_left);
    mNamesSwitcher.setOutAnimation(v.getContext(), android.R.anim.slide_out_right);

    v.findViewById(R.id.fragment_main).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            click();
        }
    });

    return v;
}

From source file:com.android.tabletcustomui.views.LeftCircleContainer.java

private void init(Context context, int width) {

    LinearLayoutCompat.LayoutParams layoutParams = new LinearLayoutCompat.LayoutParams((int) (width),
            (int) (width));
    layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
    this.setLayoutParams(layoutParams);

    LeftOuterCircleView leftOuterCircleView1 = new LeftOuterCircleView(context, width, false);
    LeftOuterCircleView leftOuterCircleView2 = new LeftOuterCircleView(context, (width - 40), true);

    circleTitleView = new CircleTitleView(context, (int) (width));
    circleTitleView.setTextColor(getContext().getResources().getColor(android.R.color.white));
    circleTitleView.setTextSize(20);//from  w w w.ja  v a  2 s  .c  o m
    circleTitleView.setTypeface(Typeface.DEFAULT, Typeface.BOLD_ITALIC);
    circleTitleView.setText("App Name");

    this.addView(leftOuterCircleView1);
    this.addView(leftOuterCircleView2);
    addCircle(1, width - 78, (float) 0.0, 0.85);
    addCircle(2, width - 150, (float) 0.0, 0.97);
    addCircle(3, width - 170, (float) 0.0, 0.97);
    addCircle(4, width - 190, (float) 0.0, 0.98);
    addCircle(5, width - 210, (float) 0.0, 0.98);
    addCircle(6, width - 230, (float) 0.0, 0.85);
    addCircle(7, width - 280, (float) 0.0, 0.85);
    addCircle(0, width - 35, (float) 0.0, 0.9);
    addCircle(0, width - 92, (float) 0.1, 0.9);

    this.addView(circleTitleView);

    animateClockWise(leftOuterCircleView1);

    animateAntiClockWise(leftOuterCircleView2);

}