简体   繁体   中英

getHeight(), getWidth() in textView return 0

I want to getHight and getWidth of text in textView. I referenced many topics but have not found out my problem yet. In some topic a see some solutions as:

    @Override
public void onWindowFocusChanged(boolean hasFocus) {
   // TODO Auto-generated method stub
   super.onWindowFocusChanged(hasFocus);
   Log.d("line", ""+selectA.getLineCount());
   Log.d("hight", "" +selectA.getHeight());
}

OR

   TextView upV = (TextView)findViewById(R.id.up);
   ViewTreeObserver vto = upV.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Log.d("W ", ""+upV.getWidth());
            Log.d("H ", ""+upV.getHeight());
        }
    });

These solutions get the value as I need but not every time the app run, sometimes it return 0 . Please help me

You are calling getWidth() and getHeight() too early. The UI has not been sized and laid out on the screen yet, and as a result, the methods are correctly returning 0.

To get height or width of a View you can override onDraw(Canvas canvas) of this View:

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    int w = getWidth();
    int h = getHeight();
}

You are getting this because when you are querying the height view is not drawn.

Solution-

textView.post(new Runnable() {
        @Override
        public void run() {
             //height is ready
         int height = textView.getHeight();
        }
    });

Use below code to get Height and Width of TextView

textView.measure(0,0)
int w = textView.getMeasuredWidth();
int h = textView.getMeasuredHeight();

I have found a simple ...very simple Solution.

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SimpleActivity extends Activity {

private TextView textView;
private int displayWidth, displayHeight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textView = (TextView) findViewById(R.id.hello_tv);      
    textView.setOnClickListener(                
            new OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    displayWidth = textView.getWidth();
                    displayHeight = textView.getHeight();
                    textView.setText(displayWidth + "\n" + displayHeight);                      
                }
            }
            );              
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM