Example usage for android.widget Toast setDuration

List of usage examples for android.widget Toast setDuration

Introduction

In this page you can find the example usage for android.widget Toast setDuration.

Prototype

public void setDuration(@Duration int duration) 

Source Link

Document

Set how long to show the view for.

Usage

From source file:com.scoreloop.android.coreui.BaseActivity.java

void showToast(final String message) {
    final View view = getLayoutInflater().inflate(R.layout.sl_dialog_custom, null);
    ((TextView) view.findViewById(R.id.message)).setText(message);
    final Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(view);/* ww w .  j  a v  a2  s .  c  o  m*/
    toast.show();
}

From source file:org.apps8os.motivator.ui.MoodQuestionActivity.java

/** Called when the activity is first created. */
@Override/*from  ww w. j a v  a2 s . com*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mood_question);
    mDayDataHandler = new DayDataHandler(this);

    mCardsViewPagerEnergy = (ViewPager) findViewById(R.id.mood_question_viewpager_cards);
    mCardsViewPagerEnergy.setAdapter(new ImagesPagerAdapter(mImages1, mTitles1, this));

    setViewPager(mCardsViewPagerEnergy);

    mEnergyLevelText = (TextView) findViewById(R.id.mood_question_energylevel_textview);
    mEnergyLevelText.setText(mCardsViewPagerEnergy.getAdapter().getPageTitle(DEFAULT_MOOD_SELECTION));

    // Set an OnPageChangeListener to the ViewPager, change the text when a page is selected
    mCardsViewPagerEnergy
            .setOnPageChangeListener(new ViewPageChangeListener(mCardsViewPagerEnergy, mEnergyLevelText));

    mCardsViewPagerMood = (ViewPager) findViewById(R.id.mood_question_viewpager_cards2);
    mCardsViewPagerMood.setAdapter(new ImagesPagerAdapter(mImages2, mTitles2, this));

    setViewPager(mCardsViewPagerMood);

    // Get the text field for energy level
    mMoodLevelText = (TextView) findViewById(R.id.mood_question_moodlevel_textview);
    mMoodLevelText.setText(mCardsViewPagerMood.getAdapter().getPageTitle(DEFAULT_MOOD_SELECTION));

    // Set an OnPageChangeListener to the ViewPager, change the text when a page is selected
    mCardsViewPagerMood
            .setOnPageChangeListener(new ViewPageChangeListener(mCardsViewPagerMood, mMoodLevelText));
    final LayoutInflater inflater = getLayoutInflater();
    LinearLayout buttons = (LinearLayout) findViewById(R.id.mood_question_buttons);

    // Change the OK button to next button which opens the checking events activity if there are events to check.
    final Bundle extras = getIntent().getExtras();
    if (extras != null && extras.getBoolean(EventDataHandler.EVENTS_TO_CHECK, false)) {
        Button nextButton = (Button) inflater.inflate(R.layout.element_ok_button, buttons, false);
        nextButton.setText(getString(R.string.next));
        nextButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                saveMood(v);
                Intent intent = new Intent(MoodQuestionActivity.this, CheckEventsActivity.class);
                intent.putExtra(MotivatorEvent.YESTERDAYS_EVENTS,
                        extras.getParcelableArrayList(MotivatorEvent.YESTERDAYS_EVENTS));
                startActivity(intent);
                NotificationManager notificationManager = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                notificationManager.cancel(NotificationService.NOTIFICATION_ID_MOOD);
                finish();
            }

        });
        buttons.addView(nextButton);
    } else {

        Button okButton = (Button) inflater.inflate(R.layout.element_ok_button, buttons, false);
        okButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                saveMood(v);

                String toastMsg;
                if (mCardsViewPagerMood.getCurrentItem() > 2) {
                    toastMsg = getString(R.string.questionnaire_done_toast_good_mood);
                } else if (mCardsViewPagerMood.getCurrentItem() == 2) {
                    toastMsg = getString(R.string.questionnaire_done_toast_ok_mood);
                } else {
                    toastMsg = getString(R.string.questionnaire_done_toast_bad_mood);
                }
                View toastLayout = (View) inflater.inflate(R.layout.element_mood_toast,
                        (ViewGroup) findViewById(R.id.mood_toast_layout));
                TextView toastText = (TextView) toastLayout.findViewById(R.id.mood_toast_text);
                toastText.setText(toastMsg);
                toastText.setTextColor(Color.WHITE);

                Toast questionnaireDone = new Toast(getApplicationContext());
                questionnaireDone.setDuration(Toast.LENGTH_SHORT);
                questionnaireDone.setView(toastLayout);
                questionnaireDone.show();

                NotificationManager notificationManager = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                notificationManager.cancel(NotificationService.NOTIFICATION_ID_MOOD);

                finish();
            }

        });
        buttons.addView(okButton);
    }
}

From source file:de.tudarmstadt.informatik.secuso.phishedu2.MainActivity.java

@Override
public void displayToastScore(int score) {
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.fragment_toast_score,
            (ViewGroup) findViewById(R.id.toast_layout_root));

    String scoreString = Integer.toString(score);

    TextView text = (TextView) layout.findViewById(R.id.text);
    if (score < 0) {
        // red/*from w w w.  ja v  a2  s .c o  m*/
        text.setTextColor(Color.rgb(135, 0, 0));
    } else {
        // green
        text.setTextColor(Color.rgb(0, 135, 0));
        scoreString = "+ " + score;
    }

    text.setText(getString(R.string.scoreString, scoreString)); // scoreString + " Punkte"
    text.setTypeface(Typeface.DEFAULT_BOLD);
    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();
}

