title: Randomly Placing view in a fragment
body: <p>I am new to android development, and I am trying to place a view (<code>imageView</code> to be specific) in a random place inside of a fragment, but I'm having a lot of trouble. I've spent a ridiculous amount of time trying to find out how to do this, but I still can't figure it out.</p>
  
  <p>So originally, I had the random position of the View thing working in a regular activity using this method:</p>
  
  <pre><code>    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
      DisplayMetrics metrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(metrics);
  
      params.leftMargin = new Random().nextInt(metrics.widthPixels - button.getWidth());
      params.topMargin = new Random().nextInt(metrics.heightPixels - 2 * button.getHeight());
      button.setLayoutParams(params); 
  </code></pre>
  
  <p>But this doesn't work in a <code>Fragment</code> because I can't do <code>button.getWidth()</code>. So then I tried using the <a href="https://stackoverflow.com/questions/9845197/android-get-fragment-width">ViewTreeObserver method</a>, but I really didn't understand what was going on and I couldn't get it to work for me.</p>
  
  <p>Could someone please explain how I can randomly place a view inside of a fragment, without the view going outside of the boundaries of the fragment?</p>
  
  <p>P.S. If someone just answers with "you should do some more research," please just understand that I have literally spent about 10 hours trying to figure this out, and have completely exhausted any ideas of how I can do this. </p>