1

I know there have been a lot of other questions on this topic, but I've looked through all of them, and still not gotten it to work.

I've made this code for testing:

public class MainActivity extends Activity { RelativeLayout layout; TextView widthtext; TextView heighttext; int width; int height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout = (RelativeLayout) findViewById(R.id.rela); widthtext = (TextView) findViewById(R.id.textView); heighttext = (TextView) findViewById(R.id.textView2); layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { width = layout.getWidth(); height = layout.getHeight(); } }); widthtext.setText(width); heighttext.setText(height); } 

and

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:id="@+id/rela"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="123" android:id="@+id/textView" android:layout_alignTop="@+id/textView2" android:layout_toLeftOf="@+id/textView2" android:layout_toStartOf="@+id/textView2" android:layout_marginRight="50dp" android:layout_marginEnd="50dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="123" android:id="@+id/textView2" android:layout_marginRight="50dp" android:layout_marginEnd="50dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> 

After a lot of hassle I've now notised that the width and height int's have the right values, though I can't make them display in my TextViews, getting the NullPointerExeption.

Anyone who can make this work?

EXTRA:

public class MainActivity extends Activity { RelativeLayout layout; TextView widthtext; TextView heighttext; int width; int height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout = (RelativeLayout) findViewById(R.id.rela); widthtext = (TextView) findViewById(R.id.textView); heighttext = (TextView) findViewById(R.id.textView2); layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { width = layout.getWidth(); height = layout.getHeight(); } }); widthtext.setText(width + ""); heighttext.setText(height + ""); } 

}

1
  • are you sure it is a NPE? try converting the int in string before passing it to the setText. E.g widthtext.setText("""+width); andd heighttext.setText(""+height); Commented Mar 1, 2015 at 17:31

2 Answers 2

5

You are doing this

widthtext.setText(width); heighttext.setText(height); 

outside of your listener so naturally it doesn't have the values yet.

Move them inside

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { width = layout.getWidth(); height = layout.getHeight(); // move here widthtext.setText(width); heighttext.setText(height); } }); } 

As blackbelt pointed out in a comment, you are calling setText() and passing an int. This will look for a resource id of that value. Change those to Strings.

widthtext.setText("" + width); heighttext.setText("" + height); 
Sign up to request clarification or add additional context in comments.

16 Comments

orb, be aware. int width, int height
Thanks. bor @Blackbelt will fix
That is what I tried first, but then the application crashed. Is still doing this.
well at least it doesn't crash :D
put widthtext.setText(width + ""); heighttext.setText(height + ""); in onGlobalLayout after you retrieve width and height
|
0

setText takes string value

so instead of

setText(width); setText(height); 

do

setText(width+""); setText(height+""); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.