I have recently started using ConstraintLayout and am loving it. But have come across a problem to be solved. Need to take a screenshot of a part view of the layout and that is easy in relative or linear layout as we make a layout around the view needed screenshot which is not the case in ConstraintLayout.

This is my view :

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <View
        android:id="@+id/sharable_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv_streak_best"
        app:layout_constraintLeft_toLeftOf="@id/total_distance_steps_left_guideline"
        app:layout_constraintRight_toRightOf="@id/total_distance_steps_right_guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/streak_progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:max="360"
        android:progress="0"
        android:progressDrawable="@drawable/progress_gradient_bg"
        android:secondaryProgress="360"
        app:layout_constraintBottom_toBottomOf="@id/streak_bottom_guideline"
        app:layout_constraintDimensionRatio="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.sharesmile.share.views.LBTextView
        android:id="@+id/tv_streak_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="@color/clr_5d"
        android:textSize="58sp"
        app:layout_constraintBottom_toBottomOf="@id/streak_progress"
        app:layout_constraintLeft_toLeftOf="@id/streak_progress"
        app:layout_constraintRight_toRightOf="@id/streak_progress"
        app:layout_constraintTop_toTopOf="@id/streak_progress"
        app:layout_constraintVertical_bias="0.35" />

    <com.sharesmile.share.views.LBITextView
        android:id="@+id/tv_streak_count_days"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="@string/day_streak"
        android:textColor="@color/clr_5d"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="@id/streak_progress"
        app:layout_constraintRight_toRightOf="@id/streak_progress"
        app:layout_constraintTop_toBottomOf="@+id/tv_streak_count" />

    <LinearLayout
        android:id="@+id/streak_goal_icons_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="@id/tv_streak_count_days"
        app:layout_constraintRight_toRightOf="@id/tv_streak_count_days"
        app:layout_constraintTop_toBottomOf="@id/tv_streak_count_days">


    </LinearLayout>
... more code here
</android.support.constraint.ConstraintLayout>

Now I wish to take screen shot of the progressbar and few elements below it. I tried to create a view around it and take screenshot of that but it doesn't work. What can be done to take the screenshot?

2 Answers

0
Akash Dubey On

You can use

tools:visibility="gone"

to hide the view as below

<View
    android:id="@+id/sharable_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/tv_streak_best"
    app:layout_constraintLeft_toLeftOf="@id/total_distance_steps_left_guideline"
    app:layout_constraintRight_toRightOf="@id/total_distance_steps_right_guideline"
    app:layout_constraintTop_toTopOf="parent"
    tools:visibility="gone" />

add below as attribute at root view

xmlns:tools="http://schemas.android.com/tools"
0
Sajith On

Do not need to hide/show your view

private void captureScreen(View v) {
    v.setDrawingCacheEnabled(true);
    Bitmap bitmap = v.getDrawingCache();
    String extr = Environment.getExternalStorageDirectory().toString();
    File file = new File(extr, "fff" + ".jpg");
    FileOutputStream f = null;
    try {
        f = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, f);
        f.flush();
        f.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

pass your view to above function

captureScreen(view you want to take a screen shot);