Android Open Source - ElyTheme Clickable Toast






From Project

Back to project page ElyTheme.

License

The source code is released under:

GNU General Public License

If you think the Android project ElyTheme listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.afollestad.silk.views;
//from  ww  w  .java2  s  .c o  m
import android.app.Activity;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.afollestad.silk.R;

/**
 * Almost a replica of the stock {@link android.widget.Toast}, but has an OnClickListener that notifies you when the toast is tapped.
 *
 * @author Aidan Follestad (afollestad)
 */
public class ClickableToast {

    public final static int LENGTH_SHORT = 0;
    public final static int LENGTH_LONG = 1;

    private final Handler mHandler;
    private final View mParent;
    private final int mDuration;
    private final View mView;
    private View.OnClickListener mListener;
    private PopupWindow mWindow;
    private int mGravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
    private int mVerticalOffset;

    private ClickableToast(Activity context, View parent, int duration) {
        mHandler = new Handler();
        mParent = parent;
        mDuration = duration;
        mView = context.getLayoutInflater().inflate(R.layout.clickable_toast, null);
        mVerticalOffset = context.getResources().getDimensionPixelSize(R.dimen.toast_offset);

        assert mView != null;
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
                if (mListener != null) mListener.onClick(v);
            }
        });
    }

    public ClickableToast setText(int text) {
        ((TextView) mView.findViewById(android.R.id.message)).setText(text);
        return this;
    }

    public ClickableToast setGravity(int gravity) {
        mGravity = gravity;
        return this;
    }

    public ClickableToast setVerticalOffset(int verticalOffset) {
        mVerticalOffset = verticalOffset;
        return this;
    }

    public ClickableToast setOnClickListener(View.OnClickListener listener) {
        mListener = listener;
        return this;
    }

    public static ClickableToast makeText(Activity context, View parent, int text, int duration) {
        return new ClickableToast(context, parent, duration).setText(text);
    }

    public boolean isShowing() {
        return mWindow != null && mWindow.isShowing();
    }

    public void show() {
        mWindow = new PopupWindow(mView, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mWindow.setAnimationStyle(R.style.ClickableToastAnimation);
        mWindow.showAtLocation(mParent, mGravity, 0, mVerticalOffset);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                cancel();
            }
        }, mDuration == Toast.LENGTH_LONG ? 3000 : 1500);
    }

    public void cancel() {
        if (mWindow == null) return;
        mWindow.dismiss();
    }
}




Java Source Code List

com.afollestad.cardsui.ApplicationTest.java
com.afollestad.cardsui.CardAdapter.java
com.afollestad.cardsui.CardBase.java
com.afollestad.cardsui.CardCenteredHeader.java
com.afollestad.cardsui.CardCompressed.java
com.afollestad.cardsui.CardCursorAdapter.java
com.afollestad.cardsui.CardHeader.java
com.afollestad.cardsui.CardListView.java
com.afollestad.cardsui.CardTheme.java
com.afollestad.cardsui.Card.java
com.afollestad.silk.ApplicationTest.java
com.afollestad.silk.SilkComparable.java
com.afollestad.silk.SilkCursorItem.java
com.afollestad.silk.Silk.java
com.afollestad.silk.activities.SilkDrawerActivity.java
com.afollestad.silk.adapters.ScrollStatePersister.java
com.afollestad.silk.adapters.SilkAdapter.java
com.afollestad.silk.adapters.SilkCursorAdapter.java
com.afollestad.silk.adapters.SilkSpinnerAdapter.java
com.afollestad.silk.dialogs.SilkDialog.java
com.afollestad.silk.fragments.feed.SilkCursorFeedFragment.java
com.afollestad.silk.fragments.feed.SilkFeedFragment.java
com.afollestad.silk.fragments.list.SilkCursorListFragment.java
com.afollestad.silk.fragments.list.SilkListFragment.java
com.afollestad.silk.http.SilkHttpBase.java
com.afollestad.silk.http.SilkHttpBody.java
com.afollestad.silk.http.SilkHttpCallback.java
com.afollestad.silk.http.SilkHttpClient.java
com.afollestad.silk.http.SilkHttpException.java
com.afollestad.silk.http.SilkHttpHeader.java
com.afollestad.silk.http.SilkHttpResponse.java
com.afollestad.silk.utilities.IOUtils.java
com.afollestad.silk.utilities.TimeUtils.java
com.afollestad.silk.views.ClickableToast.java
com.afollestad.silk.views.list.OnSilkScrollListener.java
com.afollestad.silk.views.list.SilkGridView.java
com.afollestad.silk.views.list.SilkListView.java
com.afollestad.silk.views.time.SilkDatePicker.java
it.gcaliendo.elytheme.Adw.java
it.gcaliendo.elytheme.ApplicationTest.java
it.gcaliendo.elytheme.DocksProvider.java
it.gcaliendo.elytheme.Docks.java
it.gcaliendo.elytheme.IconActivity.java
it.gcaliendo.elytheme.IconPack.java
it.gcaliendo.elytheme.IconsProvider.java
it.gcaliendo.elytheme.Icons.java
it.gcaliendo.elytheme.RequestActivity.java
it.gcaliendo.elytheme.ThemeActivity.java
it.gcaliendo.elytheme.Wallpaper.java
it.gcaliendo.elytheme.fragments.FragmentAbout.java
it.gcaliendo.elytheme.fragments.FragmentContact.java
it.gcaliendo.elytheme.fragments.FragmentExtras.java
it.gcaliendo.elytheme.fragments.FragmentTheme.java
it.gcaliendo.elytheme.helper.AppInfo.java
it.gcaliendo.elytheme.iconfragment.IconFragmentGames.java
it.gcaliendo.elytheme.iconfragment.IconFragmentLatest.java
it.gcaliendo.elytheme.iconfragment.IconFragmentMisc.java
it.gcaliendo.elytheme.iconfragment.IconFragmentPlay.java
it.gcaliendo.elytheme.iconfragment.IconFragmentSystem.java