Skip to content

Instantly share code, notes, and snippets.

View noaht11's full-sized avatar

Noah Tajwar noaht11

View GitHub Profile
@noaht11
noaht11 / pdfmerge.sh
Created November 23, 2020 04:33
GhostScript pdfmerge function
function pdfmerge () {
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=$1 ${@:2}
}
@Override
protected void replaceText(CharSequence text) {
// Do nothing
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
...
Object data = getDataForSuggestion(adapter, position);
CharSequence text = getFilter().convertResultToString(adapter.getItem(position));
@Override
public void afterTextChanged(Editable message) {
if(mIgnoreTextChangedEvents) {
return;
}
// Avoid triggering text changed events from changes we make in this method
mIgnoreTextChangedEvents = true;
// Perform text changes
private void updatePadding() {
boolean chipsArePresent = !getAllChips().isEmpty();
if (!chipsArePresent && mUsingDefaultPadding) {
mUsingDefaultPadding = false;
Paint paint = getPaint();
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
int textHeight = fm.descent - fm.ascent;
// Calculate how tall the view should be if there were chips
int newTextHeight = mChipHeight + (mChipVerticalSpacing != -1 ? mChipVerticalSpacing : 0);
// We need to add half our missing height above and below the text by increasing top and bottom padding
private void adjustFontMetrics(FontMetricsInt fm) {
paint.getFontMetricsInt(fm);
int textHeight = fm.descent - fm.ascent;
// Break up the vertical spacing in half because half will go above the chip, half will go below the chip
int halfSpacing = mChipVerticalSpacing / 2;
// Given that the text is centered vertically within the chip, the amount of space above or below the text (inbetween the text and chip)
// is half their difference in height:
int spaceBetweenChipAndText = (mChipHeight - textHeight) / 2;
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
// Shift everything mLeftMarginPx to the left to create an empty space on the left (creating the margin)
x += mLeftMarginPx;
if (mChipHeight != -1) {
// If we set a chip height, adjust to vertically center chip in the line
// Adding (bottom - top) / 2 shifts the chip down so the top of it will be centered vertically
// Subtracting (mChipHeight / 2) shifts the chip back up so that the center of it will be centered vertically (as desired)
top += ((bottom - top) / 2) - (mChipHeight / 2);
bottom = top + mChipHeight;
@noaht11
noaht11 / OC-AsyncTaskRetainedFragment.java
Last active October 3, 2017 08:01
Using a retained fragment to handle AsyncTasks across orientation changes
public class NetworkRequestFragment extends Fragment {
// Declare some sort of interface that your AsyncTask will use to communicate with the Activity
public interface NetworkRequestListener {
void onRequestStarted();
void onRequestProgressUpdate(int progress);
void onRequestFinished(SomeObject result);
}
private NetworkTask mTask;
@noaht11
noaht11 / OC-RxUnsubscribeActivity.java
Last active August 8, 2016 04:32
Unsubscribing from an RxJava subscription to handle orientation changes
private Subscription mDoSomethingSubscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
// Setup views
...
@noaht11
noaht11 / OC-AsyncTaskCancelActivity.java
Last active January 17, 2018 13:02
Cancelling an AsyncTask in onDestroy
private static final String STATE_TASK_RUNNING = "taskRunning";
private MyTask mTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
@noaht11
noaht11 / OC-AddingFragmentsActivity.java
Last active July 11, 2016 16:33
How to properly add a Fragment to an Activity so that you can handle orientation changes
private static final String TAG_MY_FRAGMENT = "myFragment";
private MyFragment mFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adding_fragments);