How to take screenshot in ConstraintLayout? - android

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?

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"

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

Related

Taken photo is bigger than the preview in cameraview

I'm facing a problem using a Samsung J1 device here.
But the image taken is bigger than the preview one.
This is the preview that I got:
and this is the final bitmap result:
I'm calling camera view with this attributes:
<com.otaliastudios.cameraview.CameraView
android:id="#+id/camera"
app:cameraGesturePinch="zoom"
app:cameraFlash="auto"
app:cameraAutoFocusResetDelay="0"
app:cameraGestureTap="focusWithMarker"
android:keepScreenOn="true"
app:cameraPreview="glSurface"
app:cameraPictureSizeSmallest="true"
android:adjustViewBounds="true"
app:cameraGestureScrollHorizontal="exposureCorrection"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I already tried to use cameraPictureSizeAspectRatio with 4:3 but no success with it.
Did I do something wrong?
i have same problem .
after save image from cameraview ( i use camerakit lib) , display file by convert it to bitmap in imageview .
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraKitView = findViewById(R.id.camera)
val captureButton = findViewById<View>(R.id.button_capture) as Button
val imageView = findViewById<ImageView>(R.id.myImageView)
captureButton.setOnClickListener {
cameraKitView!!.captureImage { _, capturedImage ->
val savedPhoto = getOutputMediaFile()
try {
val outputStream = FileOutputStream(savedPhoto!!.path)
outputStream.write(capturedImage)
outputStream.close()
val myBitmap = BitmapFactory.decodeFile(savedPhoto.getAbsolutePath())
imageView.setImageBitmap(myBitmap)
} catch (e: java.io.IOException) {
e.printStackTrace()
}
}
}
}
and this is my layout :
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.camerakit.CameraKitView
android:id="#+id/camera"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:adjustViewBounds="true"/>
<Button
android:id="#+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="#string/save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/myImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:alpha="0.5"
android:adjustViewBounds="true"
android:background="#android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="#string/app_name" />
</android.support.constraint.ConstraintLayout>
but when show image and want to take another image, cameraview is not as scale as imageview
Solved :
just change scaleType to CentreCrop . that is it

Converting FrameLayout to Video?

In my Framelayout I have an ImageView and TextView and I was able to convert the FrameLayout into a Bitmap and Save it by using the following snippet code
Bitmap bitmap = Bitmap.createBitmap(frameLayout.getWidth(), frameLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
frameLayout.draw(canvas);
try {
FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + path));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
}
Now I want to achieve the same behavior with my VideoView. So I have another FrameLayout with VideoView and TextView and I want to convert that FrameLayout into a VideoView so the TextView is incorporated inside the Video.
I am wondering if it is possible and any ideas are welcome..
Edit 1: My .xml content
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/surface_camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/buttonstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" /></FrameLayout>

Need to take screenshot without Imagebuttons

