ในปัจจุบัน การพัฒนา App บน Android นั้น นิยมนำ Library ต่าง ๆ มาช่วยให้การพัฒนา App นั้นง่ายขึ้น ลดความยุ่งยาก ซับซ้อน ในแต่ละเรื่องเป็นอย่างดี เพราะผู้พัฒนา Library นั้นได้จัดการไว้ให้หมดแล้ว และยิ่งบน Android Studio ด้วยแล้ว มีระบบ Dependency ที่ทำให้เรานำ Library ต่าง ๆ มาใช้งานได้ง่ายมาก
แต่ Library ส่วนใหญ่นั้น มักจะติด License หรือสัญญาอนุญาตประเภทต่าง ๆ ไว้ ไม่ว่าจะเป็น Apache, MIT, GPL, BSD หรืออื่น ๆ ซึ่งแทบจะทุกประเภทสัญญา มีข้อกำหนดว่า ต้องแสดงข้อสัญญาลงในส่วนใดส่วนหนึ่งของงานด้วย การจะนำ Library ต่าง ๆ มาใช้งานนั้น จึงควรที่จะทำให้ถูกต้องซะ จะได้ไม่เกิดปัญหา หรือถูกฟ้องร้องได้ในภายหลัง
รายละเอียดเบื้องต้นของ License ประเภทต่าง ๆ ลองอ่านบทความแนะนำ ที่ Link ท้ายบทความ
สำหรับบทความนี้ ขอแนะนำ Library ที่มีชื่อว่า License Fragement ซึ่งผมเป็นผู้พัฒนาขึ้นมาเอง เพื่อช่วยสร้างหน้าจอแสดงผลข้อมูล Open-source licenses ได้แบบง่าย ๆ นั่นเอง
List of contents
- App Example
- What’s License Fragment Library
- License Fragment Goals
- Build-in License Support
- How to use License Fragment
- Screenshot
- Contributor
App Example
ก่อนอื่น… มาดูตัวอย่างกันก่อนว่า App ต่าง ๆ นั้น เขาแสดงผลข้อมูล Open-source licenses อย่างไรบ้าง!?
รูปด้านล่าง มาจาก App Google Play Store ซึ่งได้ใส่ข้อมูล Open-source licenses ไว้ในเมนู Settings > About > Open-source licenses

ส่วนรูปนี้ มาจาก App YouTube ซึ่งก็ได้ใส่ข้อมูล Open-source licenses ไว้ที่เมนู About > Open-source licenses

อีกสักตัวอย่างนึง เป็น App Facebook ก็ได้ใส่ข้อมูล Open-source licenses ไว้ที่หน้า About > licenses

