[Java for Android App Development] DHD22800:
New Tutorial :P :D

New Tutorial here [emoji14] :D

I will show you how to use Quick Tips in your application.

Declare these global variables:

QuickTipPopup mQuickTip;
private static int mScreenOrientation = -1;
private boolean bShowQuickTip;
private View mShowQuickTip;
Runnable mQuickTipRunnable;
Handler mHandler;

Make sure you will import QuickTipPopup from here:

com.htc.widget.quicktips.QuickTipPopup

Here is the method that "init" the QuickTipPupup:

/*
* Here is the initQuickTips method
* The variable text allow you to setup a custom text
* The variable pref is passed the dismissQuicktipsInSystemSetting method (will see it later)
* This method is currently runned on a fragment..if you want to run in an Activity, simply replace "getActivity()" with "YourActivity.this" like "MainActivity.this"
*/
private void initQuickTips(String text, String pref)
{
mScreenOrientation = -1;
mQuickTip = new QuickTipPopup(getActivity());
mQuickTip.setText(text);
mQuickTip.setImage(null);
mQuickTip.setOnUserDismissListener(new PopupBubbleWindow.OnUserDismissListener() {

public void onDismiss()
{
dismissQuicktipsInSystemSetting(pref);
}
});
if (mQuickTip != null)
mQuickTip.setExpandDirection(0);
getActivity().setRequestedOrientation(mScreenOrientation);
}

Here is both (from Fragment and Activity) method dismissQuicktipsInSystemSetting(String pref):

Activity:

/*
* This method use SharedPreferences for dismissing your quick tip,
* it simply saves a pref and set it to false, so the tip will not appear a second time.
*/
private void dismissQuicktipsInSystemSetting(String pref)
{
Log.i(YourActivity.class.getSimpleName(), "dismiss QT: " + pref);
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
prefs.edit().putBoolean(pref, false).commit();
}

----------

Fragment:

/*
* This method use SharedPreferences for dismissing your quick tip,
* it simply saves a pref and set it to false, so the tip will not appear a second time.
*/
private void dismissQuicktipsInSystemSetting(String pref)
{
Log.i(getActivity().class.getSimpleName(), "dismiss QT: " + pref);
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.edit().putBoolean(pref, false).commit();
}

Here the method that show your QuickTip:

Activity:

/*
* This method show your QuickTip
* If only a variable is null the QuickTip will not show
*/
private void showQuickTips(String pref)
{
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
bShowQuickTip = prefs.getBoolean(pref, true);
Log.d(YourActivity.class.getSimpleName(), " show QT:" + bShowQuickTip + ", mShowTip: " + mShowQuickTip + ", mQuickTip: " + mQuickTip);
if (bShowQuickTip && mShowQuickTip != null && mQuickTip != null)
{
if (mQuickTipRunnable == null)
mQuickTipRunnable = new Runnable() {
public void run()
{
mQuickTip.showAsDropDown(mShowQuickTip, 0, 0);
}
};
mHandler.postDelayed(mQuickTipRunnable, 200L);
}
}

---------

Fragment:

/*
* This method show your QuickTip
* If only a variable is null the QuickTip will not show
*/
private void showQuickTips(String pref)
{
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
bShowQuickTip = prefs.getBoolean(pref, true);
Log.d(getActivity().class.getSimpleName(), " show QT:" + bShowQuickTip + ", mShowTip: " + mShowQuickTip + ", mQuickTip: " + mQuickTip);
if (bShowQuickTip && mShowQuickTip != null && mQuickTip != null)
{
if (mQuickTipRunnable == null)
mQuickTipRunnable = new Runnable() {
public void run()
{
mQuickTip.showAsDropDown(mShowQuickTip, 0, 0);
}
};
mHandler.postDelayed(mQuickTipRunnable, 200L);
}
}

Now we must show our QuickTip, here is the way:

/*
* first we set mShowQuickTip to null
* next we set mShowQuickTip to a view
*/
mShowQuickTip = null;
mShowQuickTip = SomeView; //SomeView is the View that the popup will "indicate"
initQuickTips("This is the QuickTip!", "firstTimeQuickTip");
showQuickTips("firstTimeQuickTip");

The result will be:

The background, text color and X button are automatically build from Quick Tip.

You can use ActionBarItemView as view for QuickTip (see screenshot).

Hope this will help :D