Skip to content

Instantly share code, notes, and snippets.

View atoennis's full-sized avatar

Adam Toennis atoennis

View GitHub Profile
@atoennis
atoennis / SomeFragment.java
Last active April 23, 2020 06:30
Solution to having nested scrolling within Android. In this scenario a multiline EditText resides within a root level ScrollView. In order to have scroll momentum, the EditText itself doesn't scroll but it is wrapped within a ScrollView.
// When the EditText is touched, disable touches on it's ScrollView's parents.
// Once the user lifts up their finger, enable touches on on the ScrollView's parents once again.
@OnTouch(R.id.edit_text)
boolean handleNoteFieldTouch(View v, MotionEvent event) {
v.getParent().getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
v.getParent().getParent().requestDisallowInterceptTouchEvent(false);
break;
@atoennis
atoennis / RecyclerViewFragment.java
Last active August 29, 2015 14:25
How to implement swipe to delete with a Recyclerview using the support library. Functionality was added in version 22.2 of the support library.
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
SimpleCallback simpleCallback = new SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder viewHolder1) {
return false;
}
@atoennis
atoennis / AndroidManifest.xml
Last active August 29, 2015 14:22
Need to navigate to multiple levels deep in your hierarchy but need to preserve navigation? This is a common scenario for tapping on a notification. Doing multiple startActivity() calls in a row can make for some sloppy code. Enter TaskStackBuilder, a clear and concise way to manage your back stack!
<!-- Setting the parentActivityName can help the TaskStackBuilder build out the back stack
if methods like .addNextIntentWithParentStack(Intent intent) are used -->
<activity
android:name=".ui.home.HomeActivity"
android:launchMode="singleTop"
android:label="@string/app_name"/>
<activity
android:name=".ui.upload.UploadActivity"
android:parentActivityName=".ui.home.HomeActivity"
@atoennis
atoennis / RetrofitModule.java
Last active August 29, 2015 14:21
How to build a Retrofit RestAdapter using Dagger2 dependency injection.
@Singleton
@Provides
CustomRequestInterceptor provideRequestInterceptor(final Application application, SharedPreferencesCookieStore cookieStore) {
return new CustomRequestInterceptor(application, cookieStore);
}
@Singleton
@Provides
RestAdapter provideRestAdapter(Client client, DynamicEndpoint endpoint,
Converter converter,
@atoennis
atoennis / fragment.java
Last active November 10, 2023 01:38
WebViews automatically persist cookies into a android.webkit.CookieManager. Need to store cookies from a WebView into a java.net.CookieManager? Here's one way to do so.
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String cookieStr = CookieManager.getInstance().getCookie(url); // android.webkit.CookieManager
storeCookies(url, cookieStr);
return super.shouldOverrideUrlLoading(view, url);
}
// Convert cookie string into a list of cookies. Persist cookies in a java.net.CookieStore
@atoennis
atoennis / adb+
Last active August 29, 2015 14:17
Run any command adb provides on all your currently connected devices. Credit: http://stackoverflow.com/a/17882578
#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provides on all your currently connected devices
# ./adb+ <command> is the equivalent of ./adb -s <serial number> <command>
adb devices | while read line
do
  if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
  then
      device=`echo $line | awk '{print $1}'`