Mitesh Shah
Mitesh Shah

Reputation: 1739

Android - Capture ScreenShot Programmatically without title bar

I am developing an app in that I want to take a screen shot of current screen but with out title bar. I know the code to Capture a ScreenShot, but unable to customize it.

Code:

munchscreen.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                View v1 = L1.getRootView();
                v1.setDrawingCacheEnabled(true);
                Bitmap bm = v1.getDrawingCache();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                image = (ImageView) findViewById(R.id.screenshots);
                image.setBackgroundDrawable(bitmapDrawable);
            }
        });

When I click on Clip then it should capture a screen shot of the screen shown below but should not include the part which I have circled..

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8D5AA"
>


  <LinearLayout 
    android:id="@+id/clip_from_web_linearlayout1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/header"
    android:orientation="horizontal"             
    >
         <TextView 
        android:id="@+id/clip_from_web_textview_back"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Back"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        /> 

    <TextView 
        android:id="@+id/clip_from_web_textview_header_title"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Clip From Web"
        android:layout_gravity="center"
        android:layout_marginLeft="60dp"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        /> 

      <TextView 
        android:id="@+id/clip_from_web_textview_clip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Clip"
        android:layout_gravity="center"
        android:layout_marginLeft="60dp"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        /> 

    </LinearLayout>


          <Button
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="@string/munch"
             android:id="@+id/munchscreen"
             android:layout_below="@+id/clip_from_web_linearlayout1"
           />
           <ImageView
             android:layout_width="200dp"
             android:layout_height="200dp"
             android:id="@+id/screenshots"
             android:layout_below="@+id/munchscreen"
             android:contentDescription="@string/app_name"
             android:layout_centerInParent="true"

             />

<RelativeLayout
    android:id="@+id/clip_from_web_bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/header" >

    <Button 
        android:id="@+id/clip_from_web_previous"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="4dp"
        android:background="@android:drawable/ic_media_previous"
        android:layout_marginTop="2dp"
        />

    <Button 
        android:id="@+id/clip_from_web_reload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@android:drawable/ic_menu_rotate"
        android:layout_marginTop="5dp"
        />

    <Button 
        android:id="@+id/clip_from_web_next"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/clip_from_web_reload"
        android:layout_marginLeft="102dp"
        android:background="@android:drawable/ic_media_next"
        android:layout_marginTop="2dp"
        />

</RelativeLayout>

Image before Capturing screen shot..

enter image description here

Image after Capturing screen shot..

enter image description here

Upvotes: 2

Views: 10284

Answers (3)

sachin pangare
sachin pangare

Reputation: 1527

Hey friends I got the solution of above problem.

Follow below steps:-

  1. activity_main.xml

    <RelativeLayout
        android:id="@+id/toolbar_rl"
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        android:background="@color/header_color"
        android:visibility="visible">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="0"
            tools:ignore="UselessParent">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:contentDescription="@string/app_name"
                android:padding="@dimen/_8sdp"
                android:src="@drawable/ic_back_24" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="@dimen/_10sdp"
                android:layout_marginStart="@dimen/_20sdp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/one_time"
                android:textColor="@color/white"
                android:textSize="@dimen/_13sdp"
                tools:ignore="RtlHardcoded" />
    
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:contentDescription="@string/app_name"
                android:padding="@dimen/_8sdp"
                android:src="@drawable/ic_menu_icon" />
    
    
        </LinearLayout>
    
    </RelativeLayout>
    
    <ScrollView
        android:id="@+id/tabScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1">
    
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="210dp"
                android:adjustViewBounds="true"
                android:background="@drawable/otp_background"
                android:contentDescription="@string/app_name"
                android:scaleType="fitXY">
    
            </ImageView>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_16sdp"
                android:background="#fff"
                android:gravity="center"
                android:text="Enter Unique Identification Number"
                android:textColor="@color/header_color"
                android:textSize="@dimen/_16sdp" />
    
            <LinearLayout
                android:id="@+id/layout_otp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/_26sdp"
                android:gravity="top|center"
                android:orientation="horizontal">
    
                <EditText
                    android:id="@+id/editTextone"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_42sdp"
                    android:inputType="number"
                    android:maxLength="1"
                    android:nextFocusRight="@+id/editText_two"
                    tools:ignore="LabelFor">
    
                    <requestFocus />
                </EditText>
    
                <EditText
                    android:id="@+id/editTexttwo"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_42sdp"
                    android:inputType="numberDecimal"
                    android:maxLength="1"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/editTextthree"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_42sdp"
                    android:inputType="numberDecimal"
                    android:maxLength="1"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/editTextfour"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_42sdp"
                    android:inputType="numberDecimal"
                    android:maxLength="1"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/editTextfive"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_42sdp"
                    android:inputType="numberDecimal"
                    android:maxLength="1"
                    tools:ignore="LabelFor" />
    
            </LinearLayout>
    
    
            <Button
                android:id="@+id/btn_submit"
                android:layout_width="@dimen/_220sdp"
                android:layout_height="@dimen/_40sdp"
                android:layout_gravity="center"
                android:layout_marginBottom="@dimen/_46sdp"
                android:layout_marginLeft="@dimen/_16sdp"
                android:layout_marginRight="@dimen/_16sdp"
                android:layout_marginTop="@dimen/_26sdp"
                android:background="@drawable/editbox_one"
                android:text="@string/capture"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="@dimen/_16sdp"
                android:typeface="serif" />
        </TableLayout>
    </ScrollView>
    

    1. This xml looks like.

