Convert inflated layout into a bitmap android - android

I am inflating a layout and converting it into and then i will share it using the as usual intents.
inflating the layout using -
LayoutInflater inflater2 = LayoutInflater.from(this);
qr_sheetview = inflater2.inflate(R.layout.dummy_layout, mainlayout, false);
I tried three methods -
convert the inflated view into bitmap using this method
qr_sheetview.setDrawingCacheEnabled(true);
qr_sheetview.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
qr_sheetview.layout(0, 0, qr_sheetview.getMeasuredWidth(), qr_sheetview.getMeasuredHeight());
qr_sheetview.buildDrawingCache(true);
sharedbmap = Bitmap.createBitmap(qr_sheetview.getDrawingCache());
But using this method my layout gets shrinked and overlapped.
width was 1107 height was 448
I am using Framelayout as rootlayout and relative layout for inner views.
Second method i am using is this - >
sharedbmap = Bitmap.createBitmap(qr_sheetview.getWidth(), qr_sheetview.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(sharedbmap);
qr_sheetview.draw(canvas);
in this method error occurred : height and width must be > 0
using this also gives me the same error
qr_sheetview.post(new Runnable() {
#Override
public void run() {
sharedbmap = loadBitmapFromView(qr_sheetview);
shareBitmap(adjustOpacity(sharedbmap), "temporary");
}
});
the third method is this - >
public static Bitmap loadBitmapFromView(View v) {
int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
v.measure(specWidth, specWidth);
Log.d("TAG", "height" + v.getMeasuredHeight());
Log.d("TAG", "width" + v.getMeasuredWidth());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
in this i am getting the same result as in first.
the Layout is ->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<RelativeLayout
android:id="#+id/mainqrlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<RelativeLayout
android:id="#+id/layout001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:gravity="center|bottom"
android:text="Some text"
android:textColor="#212121"
android:textStyle="bold"
android:textSize="24sp" />
<ImageView
android:id="#+id/logo"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/appname"
android:layout_marginTop="10dp"
android:src="#drawable/logomain" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/layout003"
android:layout_below="#+id/layout001">
<ImageView
android:id="#+id/qrimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:visibility="gone" />
<ImageView
android:id="#+id/qr_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
card_view:srcCompat="#drawable/kj_qr_logo_svg" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout003"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/vpaname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="kdvkjadsfkasmd"
android:textColor="#212121" />
<TextView
android:id="#+id/qr_vpa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vpaname"
android:layout_centerHorizontal="true"
android:text="skdmfkjasdf"
android:textColor="#444444" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/qr_vpa"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:gravity="center"
android:padding="10dp"
android:text="ckasfdowmdmwkmdwkdmkwmdklmekld \n sdafasdfasdfaasdf"
android:textColor="#727272" />
<TextView
android:id="#+id/companyname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="#string/companyname"
android:textColor="#727272"
android:textSize="10sp" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>

Related

How to take screenshot of a layout having a camera preview and text view?

i want to take screenshot of camera and map and address text view only, whenever i try to take screenshot its only saving map and text area but the camera area gets all white screen onlyHere is my xml layout , and i want to take screenshot of relative layout with id rl_photo, but after taking screenshot of layout its only taking the map and textview but not camera image , camera image gets all white screen .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#android:color/black"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Photo"
android:visibility="gone"
android:textStyle="bold"
android:id="#+id/tv_save"
android:background="#android:color/black"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:textColor="#ffffff"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_above="#+id/rl_action"
android:id="#+id/rl_photo"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="120dp"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:layout_height="120dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="gravity"
android:gravity="right"
android:layout_toRightOf="#+id/map"
android:layout_alignParentBottom="true"
android:background="#66000000"
android:layout_margin="10dp"
android:padding="10dp"
android:textColor="#android:color/white"
android:layout_alignParentRight="true"
android:id="#+id/tv_latlong"
android:text="xbdjhgjrejgtrjbtbjr"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/rl_action"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/button_capture"
android:layout_width="50dp"
android:layout_margin="10dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:src="#drawable/record"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="30dp"
android:src="#drawable/flip"
android:id="#+id/img_flip"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_height="30dp"/>
<ImageView
android:layout_width="30dp"
android:src="#drawable/gallery"
android:id="#+id/img_gallery"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_height="30dp"/>
</RelativeLayout>
</RelativeLayout>
For screenshot
Add image view on above of FrameLayout
While capturing the ss set camera bitmap in image view and make Imageview visible one process is done to make image view invisible
For SS follow below Process
XML file
<?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="#android:color/black">
<TextView
android:id="#+id/tv_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:background="#android:color/black"
android:text="Save Photo"
android:textColor="#ffffff"
android:textStyle="bold"
android:visibility="gone" />
<RelativeLayout
android:id="#+id/rl_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl_action">
<ImageView
android:id="#+id/ImageViewOFCamera"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent"/>
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_margin="10dp" />
<TextView
android:id="#+id/tv_latlong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/map"
android:background="#66000000"
android:gravity="right"
android:padding="10dp"
android:text="xbdjhgjrejgtrjbtbjr"
android:textAlignment="gravity"
android:textColor="#android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/button_capture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="#drawable/record" />
<ImageView
android:id="#+id/img_flip"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="#drawable/flip" />
<ImageView
android:id="#+id/img_gallery"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/gallery" />
</RelativeLayout>
</RelativeLayout>
for ss capture in java File
private Bitmap bitmap;
bitmap = ScreenshotUtil.getInstance().takeScreenshotForView( rl_photo);
public class ScreenshotUtil {
private static ScreenshotUtil mInstance;
private ScreenshotUtil() {
}
public static ScreenshotUtil getInstance() {
if (mInstance == null) {
synchronized (ScreenshotUtil.class) {
if (mInstance == null) {
mInstance = new ScreenshotUtil();
}
}
}
return mInstance;
}
public Bitmap takeScreenshotForView(View view) {
view.measure( MeasureSpec.makeMeasureSpec( view.getWidth(), MeasureSpec.EXACTLY ),
MeasureSpec.makeMeasureSpec( view.getHeight(), MeasureSpec.EXACTLY ) );
view.layout( (int) view.getX(), (int) view.getY(),
(int) view.getX() + view.getMeasuredWidth(),
(int) view.getY() + view.getMeasuredHeight() );
view.setDrawingCacheEnabled( true );
view.buildDrawingCache( true );
Bitmap bitmap = Bitmap.createBitmap( view.getDrawingCache() );
view.setDrawingCacheEnabled( false );
return bitmap;
}
public Bitmap takeScreenshotForScreen(Activity activity) {
return takeScreenshotForView( activity.getWindow().getDecorView().getRootView() );
}
}

how convert LinearLayout to Bitmap without reshaping in android?

i'm developing an app for pax printer, so i need to convert my xml Layout(Linear,Relative,...) to Bitmap,my problem is when i convert any layout to image, the Created Bitmap width size is reshaped. why this happens? how i can solve this problem?
this is my convert code:
LayoutInflater lInflater = (LayoutInflater)getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = lInflater.inflate(R.layout.testlayout, null);
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
imgview.setImageBitmap(b);
storeImage(b);
i tried many other ways of converting but i got same results.
this is one of my layouts:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/printLL"
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="wrap_content"
android:orientation="vertical"
android:background="#fff">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/printing">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2sp"
android:gravity="center"
android:text="Header"
android:textSize="13sp" />
<TextView
android:id="#+id/counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2sp"
android:gravity="center"
android:text="Title" />
<TableLayout
android:id="#+id/record_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#ffffff"
android:gravity="center"
android:shrinkColumns="*"
android:stretchColumns="*"
>
<TableRow android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/CustomerName"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="Username" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/customer_id"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="#+id/postalcode"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/Address"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/ID"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="#+id/customer_number"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/Serial"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="#+id/Digit"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/PrevCode"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="#+id/PrevDebt"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/PrevDate"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/Usage_type"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextViwe" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
the layout view
converted layout to bitmap
note that no matter what's in layout, textview,imageview,... or which layout type i used .allways same problem
thanks for any help
Try this out :
public static Bitmap view2Bitmap(final FrameLayout view) {
if (view == null) return null;
boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
boolean willNotCacheDrawing = view.willNotCacheDrawing();
view.setDrawingCacheEnabled(true);
view.setWillNotCacheDrawing(false);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap;
if (null == drawingCache) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
drawingCache = view.getDrawingCache();
if (drawingCache != null) {
bitmap = Bitmap.createBitmap(drawingCache);
} else {
bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
}
} else {
bitmap = Bitmap.createBitmap(drawingCache);
}
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCacheDrawing);
view.setDrawingCacheEnabled(drawingCacheEnabled);
return bitmap;
}

