Example usage for android.widget Toast LENGTH_SHORT

List of usage examples for android.widget Toast LENGTH_SHORT

Introduction

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

Prototype

int LENGTH_SHORT

To view the source code for android.widget Toast LENGTH_SHORT.

Click Source Link

Document

Show the view or text notification for a short period of time.

Usage

From source file:MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override/*from w  ww.j  a v  a2s . c o  m*/
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

From source file:Main.java

/**
 * Reads a file that is embedded in our application and writes it to the device storage
 * @param context/* w  w w  .  ja v  a  2 s.c  om*/
 * @param file
 * @param assetFileDescriptor
 */
private static void saveFileToDevice(Context context, File file, AssetFileDescriptor assetFileDescriptor) {
    // The output stream is used to write the new file to the device storage
    FileOutputStream outputStream = null;
    // The input stream is used for reading the file that is embedded in our application
    FileInputStream inputStream = null;
    try {
        byte[] buffer = new byte[1024];
        // Create the input stream
        inputStream = (assetFileDescriptor != null) ? assetFileDescriptor.createInputStream() : null;
        // Create the output stream
        outputStream = new FileOutputStream(file, false);
        // Read the file into buffer
        int i = (inputStream != null) ? inputStream.read(buffer) : 0;
        // Continue writing and reading the file until we reach the end
        while (i != -1) {
            outputStream.write(buffer, 0, i);
            i = (inputStream != null) ? inputStream.read(buffer) : 0;
        }

        outputStream.flush();
    } catch (IOException io) {
        // Display a message to the user
        Toast toast = Toast.makeText(context, "Could not save the file", Toast.LENGTH_SHORT);
        toast.show();
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ex) {
                // We should really never get this far, but we might consider adding a
                // warning/log entry here...
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                // We should really never get this far, but we might consider adding a
                // warning/log entry here...
            }
        }
    }
}

From source file:Main.java

/**
 * Display a {@link Toast} letting the user know what an item does when long
 * pressed./* www.j  av a  2 s  .  c o  m*/
 * 
 * @param view The {@link View} to copy the content description from.
 */
public static void showCheatSheet(final View view) {

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g.
                                          // status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (48 * context.getResources().getDisplayMetrics().density);

    final Toast cheatSheet = Toast.makeText(context, view.getContentDescription(), Toast.LENGTH_SHORT);
    final boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                displayFrame.bottom - screenPos[1]);
    }
    cheatSheet.show();
}

From source file:GeofenceIntentService.java

protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        Toast.makeText(getApplicationContext(), "Geofence error code= " + geofencingEvent.getErrorCode(),
                Toast.LENGTH_SHORT).show();
        return;/*from   w ww . j  a v a2 s .  c o m*/
    }
    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL) {
        sendNotification();
    }
}

From source file:com.monmonja.library.utils.ViewUtils.java

public static void makeToast(Context context, int resId) {
    TypedValue tv = new TypedValue();
    int offsetY = 0;
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        offsetY = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }//from  w w  w.  jav  a 2  s. c  om

    Toast toast = Toast.makeText(context, resId, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    View view = toast.getView();
    view.setBackgroundColor(context.getResources().getColor(R.color.toast_background));
    TextView text = (TextView) view.findViewById(android.R.id.message);
    text.setTextColor(context.getResources().getColor(android.R.color.white));
    toast.show();
}

From source file:Main.java

/**
 * Internal helper method to show the cheat sheet toast.
 *///  www .  j  a  v a  2 s.c  o  m
private static boolean show(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g. status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (ESTIMATED_TOAST_HEIGHT_DIPS
            * context.getResources().getDisplayMetrics().density);

    Toast cheatSheet = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        // NOTE: We can't use Gravity.BOTTOM because when the keyboard is up
        // its height isn't factored in.
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - estimatedToastHeight);
    }

    cheatSheet.show();
    return true;
}

From source file:com.liferay.tasks.callback.UpdateTaskCallback.java

@Override
public void onFailure(Exception e) {
    String message = "Couldn't update task: " + e.getMessage();

    Toast.makeText(_activity, message, Toast.LENGTH_SHORT).show();
}

From source file:SMSBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (SMS_RECEIVED.equals(intent.getAction())) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            String format = bundle.getString("format");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                } else {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }/* w  w  w .  ja  v a2  s . c om*/
                Toast.makeText(context, messages[0].getMessageBody(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}

From source file:ru.gkpromtech.exhibition.organizations.OrganizationFilesDownloader.java

public static void download(final Context context, final Organization organization) {
    final ProgressDialog dialog = ProgressDialog.show(context, context.getString(R.string.please_wait),
            context.getString(R.string.loading_materials_info), false);

    ServiceClient.getJson("organizations/files?id=" + organization.id, new Callback<JsonNode>() {
        @Override//from   www .j a  v a2 s .  c om
        public void onSuccess(JsonNode data) throws Exception {
            dialog.dismiss();

            String url = data.get("url").asText();
            int size = data.get("size").asInt();
            confirmAndDownload(context, organization, url, size);
        }

        @Override
        public void onError(Throwable exception) {
            dialog.dismiss();
            Toast.makeText(context, R.string.error_loading_data, Toast.LENGTH_SHORT).show();
        }
    });
}

From source file:MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] countries = new String[] { "China", "France", "Germany", "India", "Russia", "United Kingdom",
            "United States" };
    ListAdapter countryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
    setListAdapter(countryAdapter);//w ww.j  a  v  a  2s .c  o m

    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s = ((TextView) view).getText() + " " + position;
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
        }
    });
}