1

I have a problem.I'm trying to create TextView with specification dimensions, and i do it in onMeasure method.My progblem is i want to change the textalignment, so i call setGravity(Gravity.RIGHT) method but it's not working, the text is still in left side.

Below is my code, i hope someone'll help me :(

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    MyLabel label = new MyLabel(this);
    label.setText("Hello");
    label.setBackgroundColor(Color.RED);
    label.setGravity(Gravity.RIGHT);
    layout.addView(label);

    setContentView(layout);

}

public class MyLabel extends TextView {

    public MyLabel(Context context) {
        super(context);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Desire Size : Mac dinh la Wrap Content
        float desireWidth = getMeasuredWidth();
        float desireHeight = getMeasuredHeight();

        int maxWidth = 0;
        int maxHeight = 0;
        float width = 0.7f;
        float height = 0.3f;

        if (maxWidth == 0) {
            maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        }

        if (maxHeight == 0) {
            maxHeight = MeasureSpec.getSize(heightMeasureSpec);
        }

        // Neu abstract model co thong tin ve kich thuoc :
        if (width > 0) {

            int parentWidth = 0;
            if (true) {
                parentWidth = maxWidth;
            } else {
                parentWidth = 480;
            }
            if (width <= 1) {
                // Gia tri duoc tinh toan theo % cua parent
                desireWidth = width * parentWidth;
            } else {
                desireWidth = width < parentWidth ? width : parentWidth;
            }
        }

        // Kich thuoc chieu cao cung tuong tu nhu vay
        if (height > 0) {

            int parentHeight = 0;
            if (true) {
                parentHeight = maxHeight;
            } else {
                parentHeight = 800;
            }
            if (height <= 1) {
                // Gia tri duoc tinh toan theo % cua parent
                desireHeight = height * parentHeight;
            } else {
                desireHeight = height < parentHeight ? height
                        : parentHeight;
            }
        }
        // Cuoi cung, ta set kich thuoc cho view
        this.setMeasuredDimension((int) desireWidth, (int) desireHeight);
    }
};
Exception
  • 11
  • 3
  • possible duplicate of [Right align text in android TextView](http://stackoverflow.com/questions/8969122/right-align-text-in-android-textview) – Bishan May 29 '14 at 15:07
  • Not a duplicate, that question deals with static xml layouts not dynamic code generated ones. – indivisible May 29 '14 at 15:10
  • I think its not problem with ur gravity. Just set a size to your layout like match_parent.den see whether ur text z at right or not. – Meghna May 29 '14 at 15:18

1 Answers1

2

Making layouts in Java can be useful, but sometimes important implementation details are hidden from you when you do. This is one of those times.

When you call addView(), the view being added gets a default LayoutParams object from the parent view. In the case of LinearLayout, this default LayoutParams object has the width and height specified as WRAP_CONTENT. Since your TextView is only as large as its content, changing its gravity has no apparent effect.

You need to create a LayoutParams object and provide it to addView(). You can set its gravity to RIGHT or you can use MATCH_PARENT for its width.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, params);

Note that LinearLayout by default has HORIZONTAL orientation, which tends not to be so useful when it's the root of a screen layout. You might want to call linearLayout.setOrientation(LinearLayout.VERTICAL) on it.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • I know, but i must use onMeasure(), because i want to set the TextView size by the percentage of parent's size.LayoutParams has WEIGHT property, but it just change width(with HORIZONTAL) or height(with VERTICAL) – Exception May 30 '14 at 01:48