From source file:org.apps8os.motivator.ui.AddGoalActivity.java

/**
 * Sets up the listeners for the buttons.
 *//*from w  w w  . ja v  a2s.  c o m*/
private void setButtons() {
    final Button nextButton = (Button) findViewById(R.id.questions_next_button);
    nextButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
        }
    });

    final Button previousButton = (Button) findViewById(R.id.questions_previous_button);
    previousButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
        }
    });
    mCompleteButton = (Button) findViewById(R.id.questions_complete_button);
    mCompleteButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int answers[] = new int[mNumberOfQuestions];
            int amountAnswer = -2;
            for (int i = 0; i < mNumberOfQuestions; i++) {
                answers[i] = mQuestionsPagerAdapter.getFragment(i).getSelectedAnswer();
                if (i == 0) {
                    amountAnswer = mQuestionsPagerAdapter.getFragment(i).getXAmount();
                }
            }
            if (answers[0] < 2 && amountAnswer < 1) {
                View toastLayout = (View) getLayoutInflater().inflate(R.layout.element_mood_toast,
                        (ViewGroup) findViewById(R.id.mood_toast_layout));
                TextView toastText = (TextView) toastLayout.findViewById(R.id.mood_toast_text);
                toastText.setText(getString(R.string.goal_not_added));
                toastText.setTextColor(Color.WHITE);

                Toast questionnaireDone = new Toast(getApplicationContext());
                questionnaireDone.setDuration(Toast.LENGTH_LONG);
                questionnaireDone.setView(toastLayout);
                questionnaireDone.show();

            } else {
                mGoalDataHandler.insertGoal(System.currentTimeMillis(), answers[1], answers[0], amountAnswer);

                View toastLayout = (View) getLayoutInflater().inflate(R.layout.element_mood_toast,
                        (ViewGroup) findViewById(R.id.mood_toast_layout));
                TextView toastText = (TextView) toastLayout.findViewById(R.id.mood_toast_text);
                toastText.setText(getString(R.string.goal_added));
                toastText.setTextColor(Color.WHITE);

                Toast questionnaireDone = new Toast(getApplicationContext());
                questionnaireDone.setDuration(Toast.LENGTH_SHORT);
                questionnaireDone.setView(toastLayout);
                questionnaireDone.show();

            }
            finish();
        }
    });
    // Disable these buttons at start.
    mCompleteButton.setEnabled(false);
    previousButton.setEnabled(false);

    // Set up a page change listener to enable and disable buttons.
    titleIndicator.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            if (arg0 == mNumberOfQuestions - 1) {
                nextButton.setEnabled(false);
            } else {
                nextButton.setEnabled(true);
            }
            if (arg0 == 0) {
                previousButton.setEnabled(false);
            } else {
                previousButton.setEnabled(true);
            }
        }
    });
}

