title: Android - Scaling my UI to fit all screen sizes?
body: <p>So far I have tried many ways of scaling images to screen sizes and none of them seem to work for me. I am still new to java and the idea of having a UI that fits to any screen size seems foreign to me. I am drawing bitmaps with the Canvas class. I have tried creating a scaledBitmap and have seen no difference. I want a simple main menu screen like this</p>
  
  <p><img src="https://i.stack.imgur.com/xbQ7P.jpg" alt="enter image description here"></p>
  
  <p>This must be a really common issue because all apps would need to do this. I really could use some help understanding how this works. On some phones it looks alright but on others the images are either too large or off centered.</p>
  
  <p>My code is below. If someone could please help me out here, this is for a school project due pretty soon :P Thanks guys!</p>
  
  <pre><code>public MainMenu(Context context) {
      super(context);
  
      titleBounds = new RectF();
      playBounds = new RectF();
      scoreBounds = new RectF();
      soundBounds = new RectF();
      creditBounds = new RectF();
  
      titlePaint = new Paint();
      playPaint = new Paint();
  
      play = BitmapFactory.decodeResource(getResources(), R.drawable.play);
      playGlow = BitmapFactory.decodeResource(getResources(), R.drawable.playglow);
  
  
      sound = BitmapFactory.decodeResource(getResources(), R.drawable.sound);
      soundGlow = BitmapFactory.decodeResource(getResources(), R.drawable.soundglow);
  
  
      // Get window size and save it to screenWidth and screenHeight.
      WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
      Display display = wm.getDefaultDisplay();
      Point size = new Point();
      display.getSize(size); 
      screenWidth = size.x;
      screenHeight = size.y;
  
      playy = Bitmap.createScaledBitmap(play, convertToPx(screenWidth), convertToPx(screenHeight), true);
  
  
      playSound(context, R.raw.loop);
  
  
  }
  
  public int convertToPx(int dp) {
      // Get the screen's density scale
      final float scale = getResources().getDisplayMetrics().density;
      // Convert the dps to pixels, based on density scale
      return (int) (dp * scale + 0.5f);
  }
  
  
  @Override protected void onDraw(Canvas canvas) {
  
  
      ViewGroup parent = (ViewGroup)getParent();
  
      playBounds.set(screenWidth / 2 - play.getWidth() / 2, screenHeight * 1/3 - play.getHeight() / 2, playBounds.left + play.getWidth(), playBounds.top + play.getHeight());
      soundBounds.set(playBounds.centerX() - sound.getWidth() / 2, playBounds.bottom + 10, soundBounds.left + sound.getWidth(), soundBounds.top + sound.getHeight());
  
  
      if (playClick == false) canvas.drawBitmap(playy, null, playBounds, null);
      else canvas.drawBitmap(playGlow, null, playBounds, null);   
  
      if (soundClick == false) canvas.drawBitmap(sound, null, soundBounds, null);
      else canvas.drawBitmap(soundGlow, null, soundBounds, null); 
  </code></pre>