본문 바로가기
프로그래밍/Android

Android Toast 유틸 클래스

by CaffeLatte 2014. 11. 2.

private static Toast m_toast;


public static void showShortToast(Context context, String sText) {

    showToast(context, sText, Toast.LENGTH_SHORT);

  }


  public static void showShortToast(Context context, int nResId) {

    showToast(context, nResId, Toast.LENGTH_SHORT);

  }


  public static void showLongToast(Context context, String sText) {

    showToast(context, sText, Toast.LENGTH_LONG);

  }


  public static void showLongToast(Context context, int nResId) {

    showToast(context, nResId, Toast.LENGTH_LONG);

  }


  private static void showToast(Context context, String sText, int nDuration) {

    if(m_toast == null) {

      m_toast = Toast.makeText(context, "", nDuration);      

    }

    m_toast.setText(sText);

    m_toast.setDuration(nDuration);

    m_toast.setGravity(Gravity.CENTER, 0, 0);

    m_toast.show();

  }


  private static void showToast(Context context, int nResId, int nDuration) {

    if(m_toast == null) {

      m_toast = Toast.makeText(context, "", nDuration);      

    }

    m_toast.setText(nResId);

    m_toast.setDuration(nDuration);

    m_toast.setGravity(Gravity.CENTER, 0, 0);

    m_toast.show();

  }

댓글