Skip to main content

Screenshot of the Activity or View


This is the code to take the screen shot of the view or activity.Just copy these classes into your project.

MainActivity.java

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 MainActivity extends Activity {



Bitmap bmScreen;



Dialog screenDialog;

static final int ID_SCREENDIALOG = 1;



ImageView bmImage;

Button btnScreenDialog_OK;

TextView TextOut;

View screen;

EditText EditTextIn;





@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();

}};

}

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>


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>




 




Comments

Popular posts from this blog

Custom camera using SurfaceView android with autofocus & auto lights & more

Custom camera using SurfaceView android with autofocus & auto lights & much more /**  * @author Tatyabhau Chavan  *  */ public class Preview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     public Camera.Parameters mParameters;     private byte[] mBuffer;     private Activity mActivity;     // this constructor used when requested as an XML resource     public Preview(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }     public Preview(Context context) {         super(context);         init();     }     public Camera getCamera() {        ...

Bitmap scalling and cropping from center

How to Bitmap scalling and cropping from center? public class ScalingUtilities {     /**      * Utility function for decoding an image resource. The decoded bitmap will      * be optimized for further scaling to the requested destination dimensions      * and scaling logic.      *      * @param res      *            The resources object containing the image data      * @param resId      *            The resource id of the image data      * @param dstWidth      *            Width of destination area      * @param dstHeight      *     ...

Improving Layout Performance

Improving Layout Performance Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint. Lessons Optimizing Layout Hierarchies In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks. Re-using Layouts with <include/> If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts. Loading Views On Demand Be...