Android Take Screeshot Width Is Too Wide

I am using the code below to programatically capture the screen layout of a linear layout, the problem is that the screenshot image is too wide, as if it is a landsacpe image, my activity is potrait where height is > width but for some reason this code captures a screenshot with width > height and it looks like a horizontal rectangle rather than a vertical rectangle. The view are correctly oriented so it definitely capturing in the correct orrientation (potrait) but just too wide with the views further apart and smaller.
private void takeScreenShot() {
ll_screenshot = findViewById(R.id.ll_screenshot);
try {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "store");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
ll_screenshot.setDrawingCacheEnabled(true);
ll_screenshot.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
ll_screenshot.layout(0, 0, ll_screenshot.getMeasuredWidth(), ll_screenshot.getMeasuredHeight());
ll_screenshot.buildDrawingCache(true);
Bitmap bm = Bitmap.createBitmap(ll_screenshot.getDrawingCache());
ll_screenshot.setDrawingCacheEnabled(false); // clear drawing cache
path = new File(android.os.Environment.getExternalStorageDirectory(),"store") + "/screenshot.jpg";
savePic(bm, path);
shareFile();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos;
try {
fos = new FileOutputStream(strFileName);
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void shareFile() {
String file_path = path.toString();
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(file_path);
if(fileWithinMyDir.exists()) {
intentShareFile.setType("image/*");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file_path));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Share File...");
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Share File...");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
}
screenshot:
Edit (XML):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bg_menu_h"
>
<LinearLayout
android:id="#+id/ll_screenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/bg_menu_h"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/bg_menu_h"
>
<TextView
android:id="#+id/tvItem"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textStyle="bold"
android:gravity="center"
android:text=""
android:textSize="25sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/bg_menu_h"
>
<TextView
android:id="#+id/tvBrandLeft"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="Sunlight"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tvBrandRight"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="Sunlight"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/bg_menu_h"
>
<TextView
android:id="#+id/tvSizeLeft"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="1KG"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tvSizeRight"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="1KG"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
>
<ImageView
android:id="#+id/ivTrophyLeft"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
/>
<ImageView
android:id="#+id/ivItemLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingRight="5dp"
/>
<ImageView
android:id="#+id/ivItemRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingRight="5dp"
/>
<ImageView
android:id="#+id/ivTrophyRight"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_price"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tvPriceLeft"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textStyle="bold"
android:gravity="center"
android:text="0.00"
android:textSize="35sp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tvPriceDifference"
android:layout_height="70dp"
android:layout_width="70dp"
android:textStyle="bold"
android:gravity="center"
android:text="0.00"
android:textSize="20sp"
android:background="#drawable/roundstar"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/tvPriceRight"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textStyle="bold"
android:gravity="center"
android:text="0.00"
android:textSize="35sp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_shop"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/ivShopLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/shop"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingRight="5dp"
android:layout_margin="5dp"
/>
<ImageView
android:id="#+id/ivShopRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|right"
android:background="#android:color/transparent"
android:src="#drawable/shop"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingLeft="5dp"
android:layout_margin="5dp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_bottom_sub"
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:gravity="right"
>
<ImageView
android:id="#+id/ivLogo"
android:layout_height="match_parent"
android:layout_width="20dp"
android:src="#drawable/ic_launcher"
android:background="#android:color/transparent"
android:scaleType="fitEnd"
/>
<TextView
android:id="#+id/tvCompare"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:textStyle="bold|italic"
android:gravity="left|bottom"
android:text="App"
android:textSize="10sp"
android:layout_marginRight="10dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_blank"
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
>
</LinearLayout>
in your method takeScreenShot() do this change.
private void takeScreenShot() {
ll_screenshot = findViewById(R.id.main);
----
//other code
----
}
// change to R.id.main inplace of R.id.ll_screenshot

Android: draw xml Layout on canvas

I have a custom PrintDocumentAdapter and draw pages on my own, so I should draw xml layout on the page canvas:
private void drawPage(PdfDocument.Page page, List<Object> objects,
int pageNumber) {
Canvas canvas = page.getCanvas();
...
}
there is an TableLayout in my xml. I want table width to fill the entire page but it exceed the page if the width bigger than page width or is smaller than the page if its width is smaller than the page width. I used both wrap_content and match_parent but none of them worked. I even tried fix width and height.
private void drawPage(PdfDocument.Page page, List<Payment> payments,
int pageNumber) {
Canvas canvas = page.getCanvas();
PdfDocument.PageInfo pageInfo = page.getInfo();
canvas.save();
canvas.translate(leftMargin , topMargin);
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.payments_table_layout, null);
v.setLayoutParams(new
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TableLayout tableLayout = (TableLayout) v.findViewById(R.id.payments_table_layout);
for(int i= 0 ; i< objects.size(); ++i){
//... some code
}
frameLayout.addView(v);
frameLayout.measure(200 , 200);
frameLayout.layout(100, 100, 100, 100);
frameLayout.draw(canvas);
canvas.restore();
}
R.layout.payments_table_layout:
<?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="wrap_content"
android:layout_gravity="center">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="some text"
style="#style/CustomFont.Wave"/>
</FrameLayout>
<TableLayout
android:id="#+id/payments_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:stretchColumns="*"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#ccc">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"/>
</TableRow>
</TableLayout>
</LinearLayout>
Now the Result:
I shouldn't stretch all columns of TableLayout so I changed this attribute to :
android:stretchColumns="1,3,7"
android:shrinkColumns="5"
and set the layout width to width of the page like this ( we should care the dimension unit): in my customPrintDocumentAdapter
#Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
myPdfDocument = new PrintedPdfDocument(context, newAttributes);
pageHeight =
newAttributes.getMediaSize().getHeightMils()/1000 * 72;
pageWidth =
newAttributes.getMediaSize().getWidthMils()/1000 * 72;
...
}
and my new drawPage method:
private void drawPage(PdfDocument.Page page, List<Payment> payments,
int pageNumber) {
...
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(pageWidth, pageHeight));
...
}
please try below xml , in which I used weightsum and layout weight in TableRow and its child textviews
android:weightSum="4"
android:layout_width="0dp"
android:layout_weight="1"
<?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="wrap_content"
android:layout_gravity="center">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="some text"
/>
</FrameLayout>
<TableLayout
android:id="#+id/payments_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:stretchColumns="*"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:layout_gravity="center_horizontal"
android:background="#ccc">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"/>
</TableRow>
</TableLayout>
</LinearLayout>

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..
Image after Capturing screen shot..
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.
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);
}
});
Hey friends I got the solution of above problem.
Follow below steps:-
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>
This xml looks like.
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 .
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();
}
}
The output be looks like..
I hope it will helpful for you.

Categories

Resources