Instantly share code, notes, and snippets.

View YouTubeHelper.java
import com.google.inject.Singleton;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Singleton
public class YouTubeHelper {
final String youTubeUrlRegEx = "^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))/";
final String[] videoIdRegex = { "\\?vi?=([^&]*)","watch\\?.*v=([^&]*)", "(?:embed|vi?)/([^/?]*)", "^([A-Za-z0-9\\_-]*)"};
View ClassExample.kt
open class Animal { // Parent class
var name: String? = null // Nullable variable
var legs: Int = 0 // Non-nullable variable
lateinit var map: HashMap<Integer, String> // Variable inited later in the code
constructor(legs: Int) {
this.legs = legs
}
constructor(legs: Int, name: String) {
View vairables.kt
// Use var to declare a variable, whose value can be changed later
var b: String = "Wow"
// val in Kotlin is same as final in Java
val a: Int = 0
// To initialize a variable as null
var c: String? = null
// Initialize a variable later in the code using lateInit
View activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvStatus"
View MainActivity.java
public class MainActivity extends AppCompatActivity {
private ApiService apiService = HttpClient.getApiService();
private TextView tvStatus, tvInfo;
private Button btnHttpRequest, btnReset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View SchedulerProvider.java
public class SchedulerProvider {
// UI thread
public static Scheduler ui() {
return AndroidSchedulers.mainThread();
}
// IO thread
public static Scheduler io() {
return Schedulers.io();
View HttpClient.java
public class HttpClient {
private static final String API_BASE_URL = "https://jsonplaceholder.typicode.com/";
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
private static ApiService apiService = retrofit.create(ApiService.class);
View ApiService.java
public interface ApiService {
@GET("/todos/1")
Flowable<Todo> getFirstTodo();
}
View Todo.java
public class Todo {
String userId;
int id;
String title;
boolean completed;
public String getFormattedInfo() {
StringBuilder builder = new StringBuilder();
builder.append("Title: " + title + "\n\n");
View response.json
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}