3

I can't add child views to a ViewGroup that I added programmatically.

First I create a GroupView (_view) and add it to the Background RelativeLayout (_root) like this...

            _view = new ViewGroup(MainActivity.this) {
                @Override
                protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
                }
            };

            _view.setBackgroundColor(0xFF00FF00);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
            params.setMargins(10, 0, 10, 0);
            _view.setLayoutParams(params);
            _view.setTranslationX(0);
            _view.setTranslationY(positionY - 80);
            selectView(_view);

            _root.addView(_view);

            _view.setTag(R.string.viewID, "" + viewNum);
            _view.setTag(R.string.viewSelected, "" + 0);
            viewNum ++;

That works fine. Then I try to add a view to that new GroupView (_view) like this...

            ImageView dragIcon = new ImageView(MainActivity.this);
            dragIcon.setImageResource(R.drawable.drabicon);
            RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            imgParams.setMargins(0,0,30,0);
            dragIcon.setLayoutParams(imgParams);
            dragIcon.setTranslationY(_view.getTranslationY());

            _view.addView(dragIcon); // This Is What I Want, But This Doesn't Work.

            //_root.addView(dragIcon); // Don't Want This, But This Works.

But it doesn't get added, or it isn't visible or something. Now if i use the Background RelativeLayout (_root) like this _root.addView(dragIcon); It works fine, the ImageView gets added.

So why doesn't work with the _view ViewGroup, _view.addView(dragIcon)?

Sufian
  • 6,405
  • 16
  • 66
  • 120
Axonshi 123
  • 123
  • 2
  • 10
  • avoid using _VarName for instance variable, even android studio code inspect will suggest you to Use m prefix for non-public and non-static fields. – MRX Aug 02 '16 at 21:15
  • Make sure that you are adding dragIcon to _view an and after that _view to _root. – Aditi Aug 03 '16 at 04:27

1 Answers1

2

Since you are using ViewGroup, which is an abstract class you should implement the abstract method onLayout in which each layout method of each child of the viewgroup must be called. For more info check this answer.

I think you can use FrameLayout which is a subclass ViewGroup. It has the onLayout method implemented so you dont have the burden to implementing it again.

Here is working code for ViewGroup where I plainly called the layout method of the only one child (dragIcon).

ExampleActivity.java

public class ExampleActivity extends AppCompatActivity {

    ViewGroup _view ;
    RelativeLayout _root;
    ImageView dragIcon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
        _root = (RelativeLayout) findViewById(R.id.exampleLayout);

        _view  = new ViewGroup(ExampleActivity.this) {
            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b) {
                dragIcon.layout(l, t, r, b); //CHECK THIS
            }
        };

        _view .setBackgroundColor(0xFF00FF00);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
        params.setMargins(10, 0, 10, 0);
        _view .setLayoutParams(params);
        _view .setTranslationX(0);
        _view .setTranslationY(80);
       // selectView(_view);

        _root.addView(_view );

        _view .setTag(R.string.app_name, "" );
        _view .setTag(R.string.app_name, "" + 0);


        dragIcon = new ImageView(ExampleActivity.this);
        dragIcon.setImageResource(R.drawable.heart1);
        RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        _view .addView(dragIcon); // This Is What I Want, But This Doesn't Work.

        //sample_root.addView(dragIcon); // Don't Want This, But This Works.
    }

}

activity_example.xml

<?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:id="@+id/exampleLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.anil.raceorganizer.ExampleActivity">
</RelativeLayout>
Community
  • 1
  • 1
Anil P Babu
  • 1,235
  • 3
  • 26
  • 47