1

The application runs. What it is supposed to do is display "=Heightofthescreen=" in a textbox where Heightofthescreen is the actual value of the screen. Instead, however, it just gives me a 0. I imagine it is something with my context but I don't know how to fix it.

Activity class:

package com.example.measuringtesting;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class StartDraw extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Drawing.context2 = getApplicationContext();
        Drawing y = new Drawing(Drawing.context2);
        int theheight = y.width2;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_draw);
        TextView t = new TextView(this); 

        t=(TextView)findViewById(R.id.textView1); 
        t.setText("=" + theheight + "=");
    }
}

here is the view class:

package com.example.measuringtesting;

import android.content.Context;
import android.view.View;

public class Drawing extends View {
public Drawing(Context context) {
    super(context);
}

public float width;
public float height;
public int width2;
public int height2;
public static Context context2;

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
float parentWidth = MeasureSpec.getSize(widthMeasureSpec);
float parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.width = (float) (parentWidth*.15);
this.height = (float) (parentHeight*.15);
this.width2 = Math.round(width);
this.height2 = Math.round(height);
this.setMeasuredDimension(width2, height2);
}

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

The problem is that onMeasure() isn't called until the layout is drawn, which is sometime later than when you set the text in your TextView. So for now y.width2 is still 0.

Look at this question: When Can I First Measure a View?

Basically you must add a GlobalLayoutListener to your ViewTreeObserver of the View you want to measure. The measurements of your view will be available in the public void onGlobalLayout() callback for the GlobalLayoutListener.

Community
  • 1
  • 1
telkins
  • 10,440
  • 8
  • 52
  • 79
  • Ah okay, so I must change the text within the `public void onGlobalLayout()` method, because as it is, `onMeasure()` is not called until `onCreate()` exits, correct? – user2957243 Nov 05 '13 at 17:33
  • @user2957243 Yup, exactly. Calling `getHeight()` or `getWidth()` inside `onGlobalLayout()` should give you a value. You should also call `setText()` for your TextView inside that method too. – telkins Nov 05 '13 at 18:23
  • I can't seem to get it to work. It now displays just "Large Text" `final ViewTreeObserver vto = y.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { t.setText(y.getHeight()); } });` Sorry for such a late reply, I have been away. It doesn't seem that setText ever gets called now, because 'Large Text' is the default. Any ideas/make new thread? – user2957243 Nov 11 '13 at 17:57
  • @user2957243 Sorry for the delay. If you debug at that location does `y.getHeight()` return a non-zero value? – telkins Nov 12 '13 at 14:52
  • Arg, I can't debug inside that method because 'step over' skips it, and step into gives me a source not found error, for whatever reason. But at the end of the onCreate method, the height and width values both equal zero, mAlive is "true", and t's text is "Large Text" – user2957243 Nov 13 '13 at 17:03
  • @user2957243 Try putting a dummy statement like `int dummy = 1` after `y.getHeight()` so that you can put a breakpoint there. `getHeight()` and `getWidth()` should both have values by the time `onGlobalLayout()` is called, unless the size of your View really is just 0. – telkins Nov 13 '13 at 17:07
  • onGlobalLayout() seems to never be called. In the method I replaced `t.setText(y.getHeight());` with this, `t.setText("Hello");` . However, the text view remains as "Large Text" instead of updating to "Hello" – user2957243 Nov 13 '13 at 17:30
  • @user2957243 Oh, I just realized that you never add `y` to any layout. You create it with the context, but it's not part of any layout and thus not drawn at all, which is why `onGlobalLayout()` is never called. You should add it to whatever `R.layout.activity_start_draw` is and then instantiate it with `findViewById()` unless you want to programmatically add it to a layout. – telkins Nov 13 '13 at 18:21
  • Aha! It works! You have been very helpful, I can not give you enough thanks. :) – user2957243 Nov 15 '13 at 17:44
  • @user2957243 Good to hear! Glad to be of help. – telkins Nov 15 '13 at 19:36