попробуйте это:
TextView textView = findViewById(R.id.textView);
Random random = new Random();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int random_width = random.nextInt(metrics.widthPixels -
textView.getWidth());
int random_height = random.nextInt(metrics.heightPixels -
textView.getHeight());
if (random_width > (metrics.widthPixels - 100)) {
random_width -= 100;
}
if (random_height > (metrics.heightPixels - 200)) {
random_height -= 200;
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.width = metrics.widthPixels;
layoutParams.height = metrics.heightPixels;
layoutParams.setMargins(random_width, random_height, 0, 0);
textView.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.width = metrics.widthPixels;
layoutParams.height = metrics.heightPixels;
layoutParams.setMargins(random_width, random_height, 0, 0);
textView.setLayoutParams(layoutParams);
и в вашем xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HI"
android:textColor="#ff9999" />
</RelativeLayout>