Topic locked

Hiding a UI element when target is not detected.

December 21, 2014 - 11:57pm #1

Hi Everyone,

I am working on Android with 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();
		}

	}

 

Hiding a UI element when target is not detected.

December 22, 2014 - 5:02am #4

Glad to help ;-)

Hiding a UI element when target is not detected.

December 22, 2014 - 4:23am #3

I managed to solve the problem by myself and wanted to answer it here. Really glad to see your reply Alessandro. Exactly like you suggested I realised that its 

state.getNumTrackableResults());

//which shows if the target is being detected or not. Therefore by adding the following code

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

displayMessage("None");

lastTrackableId = -1;

}

//add this just before the for loop - 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 this value.

This did the trick for me. Thanks a lot for your prompt reply  :)


 

Hiding a UI element when target is not detected.

December 22, 2014 - 1:24am #2

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”). 

Good to hear that was helpful ;-)

 

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. 

In general, you can know / keep track of any target currently being tracked (or not) by checking the State, as the article above already explains:

for(int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++)
        {
            // Get the trackable:
            const QCAR::TrackableResult* result = state.getTrackableResult(tIdx);
            const QCAR::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);  based on these information, you can build your logic in Java to enable/disable your UI elements.

 

Topic locked