Skip to content

Instantly share code, notes, and snippets.

@Razeeman
Razeeman / links.txt
Last active May 26, 2019 08:33
Android/Java useful links
@Razeeman
Razeeman / NetworkUtils.java
Last active January 30, 2019 12:45
Android, Java, Network, Build Url, Build Uri, HttpURLConnection.
/**
* Builds the URL from query string.
*
* @param searchQuery The search string that will be queried for.
* @return The URL to use for the query.
*/
public static URL buildUrl(String searchQuery) {
String baseUrl = "https://api.github.com/search/repositories";
String paramQuery = "q";
@Razeeman
Razeeman / AsyncTask.java
Last active January 30, 2019 12:44
Android, Java, AsyncTask.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
query = "sample query"
URL searchUrl = NetworkUtils.buildUrl(query);
new QueryTask().execute(searchUrl);
@Razeeman
Razeeman / ImplicitIntents.java
Last active January 30, 2019 17:52
Android, Java, Implicit intents, Open webpage, Open map, Share text.
/**
* This method fires off an implicit Intent to open a webpage.
*
* @param url Url of webpage to open. Should start with http:// or https:// as that is the
* scheme of the URI expected with this Intent according to the Common Intents page
*/
private void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
/*
@Razeeman
Razeeman / PersistentData.java
Last active January 30, 2019 12:42
Android, Java, onSaveInstanceState.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* If savedInstanceState is not null, that means our Activity is not being started for the
* first time. Even if the savedInstanceState is not null, it is smart to check if the
* bundle contains the key we are looking for.
*/
if (savedInstanceState != null) {
@Razeeman
Razeeman / AsyncTaskLoader.java
Last active January 30, 2019 18:14
Android, Java, AsyncTaskLoader.
public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<String> {
private static final int LOADER_ID = 22;
private static final String SEARCH_QUERY_URL_EXTRA = "query";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Razeeman
Razeeman / Util.java
Last active January 30, 2019 13:39
Get N random elements from list without shuffling whole list.
public class Main {
/**
* Returns new list of N random elements from provided list.
*
* @param list list of elements to choose from
* @param n number of elements to choose
* @param <E> type of elements in a list
* @return new list of N random elements from provided list
*/
@Razeeman
Razeeman / Singletons.md
Last active November 9, 2023 19:23
Thread safe singleton implementations in java and kotlin.

Java

Not thread safe.

class SimpleSingleton {
    private static SimpleSingleton sInstance;
  
    private SimpleSingleton() {}
 
Java. Kotlin.
OOP.
Design patterns.
Solid. Clean architecture.
Rest, crud, http, soap.
Android version names.
Android system architecture.
Android components. Activity, service, broadcast receiver, content provider.
Material design.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<color name="red_50">#FFEBEE</color>
<color name="red_100">#FFCDD2</color>
<color name="red_200">#EF9A9A</color>
<color name="red_300">#E57373</color>
<color name="red_400">#EF5350</color>
<color name="red_500">#F44336</color>
<color name="red_600">#E53935</color>