I am setting the width of EditText element to fill_parent
in XML file, but when accessing it from code, it returns 0.
What I want, is the actual width of that element at run time. How could I do it.

- 769
- 2
- 8
- 17
3 Answers
Use getMeasuredWidth(): http://developer.android.com/reference/android/view/View.html#getMeasuredWidth%28%29
Read also here:
When a View's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View's descendants. A View's measured width and measured height values must respect the constraints imposed by the View's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent View may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (i.e., if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass).
http://developer.android.com/guide/topics/ui/how-android-draws.html

- 40,203
- 9
- 86
- 108
-
`getMeasuredWidth()` is also returning zero. – Greenhorn May 11 '11 at 05:17
-
@Greenhorn is the view drawn? It will not have its width set unless it has been drawn to the window. – Aleadam May 11 '11 at 05:20
-
Yeah, I called it from `onConfigurationChanged()` and `onStart()`. – Greenhorn May 11 '11 at 05:25
You could solve this by creating a custom View and override the onMeasure() method. If you always use "fill_parent" for the layout_width in your xml then the widthMeasureSpec parameter that is passed into the onMeasusre() method should contain the width of the parent.
public class MyCustomView extends EditText {
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
}
}
Your XML would look something like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<view
class="com.company.MyCustomView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>

- 22,410
- 7
- 80
- 107
I'll refer you to a previous answer on this topic that should be helpful:

- 1
- 1

- 133,643
- 45
- 263
- 274
-
Using this way, I am getting the width, but setting that width to other EditText elements, they are not reflecting it. What I am trying to do is to make all the EditTexts of same width. – Greenhorn May 11 '11 at 05:52
-
You might want to look into using a TableLayout. What code are you using to try and set the width of the others? – Kevin Coppock May 11 '11 at 11:58