Waqas Khalid Obeidy
Waqas Khalid Obeidy

Reputation: 676

Hiding a UI element when target is not detected using Vuforia's Java API

I am working on Android with Vuforia's Java API. I want to show a button when the marker is detected and hide it when the marker is not detected just like the teapot. I can successfully show the button when any target (chips and stones) are detected. Thanks to the tutorial provided by Vuforia (“triggering-ui-events-when-target-detected”).

However, now that I can show the hidden button once the target is detected, it doesn’t hide when the target is no longer available. I am assuming that I might need to get the TrackerManager instance just like doLoadTrackersData() and doUnloadTrackersData() methods and find if the mCurrentDataset is active or not. However so far, I have failed miserably understanding this issue. Kindly help…. My onResume() code is below:

@Override
    protected void onResume() {
        Log.d(LOGTAG, "onResume");
        super.onResume();
        displayMessageHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                TrackerManager tManager = TrackerManager.getInstance();
                ImageTracker imageTracker = (ImageTracker) tManager
                        .getTracker(ImageTracker.getClassType());

                String text = (String) msg.obj;

                Toast.makeText(getApplicationContext(), text,
                        Toast.LENGTH_SHORT).show();


                    if (text.equalsIgnoreCase("stones")) {
                        Log.e("Is it Stones", text);
                        b3.setBackgroundResource(R.drawable.up);
                        b3.startAnimation(shake);
                        b3.setVisibility(View.VISIBLE);
                    } else if (text.equalsIgnoreCase("chips")) {
                        Log.e("Is it chips", text);
                        b3.setBackgroundResource(R.drawable.up);
                        b3.startAnimation(shake);
                        b3.setVisibility(View.VISIBLE);

                    } else {
                        Log.e("ELSE", text);
                        b3.setVisibility(View.GONE);
                    }
                }

        };

        // This is needed for some Droid devices to force portrait
        if (mIsDroidDevice) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        try {
            vuforiaAppSession.resumeAR();
        } catch (SampleApplicationException e) {
            Log.e(LOGTAG, e.getString());
        }

        // Resume the GL view:
        if (mGlView != null) {
            mGlView.setVisibility(View.VISIBLE);
            mGlView.onResume();
        }

    }

Upvotes: 0

Views: 876

Answers (1)

Waqas Khalid Obeidy
Waqas Khalid Obeidy

Reputation: 676

OLRITE... I managed to solve the problem. Let me share the solution so that it might prove helpful for others.

In general, you can know / keep track of any target currently being tracked (or not) by checking the State. So in the ImageTargetRenderer.java's renderFrame().

for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++) {
            TrackableResult result = state.getTrackableResult(tIdx);
            Trackable trackable = result.getTrackable();

So, if a Trackable is listed in the state (state.getTrackable( index ) and state.getNumTrackableResults() ), you will know that it is currently tracked by Vuforia, and if it is not listed there, you will know that the Trackable has been lost (not available);

Therefore by adding the following code just before the for loop defined above -

if (state.getNumTrackableResults() == 0) {

displayMessage("None");

lastTrackableId = -1;

}

the displayMessage method then does the rest of the work. In the ImageTargets.java we can hide the UI view(button in my case / b3.setVisibility(View.GONE);) by using the value we receives.

This is how my onResume lookslike:

@Override
    protected void onResume() {
        Log.d(LOGTAG, "onResume");
        super.onResume();
        displayMessageHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                String text = (String) msg.obj;

                if (text.equalsIgnoreCase("stones")) {
                    Log.e("Is it Stones", text);
                    b3.setBackgroundResource(R.drawable.up);
                    b3.startAnimation(shake);
                    b3.setVisibility(View.VISIBLE);
                } else if (text.equalsIgnoreCase("chips")) {
                    Log.e("Is it chips", text);
                    b3.setBackgroundResource(R.drawable.up);
                    b3.startAnimation(shake);
                    b3.setVisibility(View.VISIBLE);
                } else {
                    b3.setVisibility(View.GONE);
                }
            }

        };

This did the trick for me...

Upvotes: 2

Related Questions