Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ScalpelDrawer - A simple wrapper for Scalpel (https://github.com/JakeWharton/scalpel) that includes toggle controls accessible from a right-side navigation drawer. Call ScalpelDrawer.wrapInside() in a base Activity onPostCreate() to easily wrap all content in your app.
<?xml version="1.0" encoding="utf-8"?>
<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">
<com.jakewharton.scalpel.ScalpelFrameLayout
android:id="@+id/scalpel"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/scalpel_drawer"
android:orientation="vertical"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:padding="16dp"
android:background="#ff444444">
<Switch
android:id="@+id/scalpel_enabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scalpel" />
<CheckBox
android:id="@+id/scalpel_draw_views"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Draw Views" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
package com.example.scalpeldrawer;
import android.app.Activity;
import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import com.jakewharton.scalpel.ScalpelFrameLayout;
public class ScalpelDrawer extends DrawerLayout {
private ScalpelFrameLayout scalpelView;
private Switch enabledSwitch;
private CheckBox drawViews;
public ScalpelDrawer(Context context) {
super(context);
init();
}
public ScalpelDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ScalpelDrawer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void wrapInside(Activity activity) {
ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
View content = parent.getChildAt(0);
parent.removeViewAt(0);
scalpelView.addView(content);
parent.addView(this);
}
private void init() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView(inflater.inflate(R.layout.scalpel_drawer, null));
scalpelView = (ScalpelFrameLayout) findViewById(R.id.scalpel);
enabledSwitch = (Switch) findViewById(R.id.scalpel_enabled);
drawViews = (CheckBox) findViewById(R.id.scalpel_draw_views);
enabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
scalpelView.setLayerInteractionEnabled(isChecked);
drawViews.setEnabled(isChecked);
}
});
drawViews.setChecked(true);
drawViews.setEnabled(false);
drawViews.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
scalpelView.setDrawViews(isChecked);
}
});
}
}
package com.example.scalpeldrawer;
import android.app.Activity;
import android.os.Bundle;
public class TheBaseActivity extends Activity {
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
if (BuildConfig.DEBUG) {
// prepare for surgery
ScalpelDrawer scalpelDrawer = new ScalpelDrawer(this);
scalpelDrawer.wrapInside(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.