Context
Step1: Currently I have one activity which uses android:windowBackground to set the initial background while we wait for the activity to load. This loads a bitmap which should be center aligned.
Step2: Once the activity is loaded it does a simple setContentView to set the background for the activity which will now replace the android:windowBackground from step1. This loads an imageView which should be center aligned.
The problem is they are not both aligned center, there is some kind of offset on one or the other misaligning them. Maby the statusbar pushing down the one? I am not sure. Any ideas why they are not aligned the same?
I would like to have them both center aligned. I have tried using fitSystemWindows=”true” with no luck.
When I add android:layout_marginTop=”12dp” to the activity (activity_start_onboarding.xml) layouts imageView both align fine but it does not align for all densities, other densities are misaligned.
Maby there is a way to dynamically calculate this offset to align then for all densities?
Compare Images
Left (Grey) – Step1 (windowBackground):
Right (Blue) – Step2 (activity layout):
The Code
AndroidManifest.xml
android:launchMode=”singleTask”
android:screenOrientation=”portrait”
android:theme=”@style/ColdstartSplashTheme”>
styles.xml
@drawable/splash_background
@drawable/splash_background
android:gravity=”center”
android:src=”@drawable/ic_delivery_free_raster” />
ActivityStartOnboarding.java
public class ActivityStartOnboarding extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_onboarding);
}
}
activity_start_onboarding.xml
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”@color/blue_light_darker”
android:fitsSystemWindows=”true”>
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:src=”@drawable/splash_background” />
# Answer 1
android:fitsSystemWindows=”true”
That is your problem. Your FrameLayout start to inflate under statusbar, which height ~25dp, therefore AppCompatImageView a bit higher.
First solution:
Remove android:fitsSystemWindows=”true” from activity_start_onboarding.xml FrameLayout.
Second solution:
Add true to your ColdstartSplashTheme. The background starts drawing under status bar and bitmap will be higher.
# Answer 2
Use background instead of windowBackground
@drawable/splash_background
# Answer 3
For me the accepted solution didn’ work. But I found this and it worked like a charm.
Its also a more flexible solution because it doesn’t depend on API 21