From source file:org.apps8os.motivator.ui.AddEventActivity.java

/**
 * Sets up the listeners for the buttons.
 *///from w w w .j  ava2 s. co  m
private void setButtons() {
    final Button nextButton = (Button) findViewById(R.id.questions_next_button);
    nextButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mViewPager.getCurrentItem() == 1
                    && mQuestionsPagerAdapter.getFragment(1).getSelectedAnswer() == 1) {
                mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 3);
            } else {
                mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
            }
        }
    });

    final Button previousButton = (Button) findViewById(R.id.questions_previous_button);
    previousButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mViewPager.getCurrentItem() == 4
                    && mQuestionsPagerAdapter.getFragment(1).getSelectedAnswer() == 1) {
                mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 3);
            } else {
                mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
            }
        }
    });
    mCompleteButton = (Button) findViewById(R.id.questions_complete_button);
    mCompleteButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int answers[] = new int[mNumberOfQuestions];
            long date = 0L;
            for (int i = 0; i < mNumberOfQuestions; i++) {
                answers[i] = mQuestionsPagerAdapter.getFragment(i).getSelectedAnswer();
                if (i == 0) {
                    if (answers[i] == 3) {
                        date = mQuestionsPagerAdapter.getFragment(i).getSelectedDate();
                    }
                }
            }
            mEventDataHandler.insertEvent(answers[0], answers[1], answers[2], answers[3], answers[4], mName,
                    date);

            View toastLayout = (View) getLayoutInflater().inflate(R.layout.element_mood_toast,
                    (ViewGroup) findViewById(R.id.mood_toast_layout));
            TextView toastText = (TextView) toastLayout.findViewById(R.id.mood_toast_text);
            toastText.setText(getString(R.string.event_added));
            toastText.setTextColor(Color.WHITE);

            Toast eventAdded = new Toast(getApplicationContext());
            eventAdded.setDuration(Toast.LENGTH_SHORT);
            eventAdded.setView(toastLayout);
            eventAdded.show();

            //Add a flag for which section the event was added to.
            SharedPreferences motivatorPrefs = getSharedPreferences(MainActivity.MOTIVATOR_PREFS, 0);
            Editor editor = motivatorPrefs.edit();
            if (answers[0] == 1) {
                editor.putInt(EVENT_ADDED, MotivatorEvent.TODAY);
            } else {
                editor.putInt(EVENT_ADDED, MotivatorEvent.PLAN);
            }
            editor.commit();
            finish();
        }
    });
    // Disable these buttons at start.
    mCompleteButton.setEnabled(false);
    previousButton.setEnabled(false);

    // Set up a page change listener to enable and disable buttons.
    titleIndicator.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            if (arg0 == mNumberOfQuestions - 1) {
                nextButton.setEnabled(false);
            } else {
                nextButton.setEnabled(true);
            }
            if (arg0 == 0) {
                previousButton.setEnabled(false);
            } else {
                previousButton.setEnabled(true);
            }
        }
    });
}

