how convert LinearLayout to Bitmap without reshaping in android? - 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;
}

Related

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

Convert inflated layout into a bitmap 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>

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>

How to set 4 images to linear layout acording to devices in android

I have 4 images within linearlayout that is created programmatically and i want to set textview on top of fourth image how to set??if u have piece of code please share with me??
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300, 1.0f);
public void path(ArrayList<Integer> integerImg) {
// int width=mContext.getWgetWindowManager().getDefaultDispaly.getWidth();
ImageView images;
for (int i = 0; i < integerImg.size(); i++) {
if (i < 4) {
images = new ImageView(mContext);
params.setMargins(10, 0, 0, 0);
images.setLayoutParams(params);
images.setScaleType(ImageView.ScaleType.FIT_XY);
images.setBackgroundResource(R.drawable.salon_profile_back);
images.setImageResource(integerImg.get(i));
linearImgGrid.addView(images);
}
Try this
TextView tv= new TextView(this);
tv.setText("hallo hallo");
tv.setId(5);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//add the 4th linear layout
((LinearLayout) linearLayout).addView(tv);
Instead of add an ImageView in the LinearLayout, you could create a RelativeLayout put inside an ImageView and a TextView and add RelativeLayout to LinearLayout, as this:
RelativeLayout.LayoutParams layoutParamsImage = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams layoutParamsText = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsText.addRule(RelativeLayout.CENTER_IN_PARENT);
for (int i = 0; i < integerImg.size(); i++) {
if (i < 4) {
RelativeLayout relativeLayout = new RelativeLayout(mContext);
relativeLayout.setLayoutParams(params);
ImageView images = new ImageView(mContext);
params.setMargins(10, 0, 0, 0);
images.setLayoutParams(layoutParamsImage);
images.setScaleType(ImageView.ScaleType.FIT_XY);
images.setBackgroundResource(R.drawable.salon_profile_back);
images.setImageResource(integerImg.get(i));
relativeLayout.addView(images);
TextView textView = new TextView(mContext);
textView.setLayoutParams(layoutParamsImage);
textView.setText("My Text");
relativeLayout.addView(textView);
linearImgGrid.addView(relativeLayout);
}
}
You can use following code for reference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_marginTop="70dp"
android:weightSum="3"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/user" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Master" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/report1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Report" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:layout_marginRight="#dimen/feed_item_margin"
android:weightSum="2"
android:layout_weight="1"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/expense1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Expenses" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_marginTop="70dp"
android:weightSum="3"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/collection" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Collection" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:layout_weight="1"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/monthly" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Report" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/extra" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Expenses" />
</LinearLayout>
</LinearLayout>
</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