--> android source code full | Deskripsi Singkat Blog di Sini

android source code full

Find The Complete Source Code Android


Thursday, March 1, 2018

Print/Save PDF from webview

Print/Save PDF from webview

Print/Save PDF from Webview is usable when you want to save print webview content. you can see it in this tutorial.






Steps to use Print/Save PDF from Webview in your project.


  1. Crate a project(Print/Save PDF from Webview) in android studio.

  2. Create a Activity MainActivity.Java and update it-

    package com.pankaj.savepdffromwebview;
    import android.content.Context;
    import android.os.Bundle;
    import android.print.PrintAttributes;
    import android.print.PrintDocumentAdapter;
    import android.print.PrintManager;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    public class MainActivity extends AppCompatActivity {

    //create object of webView
    private WebView myWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initialize webview
    myWebView = findViewById(R.id.myWebView);

    //add webview client to handle event of loading
    myWebView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {

    //if page loaded successfully then show print button
    findViewById(R.id.fab).setVisibility(View.VISIBLE);
    }
    });

    //prepare your html content which will be show in webview
    String htmlDocument = "<html><body>" +
    "<h1>Webview Print Test </h1>" +
    "<h2>I am Webview</h2>" +
    "<p> By PointOfAndroid</p>" +
    "<p> This is some sample content.</p>" +
    "<p> By PointOfAndroid</p>" +
    "<p> This is some sample content.</p>" +
    "<p> By PointOfAndroid</p>" +
    "<p> This is some sample content.</p>" +
    "" +
    "" +
    "" + "Put your content here" +
    "" +
    "" +
    "</body></html>";

    //load your html to webview
    myWebView.loadData(htmlDocument, "text/HTML", "UTF-8");
    }

    //create a function to create the print job
    private void createWebPrintJob(WebView webView) {

    //create object of print manager in your device
    PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

    //create object of print adapter
    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

    //provide name to your newly generated pdf file
    String jobName = getString(R.string.app_name) + " Print Test";

    //open print dialog
    printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
    }

    //perform click pdf creation operation on click of print button click
    public void printPDF(View view) {
    createWebPrintJob(myWebView);
    }
    }

  3. Create a xml activity_main.xml and update it-

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
    tools:context="com.pankaj.savepdffromwebview.MainActivity">

    <!--webview-->
    <WebView
    android:id="@+id/myWebView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></WebView>

    <!--floating action button to print the webview content-->
    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="bottom|end"
    android:layout_margin="20dp"
    android:onClick="printPDF"
    android:src="@drawable/print"
    android:visibility="gone" />
    </RelativeLayout>

  4. All the application development process with Print/Save PDF from webview has completed, Now run the app and look the screen.









  5. Good bye, Thanks to read this blog.

Sunday, February 18, 2018

Bar Code & QR Code Scanner

Bar Code & QR Code Scanner

Bar Code & QR Code Scanner is used to scan BAR & QR code scanning. you can see it in this tutorial.





Steps to use Bar Code & QR Code Scanner in your project.

  1. Crate a project(Bar Code & QR Code Scanner Demo) in android studio.

  2. Create a Activity build.gradle and add a dependency to it- 

    // add dependency to use QR code scanning
    compile 'com.journeyapps:zxing-android-embedded:3.4.0'


  3. Now your build.gradle will be like-
    apply plugin: 'com.android.application'
    android {
    compileSdkVersion 26
    defaultConfig {
    applicationId "com.pankaj.barcodedemo"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    }

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    // add dependency to use QR code scanning
    compile 'com.journeyapps:zxing-android-embedded:3.4.0'
    }
     
  4. Create a activity MainActivity.Java and update it-

    package com.pankaj.barcodedemo;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.google.zxing.integration.android.IntentIntegrator;
    import com.google.zxing.integration.android.IntentResult;

    public class MainActivity extends AppCompatActivity {
    private IntentIntegrator qrScan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // initialize
    qrScan = new IntentIntegrator(this);

    //Specify scanning type like QR/Barcode. Default its scan both type.
    // qrScan.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
    //Add message to show when scannig
    qrScan.setPrompt("Scan a Barcode/QR Code");

    // Use a specific camera of the device
    qrScan.setCameraId(0);

    //enable beep on scan complete
    qrScan.setBeepEnabled(true);
    qrScan.setBarcodeImageEnabled(true);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    //open scanner
    qrScan.initiateScan();
    }
    });
    }

    //Getting the scan results
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {

    //if qrcode has nothing in it
    if (result.getContents() == null) {
    Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
    } else {
    try {

    //if qr contains data
    ((TextView) findViewById(R.id.tvScanData)).setText(result.getContents());
    Log.e("scan result", result.getContents());
    } catch (Exception e) {
    }
    }
    } else {
    super.onActivityResult(requestCode, resultCode, data);
    }
    }
    }
     
  5. Create a xml activity_main.xml and update it-

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.pankaj.barcodedemo.MainActivity">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">
    <TextView
    android:id="@+id/tvScanData"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="Scan result will show here." />
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_menu_camera" />

    </android.support.design.widget.CoordinatorLayout>
     
  6. If you want to open it in only in one mode like landscape or portrait then add below code to manifest

    <activity
    android:name="com.journeyapps.barcodescanner.CaptureActivity"
    android:screenOrientation="portrait"
    tools:replace="screenOrientation" />
     


  7. All the application development process with Bar Code & QR Code Scanner has completed, Now run the app and look the screen.
     
  8. Now click on camera button to open scanner.



     

  9. Now scan the QR or BAR code and see the result.








  10. Good bye, Thanks to read this blog.