Skip to content

Instantly share code, notes, and snippets.

View matthew-carroll's full-sized avatar

Matt Carroll matthew-carroll

View GitHub Profile
@matthew-carroll
matthew-carroll / MyView.java
Created July 6, 2019 11:10
Implements Parcelable in a custom View in Android (2/4).
public class MyView extends View {
private static class SavedState extends BaseSavedState {
String name;
int index;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
@matthew-carroll
matthew-carroll / MyView.java
Last active July 6, 2019 11:10
Implements Parcelable in a custom View in Android (1/4).
public class MyView extends View {
private static class SavedState extends BaseSavedState {
// TODO:
}
}
@matthew-carroll
matthew-carroll / MyView.java
Created July 6, 2019 11:06
Enables state saving for a custom View in Android.
public class MyView extends View {
public MyView(Context context) {
setSaveEnabled(true);
}
}
@matthew-carroll
matthew-carroll / some_activity.java
Created July 6, 2019 11:04
Example of setting a View ID programatically in Android.
public class SomeActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout);
MyView myView = new MyView(this);
myView.setId(R.id.desired_id);
addView(myView);
@matthew-carroll
matthew-carroll / some_layout.xml
Created July 6, 2019 11:02
Example of setting a View ID in XML in Android.
<MyView
android:id="@+id/desired_id"
...
/>
@matthew-carroll
matthew-carroll / CustomViewGroup.java
Created July 6, 2019 11:00
Saving child state in custom ViewGroup with new SparseArray.
@Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
// Create a SparseArray just for us!
SparseArray mySparseArray = new SparseArray(mChildrenCount + 1); // +1 for our ViewGroup's state.
// Repeat the normal logic for this method, but pass our "mySparseArray" instead of "container"
super.dispatchSaveInstanceState(mySparseArray);
final int count = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < count; i++) {
@matthew-carroll
matthew-carroll / viewgroup.java
Created July 6, 2019 10:56
Implementing of dispatchSaveInstanceState() in ViewGroup for Android.
@Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
super.dispatchSaveInstanceState(container);
final int count = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < count; i++) {
View c = children[i];
if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
c.dispatchSaveInstanceState(container);
}
@matthew-carroll
matthew-carroll / MainActivity.java
Created May 22, 2019 21:12
New Android embedding, new and old plugins, or all old plugins.
// This example assumes that the developer is using the new Android embedding
// API, some plugins that use dart-side initialization, and some old plugins
// that have no concept of dart-side initialization.
//
// This example would be the same even if all used plugins were old (no dart-side
// initialization).
public class MainActivity extends FlutterActivity {
@Override
protected void onFlutterEngineCreated(@NonNull FlutterEngine engine) {
// We register the GeneratedPluginLoader for the plugins that use
@matthew-carroll
matthew-carroll / MainActivity.java
Created May 22, 2019 21:07
Canonical GeneratedPluginLoader usage
// This example assumes that the developer is using the new Android embedding
// API, as well as plugins that have opted-in to the proposed dart-side
// plugin initialization.
public class MainActivity extends FlutterActivity {
@Override
protected void onFlutterEngineCreated(@NonNull FlutterEngine engine) {
engine.setPluginLoader(new GeneratedPluginLoader());
}
}
@matthew-carroll
matthew-carroll / with_dark_theme.dart
Last active September 16, 2019 04:58
Dark Theme
MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.red,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
),
);