I implemented a screenshot function triggered by an item in my navigation drawer. It works fine, but I want to make an screenshot of the whole page I see without the navigation drawer at that moment.
I used the layout suggested in the google tutorial to implement the navigation drawer:
<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">
<!--Main content view-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--Navigation Drawer-->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/sw21Background"/>
</android.support.v4.widget.DrawerLayout>
My code starts in the overridden listener method:
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position, view); // my code, e.g. takeScreenshot(view);
}
}
/**
* Swap fragments in the main content view
*/
private void selectItem(int position, View view) {
Fragment fragment = new Fragment();
if (position == 0) {
...
} else if (position == 3) {
//Feedback
// TODO get proper view of current page not the navigation drawer
Bitmap bitmap = this.takeScreenShot(view.getRootView());
Uri uri = saveBitmap(bitmap);
sendEmail(uri);
} ...
//Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
//Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(navigationItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
The current view is partially hidden by the navigation bar, so the following code also takes a shot of the bar:
private Bitmap takeScreenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
I tried to use the view nested in the FrameLayout (content), but it does not cover the action bar.