skip to Main Content

How could I disable edge-to-edge in API 35 so that layer elements is missing in the status bar,
or how to set top margins for elements ( android:layout_marginTop ) below of the status bar?

Height of status bar:
val status_bar_height = (getResources().displayMetrics.density * 24F).toInt()

How to shift all elements of layer in Activity to down?:

not working code in Activity:

mybutton.top = 50 

not working code in Layout:

android:layout_marginTop="@*android:dimen/status_bar_height"

2

Answers


  1. Here are a few approaches to achieve what you want:

    1. To disable edge-to-edge and revert to the old behavior in API 35:

       class MainActivity : AppCompatActivity() {
           override fun onCreate(savedInstanceState: Bundle?){
               super.onCreate(savedInstanceState)
      
               // Disable edge-to-edge
               WindowCompat.setDecorFitsSystemWindows(window, true)
      
               setContentView(R.layout.activity_main)
           }
       }
      

    2.If you want to keep edge-to-edge but add padding/margin below the status bar, you can use WindowInsets:

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            
            // Get the root view
            val rootView = findViewById<View>(android.R.id.content)
            
            // Apply window insets
            ViewCompat.setOnApplyWindowInsetsListener(rootView) { view, windowInsets ->
                val insets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars())
                view.setPadding(0, insets.top, 0, 0)
                windowInsets
            }
        }
    }
    

    3.If you want to add margin programmatically to specific views:

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            
            val myButton = findViewById<Button>(R.id.mybutton)
            
            // Get status bar height from resources
            val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
            val statusBarHeight = if (resourceId > 0) {
                resources.getDimensionPixelSize(resourceId)
            } else {
                (resources.displayMetrics.density * 24f).toInt()
            }
            
            // Apply margin to the button
            (myButton.layoutParams as ViewGroup.MarginLayoutParams).apply {
                topMargin = statusBarHeight
                myButton.layoutParams = this
            }
        }
    }
    
    1. In XML layout, you can use a top-level container with padding:
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true">
        
        <!-- Your content here -->
        
    </LinearLayout>
    1. Or if you want to apply it to all direct children of your layout:

    // In your Activity

    private fun applyStatusBarMarginToChildren() {
        val rootView = findViewById<ViewGroup>(android.R.id.content)
        val statusBarHeight = resources.getIdentifier("status_bar_height", "dimen", "android")
            .let { if (it > 0) resources.getDimensionPixelSize(it) else 0 }
            
        for (i in 0 until rootView.childCount) {
            val child = rootView.getChildAt(i)
            (child.layoutParams as ViewGroup.MarginLayoutParams).apply {
                topMargin = statusBarHeight
                child.layoutParams = this
            }
        }
    }
    

    The reason your original attempts weren’t working:

    mybutton.top = 50 – This directly modifies the view’s position property, which doesn’t affect the layout parameters
    @*android:dimen/status_bar_height – This is a system resource that’s not directly accessible in this way

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search