I am trying to take screenshot of my app screen programatically. The Layout file has some ImageButtons and a Textview field. When the capture screen happens, the resulted image contains the buttons too. Is there a way to take screenshot without the imagebuttons?
Below are the code snippet and layout files
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.abc.def.MainActivity">
<include
layout="#layout/buttons_layout"
android:id="#+id/includedLayout"
android:visibility="visible"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:id="#+id/text1"
android:gravity="center"
android:textSize="32dp"
android:shadowColor="#edeff2"
android:textStyle="bold"
android:shadowDx="0.0"
android:shadowDy="0.0"
android:shadowRadius="25"
/>
</RelativeLayout>
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<ImageButton
android:id="#+id/refreshButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/refresh"
/>
<ImageButton
android:id="#+id/shareButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/share"
/>
</LinearLayout>
</LinearLayout>
Code to take screenshot
private void captureScreen() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String filePath = Environment.getExternalStorageDirectory() + File.separator +now+".jpg";
View view = findViewById(R.id.activity_main);
view.setDrawingCacheEnabled(true);
mBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendScreen(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
Any help will be appreciated. Thanks :)
If it is just the text view you want as I can see it is full screen.
Try using
View view = findViewById(R.id.text1); // instead of R.id.activity_main
view.setDrawingCacheEnabled(true);
Basically point is keep those views in separate container whose screen shot you want and then get the drawable of that particular container.
You can make the image button invisible just before taking the screenshot and then make it visible.
private void captureScreen() {
//make unwanted views invisible
findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
findViewById(R.id.shareButton).setVisibility(View.INVISIBLE);
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String filePath = Environment.getExternalStorageDirectory() + File.separator +now+".jpg";
View view = findViewById(R.id.activity_main);
view.setDrawingCacheEnabled(true);
mBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendScreen(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
//make those views visible again
findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
findViewById(R.id.shareButton).setVisibility(View.VISIBLE);
}

PrintedPDFDocument results in PDF containing Grey box with sort of Grid Layout

After many problems, I have finally been able to generate a PDF Document that has content and isn't corrupted using the PrintedPDFClass.
The problem is, however, the content in the PDF doesn't seem to bear any relation to the VIew I gave it to render...
The view that I want to render:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:kingdomspas="http://www.kingdomspas.com/android/custom"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/salesAgreementTableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableLayout>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/companyLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/logo_description"
android:src="#drawable/company_logo" />
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:id="#+id/companyAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/address_description"
android:src="#drawable/company_address" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/salesExecInitials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/round"
android:inputType="text"
android:maxLines="1"
android:paddingLeft="130dp"
android:singleLine="true" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:text="#string/sales_exec_initials" />
</FrameLayout>
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/salesExecInitials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/round"
android:inputType="text"
android:maxLines="1"
android:paddingLeft="130dp"
android:singleLine="true" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:text="#string/sales_exec_initials" />
</FrameLayout>
</TableRow>
</TableLayout>
<View
android:paddingTop="50dp"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_weight="1"
android:background="#android:color/darker_gray" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/submitButton"
android:text="#string/submit_form"
android:padding="10dp"/>
</LinearLayout>
</ScrollView>
Which results in a UI like this:
The code I use to generate the PDF is:
Builder printAttrsBuilder = new Builder();
printAttrsBuilder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
printAttrsBuilder.setMinMargins(new Margins(5, 5, 5, 5));
PrintedPdfDocument document = new PrintedPdfDocument(context, printAttrsBuilder.build());
PageInfo pageInfo = new PageInfo.Builder(150, 150, 1).create();
Page page = document.startPage(pageInfo);
Canvas pdfCanvas = page.getCanvas();
salesFragmentTableLayout.draw(page.getCanvas());
document.finishPage(page);
File result = null;
FileOutputStream fos = null;
BufferedOutputStream bos= null;
FileDescriptor descriptor = null;
try {
result = File.createTempFile("Kingdom Spas Agreement", ".pdf", context.getCacheDir());
fos = new FileOutputStream(result);
bos = new BufferedOutputStream(fos);
document.writeTo(bos);
descriptor = fos.getFD();
descriptor.sync();
} catch (FileNotFoundException e) {
throw new KingdomSpasException("Failed to find relevent file", e);
} catch (IOException e) {
throw new KingdomSpasException("IO Problem occured while creatin the PDF", e);
} finally {
try {
if (bos != null) { bos.flush(); bos.close(); }
} catch (SyncFailedException e) {
throw new KingdomSpasException("Failed to correctly sync PDF file - may be corrupted", e);
} catch (IOException e) {
throw new KingdomSpasException("Failed to correctly clean up streams", e);
}
}
document.close();
But the PDF that is created looks like (viewing at 100% zoom):
Can anyone explain to me why the resulting PDf is (to my eyes at least) so different from my Original view and explain how to fix it?
Edit: Here's a link to the generated PDF in case that rovides any useful information: https://dl.dropboxusercontent.com/u/134344/KingdomSpasAgreement.pdf
According to the documentation:
public PdfDocument.PageInfo.Builder (int pageWidth, int pageHeight, int pageNumber)
Creates a new builder with the mandatory page info attributes.
Parameters
pageWidth The page width in PostScript (1/72th of an inch).
pageHeight The page height in PostScript (1/72th of an inch).
pageNumber The page number.
You use a
PageInfo pageInfo = new PageInfo.Builder(150, 150, 1).create();
Page page = document.startPage(pageInfo);
So, you create a page merely about 2 in x 2 in in size. In this small area only a part of the submit button fits. (The PDF page content actually even contains the string "Submit" but it is far outside the viewable area.)
As you are using the PrintedPdfDocument, you should instead use:
Page page = document.startPage(1);
(cf. the documentation again)

Taking a "screenshot" of a specific layout in Android

I have two main issues which are closely linked. I am looking at these problems from a programmatic point of view.
(1) - I wish to take a screenshot of the contents of a SPECIFIC layout, i.e. a ScrollView nested in a LinearLayout.
(2) - As the ScrollView has content that spills out of the screen (hence scrolling made possible), how can I ensure that the screenshot includes the elements that are not visible on the screen?
This is the current block of code I use. It does the job of taking a screenshot but only for the entire screen. This is even though R.id.boss is the ID of the ScrollView and not the main LinearLayout.
View view = findViewById(R.id.boss);
View v = view.getRootView();// this does not seem to make a difference
v.setDrawingCacheEnabled(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
v.setDrawingCacheEnabled(false);
Thanks in advance.
EDIT:
I've made a few mistakes. I used R.id.boss which is the wrong resource. I am now able to take a screenshot of the scrollview alone, less the out-of-screen parts.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/boss"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="F"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Analyze via image URL"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/mUrl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="http://" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/call"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_weight="0.3"
android:text="ABC"
android:src="#drawable/run" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ABC"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/filepath"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.55" />
<ImageView
android:id="#+id/cam"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_weight="0.15"
android:src="#drawable/cam" />
<ImageView
android:id="#+id/browse"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_weight="0.15"
android:src="#drawable/folder"
android:text="B" />
<ImageView
android:id="#+id/upload"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_weight="0.15"
android:src="#drawable/run"
android:text="A" />
</LinearLayout>
<LinearLayout
android:id="#+id/baba"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_weight="0.7" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="186dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Facial recognition"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/avmarwe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gender and age"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/skahasd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Expression and mood"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/dsfsfs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Celebrity Facial Match"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/c"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="0.7"
android:text="" />
<Button
android:id="#+id/share"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_weight="0.3"
android:text="" />
</LinearLayout>
</LinearLayout>
Thanks to you guys, I've finally found out what was wrong.
View v = view.getRootView(); should not be used because it will call the root view which I do not want. I mistakenly thought this did not make a difference because I had entered the wrong resource ID.
MeasureSpec somehow did not give a good account of the width and height. So I ended up using another method:
...
ScrollView z = (ScrollView) findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
...
As ScrollView's total height can be determined by the single child element that it has.
After making these changes, I am now able to take a screenshot of a nested ScrollView and all its contents, visible or not. For anyone interested, here is the block of code including the saving of the bitmap:
View u = findViewById(R.id.scroll);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
//Save bitmap
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_report.jpg'").format(new Date());
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try this it works fine for me
TableLayout tabLayout = (TableLayout) findViewById(R.id.allview);
if (tabLayout != null) {
Bitmap image = Bitmap.createBitmap(tabLayout.getWidth(),
tabLayout.getHeight(), Config.ARGB_8888);
Canvas b = new Canvas(image);
tabLayout.draw(b);
}
EDIT : after seeing OP's comment
You dont need to think about new activities at all.. Say you are in Activity right now.. Layout A is the main layout for the activity, Layout B and C are two child layouts inside Layout A. Like this,
Layout A -> Parent
|
-------Layout B
|
-------Layout C
Now if you want to take screenshot of C only
1) in onCreate() of activity
LinearLayout myCLayout = (LinearLayout)this.findViewbyId(R.id.my_c_layout);
ViewTreeObserver vto = myCLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//fully drawn, no need of listener anymore
myCLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
getDrawingBitmap();
}
});
where getDrawingBitmap() is a function..to take your screenshot..
public void getDrawingBitmap(){
LinearLayout myCLayout = (LinearLayout)this.findViewbyId(R.id.my_c_layout);
Bitmap b = myCLayout.getDrawingCache();
File file = saveBitmapAsFile(b);
}
EDIT: For ScrollView
I never tried it.. But I think you can do this..
1) first override scrollView, and override onMeasure function..
public class MyScrollView extends ScrollView{
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, yourFullScrollViewHeight));
}
}
and use MyScrollView in your layout.. here yourFullScrollViewHeight is the height of all scrollView content you need to take screenshot of.. I never tried this.. But it might work..
Here is the exact solution you want, It will display entire screen including content hidden in your ScrollView
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
FrameLayout root = (FrameLayout) inflater.inflate(R.layout.activity_main, null); // activity_main is UI(xml) file we used in our Activity class. FrameLayout is root view of my UI(xml) file.
root.setDrawingCacheEnabled(true);
Bitmap bitmap = getBitmapFromView(this.getWindow().findViewById(R.id.frameLayout)); // here give id of our root layout (here its my FrameLayout's id)
Display bitmap in your ImageView or use it as you want. Enjoy..
I also had this problem and coded it myself.
I used this code to take screenshot of whole layout.
I did modified the list size and some mathematical items.
See the following website,
Try this code
public Bitmap screenShot(View view)
{
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}

Categories

Resources