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 + ""); } }
widthtext.setText("""+width);anddheighttext.setText(""+height);