Thursday, March 01, 2018

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

APP Localization

APP Localization is used to provide the user interference in local languages to user for better understanding. you can see it in this tutorial.





Steps to use APPLocalization in your project.


  1. Crate a project(APPLocalizationDemo) in android studio.

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

    package com.pankaj.applocalizationdemo;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //no need to do anything other for localization application
            //will use default device language
        }
    }

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

    <?xml version="1.0" encoding="utf-8"?>
    <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:gravity="center"
        android:orientation="vertical"
        tools:context="com.pankaj.applocalizationdemo.MainActivity">
        <!--create four object to show in different language-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/app_name" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/app_pankaj" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/app_gurjar" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/app_developer" />
    </LinearLayout>
    
    
    
     
  4. Create a resource strings.xml in values of res and update it-

    <resources>
        <string name="app_name">App Localization Demo</string>
        <string name="app_pankaj">Pankaj</string>
        <string name="app_gurjar">Gurjar</string>
        <string name="app_developer">Android Developer</string>
    </resources>
    
    
    
     
  5. Create a folder values-ar in res.
  6. Create strings-ar.xml in this folder for Arabic language and update it-

    <resources>
        <string name="app_name">التطبيق التعريب التجريبي</string>
        <string name="app_pankaj">بانكاج</string>
        <string name="app_gurjar">غورجار</string>
        <string name="app_developer">الروبوت المطور</string>
    </resources>
    
        


  7. Create a folder values-hi in res.
  8. Create strings-hi.xml in this folder for Hindi language and update it-

    <resources>
        <string name="app_name">ऐप लोकलाइजेशन डेमो</string>
        <string name="app_pankaj">पंकज</string>
        <string name="app_gurjar">गुर्जर</string>
        <string name="app_developer">एंड्रॉइड डेवलपर</string>
    </resources>
    
     
  9. All the application development process with App Localization has completed, Now run the app and look the screen.
    Note : please set your device language to english


  10. Now please set your device language to Hindi  and open application.
  11. Now please set your device language to Arabic  and open application.





  12. Good bye, Thanks to read this blog.