Open top menu


Print HTML document and webpage in android application.
Printing an html doc with webview involves loading an html res or building an html doc as a string.

Creating custom print view step by step

1.       Creat a webviewclient that starts a print job after the html res is loaded.
2.       Load the html res into the webview object.

private WebView testWebView;

private void TestWebViewPrint() {
   
// Create a WebView object specifically for printing
   
WebView webView = new WebView(this);
    webView.setWebViewClient(
new WebViewClient() {

       
public boolean shouldOverrideUrlLoading(WebView view, String url) {
           
return false;
        }

       
@Override
        
public void onPageFinished(WebView view, String url) {
            Log.i(
"TAG", "page finished loading " + url);
            createTestWebPrintJob(view);
           
testWebView = null;
        }
    });

   
// Generate an HTML document on the fly:
   
String htmlDocument = "

Test Content

Androidbeginner tutorial point, "
+
           
"Hello, Hello...
";
    webView.loadDataWithBaseURL(
null, htmlDocument, "text/HTML", "UTF-8", null);

   
// Keep a reference to WebView object until you pass the PrintDocumentAdapter
    // to the PrintManager
   
testWebView = webView;
}


private void createTestWebPrintJob(WebView webView) {
   
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {

        PrintManager printManager = (PrintManager)
this.getSystemService(Context.PRINT_SERVICE);

        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

        String jobName = getString(R.string.
app_name) + " Print Test";

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

you can load a webpage for printing by replacing the method loadUrl().
 webView.loadUrl("https://developer.android.com/distribute/index.html");



Read more


Example - How to check Bluetooth Off Or On

<uses-permission android:name="android.permission.BLUETOOTH" />

private boolean getBlueToothOn(Context context) {
    BluetoothAdapter btAdapter = ((Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
            ? ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter()
            : (BluetoothAdapter.getDefaultAdapter()));

    if (btAdapter == null) {
        return false;
    }
    if (btAdapter.getState() == BluetoothAdapter.STATE_ON) {
        return true;
    }
}
Read more

Example :-How to set Button background Tint

1.In AppCompact library, we can set button background tint .

<Button
   
android:id="@+id/btn_done"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_below="@+id/seq_layout_attr"
    android:layout_margin="30dp"
    android:backgroundTint="@color/colorPrimary"
    android:text="@string/summit"
    android:textSize="@dimen/textSize"
    android:textColor="@color/white"
    android:textStyle="bold" />


2. Drawable background for a view based on a color (int) value. I succeeded by using the code:

ColorStateList csl = new ColorStateList(new int[][]{{}}, new int[]{colors});
btn_done.setBackgroundTintList(csl);

3. create a Color State List Resource.

xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <
item
       
android:color="#your_color_here" />
</
selector>


  btn_done .setBackgroundTintList(this.getResources()
                                                             .getColorStateList(R.color.your_xml_name));





Read more


Example-Custom Multiple Selection Spinner with Searching in android?


activity_maim.xml

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.project.mespl.multipleseleectspinner.MainActivity">


    <com.project.mespl.multipleseleectspinner.MultiSpinnerSearch
       
android:id="@+id/searchMultiSpinner"
        android:layout_margin="20dp"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>


 


MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

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

        ArrayList list = new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            list.add(String.valueOf(i));
        }

        List listArray0 = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            KeyPairBoolData h = new KeyPairBoolData();
            h.setId(i + 1);
            h.setName(list.get(i));
            h.setSelected(false);
            listArray0.add(h);
        }

        MultiSpinnerSearch searchMultiSpinnerUnlimited = (MultiSpinnerSearch) findViewById(R.id.searchMultiSpinner);
        searchMultiSpinnerUnlimited.setItems(listArray0, -1, new SpinnerListener() {

            @Override            public void onItemsSelected(List items) {

                for (int i = 0; i < items.size(); i++) {
                    if (items.get(i).isSelected()) {
                        Log.i(TAG, i + " : " + items.get(i).getName() + " : " + items.get(i).isSelected());
                    }
                }
            }
        });

    }

}





