2

I am trying to use Fragments without using XML files. My CustomView is being created, but its size is zero, therefore onDraw is not being called.

I already tried different approaches, such as the ones mentioned here: Custom onDraw() method not called but I am still not getting my view to be draw.

This is my CustomView class:

public class CameraView extends ConstraintLayout {

    View view;

    public CameraView(Context context) {
        super(context);
        initViews();
        initLayouts();
    }

    void initViews() {
        view = new View(this.getContext());
        view.setBackgroundColor(Color.RED);
        addView(view);
    }

    void initLayouts() {
        // --------------------------------------------------
        // use Android layout params to position subviews
        // within this custom view class
        // --------------------------------------------------

        view.setId(View.generateViewId());
        view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_CONSTRAINT, LayoutParams.MATCH_CONSTRAINT));

        ConstraintSet set = new ConstraintSet();
        set.connect(view.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
        set.connect(view.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
        set.connect(view.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
        set.connect(view.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
        set.applyTo(this);
    }
}

This is the Fragment Class:

public class CameraFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        CameraView customView = new CameraView(getContext());
        customView(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return customView;
    }
}

I notice that onCreateView the ViewGroup container parameter, according to the documentation, can be used to generate the LayoutParams of the view.

ViewGroup container: If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.

But I am not sure how I suppose to do it, in order to solve my problem.

Also, I tried another approach as well. Instead creating my customView on the method onCreateView, I created a container and added my customView latter, as in this post, by doing:

public class CameraFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        return linearLayout;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        CameraView cameraView = new CameraView(getContext());
        cameraView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        LinearLayout linearLayout = (LinearLayout) view;
        linearLayout.addView(cameraView);
    }

But it also didn't work.


EDIT:

whats the output of adb shell dumpsys activity top?

TASK com.qwantech.blupper id=914
  ACTIVITY com.qwantech.blupper/.MainActivity 7fbc9c7 pid=18837
    Local FragmentActivity 83a63ad State:
      mCreated=truemResumed=true mStopped=false mReallyStopped=false
      mLoadersStarted=true
    Active Fragments in 27f7663:
      #0: CameraFragment{9e9c260 #0 id=0x1}
        mFragmentId=#1 mContainerId=#1 mTag=null
        mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
        mAdded=true mRemoving=false mFromLayout=false mInLayout=false
        mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
        mRetainInstance=false mRetaining=false mUserVisibleHint=true
        mFragmentManager=FragmentManager{27f7663 in HostCallbacks{8013319}}
        mHost=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
        mContainer=android.support.constraint.ConstraintLayout{46a8fde V.E...... ........ 0,0-1536,2000 #1}
        mView=android.widget.LinearLayout{ce915bf V.E...... ......I. 0,0-0,0}
        mInnerView=android.widget.LinearLayout{ce915bf V.E...... ......I. 0,0-0,0}
        Child FragmentManager{498a48c in CameraFragment{9e9c260}}:
          FragmentManager misc state:
            mHost=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
            mContainer=android.support.v4.app.Fragment$2@768e7d5
            mParent=CameraFragment{9e9c260 #0 id=0x1}
            mCurState=5 mStateSaved=false mDestroyed=false
    Added Fragments:
      #0: CameraFragment{9e9c260 #0 id=0x1}
    FragmentManager misc state:
      mHost=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
      mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
      mCurState=5 mStateSaved=false mDestroyed=false
    View Hierarchy:
      com.android.internal.policy.PhoneWindow$DecorView{278f248 V.E..... ... 0,0-1536,2048}
        android.widget.LinearLayout{20bcbea V.E..... ... 0,0-1536,2048}
          android.view.ViewStub{8234edb G.E..... ... 0,0-0,0 #102046a android:id/action_mode_bar_stub}
          android.widget.FrameLayout{1717d78 V.E..... ... 0,48-1536,2048}
            android.support.v7.widget.FitWindowsLinearLayout{45d2c51 V.E..... ... 0,0-1536,2000 #7f0b004a app:id/action_bar_root}
              android.support.v7.widget.ViewStubCompat{6d2bcb6 G.E..... ... 0,0-0,0 #7f0b004b app:id/action_mode_bar_stub}
              android.support.v7.widget.ContentFrameLayout{7fdfdb7 V.E..... ... 0,0-1536,2000 #1020002 android:id/content}
                android.support.constraint.ConstraintLayout{46a8fde V.E..... ... 0,0-1536,2000 #1}
                  android.widget.LinearLayout{ce915bf V.E..... ... 0,0-0,0}
                    com.qwantech.blupper.CameraView{cfcf924 V.E..... ... 0,0-0,0}
                      android.view.View{1773c8d V.ED.... ... 0,0-0,0 #2}
        android.view.View{645ee42 V.ED.... ... 0,0-1536,48 #102002f android:id/statusBarBackground}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ConstraintLayout container = new ConstraintLayout(getApplicationContext());
        container.setId(View.generateViewId());
        container.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        container.setBackgroundColor(Color.CYAN);
        setContentView(container);

        CameraFragment f = CameraFragment.newInstance();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(container.getId(), f);
        fragmentTransaction.commit();

    }
}
Eduardo Reis
  • 1,691
  • 1
  • 22
  • 45
  • whats the output of `adb shell dumpsys activity top`? – pskink May 30 '17 at 04:19
  • @pskink, I added it to the question description. – Eduardo Reis May 30 '17 at 12:49
  • 1
    `android.support.constraint.ConstraintLayout{46a8fde V.E..... ... 0,0-1536,2000 #1}` what do you need it for? cannot you just use a `FrameLayout`? also, the only child of that `ConstraintLayout` it is a `LinearLayout`, what for? as you can see `CL` has size `0,0-1536,2000` and `LL` `0,0-0,0` – pskink May 30 '17 at 12:52
  • Thank you a lot. Now I got my red screen! I was using a Constraint Layout on my main activity. I was using it to test another widget I was testing. I probably have to check that widget again to see if I had the constraints set. Thank for the tip of using frame layout in cases I have only one child. I am new on Android Dev. – Eduardo Reis May 30 '17 at 13:17
  • sure, no problem – pskink May 30 '17 at 13:21

0 Answers0