Search code examples
javaandroidlayoutandroid-relativelayout

The Relative Layout doesn't cover the width of the screen


I'm making an application that uses the relative layout to create a transparent black activity on the screen but the layout that it creates doesn't fit with the width of my phone screen.

Here's the result from my coding

Screenshot https://ibb.co/w44WcWV

Here's my xml file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A6000000"
        >

    </RelativeLayout>
</RelativeLayout>

Here's the styles.xml that I use to create transparent activity

<style name="Theme.Transparent" parent = "android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

I have already set match_parent for both width and height. Thank you.


Solution

  • You need to set this in your styles.xml for your Theme.Transparent:

    <item name="android:windowIsFloating">false</item>
    

    A floating window is usually a dialog, so has automatic spacing on either side. Setting this to false removes this automatic spacing. You can read a bit more about it in the related documentation, specifically:

    ... whether this window is being displayed with a floating style (based on the R.attr.windowIsFloating attribute in the style/theme).

    Although, if you're doing a fullscreen RelativeLayout, one wonders why you need transparency after all..!