Balasubramanian
Balasubramanian

Reputation: 5520

Unable to change visibility of view other than onCreate method

I am unable to change the view visibility inside other function rather than onCreate method. Its working only at time of onCreate is called.

public class CameraXActivity extends AppCompatActivity {
    ...
    public Group fipGroup;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camerax_layout); // Created xml using constraintLayout.

        //intial setup
        fipGroup = (Group)findViewById(R.id.fip_group);
        startCamera();

        //hideFipGroup(); <<--- This is working
    }

    private void hideFipGroup() {
        Log.d(TAG, "=== hideFipGroup ===");
        fipGroup.setVisibility(View.INVISIBLE);
    }

    private void startCamera() {
        CameraX.unbindAll();
        preview = setPreview(); 
        imageAnalysis = setImageAnalysis();

        //bind to lifecycle:
        CameraX.bindToLifecycle(this, preview , imageAnalysis);
        preview.enableTorch(true);
    }

    private ImageAnalysis setImageAnalysis() {
        hideFipGroup() // This is working
        imageAnalysis.setAnalyzer(
                new ImageAnalysis.Analyzer() {
                    @Override
                    public void analyze(ImageProxy image, int rotationDegrees) {
                        hideFipGroup() // Exactly, Failed at this place.
                    }
                }
        )
    }

}

Edit Update:

It's failing to update on the analyze method of imageAnalysis. Just to test, called toast message which is showing on the UI. But am not able to control the UI.

private void raiseToast(String msg) {

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 500);
                toast.show();
            }
        });
    }

Upvotes: 0

Views: 306

Answers (3)

Balasubramanian
Balasubramanian

Reputation: 5520

The problem is with Group visibility on constraint Layout.

private void setGroupVisiblity(Group group, int visibility) {
   group.setVisibility(visibility);
   group.requestLayout();
}

or manually turn off each views in the group:

private void setGroupVisibility(ConstraintLayout layout, Group group, Integer visibility) {
     int[] refIds = group.getReferencedIds();
     for (int id : refIds) {
          layout.findViewById(id).setVisibility(visibility);
     }
}

or upgrade to ConstraintLayout version 2.0.0 beta 6

source: Can't set visibility on constraint group

Upvotes: 1

Dennis Nguyen
Dennis Nguyen

Reputation: 474

you should check the function you called. Remember that you can only update the UI in the main thread

Upvotes: 0

Daniel.Wang
Daniel.Wang

Reputation: 2496

Please try to view.post or runOnUIThread method.

so like this.

private ImageAnalysis setImageAnalysis() {
    hideFipGroup() // This is working
    imageAnalysis.setAnalyzer(
            new ImageAnalysis.Analyzer() {
                @Override
                public void analyze(ImageProxy image, int rotationDegrees) {
                    fipGroup.post(new Runnable() {
                       @Override
                       public void run() {
                           hideFipGroup();  // Exactly, Failed at this place.
                       }
                    });
                }
            }
    )
}

Or you can simply call view.post() method in hideFipGroup() method

private void hideFipGroup() {
    Log.d(TAG, "=== hideFipGroup ===");
    fipGroup.post(new Runnable() {
        @Override
        public void run() {
            fipGroup.setVisibility(View.INVISIBLE);
        }
    });
}

Upvotes: 0

Related Questions