Converting FrameLayout to Video? - android

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>

Related

How to take screenshot in ConstraintLayout?

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

How to share specific 1 card view of activity from Android App programmatically like Google Analytics App

I am working on an android app of social network and i am stuck at one place.
I want to share specific card view from an activity to facebook or whatsapp wherever user wants to share as an image like google analytics app.
check screenshot below
Suppose i want to share top 2 card then i can share like this to whatsapp or fb whever i want
First card
Second card
First i thought to take screenshot of app and share it to user's social app but it doesn't match to requirements and i have no idea how can we achieve this.
Help me to solve this problem.
You cannot share a 'Card' to other Apps, however you share a Bitmap (Screenshot) of the Card...
Here's how to do it -
Create a method like below -
public Bitmap ViewShot(View v) {
int height = v.getHeight();
int width = v.getWidth();
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas (b);
v.layout(0, 0 , v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Pass the CardView in the View parameter of ViewShot like -
Bitmap screenshot = ViewShot(myCardView);
Now you can save that Bitmap to a Temp file & share it with other app!
Create an image and sent it via intent. An easy way to create a bitmap of a view is to create a bitmap object with width and height equal to that of the view, create a Canvas for that bitmap, and call the view's onDraw function passing in that Canvas. That will make the View draw itself to the Bitmap.
If you want to share specific data of that card, my suggestion is Firebase Dynamic Links. I think this is what you are looking for. You can create a dynamic link for specific page of your app and share the link via whatever you want.
I was working on a Project where my task was to create a business card using XML and share the card on any social media app or mail
Here's the code that worked for me.
public class BusinessCard extends AppCompatActivity {
Button share;
CardView cardView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business_card);
share=findViewById(R.id.share);
cardView=findViewById(R.id.visitingCard);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Text",Toast.LENGTH_SHORT).show();
Bitmap screenshot = ViewShot(cardView);
}
});
}
public Bitmap ViewShot(View v) {
int height = v.getHeight();
int width = v.getWidth();
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas (b);
v.layout(0, 0 , v.getLayoutParams().width=750, v.getLayoutParams().height=700);
v.draw(c);
Intent intent =new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Select"));
return b;
}
}
XML layout(Code):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#color/purple_200">
<androidx.cardview.widget.CardView
android:id="#+id/visitingCard"
android:backgroundTint="#BCB14F"
android:layout_width="400dp"
android:layout_height="350dp"
app:cardElevation="10dp"
app:cardCornerRadius="30dp"
app:cardBackgroundColor="#color/gray"
app:cardPreventCornerOverlap="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:src="#drawable/e_removebg_preview"
android:layout_width="100dp"
android:layout_height="100dp">
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:textStyle="bold"
android:text="User Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="10dp"
android:textStyle="bold"
android:text="CEO"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Number :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="10dp"
android:text="+919876543210"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="email :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:text="www.googleuser#gmail.com"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="website :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:text="userwebsite.com"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3790 Las Vegas Blvd, South"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Las Vegas,Nevada ,United Status, 897845"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share">
</Button>
</LinearLayout>
I hope this will be helpful to you

capture frame layout has this issue

this is my xml file:
<RelativeLayout
android:id="#+id/relativeLayoutTemplateMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/LinearLayouttools"
android:layout_above="#+id/lltexttools"
android:layout_gravity="center">
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="centerInside" />
</FrameLayout>
</RelativeLayout>
i capture framelayout with this code:
fOut = new FileOutputStream(file);
framelayout.setDrawingCacheEnabled(true);
Bitmap bitmap = framelayout.getDrawingCache();
bitmap.compress(Bitmap.CompressFormat.jpeg, 100, fOut);
fOut.flush();
fOut.close();
but Around of the picture is black:
http://uupload.ir/files/z8cv_smsbaaz.ir_4.jpg
also when i set scaleType="FitXY":
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="FitXY"/>
image scale(i dont want scale image)
how i solve this problem?
you can use below method to create view to Bitmap .
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
Converting a view to Bitmap without displaying it in Android?

Cannot Convert Listview to Bitmap

I am trying to convert layout to bitmap. The layout contains Textview and Listview. Textview is displaying in Bitmap but Listview is displaying but not displaying(showing empty) in drawing cache. Here is my code
Print.xml
<LinearLayout
android:id="#+id/screen_print1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tax_amnt"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Tax : $200" />
<ListView
android:id="#+id/item_listview1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_gravity="center" />
</LinearLayout>
Print.java
LinearLayout screenshot = (LinearLayout) findViewById(R.id.screen_print1);
ListView items_display=(ListView)findViewById(R.id.item_listview1);
TextView tax_amount=(TextView)findViewById(R.id.tax_amnt);
tax_amount.setText("$5000");
ArrayList<HashMap<String, String>> bill_list1=OrderSummaryFragment.item_lists;
itl1 = new Itemlist(getApplicationContext(), bill_list1);
items_display.setAdapter(itl1);
screenshot.setDrawingCacheEnabled(true);
screenshot.buildDrawingCache();
Bitmap bm = screenshot.getDrawingCache();
screen_imageview.setImageBitmap(bm);
Update the lines
screenshot.setDrawingCacheEnabled(true);
screenshot.buildDrawingCache();
to
screenshot.setDrawingCacheEnabled(true);
screenshot.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
screenshot.layout(0, 0, screenshot.getMeasuredWidth(), screenshot.getMeasuredHeight());
screenshot.buildDrawingCache(true);

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