-2

I am trying to inflate a view beneath my layout, but my to layout becomes transparent. Any idea why?

Here is my mainActivity:

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

        setContentView(R.layout.activity_main)

        val rootView = (this.findViewById<View>(android.R.id.content) as ViewGroup)
            .getChildAt(0) as ViewGroup

        val inflater: LayoutInflater = LayoutInflater.from(this)
        val view: View = inflater.inflate(R.layout.view_blueberry, rootView, false)

        rootView.addView(view)

my activity layout:

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

    <RelativeLayout
            android:layout_width="match_parent"
            android:id="@+id/rlContainer"
            android:layout_height="match_parent"
            android:background="#ffffff"
            tools:ignore="UselessParent">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btStartAnim"
                android:text="start"/>
    </RelativeLayout>
</RelativeLayout>

and my inflated view:

<?xml version="1.0" encoding="utf-8"?>
<View
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#abab"
        />

This is how it looks: if I add more content it's as transparent as that button. Even weirder considering that I've set a white background in my main layout.

enter image description here

Any idea on how to fix this?

RFM
  • 107
  • 7

2 Answers2

1

In inflated view you setted transparent background thats why issue ocurres.

  <?xml version="1.0" encoding="utf-8"?>
  <View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#abab"
    />

In this please setted proper background without opacity and check once.

    android:background="#f00"
Mayur Coceptioni
  • 433
  • 4
  • 17
0

Use this code

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val inflater: LayoutInflater = LayoutInflater.from(this)
        val view: View = inflater.inflate(R.layout.view_blueberry, rlContainer, false)
        rlContainer.addView(view)
    }
}

Solution images looks like this Solution images looks like this

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29