เห็นไม๊ครับ App ใหญ่ ๆ ดัง ๆ เค้าก็ทำให้ดูเป็นตัวอย่างหมดแล้ว โดย UI ที่เห็น ก็เรียบ ๆ ไม่เน้นสวยงามสักเท่าไหร่ ดูอย่างของ Google Play Store ยังแสดงผลบน Dialog แต่ข้อมูลล้นไปด้านข้างซะอย่างงั้น
What’s License Fragment Library
License Fragment คือ Android Library ที่ช่วยสร้างหน้าจอแสดงผลข้อมูล Open-source licenses ได้แบบง่าย ๆ โดยจะสร้างออกมาในรูปแบบของชิ้นส่วนหน้าจอ ที่เรารู้จักกันในชื่อว่า Fragment
โดยที่ License Fragment นั้น พัฒนาต่อยอดจาก Fragment
ใน Support Library v4 เพื่อให้สามารถใช้งานกับ Android เวอร์ชั่นเก่า ๆ ได้ และ RecyclerView Library v7 ในส่วนของคลาส RecyclerViewLicenseFragment
License Fragment Goals
เป้าหมายของ License Fragment ที่ตั้งไว้ นั้นมีดังนี้
- สร้าง Fragment สำหรับแสดงข้อมูล Open-source licenses ได้ง่าย ๆ ด้วยชุดคำสั่งเพียง 1 บรรทัด
- รองรับการใช้งานด้วย Tag
<fragment>
- แสดงข้อมูล License ของ Library ที่เกี่ยวข้องให้โดยอัตโนมัติ สามารถเปิด/ปิดความสามารถนี้ได้ (ตัวอย่าง – ถ้ากำหนดให้แสดงข้อมูลจาก Library Otto ก็จะแสดงข้อมูล Library OkHttp ให้อัตโนมัติ)
- มีหน้าจอพื้นฐานให้เลือก 3 ประเภท (
ScrollViewLicenseFragment
,ListViewLicenseFragment
และRecyclerViewLicenseFragment
) - สามารถเพิ่มข้อมูล License เองได้
- ปรับแต่งหน้าตาของหน้าจอได้
Build-in License Support
ภายใน License Fragment จะมีข้อมูลเบื้องต้นมาให้แล้ว ดังนี้
1. ประเภท License
- Apache License 2.0
- BSD 3-Clause
- BSD 2-Clause
- GPL-3.0
- MIT license
อ้างอิง – http://opensource.org/licenses
2. Library ยอดนิยม
How to use License Fragment
1. Installation
การใช้งาน License Fragment Library ใน Android Project สามารถเพิ่ม Dependency ใน build.gradle ได้ ดังนี้
- Gradle
1 2 3 |
dependencies { compile 'com.artit-k:license-fragment:1.0.1' } |
- Maven
1 2 3 4 5 |
<dependency> <groupId>com.artit-k</groupId> <artifactId>license-fragment</artifactId> <version>1.0.1</version> </dependency> |
2. Create License Fragment
2.1 สร้าง License Fragment จาก java code
ตัวอย่างข้อมูล
1 2 3 |
ArrayList<Integer> licenseIds = new ArrayList<>(); licenseIds.add(LicenseID.GSON); // Add License ID from LicenseID class licenseIds.add(LicenseID.RETROFIT); // Add License ID from LicenseID class |
สร้าง License Fragment
1 2 3 4 5 6 7 8 |
// Ex1 - Call newInstance() using ArrayList<Integer> Fragment fragment = ScrollViewLicenseFragment.newInstance(licenseIds); // Ex2 - Call newInstance() using array of int Fragment fragment = ListViewLicenseFragment.newInstance(new int[] { LicenseID.PICASSO }); // Ex3 - Call newInstance() using without parameter Fragment fragment = RecyclerViewLicenseFragment.newInstance(); |
2.2 สร้าง License Fragment จาก xml layout
ประกาศ Namespace whatever
(อะไรก็ได้) ใน root view ของ Layout
1 2 3 4 |
<YOUR_ROOT_VIEW ... xmlns:whatever="http://schemas.android.com/apk/res-auto" ...> |
เพิ่ม Tag <fragment>
และกำหนดข้อมูล License ด้วยการกำหนด Attribute whatever:lfLicenseID
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<!-- Ex1 - fragment tag with attribute whatever:lfLicenseID --> <fragment android:id="@+id/sv_license_fragment" android:name="com.artitk.licensefragment.ScrollViewLicenseFragment" android:layout_width="match_parent" android:layout_height="match_parent" whatever:lfLicenseID="GSON|RETROFIT" /> <!-- Ex2 - fragment tag with attribute whatever:lfLicenseID --> <fragment android:id="@+id/lv_license_fragment" android:name="com.artitk.licensefragment.ListViewLicenseFragment" android:layout_width="match_parent" android:layout_height="match_parent" whatever:lfLicenseID="PICASSO" tools:layout="@layout/layout_item_license" /> <!-- Ex3 - fragment tag without attribute whatever:lfLicenseID --> <fragment android:id="@+id/rv_license_fragment" android:name="com.artitk.licensefragment.RecyclerViewLicenseFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@layout/layout_item_license" /> |
Optional – เปิด/ปิด ความสามารถการแสดงผล Library ที่เกี่ยวข้อง โดยกำหนด Attribute whatever:lfLicenseChain
1 |
whatever:lfLicenseChain="false" <!-- Disable license chain (Default : Enable) --> |
Optional – ปรับแต่งหน้าตาของหน้าจอ โดยกำหนด Attribute whatever:lfTitleBackgroundColor
, whatever:lfTitleTextColor
, whatever:lfLicenseBackgroundColor
และ whatever:lfLicenseTextColor
1 2 3 4 |
whatever:lfTitleBackgroundColor="@color/title_bg_color" whatever:lfTitleTextColor="@color/title_text_color" whatever:lfLicenseBackgroundColor="@color/license_bg_color" whatever:lfLicenseTextColor="@color/license_text_color" |
3. Customize
หลังจากสร้าง License Fragment แล้ว สามารถปรับแต่งได้อีก ดังนี้
- เปิด/ปิด ความสามารถการแสดงผล Library ที่เกี่ยวข้อง
1 |
fragment.withLicenseChain(false); // Disable license chain (Default : Enable) |
- เพิ่มข้อมูล License อื่น ๆ
1 2 3 4 5 6 7 |
ArrayList<License> customLicenses = new ArrayList<>(); customLicenses.add(new License(this, "Test Library 1", LicenseType.MIT_LICENSE, "2000-2001", "Test Owner 1")); customLicenses.add(new License(this, "Test Library 2", LicenseType.GPL_30, "2002", "Test Owner 2")); fragment.addLicense(new int[] { LicenseID.PICASSO }) // Add More Licenses by array of int fragment.addLicense(licenseIds) // Add More Licenses by ArrayList<Integer> fragment.addCustomLicense(customLicenses); // Add Custom Licenses by ArrayList<License> |
- ปรับแต่งหน้าตาของหน้าจอ
1 2 3 4 5 6 7 |
CustomUI customUI = new CustomUI() // Create Customize UI from CustomUI class .setTitleBackgroundColor(Color.parseColor("#7fff7f")) .setTitleTextColor(getResources().getColor(android.R.color.holo_green_dark)) .setLicenseBackgroundColor(Color.rgb(127, 223, 127)) .setLicenseTextColor(Color.DKGRAY); fragment.setCustomUI(customUI); // Set Customize UI |
Screenshot
รูปต่อไปนี้ แสดง License Fragment เมื่อนำไปวางบน Container แล้ว

