📜 ⬆️ ⬇️

Implementing a list of used libraries using DialogFragment in Android application

Often, when implementing a mobile application for Android using various third-party libraries, it is necessary to mention them in your project in order not to violate the rights of 3 persons and not to get banned with your project in the Google Play app store.

In the application, we need to put a description of the libraries used and indicate their license.
These are libraries that are on github .

The license description is located in the project tree in the README.md file. From this link, you can look at the description of the license library Picasso (you need to scroll to the bottom).

So, this requirement is implemented with the help of DialogFragment, you can implement it with the help of AlertDialog (Fragment is better to use, since it retains its state when the device is flipped). It is recommended to place this feature either in the Navigation Drawer or in Overflow.
')
To make it clear what is at stake, here’s how it looks from one very popular application:

image

Implementation:

one.

We need to create an assets folder in the app's root directory (right click on app> new> folder> assets folder and click finish without changing anything), where to place our .html file with the description of libraries and licenses that we use.

Right click on assets> new> file> name.html

I called the file: open_source_licenses.html

Contents of the open_source_licenses.html file:

<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

Create a file in the layout folder in which we simply place only WebView and name 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

Create a new java class and call it, for example - LicensesDialogFragment.java and put the following code into 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(); } 


four.

Put strings in strings.xml in which we specify the title for our dialogue:

 <string name="action_licenses">  </string> 


five.

In MainActivity.java or in your activit, where Navigation Drawer is located, we write the following method:

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


6

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

 displayLicensesDialogFragment(); 


Remember to create an appropriate item in main.xml or activity_main_drawer.xml depending on where you want to put your method.

That's what happened with me:

image

PS Yes, the html-code itself can and should be podrarafet a little, so that it does not spread out, but this is already the case for everyone personally, I in this article wanted to show how to work with licenses of libraries.

Source: https://habr.com/ru/post/274859/


All Articles