Developers Club geek daily blog

1 year, 10 months ago
Often at implementation of mobile application for Android c use in it different third-party libraries requires to mention them in the project not to violate the right of 3 persons and not to get in Bang with your project in app store of Google Play.

We need to put the description of the used libraries in the appendix and to specify their license.
It is about libraries which are on GitHub.

The description of licenses is in a project tree in the README.md file. According to this link, it is possible to look at the description of the license of Picasso library (it is necessary to thumb through in the bottom).

So, this requirement is implemented by means of DialogFragment, it is possible to implement it and by means of AlertDialog (it is better for Fragment to use as it saves the status at revolution of a device). It is recommended to place this feature either in Navigation Drawer, or in Overflow.

That it was clear what there is a speech about, here is how it looks at one very popular application:

image

Implementation:

1.

It is necessary to create the assets folder in an app directory root (the right button it is clicked on app> new> folder> assets folder and we click finish of nothing without changing), where to place our .html the file with the description of libraries and licenses which we use.

With the right button it is clicked on assets> new> file> name.html

I called the file: open_source_licenses.html

Open_source_licenses.html file contents:

<html>
<head>
    <style>
        body {
        font-family: sans-serif;
        }
        pre {
        background-color: #eeeeee;
        padding: 1em;
        white-space: pre-wrap;
        }
    </style>
</head>
<body>

<h3>
    Apache License
    <br/>
    Version 2.0, January 2004
</h3>
<ul>
    <li>
        <b>Picasso</b>
        <br/>
        Copyright 2013 Square, Inc.
    </li>
</ul>
<pre>
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
</pre>

</body>
</html>



2.

We create the file in the layout folder in which we will just place only WebView and we will call it — dialog_license.xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />


3.

We create new java a class and we call it, for example — LicensesDialogFragment.java and we place such code in this class:

public class LicensesDialogFragment extends DialogFragment {

    public static LicensesDialogFragment newInstance() {
        return new LicensesDialogFragment();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        WebView view = (WebView) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_license, null);
        view.loadUrl("file:///android_asset/open_source_licenses.html");
        return new AlertDialog.Builder(getActivity(), R.style.Theme_AppCompat_Light_Dialog_Alert)
                .setTitle(getString(R.string.action_licenses))
                .setView(view)
                .setPositiveButton(android.R.string.ok, null)
                .create();
    }


4.

We place a line in which we will specify heading for our dialog in strings.xml:

<string name="action_licenses">Лицензии открытого ПО</string>


5.

To MainActivity.java or in yours of an aktivita where there is Navigation Drawer, we write the following method:

private void displayLicensesDialogFragment() {
        LicensesDialogFragment dialog = LicensesDialogFragment.newInstance();
        dialog.show(getFragmentManager(), "LicensesDialog");
    }


6.

Now, we call this method where we want. I put it on onNavigationItemSelected:

displayLicensesDialogFragment();


Do not forget to create the corresponding item in main.xml or activity_main_drawer.xml in dependence where you want to place your method.

Here that turned out at me:

image

P.S. Yes, a html-code it is possible and it is necessary to podmarafetit a little that it did not creep away, but it put already personally everyone, I wanted to show in this article how to work with licenses of libraries.
And you in the projects add the description of the used libraries?

29 people voted. 12 people refrained.

The users only registered can participate in poll. Enter, please.


This article is a translation of the original post at habrahabr.ru/post/274859/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus