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

public static void centerToast(Context context, String message) {
    Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
    toast.show();//w  w  w.j  a v a  2 s . c  o m
}

From source file:Main.java

public static void showToast(Context context, String text, int duration) {
    Toast toast = Toast.makeText(context, text, duration);
    toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.show();/*ww w . j a  v  a 2s .c o m*/
}

From source file:Main.java

public static void showToast(Context context, String message) {
    Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.show();//from   ww  w  .  j  a  va  2  s. c om
}

From source file:Main.java

@SuppressLint("ShowToast")
public static Toast createToast(Context context, String message) {
    Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
    return toast;
}

From source file:Main.java

static void preDraw(TextView view, Canvas canvas) {
    Drawable[] drawables = view.getCompoundDrawables();
    if (drawables[0] != null) {
        view.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
        onCenterDraw(view, canvas, drawables[0], Gravity.START);
    } else if (drawables[1] != null) {
        view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        onCenterDraw(view, canvas, drawables[1], Gravity.TOP);
    } else if (drawables[2] != null) {
        view.setGravity(Gravity.CENTER_VERTICAL | Gravity.END);
        onCenterDraw(view, canvas, drawables[2], Gravity.END);
    } else if (drawables[3] != null) {
        view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        onCenterDraw(view, canvas, drawables[3], Gravity.BOTTOM);
    }/*  w w w .j a v  a2s  . c  om*/
}

From source file:Main.java

public static void preDraw(TextView view, Canvas canvas) {
    Drawable[] drawables = view.getCompoundDrawables();
    if (drawables != null) {
        if (drawables[0] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            onCenterDraw(view, canvas, drawables[0], Gravity.LEFT);
        } else if (drawables[1] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
            onCenterDraw(view, canvas, drawables[1], Gravity.TOP);
        } else if (drawables[2] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
            onCenterDraw(view, canvas, drawables[2], Gravity.RIGHT);
        } else if (drawables[3] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            onCenterDraw(view, canvas, drawables[3], Gravity.BOTTOM);
        }/*from ww w  .  j a  v a2s  .c om*/
    }
}

From source file:Main.java

protected static Bitmap creatCodeBitmap(String contents, int width, int height, Context context) {
    TextView tv = new TextView(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(layoutParams);/*w  w  w  .j a v  a2s . com*/
    tv.setText(contents);
    tv.setHeight(height);
    tv.setGravity(Gravity.CENTER_HORIZONTAL);
    tv.setWidth(width);
    tv.setDrawingCacheEnabled(true);
    tv.setTextColor(Color.BLACK);
    tv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

    tv.buildDrawingCache();
    Bitmap bitmapCode = tv.getDrawingCache();
    return bitmapCode;
}

From source file:Main.java

public static PopupWindow showPopWindow3(Context context, View targetView, View contentView, Integer width) {
    PopupWindow popupWindow = null;/*from   w w  w.j  a  v a2s .c  om*/
    popupWindow = new PopupWindow(contentView, -2, -2);
    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
    if (width != null) {
        popupWindow.setWidth(width);
    }
    popupWindow.setOutsideTouchable(true);
    popupWindow.showAtLocation(targetView, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
    return popupWindow;
}

From source file:com.android.services.Helper.java

public static void showMessage(Context ctx, String msg) {
    Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 580);
    toast.show();//from  w w  w  . j a  v  a 2  s  . c o m

}

From source file:Main.java

private static boolean showToolTip(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }/*from  w  ww  .  ja  va 2s.c o m*/

    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;
}