Example usage for android.widget TextView getGravity

List of usage examples for android.widget TextView getGravity

Introduction

In this page you can find the example usage for android.widget TextView getGravity.

Prototype

public int getGravity() 

Source Link

Document

Returns the horizontal and vertical alignment of this TextView.

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   ww w.j ava  2s  . c  om*/

    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:org.appcelerator.titanium.util.TiUIHelper.java

public static void setAlignment(final TextView tv, final String textAlign, final String verticalAlign) {
    int gravity = Gravity.NO_GRAVITY;

    if (textAlign != null) {
        if ("left".equals(textAlign)) {
            gravity |= Gravity.LEFT;//  ww w . ja v a  2s  .  c  om
        } else if ("center".equals(textAlign) || "middle".equals(textAlign)) {
            gravity |= Gravity.CENTER_HORIZONTAL;
        } else if ("right".equals(textAlign)) {
            gravity |= Gravity.RIGHT;
        } else {
            Log.w(TAG, "Unsupported horizontal alignment: " + textAlign);
        }
    } else {
        // Nothing has been set - let's set if something was set previously
        // You can do this with shortcut syntax - but long term maint of code is easier if it's explicit
        //         Log.w(TAG,
        //            "No alignment set - old horizontal align was: " + (tv.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK),
        //            Log.DEBUG_MODE);

        if ((tv.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.NO_GRAVITY) {
            // Something was set before - so let's use it
            gravity |= tv.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;
        }
    }

    if (verticalAlign != null) {
        if ("top".equals(verticalAlign)) {
            gravity |= Gravity.TOP;
        } else if ("middle".equals(verticalAlign) || "center".equals(verticalAlign)) {
            gravity |= Gravity.CENTER_VERTICAL;
        } else if ("bottom".equals(verticalAlign)) {
            gravity |= Gravity.BOTTOM;
        } else {
            Log.w(TAG, "Unsupported vertical alignment: " + verticalAlign);
        }
    } else {
        // Nothing has been set - let's set if something was set previously
        // You can do this with shortcut syntax - but long term maint of code is easier if it's explicit
        //         Log.w(TAG, "No alignment set - old vertical align was: " + (tv.getGravity() & Gravity.VERTICAL_GRAVITY_MASK),
        //            Log.DEBUG_MODE);
        if ((tv.getGravity() & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.NO_GRAVITY) {
            // Something was set before - so let's use it
            gravity |= tv.getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
        }
    }

    tv.setGravity(gravity);
}