Skip to content

Instantly share code, notes, and snippets.

View MohammadSamandari's full-sized avatar
💭
Android Is SO MUCH FUN

Mohammad Samandari MohammadSamandari

💭
Android Is SO MUCH FUN
View GitHub Profile

Custom View

Steps for creating the custom view

Create a custom view class

this extends View or another subclass of the view like button or textview

  1. Add the new class and extend it with edittext. (notice it is the appcompat text view)
  2. adding the 3 constructor for the class
  3. Adding the behavior to the custom view.
  • add 2 drawable for the clear(X) buttton for the textview
  • adding the init() method inside the new class and then calling it from all constructors.
@MohammadSamandari
MohammadSamandari / Android-ViewModel-LiveData.java
Last active April 6, 2020 22:50
Android- ViewModel and Live Data
package mohammad.samandari.testviewmodel;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
package mohammad.samandari.appwithsettings;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
@MohammadSamandari
MohammadSamandari / Android-SharedPreferences.md
Last active April 5, 2020 21:20
Data Storage - Shared Preferences

Data storage

Files

  1. Internal Storage
  2. External Storage

Internal Storage

  • You don't need any permissions to save files on the internal storage. Your app always has permission to read and write files in its internal storage directory.

To create a new file in one of these directories, you can use the File() constructor, passing the File provided by one of the above methods that specifies your internal storage directory. For example:

Efficient data transfer

Monitor connectivity state

You can use the ConnectivityManager to determine the active wireless radio and modify your prefetching routines depending on network type:

ConnectivityManager cm =
   (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager tm =
    (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
@MohammadSamandari
MohammadSamandari / Android-AlarmManager.md
Last active August 6, 2024 22:44
Android Alarm Manager

Alarm Manager

how to use alarms to trigger tasks at specific times, whether or not your app is running when the alarms go off.

For example, you can use a repeating alarm to schedule a download every day at the same time.

To create alarms, you use the AlarmManager class. Alarms in Android have the following characteristics:

  • Alarms let you send an Intent at set times or intervals. You can use alarms with broadcast receivers to start services and perform other operations.
  • Alarms operate outside your app. You can use them to trigger events or actions even when your app isn't running, and even if the device is asleep.
  • When used correctly, alarms can help you minimize your app's resource requirements. For example, you can schedule operations without relying on timers or continuously running background services.

Notification

No need to explain.

Notification Channels

In the Settings app on an Android-powered device, users can adjust the notifications they receive. Starting with Android 8.0 (API level 26), you can assign each of your app's notifications to a notification channel. Each notification channel represents a type of notification, and you can group several notifications in each channel.

check the device's SDK version before building the notification channel.

When you create a notification channel in your code, you set behavior for that channel, and the behavior is applied to all of the notifications in the channel. For example, your app might set the notifications in a channel to play a sound, blink a light, or vibrate. Whatever behavior you set for a notification channel, the user can change it, and they can turn off notifications from your app altogether.

Creating Notification channels

@MohammadSamandari
MohammadSamandari / Android-Broadcasts.md
Created April 3, 2020 20:46
Broadcasts and Broadcast Receivers

Broadcasts

Broadcasts are messages that the Android system and Android apps send when events occur that might affect the functionality of other apps. For example, the Android system sends an event when the system boots up, when power is connected or disconnected, and when headphones are connected or disconnected. Your Android app can also broadcast events, for example when new data is downloaded.

In general, broadcasts are messaging components used for communicating across apps when events of interest occur. There are two types of broadcasts:

  • System broadcasts are delivered by the system.
@MohammadSamandari
MohammadSamandari / Android-InternetConnection.md
Created April 3, 2020 19:01
Internet connection - hide keyboard - Parsing JSON data

Internet Connection

getting data from the internet using

see the NetworkUtils class to see how to get data from internet. also we can use the volley library to fetch data.

Hiding the keyboard Programatically

        //Hiding the keyboard after the button pressed.
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
@MohammadSamandari
MohammadSamandari / Android-AsyncTaskLoader.md
Last active April 3, 2020 17:12
Android - Hide keyboard Programatically - Get data from internet class - checking network status

Asynctask

getting data from the internet using

see the NetworkUtils class to see how to get data from internet.

Hiding the keyboard Programatically

        //Hiding the keyboard after the button pressed.
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);