Item List




 Search View





More




























































Read more



Why we Used Manifest file in android App ?

Explain:-the manifest file in its root Directory.  Must every application Manifest File.The manifest file provide all essential information about your app to the android system.
1.       Its name the java package for the application .package name serves as a unique identifier for the application.
2.       Described the components of the application, which include the activites,services,broadcast receivers, and content providers .
3.       The classes that implement each of the components and publishes the capabilities.
4.       It determines the processes that host the application component.
5.       Declares the permissions that the application must have in order to access protected parts of the API and interact with other application.
6.       It declares the minimum level of the Android API that the application requires.



Here is an example of the Manifest file.

xml version=”1.” Encoding=”utf-8”?>

<manifest>

<uses-permission />

<permission />

<permission- group />

<instrumentation />

<uses-sdk />

<uses-configuraton />

<uses-feature />

<support-screen />

<support-gl-texture />



<application >

    <activity>

        <intent-filter>

            <action />

            <category />



            <data />

        </intent-filter>

    </activity>



    <activity–alias>

    <intent-filter />

    < Meta-data />

</activity–alias>





<service>

    <intent-filter />

    <mete-data />

</service>


<
receiver>
    <
intent-filter />
    <
meta-data />
</
receiver>

<
provider >
    <
grant-uri-permission/>
    <
path-permission />
    <
meta-data />
</
provider>

<
uses-library />

</
application>

    </
manifest>


Action

Add action to an intent filter .for eg

<intent-filter>
<
action android:name="android.intent.action.EDIT" />
<
action android:name="android.intent.action.INSERT" />
<
action android:name="android.intent.action.DELETE" />
</
intent-filter>

An element must contain multiple  element.
But to assigine  one of these action to the attribute

<action android:name="com.example.project.TRANSMOGRIFY" />


Activity-alias

 The target must be in the same Application as the alias and it must be declared before the alias in the manifest.


    android:exported=["true" | "false"]
    android:icon="drawable resource"
    android:label="string resource"
    android:name="string"
    android:permission="string"
    android:targetActivity="string" >
    . . .
   
 
 

Category

Adds a category name to an intent filter. See Intents and Intent Filters for details on intent filters and the role of category specifications within a filter.

Syntax-
<category android:name="string" />
 


Data

The specification can be just a data type (the mimeType attribute), just a URI, or both a data type and a URI.

Syntax-

<data android:scheme="string"
   
android:host="string"
   
android:port="string"
   
android:path="string"
   
android:pathPattern="string"
   
android:pathPrefix="string"
   
android:mimeType="string" />

 
 
Grant-URI-Permission

Data subsets are indicated by the path part of a content: URI. (The authority part of the URI identifies the content provider.) Granting permission is a way of enabling clients of the provider that don't normally have permission to access its data to overcome that restriction on a one-time basis.

Syntax-
<grant-uri-permission android:path="string"
   
android:pathPattern="string"
   
android:pathPrefix="string"
/>

Instrumentation

Base class for implementing application instrumentation code . class that enables you to monitor an application's interaction with the system.

Syntax –

<instrumentation android:functionalTest=["true" | "false"]
android:handleProfiling=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:targetPackage="string" />

 
Meta –Data

A name-value pair for an item of additional, arbitrary data that can be supplied to the parent component. A component element can contain any number of <meta-data> subelements.

Syntax-
<meta-data android:name="string"
   
android:resource="resource specification"
   
android:value="string" />


Receiver

Declares a broadcast receiver (a BroadcastReceiver subclass) as one of the application's components. Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications, even when other components of the application are not running.

Syntax-
  <receiver android:directBootAware=["true" | "false"]
    android:enabled=["true" | "false"]
    android:exported=["true" | "false"]
    android:icon="drawable resource"
    android:label="string resource"
    android:name="string"
    android:permission="string"
    android:process="string" >
    
</
receiver>
Read more