0

I want load some Custom Views within my RelativeLayout but don't know how. The code I've tried doesn't work hence does anyone know how to do this correctly?

XML

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/textView0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="20dp"
            style="@android:style/TextAppearance.Medium" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="2dp"
            style="@android:style/TextAppearance.Medium"
            android:layout_below="@id/textView0" />
        <View
            android:id="@+id/drawing0"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_below="@id/textView1" />
        <View
            android:id="@+id/drawing1"                
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="2dp"
            android:layout_below="@id/drawing0" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Large"
            android:layout_below="@id/drawing1" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Large"
            android:layout_marginStart="10dp"
            android:layout_below="@id/drawing1"
            android:layout_toEndOf="@id/textView2" />
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Medium"
            android:layout_below="@id/textView3" />
        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Medium"
            android:layout_marginBottom="30dp"
            android:layout_below="@id/textView4" />
    </RelativeLayout>

</ScrollView>

Shapes.java

public class Shapes extends android.support.v4.app.Fragment {

    public Shapes() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.shapes, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        //blue shape drawing
        View cv0 = (View)v.findViewById(R.id.drawing0);
        v.addView(new BlueShape(getActivity()));

        //green shape drawing
        View cv1 = (View)v.findViewById(R.id.drawing1);
        v.addView(new GreenShape(getActivity()));

        super.onActivityCreated(savedInstanceState);
    }
}

BlueShape.java

public class BlueShape extends View {
    private final Paint mBlue = new Paint();

    ...

}

GreenShape.java

public class GreenShape extends View {
    private final Paint mGreen = new Paint();

    ...

}
wbk727
  • 8,017
  • 12
  • 61
  • 125

3 Answers3

1

cv0, cv1 are not ViewGroup. Try to change cv0, cv1 to ViewGroup and move onActivityCreated's code to onViewCreated callback method.

here is reference site. android/view/ViewGroup

jihoon kim
  • 1,270
  • 1
  • 16
  • 22
1

I think that the problem is that you are using v.addView() instead of

cv0.addView()

But I would suggest to use the following instead of your View:

 <LinearLayout
            android:id="@+id/drawing1"                
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="2dp"
            android:layout_below="@id/drawing0" />

then, inside the Java code:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    LinearLayout cv0 = (LinearLayout)getView().findViewById(R.id.drawing0);
    cv0.addView(new BlueShape(getActivity()), 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
wbk727
  • 8,017
  • 12
  • 61
  • 125
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
1

refer to https://developer.android.com/guide/topics/ui/custom-components.html

specifically under

Modifying an Existing View Type

  1. Use the Custom Component

you can change the xml in your RelativeLayout

<View... 

to

<your.package.GreenShape...

if you do not know beforehand what the View is going to be, then change it to

<FrameLayout... instead of <View...

and then add the child (GreenShape or BlueShape) programmatically to that Frame layout

Community
  • 1
  • 1
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
  • I already did that but need to reuse the layout to show dozens of other different views hence doing that is not a good idea. – wbk727 Aug 14 '17 at 15:23
  • @MacaronLover does changing drawing0 and drawing1 from View to FrameLayout work? you need a layout to contain child views. also the BlueShape/GreenShape may have width and height of 0dp (hence it's not showing up) – Angel Koh Aug 14 '17 at 15:30