1

I'm developing a new personal project using compose and the new Material 3 components and I was just using the emulator to test it, everything was looking fine. Today, I used a real device to test it (Galaxy S10E, on Android 12 - Same version as my emulator) and I noticed some differences between the app running on the emulator and on the device.

I noticed two differences:

  • Action bar is not showing on emulator, but is showing on the Galaxy S10E, and I'm using the NoActionBarTheme.
  • Alert Dialog is is a lot different on emulator and on real device. On emulator, alert dialog is centralized on screen and does not occupy all width, but on real device the dialog is being shown at the bottom of the screen and occupying all width, like a Modal Bottom Sheet.

Code:

package com.alexandrekva.calculadorarv

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.*
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.alexandrekva.calculadorarv.ui.theme.CalculadoraRVTheme

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            CalculadoraRVTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {val openDialog = remember { mutableStateOf(true) }
                    if (openDialog.value) {
                        AlertDialog(
                            onDismissRequest = {
                                // Dismiss the dialog when the user clicks outside the dialog or on the back
                                // button. If you want to disable that functionality, simply use an empty
                                // onDismissRequest.
                                openDialog.value = false
                            },
                            title = {
                                Text(text = "Title")
                            },
                            text = {
                                Text(text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer dignissim consequat euismod. Pellentesque volutpat aliquam tortor vitae iaculis. Nulla vel felis ante. Ut ultrices a leo vitae tempus.")
                            },
                            confirmButton = {
                                TextButton(
                                    onClick = {
                                        openDialog.value = false
                                    }
                                ) {
                                    Text("Confirm")
                                }
                            },
                            dismissButton = {
                                TextButton(
                                    onClick = {
                                        openDialog.value = false
                                    }
                                ) {
                                    Text("Dismiss")
                                }
                            }
                        )
                    }
                }
            }
        }
    }
}

Theme

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.CalculadoraRV" parent="android:Theme.Material.Light.NoActionBar" />

    <style name="Theme.CalculadoraRV.Starting" parent="Theme.SplashScreen">
        <!-- Set the splash screen background, animated icon, and animation duration. -->
        <item name="windowSplashScreenBackground">@color/white</item>

        <!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
             animated drawable. One of these is required. -->
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_round_trending_up_24</item>
        <!-- Required for animated icons -->
        <item name="windowSplashScreenAnimationDuration">400</item>

        <!-- Set the theme of the Activity that directly follows your splash screen. -->
        <!-- Required -->
        <item name="postSplashScreenTheme">@style/Theme.CalculadoraRV</item>
    </style>
</resources>

Dependencies


    implementation 'androidx.core:core-ktx:1.8.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material3:material3:1.0.0-alpha16"
    implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha16"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'androidx.activity:activity-compose:1.5.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

    // Dagger Hilt
    implementation 'com.google.dagger:hilt-android:2.42'
    annotationProcessor 'com.google.dagger:hilt-compiler:2.42'
    implementation 'com.google.dagger:hilt-android:2.42'
    kapt 'com.google.dagger:hilt-compiler:2.42'
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'


    // Splash Screen
    implementation 'androidx.core:core-splashscreen:1.0.0'

    // Navigation
    implementation 'androidx.navigation:navigation-compose:2.5.1'

    //Gson
    implementation 'com.google.code.gson:gson:2.9.1'
  • Provide the calling site of the AlertDialog. There has to be something setting it off. Also, if the app is built fully in Compose, there IS no default action-bar unless you explicitly write one. So, that's not normal. Look up your codebase for an `AppBar` Composable, might have been added as a part of the default template. – Richard Onslow Roper Aug 24 '22 at 06:46
  • @RichardOnslowRoper I'm using the AlertDialog code above, not setting anything related to its size or positioning, and as I said, it is working, the problem is that I'm having this completely different behavior on the real device. I'm using the exact same code on the two screenshots the only difference is on the colors due to the material dynamic color getting some colors from the system. So, there is no AppBar added. – Alexandre Alves Aug 24 '22 at 12:16
  • Sir... The `AlertDialog` Composable that you created above. You HAVE to place/position it somehwere right? I meant, post the calling site of the Composable. Also, did you do a quick `Ctrl`+`Shift`+`F` on the entire project to find an `AppBar`? Repeating again, even if you didn't add one, the template might have a default bar that you might be missing. – Richard Onslow Roper Aug 24 '22 at 14:24
  • @RichardOnslowRoper No, you don't need to place/position the Alert Dialog component, it acts like an overlay. And yes, I don't have any AppBar on the app. I think you're missing the point here... Did you saw the screenshots on the post? It's the same app. The AlertDialog is positioned correctly on the emulator and there is no ActionBar on the emulator. What I'm asking here is what could be causing the different behavior on the real device. – Alexandre Alves Aug 24 '22 at 15:33
  • Good heavens... You posted a code, defining an `@Composable` function. Now where in the `setContent{ ... }` hierarchy do you type this piece of code? – Richard Onslow Roper Aug 24 '22 at 16:12
  • I need to know the place in your `setContent` block where this piece of code is written. – Richard Onslow Roper Aug 24 '22 at 16:13
  • @RichardOnslowRoper Just updated the question with a code example, I'm now setting it directly on the MainActivity. On the other example I gave it was inside a component that was being used inside another component (screen full in compose, no fragments) that was being set on the main activity with navigation compose. The behavior is still the same, but it removes other variables from it so all the code on the example. Sorry for the confusion. – Alexandre Alves Aug 24 '22 at 18:11
  • In the `Theme.CalculadoraRV.Starting`, try adding `NoActionBar` theme? Concatenate using XML syntax. – Richard Onslow Roper Aug 24 '22 at 18:42
  • Any explicit use for `androidx.compose.material3:material3-window-size-class`? – Richard Onslow Roper Aug 24 '22 at 18:43
  • @RichardOnslowRoper The issue seems to be related with something on the splash screen api, removing the theme solved both issues, the action bar and the modal positioning/size. Thanks for your help and patience! – Alexandre Alves Aug 24 '22 at 20:14

0 Answers0