title: How to change button size according to screen's device?
body: <p>I have a button with size <code>width=216</code> and <code>height=110</code> in a device screen resolution is 1080 x 1920.  When I change to other device (smaller resolution), the button is so big. Hence, I need to write a fuction to dynamically change button size according to screen's device resolution. First, I will check target device resolution then I change it based on above my current device. However, the button resolution is not correct. How can I fix it? This is my code</p>
  
  <p>My code in the 1080x1920</p>
  
  <pre><code>        &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_gravity="center"
          &gt;
          &lt;Button
              android:id="@+id/btn"
              android:layout_width="216dp"
              android:layout_height="110dp"
              android:layout_gravity="center"
              android:scaleType="fitCenter"
            /&gt;
         &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Changed to dynamic way:</p>
  
  <pre><code>    public int WIDTH ;
      public int HEIGHT ;
      //Get screen resolution
      DisplayMetrics metrics = getResources().getDisplayMetrics();
      HEIGHT = metrics.heightPixels;
      WIDTH= metrics.widthPixels;           
  
      int height_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, getResources().getDisplayMetrics());
      int weight_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 216, getResources().getDisplayMetrics());
      int width_linearlayout = Math.round((float)weight_px/1080*WIDTH);
      int height_linearlayout = Math.round((float)height_px/1920*HEIGHT);
      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width_linearlayout,height_linearlayout);
      btn.setLayoutParams(layoutParams);
  </code></pre>