0

I have XML file

<ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:fillViewport="false">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mainView"  
        tools:context="${relativePackage}.${activityClass}" >

       <items.Circle
            android:id="@+id/circle"
            android:layout_width="300dp"
            android:layout_height="300dp" />
    </RelativeLayout>
</ScrollView>

In code I'm trying to add Views to my relativeLayout. I want both views displayed but I got this error The specified child already has a parent. you must call removeview() on the child's parent first. Classes circle and rectView extend View. Is there a way to display both views on a screen?

public class ProfileScreen extends FragmentActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_screen);
        RelativeLayout rLView = (RelativeLayout) findViewById(R.id.mainView);

        rectView myView = new rectView(this);
        rLView.addView(myView,Math.round(Data.displayResolution.widthPixels),Math.round(Data.displayResolution.heightPixels*2));   

        Circle circle = (Circle) findViewById(R.id.circle);
        rLView.addView(circle,500,500); 
    }
M. Krsak
  • 13
  • 2

1 Answers1

1

Is there a way to display both views on a screen?

Sure. Leave Circle alone. Or, change its LayoutParams if needed. You cannot add it as a child of the RelativeLayout, because it already is a child of the RelativeLayout.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank's for the answer. When I leave circle alone it won't display on a screen. Maybe it's under the myView. – M. Krsak Feb 28 '16 at 16:40
  • @M.Krsak: It will definitely be under it from a Z axis standpoint, insofar as later children of the `RelativeLayout` are higher on the Z axis than are earlier children. If you need to change the Z axis ordering, you would need to remove the `Circle` from the `RelativeLayout` before adding it again. – CommonsWare Feb 28 '16 at 16:42