Skip to content

Instantly share code, notes, and snippets.

Avatar

Magda Miu magdamiu

View GitHub Profile
View build.gradle
def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
@magdamiu
magdamiu / ApplicationData.java
Last active Jan 5, 2021
Sample of implementing a class for managing SharedPreferences
View ApplicationData.java
import android.content.Context;
import android.content.SharedPreferences;
public class ApplicationData {
private static final String APP_KEY = "android_course_key";
// save a String value by key
public static void setStringValueInSharedPreferences(Context context, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
@magdamiu
magdamiu / MainActivity.java
Last active Dec 29, 2020
Check permissions in Android
View MainActivity.java
// manifest add => <uses-permission android:name="android.permission.CAMERA" />
import static android.Manifest.permission.CAMERA;
private static final int REQUEST_CODE_CAMERA = 23;
if (ContextCompat.checkSelfPermission(StylesActivity.this, CAMERA) != PackageManager.PERMISSION_GRANTED) {
// unhappy path
ActivityCompat.requestPermissions(StylesActivity.this, new String[]{CAMERA}, REQUEST_CODE_CAMERA);
@magdamiu
magdamiu / MainActivity.java
Created Dec 28, 2020
Setup swipe to refresh
View MainActivity.java
private void setupSwipeToRefresh() {
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Make sure you call swipeRefreshLayout.setRefreshing(false)
// once the network request has completed successfully.
inbox();
}
});
// Configure the refreshing colors
View EmailAdapter.java
// Clean all elements of the recycler
public void clear() {
emails.clear();
notifyDataSetChanged();
}
// Add a list of items
public void addAll(List<Email> list) {
emails.addAll(list);
notifyDataSetChanged();
@magdamiu
magdamiu / main_layout.xml
Created Dec 28, 2020
Wrap RecyclerView in a SwipeRefreshLayout
View main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewEmails"
android:layout_width="match_parent"
android:layout_height="match_parent" />
@magdamiu
magdamiu / build.gradle
Created Dec 28, 2020
Add swiperefreshlayout dependency
View build.gradle
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
@magdamiu
magdamiu / MainActivity.java
Created Dec 28, 2020
Set an item decorator
View MainActivity.java
private void setItemDecorator() {
RecyclerView.ItemDecoration itemDecoration = new
DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
recyclerViewEmails.addItemDecoration(itemDecoration);
}
@magdamiu
magdamiu / EmailDiffCallback.java
Created Dec 28, 2020
EmailDiffCallback for fast rendering
View EmailDiffCallback.java
public class EmailDiffCallback extends DiffUtil.Callback {
private List<Email> oldList;
private List<Email> newList;
public EmailDiffCallback(List<Email> oldList, List<Email> newList) {
this.oldList = oldList;
this.newList = newList;
}
@magdamiu
magdamiu / MainActivity.java
Created Dec 28, 2020
Slow rendering best practices
View MainActivity.java
// NOT OK
void onNewEmailsArrivedNotRecommended(List<Email> newEmails) {
emailAdapter.setEmails(newEmails);
emailAdapter.notifyDataSetChanged();
}
// OK
void onNewDataArrivedFastRendering(List<Email> newEmails) {
List<Email> oldEmails = emailAdapter.getEmails();
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new EmailDiffCallback(oldEmails, newEmails));