M.KJ
M.KJ

Reputation: 15

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.

Upvotes: 1

Views: 404

Answers (3)

Ankit Tale
Ankit Tale

Reputation: 2004

I will go with Constraintlayout for single parent multiple view

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    >

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A6000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    </View>
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

kashyap
kashyap

Reputation: 508

Change the style.xml with this.

 <style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
        <item name="android:background">#33000000</item> 
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

In Manifest

<activity
        android:theme="@style/Theme.AppCompat.Translucent"
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Upvotes: 3

Jake Lee
Jake Lee

Reputation: 7979

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..!

Upvotes: 3

Related Questions