Skip to content

Instantly share code, notes, and snippets.

View hilfritz's full-sized avatar

Hilfritz hilfritz

View GitHub Profile
@hilfritz
hilfritz / gist:08c4fe44dc533ed211b69c49593cff35
Last active June 21, 2018 14:23
Android Java logger that makes sure the logs are not cut when the string is too long
public class Logger {
public static final void logd(String tag, String message){
Logger.d(tag, message);
}
public static void d(String TAG, String message) {
int maxLogSize = 1000;
for(int i = 0; i <= message.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i+1) * maxLogSize;
@hilfritz
hilfritz / android questions
Created July 10, 2018 13:41
android questions
https://github.com/MindorksOpenSource/android-interview-questions#data-structures-and-algorithms
https://www.toptal.com/android/interview-questions
What is a ContentProvider and what is it typically used for?
-A ContentProvider manages access to a structured set of data. It encapsulates the data and provide mechanisms for defining data security. ContentProvider is the standard interface that connects data in one process with code running in another process.
Under what condition could the code sample below crash your application? How would you modify the code to avoid this potential problem? Explain your answer.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
@hilfritz
hilfritz / java decimal places for double
Created July 17, 2018 06:44
java decimal places for double
/**
* from here: https://stackoverflow.com/questions/22186778/using-math-round-to-round-to-one-decimal-place
* @param value double value with decimal places 123.123456789
* @param precision 1 number of decimal places desired ex. 1 (result => 123.1)
* @return
*/
public static double roundToDecimalPlaces(double value, int precision) {
int scale = (int) Math.pow(10, precision);
return (double) Math.round(value * scale) / scale;
}
@hilfritz
hilfritz / Android: Perform process in background
Created July 18, 2018 07:31
Android: Perform process in background
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//ADD YOUR CODE TO EXECUTE IN BACKGROUND THREAD HERE
}
});
@hilfritz
hilfritz / Android: open pdf using external app
Created July 19, 2018 03:41
Android: open pdf using external app
public static void openPDFFiile(Context context,File file){
//OPEN THE PDF
String mimeTypeStr = "application/pdf";
Intent intent2 = new Intent(Intent.ACTION_VIEW);
//intent2.setDataAndType(uri, mimeTypeStr);
//Uri apkURI = FileProvider.getUriForFile(
// context,
// context.getApplicationContext()
// .getPackageName() + ".provider", file);
@hilfritz
hilfritz / Android: full screen dialog fragment
Created August 15, 2018 09:24
Android: full screen dialog fragment
//https://stackoverflow.com/questions/7189948/full-screen-dialogfragment-in-android
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
//val dialog = super.onCreateDialog(savedInstanceState)
// request a window without the title
//dialog.window!!.requestFeature(Window.FEATURE_NO_TITLE)
// the content
val root = RelativeLayout(activity)
root.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
@hilfritz
hilfritz / Android: full screen dialogfragment (no statusbar)
Created August 15, 2018 09:25
Android: full screen dialogfragment (no statusbar)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen)
}
1. Find the longest String in an array of Strings
//ANSWER:
public String getLongestStringInArray(String[] array){
int index = 0;
int elementLength = array[0].length();
int arraySize = array.length;
for(int i=1; i< arraySize; i++) {
@hilfritz
hilfritz / Android + RxJava: Listen to view clicks
Created August 22, 2018 17:18
Android + RxJava: Listen to view clicks
//https://stackoverflow.com/questions/25457737/how-to-create-an-observable-from-onclick-event-android
Observable<View> clickEventObservable = Observable.create(new Observable.OnSubscribe<View>() {
@Override
public void call(final Subscriber<? super View> subscriber) {
viewIWantToMonitorForClickEvents.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (subscriber.isUnsubscribed()) return;
subscriber.onNext(v);
@hilfritz
hilfritz / Android: Recyclerview inside NestedScrollview - Scroll problem
Last active September 25, 2018 09:58
Android: Recyclerview inside Scrollview - Scroll problem
//issue happens one pre lolipop devices
//https://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working
//must use NestedScrollView
fix:
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
int action = e.getAction();