Skip to content

Instantly share code, notes, and snippets.

View drawer setup
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
View find views with TAG
private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){
ArrayList<View> views = new ArrayList<View>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
View LongLogs.java
public static void logsOf(String veryLongString, int maxLogSize) {
for (int i = 0; i <= veryLongString.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i + 1) * maxLogSize;
end = end > veryLongString.length() ? veryLongString.length() : end;
Log.v(TAG, veryLongString.substring(start, end));
}
}
@Marchuck
Marchuck / AGDS_Visualiser.java
Created Apr 1, 2016
Graphs drawing in java
View AGDS_Visualiser.java
import org.graphstream.graph.Edge;
import org.graphstream.graph.Element;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import util.Log;
import java.util.Iterator;
View rxJava.java
String[] getIds = new String[]{"id:0", "id:1", "id:2", "id:3"};
rx.Observable.just(getIds)
.flatMap(new Func1<String[], Observable<String>>() {
@Override
public Observable<String> call(String[] strings) {
//dla kazdego id odpytaj api
return Observable.from(strings);
}
}).flatMap(new Func1<String, Observable<Integer>>() {
@Override
View cesar.java
public static void main(String[] argv) {
new CesarCipher().solve();
}
public void solve() {
Scanner in = new Scanner(System.in);
int t = Integer.valueOf(in.nextLine());
String line = in.nextLine();
StringBuilder sb = new StringBuilder();
String keys = in.nextLine();
View RxJava parallel execution
int threadCt = Runtime.getRuntime().availableProcessors() + 1;
ExecutorService executor = Executors.newFixedThreadPool(threadCt);
Schedulers scheduler = Schedulers.from(executor);
Observable.range(1,1000)
.groupBy(i -> batch.getAndIncrement() % threadCt )
.flatMap(g -> g.observeOn(scheduler)
.map(i -> intenseCalculation(i))
@Marchuck
Marchuck / OrientationChangeWithPendingNetworkRequestActivity.java
Created Nov 15, 2016
workaround for request with activity and screen orientation changes
View OrientationChangeWithPendingNetworkRequestActivity.java
/**
* @author Lukasz Marczak
* @since 28.09.16.
*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
View fizzbuzz.java
rx.Observable.range(1, 100).map(i -> String.valueOf(i).concat(
(i % 15 == 0 ? "FizzBuzz" : i % 3 == 0 ? "Fizz" : i % 5 == 0 ? "Buzz" : "")))
.subscribe(System.out::println);
OR, more readable:
rx.Observable.range(1, 100).map(i -> i + suffixFor(i)).subscribe(System.out::println);
...
View Connectivity.java
package com.emil.android.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Check device's network connectivity and speed
* @author emil http://stackoverflow.com/users/220710/emil