From source file:com.geomoby.geodeals.DemoService.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.geomoby_main);
    mContext = this;

    mToggle = (CompoundButton) findViewById(R.id.togglebutton);

    spref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    isCheckedStatus = spref.getBoolean("check", false); //default is false

    /*/* w w w .j  a v a2 s . c  o  m*/
     * Set up toggle status
     */
    if (!isCheckedStatus)
        mToggle.setChecked(false);
    else
        mToggle.setChecked(true);

    /*
     *  Save the tags in the GeoMoby shared preferences in private mode for the user. These tags will be used
     *  to segment your audience when creating your proximity alerts. Please make sure that they match with
     *  the ones configured in your dashboard when you create an alert.
     *  Ex: 'test' is the default tag so make sure that it is set up in your Account page
     */
    SharedPreferences mySharedPreferences = getSharedPreferences(PREF, MODE_PRIVATE);

    // Build the string of tags - empty for testing. Make sure that you create your first geofences with no tags in your dashboard.
    // Add your own logic here: "male,vip,monday"...
    String tags = "";

    // Commit the string
    SharedPreferences.Editor prefEditor = mySharedPreferences.edit();
    prefEditor.putString(SETTING_TAGS, tags);
    prefEditor.commit();

    /*
     *  Monitor the toggle - Our SDK will ensure that all services are running/stopping properly
     */
    mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked == false) {

                mToggle.setPressed(false);

                // Stop the GeoMoby tracking service
                startService(new Intent(DemoService.this, GeomobyStopService.class));

                SharedPreferences.Editor editor = spref.edit();
                editor.putBoolean("check", false);
                editor.commit();

            } else {

                mToggle.setPressed(true);

                // Start the GeoMoby tracking service
                startService(new Intent(DemoService.this, GeomobyStartService.class));

                SharedPreferences.Editor editor = spref.edit();
                editor.putBoolean("check", true);
                editor.commit();

                LayoutInflater inflater = getLayoutInflater();
                // Inflate the Layout
                View layout = inflater.inflate(R.layout.geomoby_toast,
                        (ViewGroup) findViewById(R.id.custom_toast_layout));

                // Set the Text to show in TextView
                TextView text = (TextView) layout.findViewById(R.id.textToShow);
                text.setText("GREAT! YOU ARE READY TO RECEIVE REAL-TIME NOTIFICATIONS!");
                Typeface face;
                face = Typeface.createFromAsset(getAssets(), "Bitter-Bold.otf");
                text.setTypeface(face);

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.BOTTOM, 0, 50);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        }
    });

    // Initialise GeoMoby Notification Listener
    GeomobyNotificationsReceiver receiver = new GeomobyNotificationsReceiver();
    receiver.setNotificationListener(this);
}

From source file:org.silena.main.RegistrationOld.java

private void RenderError(String massage) {

    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.toast_error, (ViewGroup) findViewById(R.id.toast_layout_root));

    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText(massage);/*from  w w w . j av  a2s . com*/

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
    return;
}

From source file:com.apptentive.android.sdk.module.engagement.interaction.fragment.SurveyFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (interaction == null) {
        getActivity().finish();/*from  w  ww .  j av a  2  s  .co m*/
    }

    List<Question> questions = interaction.getQuestions();
    answers = new LinkedHashMap<String, Object>(questions.size());

    View v = inflater.inflate(R.layout.apptentive_survey, container, false);

    TextView description = (TextView) v.findViewById(R.id.description);
    description.setText(interaction.getDescription());

    final Button send = (Button) v.findViewById(R.id.send);

    String sendText = interaction.getSubmitText();
    if (!TextUtils.isEmpty(sendText)) {
        send.setText(sendText);
    }
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Util.hideSoftKeyboard(getActivity(), view);
            boolean valid = validateAndUpdateState();
            if (valid) {
                if (interaction.isShowSuccessMessage() && !TextUtils.isEmpty(interaction.getSuccessMessage())) {
                    Toast toast = new Toast(getContext());
                    toast.setGravity(Gravity.FILL, 0, 0);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    View toastView = inflater.inflate(R.layout.apptentive_survey_sent_toast,
                            (LinearLayout) getView().findViewById(R.id.survey_sent_toast_root));
                    toast.setView(toastView);
                    TextView actionTV = ((TextView) toastView.findViewById(R.id.survey_sent_action_text));
                    actionTV.setText(interaction.getSuccessMessage());
                    int actionColor = Util.getThemeColor(getContext(),
                            R.attr.apptentiveSurveySentToastActionColor);
                    if (actionColor != 0) {
                        actionTV.setTextColor(actionColor);
                        ImageView actionIcon = (ImageView) toastView.findViewById(R.id.survey_sent_action_icon);
                        actionIcon.setColorFilter(actionColor);
                    }
                    toast.show();
                }
                getActivity().finish();

                EngagementModule.engageInternal(getActivity(), interaction, EVENT_SUBMIT);

                ApptentiveInternal.getInstance().getApptentiveTaskManager()
                        .addPayload(new SurveyResponse(interaction, answers));
                ApptentiveLog.d("Survey Submitted.");
                callListener(true);
            } else {
                Toast toast = new Toast(getContext());
                toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                View toastView = inflater.inflate(R.layout.apptentive_survey_invalid_toast,
                        (LinearLayout) getView().findViewById(R.id.survey_invalid_toast_root));
                toast.setView(toastView);
                String validationText = interaction.getValidationError();
                if (!TextUtils.isEmpty(validationText)) {
                    ((TextView) toastView.findViewById(R.id.survey_invalid_toast_text)).setText(validationText);
                }
                toast.show();
            }
        }
    });

    questionsContainer = (LinearLayout) v.findViewById(R.id.questions);
    if (savedInstanceState == null) {
        questionsContainer.removeAllViews();

        // Then render all the questions
        for (int i = 0; i < questions.size(); i++) {
            Question question = questions.get(i);
            BaseSurveyQuestionView surveyQuestionView;
            if (question.getType() == Question.QUESTION_TYPE_SINGLELINE) {
                surveyQuestionView = TextSurveyQuestionView.newInstance((SinglelineQuestion) question);
            } else if (question.getType() == Question.QUESTION_TYPE_MULTICHOICE) {
                surveyQuestionView = MultichoiceSurveyQuestionView.newInstance((MultichoiceQuestion) question);

            } else if (question.getType() == Question.QUESTION_TYPE_MULTISELECT) {
                surveyQuestionView = MultiselectSurveyQuestionView.newInstance((MultiselectQuestion) question);
            } else if (question.getType() == Question.QUESTION_TYPE_RANGE) {
                surveyQuestionView = RangeSurveyQuestionView.newInstance((RangeQuestion) question);
            } else {
                surveyQuestionView = null;
            }
            if (surveyQuestionView != null) {
                surveyQuestionView.setOnSurveyQuestionAnsweredListener(this);
                getRetainedChildFragmentManager().beginTransaction()
                        .add(R.id.questions, surveyQuestionView, Integer.toString(i)).commit();
            }
        }
    } else {
        List<Fragment> fragments = getRetainedChildFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            BaseSurveyQuestionView questionFragment = (BaseSurveyQuestionView) fragment;
            questionFragment.setOnSurveyQuestionAnsweredListener(this);

        }
    }
    return v;
}

