Capture Current Screen In Android Programming

Description:
In the earlier post related to capture screen, screen is captured for the first time only. After that, every time same screen will get draw. This example will show how you can capture current screen in android.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it CaptureCurrentScreen.

2.) Write following into activity_main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/screen"
>
<Button
android:id="@+id/capturescreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Capture Screen"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter some text here which you will see on captured screen"
/>
<EditText
android:id="@+id/textin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

 

 

3.) Create and write following into layout/dialog.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip"
>
<TextView
android:id="@+id/textout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/image"
android:layout_width="300px"
android:layout_height="300px"
/>
<Button
android:id="@+id/okdialogbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"
/>
</LinearLayout>

 

 

4.) Run for output.

Steps:

1.) Create a project named CaptureCurrentScreen and set the information as stated in the image.

Build Target: Android 4.2
Application Name: CaptureCurrentScreen
Package Name: com.example.CaptureCurrentScreen
Activity Name: CaptureCurrentScreenActivity

2.) Open CaptureCurrentScreenActivity.java file and write following code there:

 

package com.example.capturecurrentscreen;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class CaptureCurrentScreenActivity extends Activity {

Bitmap bmScreen;

Dialog screenDialog;
static final int ID_SCREENDIALOG = 1;

ImageView bmImage;
Button btnScreenDialog_OK;
TextView TextOut;

View screen;
EditText EditTextIn;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (View)findViewById(R.id.screen);
Button btnCaptureScreen = (Button)findViewById(R.id.capturescreen);
EditTextIn = (EditText)findViewById(R.id.textin);

btnCaptureScreen.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
screen.setDrawingCacheEnabled(false);
screen.setDrawingCacheEnabled(true);
bmScreen = screen.getDrawingCache();
showDialog(ID_SCREENDIALOG);
}});
}

@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub

screenDialog = null;
switch(id){
case(ID_SCREENDIALOG):
screenDialog = new Dialog(this);
screenDialog.setContentView(R.layout.dialog);
bmImage = (ImageView)screenDialog.findViewById(R.id.image);
TextOut = (TextView)screenDialog.findViewById(R.id.textout);
btnScreenDialog_OK = (Button)screenDialog.findViewById(R.id.okdialogbutton);
btnScreenDialog_OK.setOnClickListener(btnScreenDialog_OKOnClickListener);
}
return screenDialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
switch(id){
case(ID_SCREENDIALOG):
dialog.setTitle("Captured Screen");
TextOut.setText(EditTextIn.getText().toString());
bmImage.setImageBitmap(bmScreen);
break;
}
}

private Button.OnClickListener btnScreenDialog_OKOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
screenDialog.dismiss();
}};
}

 

3.) Compile and build the project.

Output

Leave a Comment:

2 comments
Thang says July 18, 2013

I get error Force close when run project….can you help me. I think proplem in androidmanifest.xml but can not do more…

Reply
Amit says April 15, 2015

Can i develop same code using the phonegap code please help me

Reply
Add Your Reply