enter image description here

  1. in MainActivity Add some code.

    3.1 Declare the Button.

      Button button_submit;
    

    3.2 Initialize the button.

     button_submit = (Button) findViewById(R.id.btn_submit);
    

    3.3 Set set OnClickListener to this button and Call one method i.e

     takeScreenshot();
    

    Note:- When you click on this button that time automatically capture the screen without title bar and it will store in file explore with current date, time and.jpg extension(Ex. Fri May 11 151938 GMT+0530 2018.jpg) and this captured image as a bitmap if you want you can set this bitmap to ImageView .

  2. The Code of takeScreenshot().

     private void takeScreenshot() {
    
     Date now = new Date();
     android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
    
    try {
    
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
    
    View u = findViewById(R.id.tabScroll);
    u.setDrawingCacheEnabled(true);
    ScrollView z = (ScrollView) findViewById(R.id.tabScroll);
    z.setBackgroundColor(getResources().getColor(R.color.white));
    int totalHeight = z.getChildAt(0).getHeight();
    int totalWidth = z.getChildAt(0).getWidth();
    u.layout(0, 0, totalWidth, totalHeight);
    u.buildDrawingCache(true);
    Bitmap bitmap = Bitmap.createBitmap(u.getDrawingCache());
    u.setDrawingCacheEnabled(false);
    
    File imageFile = new File(mPath);
    
    FileOutputStream outputStream = new FileOutputStream(imageFile);
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    outputStream.flush();
    outputStream.close();
    
     } catch (Throwable e) {
    
      e.printStackTrace();
    }
    }
    
  3. The output be looks like..

enter image description here

I hope it will helpful for you.

Upvotes: 2

Piyush
Piyush

Reputation: 18923

Take this to another Linear Layout

  <LinearLayout 
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">       

       <Button
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/munch"
         android:id="@+id/munchscreen"

       />
       <ImageView
         android:layout_width="200dp"
         android:layout_height="200dp"
         android:id="@+id/screenshots"
         android:contentDescription="@string/app_name"
         />
  </LinearLayout>

Now find Id for this Linear Layout on oncreate() method making it global variable

 LinearLayout ll;

 ll = (LinearLayout)findViewById(R.id.linear);

now capture this screen

 ll.setDrawingCacheEnabled(true);
 ll.buildDrawingCache(true);
 Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
 ll.setDrawingCacheEnabled(false);

Now set this bitmap to your ImageView.

Upvotes: 5

Gopal Gopi
Gopal Gopi

Reputation: 11131

try this...

    munchscreen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            View contentView = getWindow().getDecorView().findViewById(android.R.id.content);
            contentView.setDrawingCacheEnabled(true);
            contentView.buildDrawingCache();
            Bitmap drawingCache = contentView.getDrawingCache();
            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), drawingCache);
            image = (ImageView) findViewById(R.id.screenshots);
            image.setBackgroundDrawable(bitmapDrawable);
        }
    });

Upvotes: 2

Related Questions