From source file:cz.babi.android.remoteme.service.ConnectionService.java

/**
 * If server is unreachable we need to stop service and notice that to user.
 *///  ww  w.  jav  a 2s. c  o m
private void closeConnection() {
    if (Common.DEBUG)
        Log.d(TAG_CLASS_NAME, "[closeConnection]");

    if (clientSocket != null)
        try {
            clientSocket.close();
        } catch (IOException e) {
            if (Common.ERROR)
                Log.e(TAG_CLASS_NAME, "[stopService][Can not close client " + "socket.]");
        }

    ConnectionService.server = null;
    ConnectionService.clientSocket = null;
    ConnectionService.in = null;
    ConnectionService.out = null;
    ConnectionService.needEncryptedCommunication = false;

    LayoutInflater mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View toastLayout = mInflater.inflate(R.layout.toast_warning, null);

    TextView text = (TextView) toastLayout.findViewById(R.id.warning_text);
    text.setText(R.string.connection_service_connection_lost_text);

    Toast toast = new Toast(this);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(toastLayout);
    toast.show();

    disconnectWithError = true;
}

From source file:cz.babi.android.remoteme.service.ConnectionService.java

@Override
public void onDestroy() {
    if (Common.DEBUG)
        Log.d(TAG_CLASS_NAME, "[onDestroy]");

    /* Cancel the persistent notification. */
    if (isNotificationVisible)
        notificationManager.cancel(NOTIFICATION_ID);

    /* If Wi-Fi is locked we need to unlock it. */
    if (wifiLock.isHeld())
        unlockWifi();//  w w  w  . j  av  a 2s .  co m

    /* If there was no error we send 'bye bye' message to server and show toast to user. */
    if (!disconnectWithError) {
        /* Just tell to server that we are disconnecting. */
        SimpleMessage byeBye = Message.BYE_BYE;

        String message = byeBye.toString();
        if (needEncryptedCommunication)
            message = AES128_DEFAULT.encryptText(byeBye.toString());

        out.println(message);
        out.flush();

        /* Tell the user we stopped. */
        LayoutInflater mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View toastLayout = mInflater.inflate(R.layout.toast_normal, null);

        TextView text = (TextView) toastLayout.findViewById(R.id.normal_text);
        text.setText(R.string.connection_service_disconnected_text);

        Toast toast = new Toast(this);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(toastLayout);
        toast.show();
    }
}