Downloading Bitmap image or image file

This post provides codes for downloading Bitmap image to Downloads folder and for copying image file from Assets or Cache to Downloads folder. 1. Download Bitmap image to Downloads folder Create a new java file ImageDownloader.java and put following codes in it. Add your package name at the top. To use it in Activity, if … Read more

How to get Context in a Fragment

In the Fragment, there are several options to get the context. Here are the most common and recommended approaches: Option 1: requireContext() (Recommended) Option 2: getActivity() Option 3: getContext() Option 4: Store context in a variable Recommendation Use requireContext() because: · It’s the most modern and recommended approach· It guarantees a non-null Context· It throws … Read more

Get video thumbnail from url of video

Here are two methods to retrieve thumbnail from a video. 1. Using MediaMetadataRetriever Works for local files and most URLs Usage 2. Using Glide (simpler, recommended for network URLs) Add this dependency Then use below code. Glide automatically extracts a representative frame from the video and displays it in the ImageView. MediaMetadataRetriever might fail for … Read more

Simple Intent operations

An Intent is a description of an operation or action to be performed. It can be used to launch an activity, to start or interact with a background service, or to send data to Broadcast receiver components. Below are some code snippets using Intent for commonly performed operations in android. Move from one Activity to … Read more

Display Toast messages at different positions

We can apply Gravity to the toast message to display it at various positions on the screen. Below are codes provided for displaying a Toast message at TOP, BOTTOM, RIGHT, LEFT, CENTER, TOP LEFT, and BOTTOM RIGHT positions. The method used for displaying Toast at different positions is .setGravity(int gravity, int xOffset, int yOffset).

Android Toast Example

A toast displays a simple message in a small popup, which automatically disappear after a timeout. The code to display a Toast is: Position of Toast: A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an … Read more