michaelsmith
michaelsmith

Reputation: 1051

onSizeChanged returns width, but not height

I need to get height and width of my View and I have followed examples given here with subclassing View and using onSizeChanged(...). But what is very strange is that onSizeChanged gives me the correct width, but height is always 0. Here's my subclass:

public class MyView extends TextView {
int xMax=0; int yMax=0;Context c;

public MyView(Context context) {
    super(context);
    this.c=context;

}
@Override
   public void onSizeChanged(int w, int h, int oldW, int oldH) {

      xMax = w;
      yMax = h;

My Activity class:

public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView dummyView = new MyView(this);

    LinearLayout ll = (LinearLayout) findViewById(R.id.mainmtc);
    ll.addView(dummyView);

and my XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainmtc" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1"
androiad:orientation="vertical">

Upvotes: 3

Views: 1429

Answers (1)

ChristopheCVB
ChristopheCVB

Reputation: 7315

I think it's normal, there is no content in your view so the height is 0.

Try to add some text in your custom textview and height will change

TextView dummyView = new MyView(this);
dummyView.setText("Hello");

Device screen dimensions : http://developer.android.com/reference/android/util/DisplayMetrics.html

Upvotes: 2

Related Questions