AndroidAutoSize

repository·master·Indexed 12 days ago

https://github.com/jessyancoding/androidautosize

A screen adaptation solution for Android applications that allows layouts designed with a fixed DP/pixel scale to automatically scale across different device resolutions and densities. Supports custom adaptation for Activities and Fragments, and provides subunit options (pt, in, mm) for pixel-perfect design.

Tokens
1.2K
Snippets
5
Records
6
Agent score
46%

What's inside AndroidAutoSize

  1. Configure Android Studio Preview for AndroidAutoSize

    master

    To see your designs accurately in the Android Studio Layout Preview, you may need to create a virtual device using specific units (dp, pt, in, mm) based on your design's resolution and DPI.

    Formula for screen size calculation:

    • DP: (sqrt(vertical resolution^2 + horizontal resolution^2)) / dpi
    • PT: (sqrt(vertical resolution^2 + horizontal resolution^2)) / 72
    • IN: sqrt(vertical resolution^2 + horizontal resolution^2)
    • MM: (sqrt(vertical resolution^2 + horizontal resolution^2)) / 25.4

    Tip: To hide the status bar and navigation bar in the preview, select the 'panel' theme in the preview settings so the vertical resolution fills the entire page.

  2. Initialize AndroidAutoSize in AndroidManifest

    master

    To enable the library, add meta-data tags to your <application> section in AndroidManifest.xml. These values define your design's width and height in DP. This allows the library to scale your layouts to match the design specifications.

    <manifest>
        <application>            
            <meta-data
                android:name="design_width_in_dp"
                android:value="360"/>
            <meta-data
                android:name="design_height_in_dp"
                android:value="640"/>           
         </application>           
    </manifest>
  3. Install AndroidAutoSize via JCenter or JitPack

    master

    You can add AndroidAutoSize to your project using either JCenter or JitPack.

    JCenter:

    implementation 'me.jessyan:autosize:1.2.1'

    JitPack:

    1. Add the JitPack repository to your root build.gradle:
    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }
    1. Add the dependency to your app-level build.gradle:
    dependencies {
        implementation 'com.github.JessYanCoding:AndroidAutoSize:v1.2.1'
    }
    implementation 'com.github.JessYanCoding:AndroidAutoSize:v1.2.1'
  4. Customize Fragment adaptation parameters

    master

    To use custom adaptation parameters for Fragments, you must first enable Fragment support in the global configuration:

    AutoSizeConfig.getInstance().setCustomFragment(true);

    Once enabled, you can customize a Fragment by implementing the CustomAdapt interface (overriding isBaseOnWidth() and getSizeInDp()) or disable it by implementing CancelAdapt.

    // 1. Enable support
    AutoSizeConfig.getInstance().setCustomFragment(true);
    
    // 2. Implement CustomAdapt in your Fragment
    public class CustomAdaptFragment extends Fragment implements CustomAdapt {
    
        @Override
        public boolean isBaseOnWidth() {
            return false;
        }
    
        @Override
        public float getSizeInDp() {
            return 667;
        }
    }
  5. Use Subunits (pt, in, mm) for pixel-perfect design

    master

    If you want to avoid the side effects of modifying DisplayMetrics#density, you can use subunits like pt, in, or mm. When using subunits, you can write the exact pixel size from your design directly into your layouts without converting it to DP.

    To use subunits, configure the UnitsManager via AutoSizeConfig:

    AutoSizeConfig.getInstance().getUnitsManager()
            .setSupportDP(false)
            .setSupportSP(false)
            .setSupportSubunits(Subunits.MM);

    Supported subunits include:

    • Subunits.PT
    • Subunits.IN
    • Subunits.MM
  6. Customize Activity adaptation parameters

    master

    You can control how an Activity adapts to different screens by implementing the CustomAdapt interface.

    To customize parameters (like setting a specific base size or changing the adaptation logic), implement CustomAdapt and override isBaseOnWidth() and getSizeInDp().

    To completely disable adaptation for a specific Activity, implement the CancelAdapt interface.

    public class CustomAdaptActivity extends AppCompatActivity implements CustomAdapt {
    
        @Override
        public boolean isBaseOnWidth() {
            return false;
        }
    
        @Override
        public float getSizeInDp() {
            return 667;
        }
    }