Contributor
สำหรับนักพัฒนาที่อยากร่วมพัฒนา License Fragment Library สามารถ Fork จาก GitHub ได้เลย โดยจุดที่อยากให้นักพัฒนาช่วยเพิ่ม มีดังนี้
- ข้อมูลประเภท License อื่น ๆ – โดยการเพิ่มไฟล์ในโฟลเดอร์ raw และเรียกใช้งานที่ไฟล์
ResourceManager.java
- ข้อมูล Library ยอดนิยม – ตรวจสอบได้จาก
TODO
- หน้าจอแบบใหม่ ๆ – โดยการสร้าง Fragment ที่ Extend จากคลาส
LicenseFragmentBase
และ Layout ใหม่ ๆ - อื่น ๆ – ตามแต่ท่าน ๆ จะคิดได้เลย
เมื่อพัฒนาเสร็จแล้ว ก็ Pull Request มาได้เลยนะ ถ้าตรงไหนทำไม่เป็น ก็หลังไมค์มาถามได้นะครับ หรือจะขอความสามารถเพิ่มเติม ก็ได้นะครับ ว่างแล้วจะทำให้
พิเศษ!! สำหรับ App ไหน ที่นำ License Fragment Library ไปใช้งาน กระซิบบอกกันหน่อยนะ เด๋วช่วยโฆษณาให้
บทความแนะนำ – Open Source License เรื่องใกล้ตัวที่ไม่ควรมองข้าม