<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pattadon Adhipanyasarij on Medium]]></title>
        <description><![CDATA[Stories by Pattadon Adhipanyasarij on Medium]]></description>
        <link>https://medium.com/@ult3mate?source=rss-31ca54a28384------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5cCo86yjRsza0U4zFZV6WA.jpeg</url>
            <title>Stories by Pattadon Adhipanyasarij on Medium</title>
            <link>https://medium.com/@ult3mate?source=rss-31ca54a28384------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 19:57:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ult3mate/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[ Android ]Notification Group ทำยังไงกันนะ]]></title>
            <link>https://medium.com/@ult3mate/android-notification-group-%E0%B8%97%E0%B8%B3%E0%B8%A2%E0%B8%B1%E0%B8%87%E0%B9%84%E0%B8%87%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B8%99%E0%B8%B0-1195c71a3dac?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/1195c71a3dac</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[notifications]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Mon, 11 Mar 2019 15:41:57 GMT</pubDate>
            <atom:updated>2019-03-11T15:41:57.327Z</atom:updated>
            <content:encoded><![CDATA[<p>บทความนี้จะสอนส่วนของการจัดการ Notification ของ Android กันนะครับ จะไม่ได้ลงลึกถึงการสอนวิธีการ Push Notification กันนะครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*B1PQAuqisywBGo5eNqCWFw.png" /><figcaption><a href="https://developer.android.com/training/notify-user/group">https://developer.android.com/training/notify-user/group</a></figcaption></figure><p>เราเคยเห็น Notification หน้าตาแบบข้างบนกันมั้ยเอ่ย ??</p><p>ซึ่งเจ้านี้เค้าเรียกว่า Group Notification นะ มันมีให้ใช้ตั้งแต่ Android 7.0 (API LEVEL 24) กันแล้ว</p><p>อย่าสับสนระหว่าง Notification Group กับ Notification Channel Group นะครับ มันเป็นคนละอย่างกันนะ !!</p><blockquote>Notification Channel Group นั้น มีให้ใช้ตั้งแต่ Android 8.0 (API LEVEL 26) นะครับ หากใครเขียนแอปไว้ที่ compileSdkVersion ตั้งแต่ 26 แล้วละก็ จำเป็นต้องทำ Notification Channel Group กันด้วยนะครับ ไม่งั้นจะยิง Notification ไม่ได้เน้อ !!</blockquote><p>เอาละ คราวนี้มาลง Code วิธีจัดการกันดีกว่า โดยจำลองโจทย์ง่ายๆกัน คือ</p><p>มี Social App อันนึง ต้องการแยก Notification Group 2 ประเภท คือ <br>1) Notification ประเภท Following <br>2) Notification เมื่อมี Post ใหม่ของ Following คนนั้น</p><p>ก่อนจะเริ่มลงมือทำ Notification เราคงต้องคิด Payload ของ Notification ก่อนว่า ต้องการแบบไหนกัน</p><p>ซึ่งผมก็จะแบ่ง Payload ตามประเภทไว้ดังนี้</p><p>ประเภท Following</p><pre>{<br>    &quot;to&quot; : &quot;token...&quot;,<br>    &quot;data&quot; : {<br>     &quot;body&quot; : &quot;I’m Following You na ja&quot;,<br>     &quot;id&quot; : &quot;dU41dg3n0gbLQpLnYOQNaa3sAw03&quot;,<br>     &quot;type&quot; : &quot;profile_type&quot;<br>    }<br>}</pre><p>ประเภท Post ใหม่ของ Following คนนั้น</p><pre>{<br>    &quot;to&quot; : &quot;token...&quot;,<br>    &quot;data&quot; : {<br>     &quot;body&quot; : &quot;Oreo create new post.&quot;,<br>     &quot;id&quot; : &quot;Ehvhus5dbMyHmoV2CV4E&quot;,<br>     &quot;type&quot; : &quot;content_type&quot;<br>    }<br>}</pre><p>โดยจากข้างบน เราจะแบ่ง Key ตามนี้กันครับ<br>Body จะเป็น Message ของ Notification ที่ได้รับ<br>id คือ id ของ type ของประเภท Notification <br>profile_type หมายถึง id ของ user คนนั้น <br>content_type หมายถึง id ของ content นั้นๆ</p><p>เอาละ ได้ Payload แล้ว มาลุยฝั่ง Code ของ Android กันดีกว่า !!!</p><p>ก่อนอื่นเลย เรามาลองสร้าง Notification แบบง่ายๆกันก่อน</p><pre>val groupNotificationBuilder = NotificationCompat.Builder(this, channelId)<br>    .setSmallIcon(R.drawable.<em>ic_launcher_foreground</em>)<br>    .setContentTitle(getString(R.string.<em>app_name</em>))<br>    .setContentText(data[&quot;body&quot;])<br>    .setSound(defaultSoundUri)<br>    .setAutoCancel(true)<br>    .setContentIntent(contentIntent)<br>    .build()</pre><pre>val notificationManager = getSystemService(Context.<em>NOTIFICATION_SERVICE</em>) as NotificationManager<br><br>if (Build.VERSION.<em>SDK_INT </em>&gt;= Build.VERSION_CODES.<em>O</em>) {<br>    val channel = NotificationChannel(channelId,<br>        &quot;Default Channel&quot;,<br>        NotificationManager.<em>IMPORTANCE_DEFAULT</em>)<br>    notificationManager.createNotificationChannel(channel)<br>}<br><br>NotificationManagerCompat.from(this).<em>apply </em><strong>{<br>    </strong>notify(0 ,newNotification)<br>}</pre><p>เอาละ ลอง Push Notification ดู</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*xXVBAv3FxJGkaGfR_jcrkQ.png" /></figure><p>ก็ดูปกติดีเนอะ คราวนี้เราไม่อยากให้ Notification Update Id เดิม เลยเปลี่ยน Notify ให้เป็น id ที่ไม่ซ้ำกันซะเลย</p><pre>NotificationManagerCompat.from(this).<em>apply </em><strong>{<br>    </strong>notify(System.currentTimeMillis().toInt() ,newNotification)<br>}</pre><p>จากนั้นยิง Payload ซะ !!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*3rLgHXztjubXVY_LkHeNxA.png" /></figure><p>ได้แล้ววว ….. แต่ไม่สวยงามเลยอะ คิดสภาพว่ายิงส่งมาเป็น 100 หน้าตา UI จะเป็นอย่างไงกันนะ !?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*RZLH_qsI7MbOUfTJglaSQQ.png" /><figcaption>OMGGGGGGG</figcaption></figure><p>OMGGGGGG !!โคตรดูไม่ดีเลยยยยยย ไม่สวยงามอีกต่างหาก เยอะเกิน ถ้าเป็นแบบนี้ User คงปัดทิ้งอย่างเดียวเลยครับ</p><p>มาลองปรับ Notification ให้เป็น Group Notification กัน</p><p>เอาละคราวนี้มาลองเล่น Group Notification ดู เพื่อให้ Noti ของเราออกมาสวยงาม</p><p>มาสร้างตัว NoticationGroup โดย การจะระบุว่า NotificationBuilder นี้เป็น Notification Group จะต้องเพิ่มคำสั่ง</p><pre><strong>.setGroup(key)<br>.setGroupSummary(true)</strong></pre><p>ตามนี้ครับ</p><pre>val groupNotificationBuilder = NotificationCompat.Builder(this, channelId)<br>    .setSmallIcon(R.drawable.<em>ic_launcher_foreground</em>)<br>    .setContentTitle(getString(R.string.<em>app_name</em>))<br>    .setContentText(data[&quot;body&quot;])<br>    .setSound(defaultSoundUri)<br>    <strong>.setGroup(data[&quot;type&quot;])<br>    .setGroupSummary(true)</strong><br>    .setAutoCancel(true)<br>    .setContentIntent(contentIntent)<br>    .build()</pre><p><strong>setGroup(key)</strong> เป็นการระบุว่า Key ของ GroupNotification กับ Notification ปกติ ถ้าหาก key เดียวกันมันจะยัด notification เข้ามาใน Group นี้ทันที</p><p><strong><em>setGroupSummary(Boolean)</em></strong> เป็นการระบุว่า Notification นี้เป็น Group ใหญ่นะครับ</p><p>คราวนี้เมื่อเราได้ตัว Group Notification แล้ว มาลองใส่ไส้ให้กับตัว Group Notification ตามนี้ดูครับ</p><pre>val newNotification = NotificationCompat.Builder(this, channelId)<br>    .setSmallIcon(R.drawable.<em>ic_launcher_foreground</em>)<br>    .setContentTitle(getString(R.string.<em>app_name</em>))<br>    .setContentText(data[&quot;body&quot;])<br><strong>    .setGroup(data[&quot;type&quot;])</strong><br>    .setAutoCancel(true)<br>    .setContentIntent(pendingIntent)<br>    .build()<br><br><br>val groupNotificationBuilder = NotificationCompat.Builder(this, channelId)<br>    .setSmallIcon(R.drawable.<em>ic_launcher_foreground</em>)<br>    .setContentTitle(getString(R.string.<em>app_name</em>))<br>    .setContentText(data[&quot;body&quot;])<br>    .setSound(defaultSoundUri)<br>    .setGroup(data[&quot;type&quot;])<br>    .setGroupSummary(true)<br>    .setAutoCancel(true)<br>    .setContentIntent(contentIntent)<br>    .build()<br><br>val notificationManager = getSystemService(Context.<em>NOTIFICATION_SERVICE</em>) as NotificationManager<br><br><strong>NotificationManagerCompat.from(this).<em>apply </em>{<br>    notify(System.currentTimeMillis().toInt() ,newNotification)</strong></pre><pre><strong>    // ไว้สำหรับแยก Group Notification<br>    if (data[&quot;type&quot;] == &quot;content_type&quot;) {<br>        notify(0, groupNotificationBuilder)<br>    }else{<br>        notify(1, groupNotificationBuilder)<br>    }<br>}</strong></pre><p>สังเกตตรง Notify เราจะยิง Group Notification ทุกครั้ง หลังจากที่ยิง Notification ใหม่ ทุกครั้ง<br>ส่วนตรง if นั้น เจ้าของ blog อยากแยก Group Notification เป็น 2 Group จึงแยก id Group Notification ไว้นั่นเอง <br>(ถ้าใช้ id เดียวกัน Group Notification ตัวเก่าจะโดนตัวใหม่ทับแทน)</p><p>คราวนี้ผมจะลองยิง Payload ชุด 1 4 ครั้ง กับชุด 2 2 ครั้ง จะได้ประมาณนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*x4CirBQn4xjOenh3yZI8JA.png" /></figure><p>ถ้า Expand ออกมา ก็จะได้ประมาณนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*yqAI5x24miN4tONpK1Alqg.png" /></figure><p>คราวนี้ลองยิงซัก 10 รอบดู</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*GVV-CLMBl-gU58vDTSunmw.png" /></figure><p>สังเกตเห็นว่า ตัวช่อง Notification ขวาล่างจะมี ​+5 ซึ่งตัว GroupNotification นี้จะอธิบายว่า มันมีมากกว่าที่เห็นอยู่อีก 5 อัน</p><p>จุดระวังอีกจุดนึงคือ Click Event ตัว Notification มันจะแยกกัน</p><p>ถ้าหากเรากดตอนที่เรายังไม่ Expand ออกมา มันจะทำงาน Intent ตัว Group<br>ถ้าพอเรา Expand ออกมาแล้วมันจะทำงานตาม Intent แต่ละตัวของมันเองเลย</p><p>สามารถศึกษาโค้ดเพิ่มเติมได้ที่นี้เลยครับ<br><a href="https://github.com/googlesamples/android-Notifications">https://github.com/googlesamples/android-Notifications</a></p><p>Blog นี้ก็คงสั้นๆเท่านี้แหละ หวังว่าคงเป็นประโยชน์เน้อ<br>Happy Coding กันเน้อ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1195c71a3dac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[ Android ] Chip Component เท่ๆ ของ Material Design]]></title>
            <link>https://medium.com/@ult3mate/android-chip-component-%E0%B9%80%E0%B8%97%E0%B9%88%E0%B9%86-%E0%B8%82%E0%B8%AD%E0%B8%87-material-design-db7cf8cf745c?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/db7cf8cf745c</guid>
            <category><![CDATA[material-design]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[chips]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Mon, 14 Jan 2019 10:58:24 GMT</pubDate>
            <atom:updated>2019-01-14T10:58:24.895Z</atom:updated>
            <content:encoded><![CDATA[<p>Chip เป็น Component ของ Material Design ไว้เพื่อ input, choice, filter หรือ action สำหรับ user</p><p>หลายๆคนอาจจะไม่รู้มันคือส่วนไหน ลองดูแอปอย่างพวก gmail ตามภาพครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bO152oMeNfqBH6L0od7Y_g.png" /><figcaption><a href="https://material.io/design/assets/1PAItZL1RXY_fWc8ZWFDJKqGRVV4OQwuy/purpose-01.png">https://material.io/design/assets/1PAItZL1RXY_fWc8ZWFDJKqGRVV4OQwuy/purpose-01.png</a></figcaption></figure><p>ตัวนี้แหละครับ คือ Chip ซึ่งมันทำอะไรได้อีกเยอะเลยครับ วันนี้จะมาลองสร้าง Chip Component กันนะครับ</p><p>ก่อนจะเริ่ม เราต้องเข้าใจพื้นฐานก่อนว่า ส่วนต่างๆของ Chip มีอะไรบ้าง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*yEHb8WOjLgH0K1dHKLWZOQ.png" /></figure><ol><li>Container</li><li>Chip Icon( optional ) + Check Icon (Optional)</li><li>Text</li><li>Close Icon ( optional )</li></ol><h3>ขั้นตอนเพิ่ม Library</h3><p>เพิ่ม dependency เข้าไปใน build.gradle กันก่อนเลยครับ</p><pre>implementation &quot;com.android.support:design:$supportLibraryVersion&quot;</pre><p>สำหรับคนที่ Update เป็น AndroidX แล้ว ให้ใช้ตัวนี้นะครับ</p><pre>implementation &#39;com.google.android.material:material:1.0.0&#39;</pre><p>ตอนนี้ androidX เวอร์ชั่นล่าสุดเป็นตัวนี้นะครับ</p><pre>implementation &#39;com.google.android.material:material:1.1.0-alpha02&#39;</pre><h3><strong>มาดูวิธีใช้งานกัน !</strong></h3><p>เรามาลองเพิ่ม layout Chip กันดีกว่า ลองพิมพ์คำว่า Chip โผล่ขึ้นมาแล้ว !!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*HuWlkvYyskjVNEUqaxbpPg.png" /></figure><p>Chip กับ ChipGroup คืออะไร ???<br>สำหรับคนที่เคยใช้ RadioButton กับ RadioGroup จะมองว่าคล้ายๆกันก็ได้ครับ</p><h3>Chip</h3><pre>&lt;com.google.android.material.chip.Chip<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;Hello World!&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;<br>        app:layout_constraintRight_toRightOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot;/&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/138/1*WHtFOsMXsW_JZ_E34aDZag.png" /><figcaption>หน้าตาประมาณนี้</figcaption></figure><h3>“Warning”</h3><p>หากใครได้ UI ตามภาพข้างล่าง ดันไปได้ CheckBox ธรรมดา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/160/1*t0gEiHdkKqoYPb1zWYSraQ.png" /><figcaption>เห้ย ทำไมเป็น CheckBox ว้าาาาาา</figcaption></figure><p>ให้ไปแก้ Style ที่ AppTheme ตามนี้เน้อ</p><pre>Theme.MaterialComponents<br>Theme.MaterialComponents.NoActionBar<br>Theme.MaterialComponents.Light<br>Theme.MaterialComponents.Light.NoActionBar<br>Theme.MaterialComponents.Light.DarkActionBar</pre><pre>&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.Light.NoActionBar&quot;&gt;<br>&lt;/style&gt;</pre><p>ถ้าดูจาก Document หรือลองกดเข้าไปดู เจ้า Chip เนี้ยมัน extends มาจาก AppCompatCheckBox นะครับ มันเลยเป็นสาเหตุที่มันกลายเป็น CheckBox</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nPTYRm7wDZUUimYrSiFt7Q.png" /><figcaption><a href="https://developer.android.com/reference/com/google/android/material/chip/Chip">https://developer.android.com/reference/com/google/android/material/chip/Chip</a></figcaption></figure><p>ถ้าไม่เปลี่ยน Style ให้เป็นตามข้างบน กด Run แล้ว แอประเบิดเลยนะครับ</p><p>Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).</p><h3>Chip Group</h3><p>เป็น ViewGroup ที่มคุณสมบัติ FlowLayout (โคตร cool)</p><p>สำหรับคนที่ไม่รู้ว่า FlowLayout คืออะไร ลองดูได้จากตัวอย่างนี้ครับ</p><pre>&lt;com.google.android.material.chip.ChipGroup<br>        android:id=&quot;@+id/entry_chip_group&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:text=&quot;Android&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:text=&quot;iOS&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:text=&quot;Application&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:text=&quot;Web&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:text=&quot;React Native&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:text=&quot;Function&quot;/&gt;<br>&lt;/com.google.android.material.chip.ChipGroup&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*y_Gknko5bw2w-x8DoYmOlQ.png" /></figure><p>แต่ถ้าเราอยากได้แค่แถวเดียว โดยที่สามารถ ScrollView แนวนอนได้ละ</p><p>ทำได้ง่ายๆเลยครับ</p><pre>&lt;HorizontalScrollView<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;&gt;<br><br>    &lt;com.google.android.material.chip.ChipGroup<br>            android:id=&quot;@+id/entry_chip_group&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            <strong>app:singleLine=&quot;true&quot;</strong>&gt;<br><br>        &lt;com.google.android.material.chip.Chip<br>                style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:text=&quot;Android&quot;/&gt;<br><br>        &lt;com.google.android.material.chip.Chip<br>                style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:text=&quot;iOS&quot;/&gt;<br><br>        &lt;com.google.android.material.chip.Chip<br>                style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:text=&quot;Application&quot;/&gt;<br><br>        &lt;com.google.android.material.chip.Chip<br>                style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:text=&quot;Web&quot;/&gt;<br><br>        &lt;com.google.android.material.chip.Chip<br>                style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:text=&quot;React Native&quot;/&gt;<br><br>        &lt;com.google.android.material.chip.Chip<br>                style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:text=&quot;Function&quot;/&gt;<br>    &lt;/com.google.android.material.chip.ChipGroup&gt;<br><br>&lt;/HorizontalScrollView&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*6l56oVLNVBiRjzgAPCaVmg.gif" /></figure><p><strong>รูปแบบของ Chip</strong></p><p>เจ้าตัว Chip Component ตาม Guideline แล้ว มันมีทั้งหมด 4 แบบด้วยกันครับ โดยที่เราสามารถเลือก type ของ chip ง่ายๆ เพียงแค่เปลี่ยน style ตามนี้ครับ</p><pre>style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>style=&quot;@style/Widget.MaterialComponents.Chip.Choice&quot;<br>style=&quot;@style/Widget.MaterialComponents.Chip.Filter&quot;<br>style=&quot;@style/Widget.MaterialComponents.Chip.Action&quot; (Default)</pre><p>** กรณีไม่ได้ใส่ style เข้าไป ตัว Chip จะเลือก type Action เป็น Default ครับ</p><h4>1. Entry Chip</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rHxAtjOs-URYSN_Jwx8z7A.png" /></figure><p>โดยที่เราจะได้ Chip icon , Close icon และ Check Icon มาให้โดยอัติโนมัติ เราน่าจะเห็นเจ้านี้กันบ่อยตอนส่ง mail ใน gmail ของ google นะครับ</p><p>ตัวอย่าง Entry</p><pre>&lt;com.google.android.material.chip.Chip<br>        style=&quot;@style/Widget.MaterialComponents.Chip.Entry&quot;<br>        app:chipIcon=&quot;@drawable/ic_lock_24dp&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:text=&quot;Message&quot;<br>        android:layout_height=&quot;wrap_content&quot;/&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*DafdrdkdljPnAQ5WQM2mlA.png" /></figure><p>โดย Event ปุ่มปิด เราจะต้อง implement code ตามนี้</p><pre>chip.setOnCloseIconClickListener <strong>{ <br>    // TODO Close Icon Click<br>}</strong></pre><p>ส่วน Event Chip นั้น ใช้ setOnClickListener ได้ปกติเลย</p><p>หรือจะ Add Chip ผ่าน Programmatically แบบนี้ก็ได้ครับ</p><pre>private fun addChip(text: String): Chip {<br>    val chip = Chip(this)<br>    chip.<em>text </em>= text<br>    chip.<em>chipIcon </em>= ContextCompat.getDrawable(<em>baseContext</em>, R.drawable.ic_lock_24dp)<br><br>    chip.setChipIconTintResource(R.color.<em>selector_choice_state</em>)<br>    chip.<em>isClickable </em>= true<br>    chip.<em>isCheckable </em>= false<br>    entry_chip_group.addView(chip as View)<br>    chip.setOnCloseIconClickListener <strong>{ </strong>entry_chip_group.removeView(chip as View) <strong>}<br>    </strong>return chip<br>}</pre><h4>2. Choice Chip</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d7DLWDdOi2b9G3gFXloWIg.png" /></figure><p>เป็น single selection จาก choice ที่มีทั้งหมด โดยเจ้า choice นี้โดยส่วนใหญ่จะใช้ครอบด้วย ChipGroup เสมอ</p><pre>&lt;com.google.android.material.chip.ChipGroup<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Choice&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            app:chipBackgroundColor=&quot;@color/selector_choice_state&quot;<br>            android:text=&quot;Easy&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Choice&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            app:chipBackgroundColor=&quot;@color/selector_choice_state&quot;<br>            android:text=&quot;Normal&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Choice&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            app:chipBackgroundColor=&quot;@color/selector_choice_state&quot;<br>            android:text=&quot;Hard&quot;/&gt;<br><br>    &lt;com.google.android.material.chip.Chip<br>            style=&quot;@style/Widget.MaterialComponents.Chip.Choice&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            app:chipBackgroundColor=&quot;@color/selector_choice_state&quot;<br>            android:text=&quot;Hardest&quot;/&gt;<br><br>&lt;/com.google.android.material.chip.ChipGroup&gt;</pre><p>selector_choice_state.xml</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br>    &lt;item android:color=&quot;@color/blue&quot; android:state_checked=&quot;true&quot; /&gt;<br>    &lt;item android:color=&quot;@color/gray&quot; android:state_checked=&quot;false&quot; /&gt;<br>    &lt;item android:color=&quot;@color/gray&quot; /&gt;<br>&lt;/selector&gt;</pre><p>ลอง Run !!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*3WUHjQDG3PfdIU8_z4mW5w.gif" /></figure><p>อ้าว ??? ไหนบอก single selection ไงฟร่ะะะะะ</p><p>อย่างที่บอกไว้ คุณสมบัติของ Chip มัน extends จาก CheckBox <br>เราจะต้องใส่ค่า app:singleSelectionให้เจ้า ChipGroup เพื่อบอกมันว่า เราจะทำ Choice หล่าวนี้ให้เป็น single selection นะ</p><pre>&lt;com.google.android.material.chip.ChipGroup<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        <strong>app:singleSelection=&quot;true&quot;</strong>&gt;</pre><pre>...</pre><pre>&lt;/com.google.android.material.chip.ChipGroup&gt;</pre><p>มาลองดูอีกที</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*5QTp1uxIcdn66xmgE76yhQ.gif" /></figure><p>สวยงาม !</p><p>คราวนี้มาลองลง Code กันบ้าง (ขอเป็น kotlin นะจ๊ะ)</p><p>ลองมา implement code เพื่อเช็คว่าเราคลิ๊ก item อันไหน ตามข้างล่างนี้</p><pre>chip_group.setOnCheckedChangeListener <strong>{ </strong>group, checkedId <strong>-&gt;<br>    </strong>when (checkedId) {<br>        R.id.<em>easy_chip </em>-&gt; {<br>            Toast.makeText(<em>baseContext </em>, &quot;Easy&quot; , Toast.<em>LENGTH_LONG</em>).show()<br>        }<br>        R.id.<em>normal_chip </em>-&gt; {<br>            Toast.makeText(<em>baseContext </em>, &quot;Normal&quot; , Toast.<em>LENGTH_LONG</em>).show()<br>        }<br>        R.id.<em>hard_chip </em>-&gt; {<br>            Toast.makeText(<em>baseContext </em>, &quot;Hard&quot; , Toast.<em>LENGTH_LONG</em>).show()<br>        }<br>        R.id.<em>hardest_chip </em>-&gt; {<br>            Toast.makeText(<em>baseContext </em>, &quot;Hardest&quot; , Toast.<em>LENGTH_LONG</em>).show()<br>        }<br>        else -&gt; Toast.makeText(<em>baseContext </em>, &quot;Not Select&quot; , Toast.<em>LENGTH_LONG</em>).show()<br>    }<br><strong>}</strong></pre><p>ก็ไม่มีอะไรมากเนอะ น่าจะเข้าใจกันดี</p><h4>3. Filter Chip</h4><p>รูปแบบ filter นั้นคล้ายกับ choice แต่สิ่งที่เพิ่มเติมคือ เครื่องหมายติ๊กถูก จะโผล่ขึ้นมาทุกครั้งที่เราเลือก chip นั้นๆ ซึ่งถ้าหากเราเลือก style filter เครื่องหมายติ๊กถูกนี้ขึ้นมาเมื่อ Chip นั้นโดน Select แล้วนะครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o5B9yfWI2VvybRuMZUYRSA.png" /></figure><pre>&lt;com.google.android.material.chip.Chip<br>        android:id=&quot;@+id/computer_chip&quot;<br>        style=&quot;@style/Widget.MaterialComponents.Chip.Filter&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:textColor=&quot;@color/selector_text_state&quot;<br>        app:chipBackgroundColor=&quot;@color/selector_choice_state&quot;<br>        android:text=&quot;คอมพิวเตอร์&quot;/&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*WIcoxKVFxJ8WL50XT5c78w.png" /></figure><p>ทำเหมือน Choice เลยครับ เปลี่ยนแค่ style แค่นั้นเอง</p><p>สามารถเอาเครื่องหมายติ๊กถูกออกได้ผ่าน xml ตามนี้เลยครับ</p><pre>app:checkedIconEnabled=&quot;false&quot;</pre><p>หรือ</p><pre>app:checkedIconVisible=&quot;false&quot;</pre><p>โดยตาม Guideline ของ Google นั้น บอกว่า อย่าใช้ Filter Chip ถ้าหากมีตัวเลือก filter เพียงอันเดียว</p><h4>4. Action Chip (Default)</h4><p>action เป็น default ของ Chip เลยครับ ทำงานคล้าย Button ของเราเลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yAou_zxx3wHnIxQBZOvV6w.png" /></figure><p>โดยเมื่อกำหนด style นี้เราจะได้แค่ chip icon มาอย่างเดียวครับ</p><pre>actionChip.setOnClickListener<strong>{<br>    <br>}</strong></pre><p><strong>ถ้าทั้ง 4 แบบ ไม่ตรงตามที่เราต้องการละ</strong></p><p>สามารถ Custom Chip ได้เองตามที่ต้องการได้เลยครับ</p><h3>Chip Attributes</h3><p>Feature | Relevant attributes — — — — –| — — — — — — — — — — Label | android:text <br>android:textAppearance Shape | app:chipCornerRadius Size | app:chipMinHeightBackground | app:chipBackgroundColor Border | app:chipStrokeColor <br>app:chipStrokeWidth Ripple | app:rippleColor Chip Icon | app:chipIconVisible <br>app:chipIcon <br>app:chipIconTint <br>app:chipIconSize Close Icon | app:closeIconVisible <br>app:closeIcon <br>app:closeIconSize <br>app:closeIconTint Checkable | android:checkable Checked Icon | app:checkedIconVisible <br>app:checkedIcon Motion | app:showMotionSpec <br>app:hideMotionSpec Paddings | app:chipStartPadding <br>app:iconStartPadding <br>app:iconEndPadding <br>app:textStartPadding <br>app:textEndPadding <br>app:closeIconStartPadding <br>app:closeIconEndPadding <br>app:chipEndPadding TouchTarget | app:chipMinTouchTargetSize <br>app:ensureMinTouchTargetSize</p><p>ลองเอาไปลองเล่นกันได้เลยนะ :)</p><p>ซึ่งตาม Guideline ของ Chip นั้น สามารถศึกษาได้จากลิงค์นี้ได้เลยครับ</p><p><a href="https://material.io/design/components/chips.html#">Chips</a></p><p>Happy Coding จ้า</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db7cf8cf745c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] Prevent Multiple Touch Recyclerview]]></title>
            <link>https://medium.com/@ult3mate/android-prevent-multiple-touch-recyclerview-2667827a1bc3?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/2667827a1bc3</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[recyclerview]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Tue, 25 Dec 2018 09:47:28 GMT</pubDate>
            <atom:updated>2018-12-25T09:47:28.015Z</atom:updated>
            <content:encoded><![CDATA[<p>มาฝาก trick เล็กๆ ของ Android กันครับ</p><p>วิธีแก้ปัญหาการกด event recyclerview ไม่ให้กดได้หลาย Adapter พร้อมๆกัน</p><p>เพิ่ม xml attribute ใน recyclerview แค่นี้ครับ</p><pre>android:splitMotionEvents=&quot;false&quot;</pre><p>หวังว่าคงเป็นประโยชน์เล็กๆนะครับ <br>Happy Coding</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2667827a1bc3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] มาลองใช้ Paging library Architecture Components]]></title>
            <link>https://medium.com/@ult3mate/android-%E0%B8%A1%E0%B8%B2%E0%B8%A5%E0%B8%AD%E0%B8%87%E0%B9%83%E0%B8%8A%E0%B9%89-paging-library-architecture-components-7d8faa7a7bbe?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d8faa7a7bbe</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Tue, 24 Jul 2018 12:44:03 GMT</pubDate>
            <atom:updated>2018-07-24T12:44:03.222Z</atom:updated>
            <content:encoded><![CDATA[<p>Blog นี้จะมาสอนวิธีการใช้งาน Paging Library แบบคร่าวๆกัน</p><h4>Paging Library คืออะไร ?</h4><p>Developer ส่วนใหญ่มักจะต้องเจอกับ application ที่ต้องแสดงข้อมูลจำนวนมากๆด้วย Recycler View และต้องเพิ่ม function บางอย่างเช่น Load more เข้าไป แต่ถ้าเราใช้ Architecture Components ของ Android เราจะต้องจัดการเขียน Code ลำบากขึ้นไปอีกขั้นนึง นั่นทำให้ทีมพัฒนา Android Framework ออกเจ้า Paging มา</p><blockquote>ก่อนที่จะเริ่มใช้งานเจ้า Paging ผมขอแนะนำให้ทุกคนรู้เรื่องเกี่ยวกับ Architecture ทั้ง 3 ตัวนี้ก่อน Room, LiveData ,ViewModel เพราะมันเกี่ยวข้องกับ Paging ที่เราจะต้องใช้นั่นเอง</blockquote><h3>ทำไมถึงต้องใช้ Paging ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OhX4GjqAcFdVfn2W9PdN-Q.png" /><figcaption>อ้างอิงจาก Slide งาน Google IO 2018</figcaption></figure><p>โดยทั่วไปการแสดงข้อมูลบน RecyclerView เราจะออกแบบ Structure ได้ประมาณนี้</p><ol><li>เรียกข้อมูลจาก Server มาเขียนลง Room (Database)</li><li>นำข้อมูลจาก Database มา query ส่งผ่านมาที่ ViewModel</li><li>LiveData จาก ViewModel จะ Update เข้าสู่ UI โดยอัติโนมัติ ทำ Update ใน RecyclerView</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GHaVKv34JR1UzvfWQBrRjg.png" /><figcaption>เบื้องหลังการทำงานทั่วไป (ไม่ใช่ Paging นะ)</figcaption></figure><p>** โดยถ้าใครไม่ได้อยากใช้ Room เปลี่ยนเป็นยิงข้อมูลตรงเข้าสู่ ViewModel เลยก็ได้ (ตัดข้อ 2 ออก)</p><p>ปัญหาที่เกิดขึ้น คือ หากเราไม่ได้ใช้ Paging Component หรือต่อให้ทำ Load more เอง ก็ลำบากในการจัดการ ถ้าเรามี ข้อมูลซัก 10000 item นั้นหมายความว่า ตอนที่เข้ามาในนี้ เราจะทำการดึงข้อมูล 10000 item มาโชว์ แล้วถ้าหากข้อมูลมีการเปลี่ยนแปลง</p><p>นั่นหมายความว่าเราต้องอัพเดพข้อมูลทั้งหมด 10000 item !!</p><p>นั่นละครับ ถึงเป็นที่มาว่า ทำไมทีมพัฒนา Android Framework จึงออกเจ้า</p><p>Paging Library มีหลักๆมีอะไรบ้าง ?</p><ul><li><a href="https://developer.android.com/reference/android/arch/paging/PagedList.html">PagedList</a></li><li><a href="https://developer.android.com/reference/android/arch/paging/DataSource.html">DataSource</a> and <a href="https://developer.android.com/reference/android/arch/paging/DataSource.Factory.html">DataSource.Factory</a></li><li><a href="https://developer.android.com/reference/android/arch/paging/LivePagedListBuilder.html">LivePagedListBuilder</a></li><li><a href="https://developer.android.com/reference/android/arch/paging/PagedList.BoundaryCallback.html">BoundaryCallback</a></li><li><a href="https://developer.android.com/reference/android/arch/paging/PagedListAdapter.html">PagedListAdapter</a></li></ul><p>หลักๆก็จะมีประมาณ 5 ตัวนี้</p><h3><strong>ก่อนเริ่มใช้งาน ก็ต้องประกาศเจ้าสิ่งนี้ใน gradle ก่อนเลย</strong></h3><pre>implementation &#39;android.arch.paging:runtime:1.0.0&#39;</pre><h3>ตัว Architecture Component Paging สามารถใช้งานแบบไหนได้บ้าง</h3><ul><li><strong>Network only</strong> จะใช้ <strong>Retrofit</strong> เป็นตัวโหลดจัดการข้อมูล</li><li><strong>Database only </strong>จะใช้ <strong>Room</strong> ของ Database เป็นตัวจัดการข้อมูล</li><li><strong>Network and database </strong>จะใช้ <strong>PagedList.BoundaryCallback </strong>เดี๋ยวจะอธิบายเพิ่มเติมทีหลังครับ</li></ul><h3>ก่อนจะเริ่มใช้งาน เราควรเข้าใจสิ่งพวกนี้ก่อนนะครับ</h3><ul><li><strong>DiffUtil</strong> ของ RecyclerView</li></ul><p>เจ้าสิ่งนี้เริ่มเพิ่มเข้ามาใน RecyclerView เวอร์ชั่น 24.2.0 ถ้าพูดสั้นๆ คือมันช่วยจัดการแทนที่เราจะใช้ notifyDataSetChange() ที่จะทำการอัพเดพ ทุก Item เจ้านี้จะช่วยปรับการ Update เฉพาะ item ที่มีการเปลี่ยนแปลง เพื่อทำให้ Performance <br>ของแอปดีขึ้น</p><h4>ตัวอย่างการใช้งาน</h4><pre>val DiffCallback = object : DiffUtil.ItemCallback&lt;FeedResult&gt;() {<br>    override fun areItemsTheSame(oldItem: FeedResult?, newItem: FeedResult?): Boolean {<br>        return oldItem?.contentId == newItem?.contentId<br>    }<br><br>    override fun areContentsTheSame(oldItem: FeedResult?, newItem: FeedResult?): Boolean {<br>        return oldItem == newItem<br>    }<br>}</pre><p>เจ้าของ blog แนะนำลองไปอ่าน DiffUtil ของคนอื่นดู น่าจะเข้าใจดีกว่า</p><h4>เริ่มเข้าใจทีละส่วนก่อนละกันเนอะ</h4><h3>Configuration ของ Paging</h3><pre>val config = PagedList.Config.Builder()<br>        .setPageSize(pageSize)<br>        .setInitialLoadSizeHint(pageSize * 2)<br>        .<a href="https://developer.android.com/reference/android/arch/paging/PagedList.Config.Builder.html#setPrefetchDistance(int)">setPrefetchDistance</a>(5)              .setEnablePlaceholders(false)<br>        .build()</pre><p><strong>setPageSize(int)</strong> คือกำหนด Size ที่โหลดได้มาจากตัว DataSource<br><strong>setInitialLoadSizeHint(int)</strong> คือกำหนดตัวค่าเริ่มต้นว่า จะโหลดมากี่ Item<br><a href="https://developer.android.com/reference/android/arch/paging/PagedList.Config.Builder.html#setPrefetchDistance(int)"><strong>setPrefetchDistance</strong></a><strong>(int)</strong> คือให้โหลดไอเทมที่ไม่แสดงผลก่อนตามจำนวน Item<br><strong>setEnablePlaceHolders(Boolean)</strong> คือให้สร้าง PlaceHolder ตอนก่อนจะเริ่มโหลดหรือไม่</p><p>ความแตกต่างระหว่าง</p><p>setEnablePlaceHolders(true) vs setEnablePlaceHolders(false)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*Kce9z-s_iIe5p0axmfVgVw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*rDOgxUt0WOGsKpDkyArbGw.png" /></figure><p>น่าจะมองเห็นภาพกันเนอะ ว่าต่างกันอย่างไร</p><h4>Placeholders มีประโยชน์ในการจัดการอย่างไร</h4><ul><li>Support for scrollbars : จำนวนไอเทมทั้งหมด จะสอดคล้องกับ scrollbars</li><li>ไม่ต้องทำ Load more spinner เพื่อบอก user ว่ามีไอเทมกำลังโหลดเพิ่มเติม</li></ul><h3>PageList</h3><p>เป็น Collection List สำหรับ Load Data ทีละก้อน (chunk)โดยหลักการทำงานเบื้องหลังของมันทำงานเป็น Asynchronous ถ้าจะเริ่มต้นใช้งาน หรือ migrate Load More ของโปรเจคเก่าๆ ก็เปลี่ยน Class เป็น PagedList ซะ</p><pre>class PageListExampleViewModel : ViewModel() {<br>    var userList: LiveData&lt;<strong>List</strong>&lt;GithubUser&gt;&gt;<br>}</pre><p>เปลี่ยนเป็น</p><pre>class PageListExampleViewModel : ViewModel() {<br>    var userList: LiveData&lt;<strong>PagedList</strong>&lt;GithubUser&gt;&gt;<br>}</pre><h3>DataSource</h3><p>คือ Base Class ของ Paging Library สำหรับ Load Data ส่งไปให้กับ PageList โดยถ้าจะสร้าง DataSource ขึ้นมานั้น จำเป็นต้องมีคลาสแม่ DataSource.Factory ขึ้นมาก่อน</p><p><strong>หน้าตา DataSource.Factory</strong></p><pre>class GithubUserDataSourceFactory(private val githubService: GithubApi) : DataSource.Factory&lt;Long, GithubUser&gt;() {<br><br>    val usersDataSourceLiveData = MutableLiveData&lt;GithubUserDataSource&gt;()<br><br><br>    override fun create(): DataSource&lt;Long, GithubUser&gt; {<br>        val usersDataSource = GithubUserDataSource(githubService)<br>        usersDataSourceLiveData.postValue(usersDataSource)<br>        return usersDataSource<br>    }<br>}</pre><h4>ประเภทของ DataSource</h4><ul><li><strong>PageKeyedDataSource</strong> โหลดแบบ Page (Pagination) โดยตัว API จะต้องส่ง NextPage สำหรับการ Load More เพิ่ม (เว็ปใช้บ่อย)</li></ul><blockquote><strong><em>ตัวอย่าง API PageKeyedDataSource</em></strong><br><a href="https://api.github.com/search/repositories?q=1&amp;page=1&amp;per_page=3">https://api.github.com/search/repositories?q={search_query}&amp;page={page}&amp;per_page={</a>per_page}</blockquote><ul><li><strong>ItemKeyedDataSource</strong> โหลดแบบ id (เช่น โหลดข้อมูล Comment โดย Load More โดยใช้ ID สุดท้ายของ Item มา fetch ต่อ)</li></ul><blockquote><strong><em>ตัวอย่าง API ItemKeyedDataSource</em></strong><br><a href="https://api.github.com/users?since=1">https://api.github.com/users?since=</a>{id}</blockquote><ul><li><strong>PositionalDataSource</strong></li></ul><p>อันนี้ถ้าให้ดูง่าย ก็คงจะคล้ายๆ Contact app บนมือถือ ที่ให้เลื่อนตำแหน่งตัวอักษรที่ต้องการโหลดได้ สำหรับ Blog นี้จะไม่ขออธิบายนะครับ ไว้ว่างๆจะมาอัพเดพให้ฟังเนอะ (ยังไม่เคยใช้งานจริงจัง)</p><p><strong>หน้าตา DataSource ประเภท ItemKeyedDataSource</strong></p><pre>class GithubUserDataSource(<br>        private val githubService: GithubApi,<br>        private val compositeDisposable: CompositeDisposable)<br>    : <strong>ItemKeyedDataSource</strong>&lt;Long, GithubUser&gt;() {</pre><pre>    val networkState = MutableLiveData&lt;NetworkState&gt;()</pre><pre>    override fun <strong>loadInitial</strong>(params: LoadInitialParams&lt;Long&gt;, callback: LoadInitialCallback&lt;GithubUser&gt;) {<br>        compositeDisposable.add(githubService.getUsers(1, params.requestedLoadSize).subscribe(<strong>{ </strong>users <strong>-&gt;<br>            </strong>callback.onResult(users)<br>        <strong>}</strong>, <strong>{ </strong>throwable <strong>-&gt;<br>            </strong>throwable.printStackTrace()</pre><pre>        <strong>}</strong>))<br>    }</pre><pre>    override fun <strong>loadAfter</strong>(params: LoadParams&lt;Long&gt;, callback: LoadCallback&lt;GithubUser&gt;) {<br>        //get the users from the api after id<br>        networkState.postValue(NetworkState.LOADING)<br>        compositeDisposable.add(githubService.getUsers(params.key, params.requestedLoadSize).subscribe(<strong>{ </strong>users <strong>-&gt;<br>            </strong>networkState.postValue(NetworkState.LOADED)<br>            callback.onResult(users)<br>        <strong>}</strong>, <strong>{ </strong>throwable <strong>-&gt;<br>            </strong>networkState.postValue(NetworkState.error(throwable.message))<br>            throwable.printStackTrace()<br>        <strong>}</strong>))<br>    }</pre><pre>    override fun <strong>loadBefore</strong>(params: LoadParams&lt;Long&gt;, callback: LoadCallback&lt;GithubUser&gt;) {<br>        // ignored, since we only ever append to our initial load<br>    }</pre><pre>    override fun getKey(item: GithubUser): Long {<br>        return item.id<br>    }<br>}</pre><p><strong>หน้าตา DataSource ประเภท PageKeyedDataSource</strong></p><pre>class PageKeydDataSource(<br>        private val willingApi: WillingApi,<br>        private val compositeDisposable: CompositeDisposable)<br>    : PageKeyedDataSource&lt;String, GithubUser&gt;() {<br><br>    val networkState = MutableLiveData&lt;NetworkState&gt;()<br><br>    override fun loadInitial(params: LoadInitialParams&lt;String&gt;, callback: LoadInitialCallback&lt;String, GithubUser&gt;) {</pre><pre>}<br><br>    override fun loadAfter(params: LoadParams&lt;String&gt;, callback: LoadCallback&lt;String, GithubUser&gt;) {</pre><pre>}<br><br>    override fun loadBefore(params: LoadParams&lt;String&gt;, callback: LoadCallback&lt;String, GithubUser&gt;) {<br>        // do nothing<br>    }<br>}</pre><p><strong>ตัว DataSource ของ ItemKeyedDataSource กับ PageKeyedDataSource จะใช้งานคล้ายๆกัน ดังนี้</strong></p><ul><li><a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.html#loadInitial(android.arch.paging.PageKeyedDataSource.LoadInitialParams%3CKey%3E,%20android.arch.paging.PageKeyedDataSource.LoadInitialCallback%3CKey,%20Value%3E)">loadInitial</a>(<a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialParams.html">LoadInitialParams</a>&lt;Key&gt; params, <a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialCallback.html">LoadInitialCallback</a>&lt;Key, Value&gt; callback)</li></ul><p>สำหรับเริ่มต้นในการโหลดครั้งแรกสุด</p><ul><li><a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.html#loadAfter(android.arch.paging.PageKeyedDataSource.LoadParams%3CKey%3E,%20android.arch.paging.PageKeyedDataSource.LoadCallback%3CKey,%20Value%3E)">loadAfter</a>(<a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams.html">LoadParams</a>&lt;Key&gt; params, <a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadCallback.html">LoadCallback</a>&lt;Key, Value&gt; callback)</li></ul><p>เริ่มทำงานเมื่อมีการโหลดไปข้างล่างสุดของ RecyclerView (Load More นั่นเอง)</p><ul><li><a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.html#loadBefore(android.arch.paging.PageKeyedDataSource.LoadParams%3CKey%3E,%20android.arch.paging.PageKeyedDataSource.LoadCallback%3CKey,%20Value%3E)">loadBefore</a>(<a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams.html">LoadParams</a>&lt;Key&gt; params, <a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadCallback.html">LoadCallback</a>&lt;Key, Value&gt; callback)</li></ul><p>เริ่มทำงานเมื่อมีการโหลดไปข้างบน</p><h4>ถามว่า ItemKeyedDataSource กับ <em>PageKeyedDataSource การใช้งานต่างกันมากมั้ย</em></h4><p>ตอบตรงๆเลย แทบจะเหมือนกันเลย ต่างกันต้อง LoadCallback ถ้าเป็น ItemKeyedDataSource เราจะต้อง callback กลับ</p><pre>override fun getKey(item: GithubUser): Long {<br>    return item.id<br>}</pre><p>เจ้านี้เป็น Key ที่ไว้สำหรับ bypass ไปยัง method <a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.html#loadInitial(android.arch.paging.PageKeyedDataSource.LoadInitialParams%3CKey%3E,%20android.arch.paging.PageKeyedDataSource.LoadInitialCallback%3CKey,%20Value%3E)">loadInitial</a> , <a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.html#loadAfter(android.arch.paging.PageKeyedDataSource.LoadParams%3CKey%3E,%20android.arch.paging.PageKeyedDataSource.LoadCallback%3CKey,%20Value%3E)">loadAfter</a> , <a href="https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.html#loadBefore(android.arch.paging.PageKeyedDataSource.LoadParams%3CKey%3E,%20android.arch.paging.PageKeyedDataSource.LoadCallback%3CKey,%20Value%3E)">loadBefore</a></p><h3>LivePagedListBuilder</h3><p>ไว้สำหรับจัดการ LiveData ที่จะส่งเข้ามาผ่าน PageList จะใช้งานได ก็ต่อเมื่อมี เรามี DataSource.Factory กับ Configuration</p><blockquote><strong><em>สำหรับใครที่ใช้ Rx แทนการใช้ LiveData นั้น ตัว Library มี RxPagedListBuilder มาใช้แทนได้เลย</em></strong></blockquote><h3>โดยทั้งหมดข้างต้น เราจะสามารถจัดการสร้างPaging ให้กับ ViewModel ได้แล้ว</h3><pre>var userList: LiveData&lt;PagedList&lt;GithubUser&gt;&gt;</pre><pre>private val pageSize = 10</pre><pre>private val sourceFactory: GithubUserDataSourceFactory</pre><pre>init {<br>    sourceFactory = GithubUserDataSourceFactory(GithubApi.create())<br>    val config = PagedList.Config.Builder()<br>            .setPageSize(pageSize)<br>            .setInitialLoadSizeHint(pageSize * 2)<br>            .setEnablePlaceholders(false)<br>            .build()<br>    userList = LivePagedListBuilder&lt;Long, GithubUser&gt;(sourceFactory, config).build()<br>}</pre><h3>PagedListAdapter</h3><p>เป็น Library ที่ Paging Library ให้มา ไว้ช่วยจัดการข้อมูลที่จะแสดงบน RecyclerView โดยมันจะทำการ Update ผ่านคำสั่ง submitList แล้วเดี๋ยวตัว PagedListAdapter จะไปจัดการข้อมูลผ่าน DiffUtil ที่เราสร้างไว้</p><p>class PageListExampleAdapter() : PagedListAdapter&lt;<strong>GithubUser</strong>, RecyclerView.ViewHolder&gt;(<strong>UserDiffCallback</strong>) {</p><p>หลักการทำงานเหมือน Adapter ทุกอย่าง เพียงแค่เราต้องใส่ Dao กับ DiffUtil ให้ Library ตัวนี้สามารถใช้งานได้</p><pre>viewModel.userList?.observe(this, <em>Observer</em>&lt;PagedList&lt;GithubUser&gt;&gt; <strong>{<br>    </strong>githubAdapter.submitList(<strong>it</strong>)<br><strong>}</strong>)</pre><p>เมื่อสั่งคำสั่งนี้ ตัว RecyclerView จะจัดการ Update ข้อมูลผ่าน DiffUtil โดยอัติโนมัติ ถ้ามีข้อมูลเดิมอยู่แล้วมันก็จะไปอัพเดพของเก่า <br>ถ้ามีข้อมูลใหม่ มันก็จะเพิ่มให้เองอัติโนมัติ</p><h3>BoundaryCallback</h3><p>concept ของมันคือ การนำโดยตัว DataSource ซึ่งข้อมูลที่ไปแสดงนั่นจะเป็นส่วนของ Database เป็นหลัก แต่ส่วนของ Network จะทำการดึง Data มาบันทึกลง Database ตัว Boundary เป็นเหมือน signal ส่งไปบอกตัว Data ว่า ข้อมูลนั่นว่าง หรือมีให้โหลดเพิ่มเติมหรือไม่</p><p>วิธีการประกาศใช้งาน</p><pre>class RepoBoundaryCallback(<br>        private val service: GithubService,<br>        private val cache: GithubLocalCache<br>) : PagedList.BoundaryCallback&lt;Repo&gt;() {<br><br><br>    <em>/**<br>     * Database returned 0 items. We should query the backend for more items.<br>     */<br>    </em>override fun onZeroItemsLoaded() {<br>         requestAndSaveData()<br>    }<br><br>    <em>/**<br>     * When all items in the database were loaded, we need to query the backend for more items.<br>     */<br>    </em>override fun onItemAtEndLoaded(itemAtEnd: Repo) {<br>         requestAndSaveData()<br>    }<br>}</pre><p>ตรง LivePagedListBuilder ประกาศ setBoundaryCallback เพิ่มเข้าไป</p><pre>val data = LivePagedListBuilder(dataSourceFactory)<br>        .<strong>setBoundaryCallback</strong>(boundaryCallback)<br>        .build()</pre><p>หลักการหลักๆเลยคือเมื่อเข้ามาในหน้านี้ ข้อมูลจะดึงจาก Database มาก่อนโดย<br>อัติโนมัติ หากไม่มีข้อมูลเลย มันเข้า method <strong>onZeroItemsLoaded() </strong>ก่อน แต่ถ้ามีข้อมูลอยู่แล้ว มันจะดึงจาก Database มาโชว์เลย ส่วน <strong>onItemAtEndLoaded</strong> จะทำงานเมื่อเราเลื่อนมาเจอไอเทมสุดท้ายแล้ว</p><p>ตรง BoundaryCallback ยังมีหลายอย่างที่ทำงานซับซ้อนอยู่ ทุกอย่างมันจะดึงจาก Database เป็นหลัก แต่ถ้าหากแอปเราต้องการโหลดข้อมูลใหม่แต่แรกละ .. ตรงจุดนี้เราต้องจัดการกันดีๆด้วย</p><h3>สรุปหน้าตาของ Paging Library ทั้งหมด</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MzY1hk_S_8IiOeGs3tWT1w.png" /></figure><p>เรื่องนี้ค่อนข้างซับซ้อนอยู่ครับ เพราะเป็นเรื่องที่มีรายละเอียดค่อนข้างเยอะพอสมควร ลองไปทำ CodeLab เพิ่มเติมดีกว่าครับ</p><p><a href="https://codelabs.developers.google.com/codelabs/android-paging/index.html#0">https://codelabs.developers.google.com/codelabs/android-paging/index.html#0</a></p><p>ส่วนเจ้าของ Blog นั่นทำตัวอย่างแบบ Network ในรูปแบบ <strong>PageKeyedDataSource</strong> และ <strong>ItemKeyedDataSource</strong> ไว้ที่ลิงค์นี้ครับ แต่ใช้ Rx กับ Retrofit มาจัดการนะครับ</p><p>Github Example : <a href="https://github.com/ult3mate/examplepagelistadapter">https://github.com/ult3mate/examplepagelistadapter</a></p><p>หวังว่าคงมีประโยชน์ซักนิดนะครับสำหรับชาว Android ถ้าผิดพลาดก็ขออภัยด้วย :(</p><p>Happy coding ครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d8faa7a7bbe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] Constraint Layout 1.1 มีอะไรใหม่บ้าง ?]]></title>
            <link>https://medium.com/@ult3mate/android-constraint-layout-1-1-%E0%B8%A1%E0%B8%B5%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3%E0%B9%83%E0%B8%AB%E0%B8%A1%E0%B9%88%E0%B8%9A%E0%B9%89%E0%B8%B2%E0%B8%87-be735a8819ed?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/be735a8819ed</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Mon, 16 Jul 2018 16:34:15 GMT</pubDate>
            <atom:updated>2018-07-17T08:34:29.533Z</atom:updated>
            <content:encoded><![CDATA[<p>เชื่อว่า Android Developer หลายๆคน น่าจะเริ่มเปลี่ยนมาใช้ Constraint Layout บ้างแล้วละ เพราะมันออกมาได้ซักพักแล้วละ ตั้งแต่สมัย Android Studio 2.2</p><p>สำหรับใครที่ยังใช้ LinearLayout , RelativeLayout , FrameLayout ลองหันใจมาลองใช้ Constraint Layout ดูครับ มันมาแทนที่การใช้ Layout แบบที่กล่าวไว้เพื่อให้นักพัฒนาสามารถสร้าง Layout ได้ง่ายขึ้น</p><p>ใน Blog นี้จะไม่ขอพูดถึงเรื่องการทำงาน Constraint Layout แต่จะมาอธิบายสิ่งที่เพิ่มเติมเข้ามาใน Version 1.1</p><p>ตรวจดู Version ที่ Gradle ด้วยนะครับ :)</p><pre>dependencies {<br>    implementation &#39;com.android.support.constraint:constraint-layout:1.1.2&#39;<br>}</pre><blockquote>สำหรับ version 1.1.2 สามารถใช้งานกับ AndroidX ได้นะครับ</blockquote><h3><a href="https://www.thinkgeek.com/whats-new/">What’s New</a> in 1.1</h3><h4>Circular positioning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*uThSX3rsTsSI2ewpUM2k_Q.png" /></figure><p>เราสามารถทำ Layout Circular ตามภาพข้างบน โดนสามารถกำหนดองศาที่เราจะวาง และระยะห่าง (รัศมี) ระหว่างไอเทม A กับ ไอเทม B</p><p>Attribute ที่ใช้งาน</p><ul><li><strong>layout_constraintCircle</strong> : id ของ view ที่อ้างอิง</li><li><strong>layout_constraintCircleRadius</strong> : ระยะห่างของ view (หน่วย dp)</li><li><strong>layout_constraintCircleAngle</strong> : องศา (0 to 360)</li></ul><p>ตัวอย่างการใช้งาน</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_1&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;button 1&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_2&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;button 2&quot;<br>        app:layout_constraintCircle=&quot;@+id/button_1&quot;<br>        app:layout_constraintCircleAngle=&quot;45&quot;<br>        app:layout_constraintCircleRadius=&quot;100dp&quot; /&gt;<br><br>&lt;/android.support.constraint.ConstraintLayout&gt;</pre><p>ผลลัพธ์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*hUbHYhQfiq0wUXreYW2EHQ.png" /></figure><h4>Percents</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*D_CRS5C6PL4Hufkk6t8NMQ.jpeg" /></figure><p>ในตอนสมัย Version 1.0 ถ้าเราต้องการแบ่งสัดส่วนจอ เราจะใช้เส้น Guideline มาเป็นตัวคั่น ซึ่งมันค่อนข้างจัดการยาก หาก Layout ของเราซับซ้อนยิ่งขึ้น ซึ่งใน Constraint Layout 1.1 เราสามารถจัดการได้ง่ายๆโดยใช้ attribute ตัวนี้</p><ul><li><strong>layout_constraintWidth_percent</strong></li><li><strong>layout_constraintHeight_percent</strong></li></ul><p>ส่วนค่าการใช้งานเป็นค่า Float จะอยู่ในช่วง 0–1 <br>เช่น (0.1 = 10% , 0.7 ช= 70%)</p><p>แต่ Percent มีเงื่อนไขการใช้งานอยู่</p><ul><li>ความกว้าง หรือความสูง เราต้องกำหนดเป็น <strong>MATCH_CONSTRAINT</strong> (0dp)</li></ul><p>ตัวอย่างการใช้งาน</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_1&quot;<br>        android:layout_width=&quot;0dp&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;button 1&quot;<br>        app:layout_constraintWidth_percent=&quot;0.8&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>&lt;/android.support.constraint.ConstraintLayout&gt;</pre><p>ผลลัพธ์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*Y10G9H7d9EfFG9iGsYI56A.png" /></figure><h4>Barriers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*6Sm175_deimYh-gUxeTw4A.gif" /></figure><p>จากภาพน่าจะพอเข้าใจแล้วว่าเจ้านี้คืออะไร <br>เจ้าของ blog มักจะเจอปัญหาตามข้างบนเลย อย่างเช่น RecyclerView ที่มี TextView 3 ตัววางเรียงกันโดยข้อความดึง message จาก API และต้องการวางรูปทางด้านขวาของ TextView แต่เราไม่รู้ว่า TextView ทั้ง 3 ตัว ตัวไหนจะมีข้อความยาวสุด นั่นทำให้ ImageView ทางขวา เราไม่สามารถอ้างอิงได้ว่า ตัวไหนมันยาวสุด เจ้า Barrier จึงออกมาแก้ไขปัญหานี้</p><p>โดยมี Attribute คือ app:barrierDirection=”top|end|bottom|start|left|right”</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_1&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;ทดสอบปุ่มข้อความยาวๆๆๆๆๆๆๆๆๆ 1&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;@+id/barrier&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot;/&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_2&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:layout_marginEnd=&quot;8dp&quot;<br>        android:layout_marginRight=&quot;8dp&quot;<br>        android:layout_marginTop=&quot;8dp&quot;<br>        android:text=&quot;ทดสอบปุ่มข้อความยาว&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;@+id/barrier&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toBottomOf=&quot;@+id/button_1&quot; /&gt;<br><br>    &lt;android.support.constraint.Barrier<br>        android:id=&quot;@+id/barrier&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        app:barrierDirection=&quot;end&quot;<br>        app:constraint_referenced_ids=&quot;button_2,button_1&quot; /&gt;<br><br>    &lt;ImageView<br>        android:id=&quot;@+id/iv&quot;<br>        android:layout_width=&quot;60dp&quot;<br>        android:layout_height=&quot;60dp&quot;<br>        android:layout_marginEnd=&quot;8dp&quot;<br>        android:layout_marginLeft=&quot;8dp&quot;<br>        android:layout_marginRight=&quot;8dp&quot;<br>        android:layout_marginStart=&quot;8dp&quot;<br>        android:layout_marginTop=&quot;8dp&quot;<br>        android:src=&quot;@drawable/ic_star&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintStart_toEndOf=&quot;@+id/barrier&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>&lt;/android.support.constraint.ConstraintLayout&gt;</pre><p>ผลลัพธ์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*Hw5XghlBfFrU1_yVuoXRkQ.png" /></figure><p>ลองเปลี่ยนความยาว Text Button</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*Ry8hOHJDYID0Ubh78ykX-Q.png" /></figure><p>รายละเอียดเพิ่มเติม :<br><a href="https://developer.android.com/reference/android/support/constraint/Barrier">https://developer.android.com/reference/android/support/constraint/Barrier</a></p><h4>Groups</h4><pre>&lt;android.support.constraint.Group<br>              android:id=&quot;@+id/group&quot;<br>              android:layout_width=&quot;wrap_content&quot;<br>              android:layout_height=&quot;wrap_content&quot;<br>              android:visibility=&quot;visible&quot;<br>              app:constraint_referenced_ids=&quot;button_1,button_2&quot; /&gt;</pre><p>อันนี้ไม่มีอะไรมาก แค่เวลาที่เราสั่ง View.VISIBLE | View.INVISIBLE| View.GONE ของ Group จะทำให้ view ที่เรา app:constraint_referenced_ids จะโดน Visible | Invisible | Gone ไปทั้งหมดทันที เพียงคำสั่งบรรทัดเดียว แทนที่จะทำการสั่งทีละ View</p><p>จากตัวอย่างข้างบน<br>group.<em>visibility </em>= View.<em>GONE<br></em>เจ้า button_1 และ button_2 จะโดน GONE ทิ้งทันที</p><p>รายละเอียดเพิ่มเติม : <a href="https://developer.android.com/reference/android/support/constraint/Group">https://developer.android.com/reference/android/support/constraint/Group</a></p><h4>Placeholders</h4><p>เจ้าตัวนี้เรียกได้ว่า ทำงานคล้าย replace ของ Fragment แต่เราจะจัดการได้ทุกอย่างที่เป็น View โดย reference จาก id ของ view นั่นเอง</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    android:id=&quot;@+id/main_layout&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_1&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:layout_marginEnd=&quot;8dp&quot;<br>        android:layout_marginRight=&quot;8dp&quot;<br>        android:layout_marginTop=&quot;8dp&quot;<br>        android:text=&quot;Button 1&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_2&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;Button 2&quot; /&gt;<br><br>    &lt;android.support.constraint.Placeholder<br>        android:id=&quot;@+id/place_holder_sample&quot;<br>        android:layout_width=&quot;80dp&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        app:layout_constraintBottom_toTopOf=&quot;@+id/button_3&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button_3&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:layout_marginBottom=&quot;8dp&quot;<br>        android:layout_marginEnd=&quot;8dp&quot;<br>        android:layout_marginLeft=&quot;8dp&quot;<br>        android:layout_marginRight=&quot;8dp&quot;<br>        android:layout_marginStart=&quot;8dp&quot;<br>        android:layout_marginTop=&quot;8dp&quot;<br>        android:text=&quot;Change Place Holder&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br><br>&lt;/android.support.constraint.ConstraintLayout&gt;</pre><p>UI จะโผล่มาประมาณนี้เนอะ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*a1mmIddL03gMr3B_oON_4A.png" /></figure><p>คราวนี้ลองมาเพิ่มคำสั่งตัวนี้กัน</p><pre>...</pre><pre>&lt;android.support.constraint.Placeholder<br>    android:id=&quot;@+id/place_holder_sample&quot;<br>    android:layout_width=&quot;wrap_content&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    <strong>app:content=&quot;@+id/button_1&quot;</strong><br>    app:layout_constraintBottom_toTopOf=&quot;@+id/button_3&quot;<br>    app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>    app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;</pre><pre>...</pre><p>คราวนี้เจ้า UI จะโผล่มาประมาณนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*CW5sMuz8RlqfD2An8OHKZA.png" /></figure><p>คราวนี้มาลง code กัน</p><pre>button_3.setOnClickListener <strong>{<br>    </strong>TransitionManager.beginDelayedTransition(main_layout)<br>    if (place_holder_sample.<em>content</em>.<em>id </em>== button_2.<em>id</em>) {<br>        place_holder_sample.setContentId(button_1.<em>id</em>)<br>    } else {<br>        place_holder_sample.setContentId(button_2.<em>id</em>)<br>    }<br><strong>}</strong></pre><p>คำสั่งนี้จะทำให้ View place_holder_sample โดน replace ระหว่าง button_1 และ button_2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PQcVGNVz_7OtSiVZFqWlwg.gif" /></figure><p>แต่ความเจ๋งต่อมา ผมอยากใส่ Animation เข้าไปเพิ่มคำสั่งนี้เข้าไป</p><pre>button_3.setOnClickListener <strong>{<br>    TransitionManager.beginDelayedTransition(main_layout)</strong><br>    if (place_holder_sample.<em>content</em>.<em>id </em>== button_2.<em>id</em>) {<br>        place_holder_sample.setContentId(button_1.<em>id</em>)<br>    } else {<br>        place_holder_sample.setContentId(button_2.<em>id</em>)<br>    }<br><strong>}</strong></pre><p>แค่นี้ตัว button_1 และ button_2 จะมี Animation ในการทำงานแล้วครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PnzqMuUrDXzoVWZUoGNp1g.gif" /></figure><p>รายละเอียดเพิ่มเติม : <a href="https://developer.android.com/reference/android/support/constraint/Placeholder">https://developer.android.com/reference/android/support/constraint/Placeholder</a></p><h4>เสริมเรื่อง Chains</h4><p>อันนี้ความจริงมีตั้งแต่ 1.0 แล้ว เพียงแต่ Bug ในการ Chains นั่นมีพอสมควรเลยทีเดียว และใน 1.1 ก็ได้รับการแก้ไขหลายอย่างเรียบร้อยแล้ว ลองดูครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*ym3uLdrsA7gAzf4GSUkG_g.gif" /></figure><p>มีอีกหลายส่วน สำหรับคนที่อยากรู้ ลองเปิด doc ดูเลยครับ ลิงค์นี้เลย</p><p><a href="https://developer.android.com/reference/android/support/constraint/ConstraintLayout">https://developer.android.com/reference/android/support/constraint/ConstraintLayout</a></p><p>ช่วงนี้ก็เริ่มมีข่าวเรื่อง ConstraintLayout 2.0 กันแล้ว เจ้า Doc ตัวนี้ก็บอกว่ามันจะเพิ่มเข้ามาใน 2.0 <br><a href="https://developer.android.com/reference/android/support/constraint/ConstraintProperties">https://developer.android.com/reference/android/support/constraint/ConstraintProperties</a></p><p>ก็ติดตามเร็วๆครับ หรือใครอยากสืบเรื่องราวก่อน ลองดู Slide นี้ได้เลยครับ<br><a href="https://www.reddit.com/r/androiddev/comments/8bwc36/slides_constraintlayout_20_sneak_peek/">https://www.reddit.com/r/androiddev/comments/8bwc36/slides_constraintlayout_20_sneak_peek/</a></p><p>เป็นนักพัฒนา จงอย่าหยุดพัฒนาตัวเองครับ<br>Happy Coding ครับ :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be735a8819ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] Generate Kotlin data classes from JSON]]></title>
            <link>https://medium.com/@ult3mate/tools-generate-kotlin-data-classes-from-json-d5b64118610c?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5b64118610c</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Sun, 08 Jul 2018 10:38:18 GMT</pubDate>
            <atom:updated>2018-07-08T13:20:52.143Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*yfyxc8uKcZSm43OwX81UKw.png" /></figure><p>ปัญหาที่เจอเลยคือ สมัยที่เป็น java นั้น ถ้าเราต้องการทำ model pojo ขึ้นมาตัวนึงนั้น เสียเวลาในการเขียนมาก ยิ่งถ้า json มีความซับซ้อนหลายชั้น ก็จะยิ่งเขียนเยอะเข้าไป ยิ่งถ้าใช้ Gson น่าจะเข้าใจเรื่องนี้เป็นอย่างดีเลย</p><blockquote>มันทำให้เสียเวลาในการทำงานไปหลายวันเลยละ</blockquote><p>ในสมัยนั้นมี tools ช่วยตัวนึงนั้นคือ<br><a href="http://www.jsonschema2pojo.org/">http://www.jsonschema2pojo.org/</a></p><p>tools ตัวนี้สามารถให้เราแปะ json ออกมาแล้ว export ไฟล์ออกมาเป็น .java ได้<br>ซึ่งช่วงนั้น ซึ่งก็ช่วยเหล่า Android ได้ระดับนึง</p><h3>แต่ tools ตัวนี้ไม่มี kotlin นะสิ อ้าว แล้วชาว Kotlin จะทำอย่างไรละ ????</h3><p>เราก็ convert java เป็น kotlin ไปเลยสิ !</p><blockquote>สำหรับชาว Android ที่ยังใช้ Java อยู่ ผมแนะนำครับ ลองใช้ Kotlin ดูครับ รับรองว่าชีวิตจะดีขึ้นมากกกกก</blockquote><p>วิธีอย่างง่ายเลยคือ copy ไฟล์ java แล้ว convert ่java file to kotlin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*HvSxfwSq2apQyAH0ohgPVg.png" /></figure><p>จากนั้นทำการ convert ด้วย IDE สุดเท่ของเรา<br>Convert Java File to Kotlin File</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*N3xE7KSdMrq5up5rGdP-Tg.png" /></figure><p>ได้แล้ว Class Kotlin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*eeRUYfJCbS8RAPGPrYS9fw.png" /></figure><p>ถือเป็น Tools ที่ช่วยเราได้เยอะเลยทีเดียว แต่เดี๋ยวก่อน ….</p><h3>ถ้าเราอยากได้เป็น data class ละ ต้องทำยังไง !?</h3><p>เชื่อว่า ชาว Kotlin ส่วนใหญ่ใช้ data class กันซะส่วนใหญ่</p><p>ไม่ต้องห่วง มีคนทำ tools ตัวนี้ไว้แล้วให้ ซึ่งใช้ง่ายมากกกกกกกก</p><p>Link : <a href="https://github.com/wuseal/JsonToKotlinClass">https://github.com/wuseal/JsonToKotlinClass</a></p><p>ซึ่ง Tools ตัวนี้สามารถช่วยให้เราทำ data class โดยการใส่ json เข้าไปได้ง่ายมาก</p><h4>มาติดตั้งกันเลยดีกว่า</h4><p>ไปที่ Settings &gt; Plugin &gt; Browse repositories</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cpt6h7SJUzIBzxHk75x0gg.png" /></figure><p>หา Tools โดย search ว่า JSON to kotlin Class</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*0Z21KIoBZiqpfspTd3T-cg.png" /></figure><p>Install แล้ว Restart Android Studio รอบนึง</p><p>เสร็จแล้วครับ ขั้นตอนติดตั้ง ง่ายมาก</p><h3>ใช้งานมันอย่างไรละ ???</h3><p>คลิ๊กขวาที่ package สร้างไฟล์ จะมี Kotlin data class File from JSON ขึ้นมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/1*nXsAHpEQxzSr6ZuS2uCilQ.png" /></figure><p>สร้างชื่อ Class Name และวาง JSON Text ลงไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*DNDoMlzl-KljGFo8davWvw.png" /></figure><p>กด Make !!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*Uqlv6bH4GnGZav4mi7ydXA.png" /></figure><p>It’s magic !!</p><h3>ถ้าเราใช้ตัว parser อื่นๆ อย่าง Gson jackson mochi ละ</h3><p>สังเกตว่าก่อนจะกด Make นั้นจะมี Settings สำหรับทำให้ไฟล์นี้มีคุณสมบัติอื่นๆได้ ซึ่งตามนี้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*nAorZPqB87RT9w16U5jGcw.png" /></figure><p>ความจริงมันทำอะไรได้อีกเยอะพอสมควรเลย นี้แค่น้ำจิ้ม เช่น<br>ทำ inner class ของ kotlin , ทำ Map Type ก็ลองเล่นเลยครับ เชื่อว่า Developer หลายคนน่าจะศึกษาไม่นาน</p><p>ก็หวังว่าจะช่วยชาว Android ทำงานได้สะดวกและเร็วยิ่งขึ้นนะครับ <br>Happy Coding ครับ :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5b64118610c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] App ของเจ้าตายแล้ว เราจะจัดการมันอย่างไร]]></title>
            <link>https://medium.com/@ult3mate/android-app-%E0%B8%82%E0%B8%AD%E0%B8%87%E0%B9%80%E0%B8%88%E0%B9%89%E0%B8%B2%E0%B8%95%E0%B8%B2%E0%B8%A2%E0%B9%81%E0%B8%A5%E0%B9%89%E0%B8%A7-%E0%B9%80%E0%B8%A3%E0%B8%B2%E0%B8%88%E0%B8%B0%E0%B8%88%E0%B8%B1%E0%B8%94%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%A1%E0%B8%B1%E0%B8%99%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B9%84%E0%B8%A3-aab72cb6c7b6?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/aab72cb6c7b6</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Sat, 30 Jun 2018 10:27:02 GMT</pubDate>
            <atom:updated>2022-09-22T17:48:17.510Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*nu919kTOUDCN2p3oKo7RIA.png" /></figure><p>นักพัฒนาหลายคนมักมองข้ามเรื่องพวกนี้กัน <br>และผมเชื่อว่ายังมีนักพัฒนาอีกหลายคนที่ยังไม่รู้วิธีการจัดการ <strong>Save Restore State <br></strong>หรือแม้แต่แอปที่อยู่บน production ตอนนี้ก็ยังไม่ได้จัดการเรื่องพวกนี้</p><blockquote>แนะนำหาอ่านเรื่อง Save / Restore Instance State กันนะครับ มีหลาย Blog เขียนไว้เยอะแยะเลย ถือเป็นพื้นฐานสำคัญมากเรื่องนึงของนักพัฒนา Android เลยนะครับ</blockquote><p>นักพัฒนาหลายคนมักจะเข้าใจผิดอยู่หลายข้อเรื่องเกี่ยวกับ Save / Restore Instance State กันอยู่นะครับ</p><h4>อ้าว เราไม่ได้ทำแอป 2 แนว ที่มีทั้งแนวนอนและแนวตั้ง ทำไมต้องมาทำ Save / Restore Instance State ด้วยละ มันก็ไม่มีทางเข้า onCreate ใหม่อยู่แล้วนี่นา?</h4><p>เอาจริงๆ หลายๆแอปมันจะล็อคจอไว้เป็นแนวตั้งซะส่วนใหญ่ละนะ</p><pre>&lt;activity<br>    android:name=&quot;.MainActivity&quot;<br>    android:screenOrientation=&quot;portrait&quot; /&gt;</pre><p>แต่เคยสังเกตมั้ย เราพักแอปไประยะเวลานึง เมื่อกลับมา ทำไมแอปเหมือนเป็นการ Restart app ไม่ได้เข้า onResume แต่ไปดันเข้า onCreate ใหม่แทน ถ้าหากไปเล่นเครื่องกากๆแล้วละก็ มันจะเกิดเหตุการณ์นี้ขึ้นบ่อยครั้งมาก</p><p>ยิ่งเล่น App ที่กิน memory มากๆเรื่อยๆ แอปของเราจะตาย (DEAD) พอตายแล้ว หากเรากลับมาในแอปเราควรที่จะต้องให้ User กลับมาหน้าตาที่เราเคยอยู่ล่าสุดไว้ก่อนที่จะพักแอปไป</p><p>ดังนั้นต่อให้เราล็อคจอไว้เป็นแนวตั้ง ก็ควรไม่เมินเฉยเรื่อง Save / Restore Instance State</p><h4>แล้วเราจะทดสอบเรื่องพวกนี้อย่างไรดีละ ?<br>เริ่มจากเอา view ง่ายๆมาก่อนละกัน</h4><pre>&lt;android.support.design.widget.BottomNavigationView<br>    android:id=&quot;@+id/main_bottom_navigation&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:background=&quot;?android:attr/windowBackground&quot;<br>    android:layout_alignParentBottom=&quot;true&quot;<br>    app:menu=&quot;@menu/navigation_bottom_items&quot; /&gt;</pre><p>ประกาศ Bottom Navigation View ไว้ตรงนี้ ส่วน Class ประมาณนี้</p><p>MainActivity.kt</p><pre>override fun onCreate(savedInstanceState: Bundle?) {<br>    super.onCreate(savedInstanceState)<br>    setContentView(R.layout.<em>activity_main</em>)<br>    main_bottom_navigation.<em>onNavigationItemSelectedListener </em>= mOnNavigationItemSelectedListener<br>}</pre><p>ตรงนี้คงไม่มีอะไร เนื่องจาก BottomNavigationView มันจัดการ state ของมันให้เองอยู่แล้ว ทีงี้เรามาเพิ่มโค้ดนิดนึง</p><p>activity_main.xml</p><pre>&lt;FrameLayout<br>    android:id=&quot;@+id/frame_content&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot; /&gt;<br><br>&lt;android.support.design.widget.BottomNavigationView<br>    android:id=&quot;@+id/main_bottom_navigation&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:background=&quot;?android:attr/windowBackground&quot;<br>    android:layout_alignParentBottom=&quot;true&quot;<br>    app:menu=&quot;@menu/navigation_bottom_items&quot; /&gt;</pre><p>navigation_bottom_items.xml</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br>    &lt;item<br>        android:id=&quot;@+id/home_menu&quot;<br>        android:icon=&quot;@mipmap/tabbar_home&quot;<br>        android:title=&quot;@string/home_menu_title&quot; /&gt;<br><br>    &lt;item<br>        android:id=&quot;@+id/inbox_menu&quot;<br>        android:icon=&quot;@mipmap/tabbar_chat&quot;<br>        android:title=&quot;@string/chat_menu_title&quot; /&gt;<br><br>    &lt;item<br>        android:id=&quot;@+id/profile_menu&quot;<br>        android:icon=&quot;@mipmap/tabbar_profile&quot;<br>        android:title=&quot;@string/profile_menu_title&quot; /&gt;<br>&lt;/menu&gt;</pre><p>เราก็จะได้หน้าตา Navigation Bottom ประมาณนี้ (แอป Custom Font ลงไปนิดหน่อย)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*jSCL1IPAx_ff_SMoDcCpRw.jpeg" /></figure><p>คราวนี้มาลองใส่ Fragment ลงในหน้าหลักกัน</p><pre>override fun onCreate(savedInstanceState: Bundle?) {<br>    super.onCreate(savedInstanceState)<br>    setContentView(R.layout.<em>activity_main</em>)<br>    main_bottom_navigation.<em>onNavigationItemSelectedListener </em>= mOnNavigationItemSelectedListener<br>    initialFragment()<br>}<br><br>private fun initialFragment() {<br>    replaceFragment(HomeFragment.newInstance(isOpenCoursePage))<br>}</pre><pre>private fun replaceFragment(fragment: Fragment) {<br>    val ft = <em>supportFragmentManager</em>.beginTransaction()<br>    ft.replace(R.id.<em>frame_content</em>, fragment)<br>    ft.setTransition(FragmentTransaction.<em>TRANSIT_FRAGMENT_OPEN</em>)<br>    ft.commit()<br>}</pre><p>โอเค เข้ามาเราจะให้มัน inflate Fragment ตัวเลยของ Tabแรกไว้ ไว้</p><p>อ้าวละทีงี้ลองมาเปิดเจ้านี้กัน ให้ไปที่ Developer options แล้วเปิด menu ที่ชื่อว่า <br>Don’t keep activities</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*X7BIwpSeiwRVo8IRy-P9FA.jpeg" /></figure><p>ลองรันแอปใหม่ โดยการเปิดแอปแล้วพักแอปแล้วเข้าไปแอปใหม่</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*gRCw1_LV8vO4t6_umhR8xQ.gif" /></figure><p>อ้าว เห้ยทำไมมันยังกลายเป็น Fragment แรก แต่ตัว Navigation ดันจำ state ไว้<br>คร่าวนี้น่าจะเข้าใจไอ้เจ้า Don’t Keep Activity กันละ</p><p>งั้นดักไว้แบบนี้ละกัน</p><pre>override fun onCreate(savedInstanceState: Bundle?) {<br>    super.onCreate(savedInstanceState)<br>    setContentView(R.layout.<em>activity_main</em>)<br>    main_bottom_navigation.<em>onNavigationItemSelectedListener </em>= mOnNavigationItemSelectedListener<br><br><strong>    if(savedInstanceState == null) {<br>        initialFragment()<br>    }else{</strong></pre><pre><strong>    }</strong><br>}</pre><p>หรือจะใช้แบบนี้ก็ได้</p><pre>override fun onSaveInstanceState(outState: Bundle?) {<br>    super.onSaveInstanceState(outState)<br>    outState?.putInt(&quot;someVarA&quot;, main_bottom_navigation.<em>currentItem</em>)<br>}<br><br>override fun onRestoreInstanceState(savedInstanceState: Bundle) {<br>    super.onRestoreInstanceState(savedInstanceState)<br>    val position = savedInstanceState.getInt(&quot;someVarA&quot;)<br>    main_bottom_navigation.<em>currentItem </em>= position<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*816lymzGXipyWVELotZdiA.gif" /></figure><p>โอเค ตรงตามที่เราต้องการแล้ว เย้<br>ที่งี้เราก็จัดการเรื่อง Save / Restore State ได้แล้ว………. เย้ หรอ ?? ไม่ต้องทำอะไรแล้ว เย้</p><h3>ใครที่เข้าใจแบบนี้อยู่ ถือว่า บาปมาก!!</h3><p>เรื่อง Don’t keep activity นั้นมันก็ช่วยได้ในเรื่องจัดการในเรื่อง saveInstanceState ได้ระดับนึง เพราะเป็นตัวช่วยนักพัฒนาในการจัดการเรื่อง Activity ถูก Create ใหม่อีกครั้ง<em> </em><strong><em>แต่ไม่ได้ช่วยในการจัดการเรื่อง Memory แต่อย่างใด !!</em></strong></p><p>เพราะถ้าคุณเก็บตัวแปรไว้ใน Singleton หรือ static variable ไว้อยู่ มันจะไม่ถูกทำลายในขณะที่อยู่ในโหมดนี้</p><p>แนะนำให้อ่านเรื่องจาก Blog คุณเอก จะเข้าใจมากขึ้นกว่าเดิมเยอะครับ<br><a href="http://www.akexorcist.com/2017/01/dont-keep-any-data-in-singleton-or-static-variable.html">http://www.akexorcist.com/2017/01/dont-keep-any-data-in-singleton-or-static-variable.html</a></p><h3>แล้วจะทดสอบอย่างไรดีละ ว่าถ้าแอปเราตายจริงๆ มันจะยังคงเล่นได้ปกติ ไม่เกิดบัคอย่าง app crash</h3><p>วิธีการทดสอบนั้นมีอยู่ครับ ซึ่งตัว Android Studio มันมี Tools ตัวนี้อยู่</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/62/1*d2u1nF9hn0LDg_oe_KTUAQ.png" /></figure><p>สังเกตตรง Logcat นั้นมันจะมีปุ่มกากบาทอยู่ ตัวนี้นั้นหมายถึง Terminate app ให้เราตายนั้นเอง ซึ่งถ้าแอปเราอยู่บนหน้าจออยู่ แอปจะ Restart ใหม่ แต่ถ้าแอปเราอยู่ใน state ที่ยังถูกซ่อน (Recent app) อยู่นั้น แล้วกดปุ่มนี้ เมื่อกลับเข้ามา สิ่งที่เราเก็บไว้ทุกอย่างใน singleton และ static variable นั้นจะถูกทำลายทันที</p><blockquote>ให้ Terminate ตอนที่แอปอยู่ Recent App นะครับ</blockquote><p>คราวนี้ก็รู้วิธีจัดการละ ลุยเลยครับ</p><h3>เรื่องนี้สำคัญมากมั้ยสำหรับนักพัฒนา ? เราควรจะจัดการหรือไม่</h3><p>จำไว้ครับว่า User ทุกคน ไม่ได้ใช้มือถือสเปคแรงๆทุกคน ยิ่งตอนนี้มี Android Go ของทาง Google ที่ออกมาบอกว่า OS จะเป็น Android ล่าสุด (Android O) แต่เครื่องสเปคต่ำมาก ก็ต้องยิ่งระวังใหญ่เลย ดังนั้นควรจัดการเรื่องพวกนี้ไว้ เราไม่สามารถคาดการณ์ได้ว่าในอนาคต จะมีมือถือแบบไหนมาอีก</p><p>ในงาน <a href="https://events.google.com/io/">Google I/O 2018</a> นั้นก็มีเรื่องนึงที่ขึ้น Slide มาเลยครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yrXOFFZVBb2q-FgCy5CpXw.jpeg" /></figure><p>ตาม Slide ที่บอกเลย :(<br>ก็หวังว่า Blog นี้คงมีประโยชน์สำหรับนักพัฒนาทุกคนนะครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aab72cb6c7b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] Publish an app with Timed publishing]]></title>
            <link>https://medium.com/@ult3mate/android-publish-an-app-with-timed-publishing-b06519987a2d?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/b06519987a2d</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[google-play-store]]></category>
            <category><![CDATA[google-play]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Sat, 02 Jun 2018 12:42:23 GMT</pubDate>
            <atom:updated>2018-07-16T17:44:32.077Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*RIUFl4t_Q11-jqP8l-vHXw.png" /></figure><p>เชื่อว่า Android Developer หลายคนคงรู้วิธีการเอา app ขึ้นบน Google Play กันอยู่แล้ว แต่ผมมั่นใจว่าทุกคนไม่รู้ว่า มันมีเจ้าสิ่งนี้อยู่ใน Google Play กัน</p><p>โดยปกติในการอัพเดพแอปบน Google Play นั้น เราจะต้องเตรียมการสร้าง APK อัพขึ้นบน Play Store ซึ่งจริงๆมันทำได้<strong> 2 แบบ</strong></p><ul><li><strong>Standard publishing</strong>: Update โดยที่เราอัพโหลด APK ขึ้นไป รอไม่กี่ชั่วโมง แอปของคุณก็จะไปอยู่บน Google Play แล้ว</li><li><strong>Timed publishing</strong>: Update แอปโดยที่เราอัพโหลด APK ขึ้นไปก่อน แต่ยังไ่ปล่อยบน Play Store หรือง่ายๆคือ Manual Update นั้นเอง</li></ul><p>ซึ่ง Timed publishing นั้นมันมีประโยชน์มาก สำหรับงานที่ใช้ใน Event สำคัญที่ต้องเปิดแอปโหลดในวันนั้น</p><h3>เปิดใช้งานมันอย่างไรละ ?</h3><p>ไปที่ <strong>Store presence</strong> &gt; <strong>Store Listing</strong> หรือ <strong>Pricing &amp; distribution</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BQM4v8X8sRSAF85DqoyCVw.png" /></figure><p>จากนั้นข้างล่าง จะเห็น Switch ตัวนึงเขียนว่า <strong>Timed publishing</strong> นั้นละครับ วิธีเปิดใช้งานมัน</p><p>เมื่อกด Switch แล้วจะขึ้น Dialog หน้าตาแบบนี้ขึ้นมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*BJ0JOom7x1YcJCovr_xsVQ.png" /></figure><p>อ่านรายละเอียดของเจ้า <strong>Timed publishing </strong>ให้ดีๆ ก่อนที่จะตัดสินใจใช้มันด้วยนะ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wl7QGOXLSRQBQof3cnyjTA.png" /></figure><p>ถ้าหากเรากด TURN ON แล้ว ขึ้นภาพแบบข้างบน จะเป็นการบอกว่า ตอนนี้เราได้ใช้การ update app แบบ <strong>Timed publishing</strong> อยู่นะ</p><p>จะสังเกตุจุดเปลี่ยนได้คือ มีปุ่มการใช้งานเพิ่มอีก 2 ปุ่มนั้นคือ</p><ul><li><strong>VIEW CHANGE LOG</strong> คือรายละเอียดที่เราแก้ไขในแอป เช่นชื่อ application , description ของ application ทุกครั้งที่เราแก้ไข มันจะถูกบันทึนไว้ในนี้</li><li><strong>GO LIVE</strong> ปุ่มนี้ สำคัญมากๆๆๆๆๆๆๆๆ เป็นการเผยแพร่ appliation หรือง่ายๆก็คือการปล่อยออกไปให้โลกรู้ ปุ่มนี้จะใช้งานได้ เมื่อมีการ Upload APK ขึ้นอย่างน้อย 1 ครั้ง</li></ul><blockquote>หากเรายังไม่ Upload APK ขึ้นไปในขณะที่อยู่ในโหมด Timed publishing เราสามารถปรับกลับให้เป็น Standard publishing ได้อยู่นะครับ แต่หาก Upload ขึ้นไปแล้ว จะไม่สามารถปรับเก่าได้นะครับ :(</blockquote><p>จากนั้นทุกอย่างจะขั้นตอน จะเหมือนกับตอนเราทำแบบปกติเลยครับ อัพโหลด APK ขึ้นไป กด SUBMIT UPDATE แล้วรอ Process</p><p>แต่มีสิ่งที่ต่างออกไปนั้นคือ <br>หลังจากที่มัน Process เสร็จ หากต้องปล่อยแอปขึ้นบน Google Play เราจะต้องกดปุ่ม GO LIVE ทุกครั้งก่อนเสมอ เพื่อให้แอปนั้นถูกเผยแพร่ขึ้นไปบน Google Play นั้นเอง</p><p>แต่ในขั้นตอนก่อนที่เราจะกด GO LIVE นั้น ไม่ว่าเราจะแก้ชื่อ Application หรือ Description ทุกอย่างนั้นจะยังไม่ถูก Update ทันทีนะครับ แต่มันจะไปถูก Update ใน VIEW CHANGE LOG ไว้ก่อนนะครับ</p><h3>“แล้วมันต่างจาก Standard publishing ที่เราใช้กันอยู่ทุกวันมากแค่ไหน”</h3><p>อย่างที่บอกนั้น <strong>Timed publishing </strong>ก็คือการ Manual Update นั้นเอง แต่ขั้นตอนนี้ ก่อนที่ Application จะถูก GO LIVE ออกไปนั้น มันจะใช้เวลา Process น้อยกว่าการทำ Standard publishing เพราะ Application เรานั้นจะถูก Process ไปก่อนในจังหวะที่เรากด SUBMIT UPDATE ไปแล้วนั่นเอง ในช่วงนี้ทาง Google จะ Process APK ที่เรา Upload ไว้ล่าสุดก่อน</p><p>แต่จากการที่ลองนั้น พอกด GO LIVE นั้น กลายเป็นว่ามันขึ้นไปบน Google Play ทันที แต่วันที่ Update App บน Google Play จะเป็นวันที่เรา Upload APK นะครับ</p><p>และจังหวะที่ GO LIVE นั้นมันจะทำงานเพียงไม่กี่นาทีในการให้แอปขึ้นไปอยู่บน Google Play</p><h3>ข้อควรระวังการใช้ Timed publishing</h3><ul><li>การใช้ Timed publishing ใช้ได้กับการ<strong>อัปเดต</strong>เท่านั้น</li><li>หากมีการ Upload APK ขึ้นไปแล้วครั้งนึง จะไม่สามารถย้อนกลับไปได้ จนกว่าจะได้รับการ GO LIVE แล้วเท่านั้น</li><li>หากเปลี่ยนแปลงข้อมูลในหน้า <strong>In-app products</strong> ข้อมูลจะถูกเผยแพร่ทันที</li><li>หากใช้ <a href="https://developers.google.com/android-publisher/#publishing">API สำหรับการ Publish application</a> (Google Play Developer API) จะต้องมากด GO LIVE ทุกครั้งเสมอ <br>(ไม่แน่ใจว่า ณ ตอนนี้มี API สำหรับ GO LIVE ทันทีแล้วหรือไม่นะ)</li></ul><blockquote>หลังจากเรากด GO LIVE ไปแล้ว จะกลับเข้ามาสู่ Standard Publishing ได้ปกตินะครับ</blockquote><p>ก็ถือเป็น Blog สั้นๆ หวังว่าคงเป็นประโยชน์สำหรับ Android Developer ทุกคนนะครับ</p><p>สามารถอ่านรายละเอียดเพิ่มเติมเกี่ยวกับ Time publishing ได้ที่นี้ครับ<br><em>link : </em><a href="https://support.google.com/googleplay/android-developer/answer/6334282?hl=en"><em>https://support.google.com/googleplay/android-developer/answer/6334282?hl=en</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b06519987a2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] Reduce Download Size with WebP Images :)]]></title>
            <link>https://medium.com/23perspective/android-reduce-download-size-with-webp-images-ac7cddd9941b?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac7cddd9941b</guid>
            <category><![CDATA[android-app-developers]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[apk-size]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Sun, 06 Aug 2017 08:53:35 GMT</pubDate>
            <atom:updated>2017-08-31T04:26:55.052Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X0OGTZNLqY1WCOYsvnBD6w.png" /></figure><p>วิธีการทำให้ APK ของเรามีขนาดที่เล็กลง มีหลายวิธี ไม่ว่าจะเป็นการทำ Proguard การลดทรัพยากร Resource ใน Project ของเรา ลด Code บลาๆๆ</p><h3>ในหัวข้อนี้ ขอนำเสนอการทำ WebP ที่ทำให้ขนาดไฟล์รูปขนาดใหญ่ ลดลง ~25 %!!</h3><p>เอาเข้าจริงๆ เรื่องขนาดไฟล์ก็เป็นเรื่องที่สำคัญมากๆเลยนะครับ User ส่วนใหญ่เวลาโหลดแอพ ก็ไม่ค่อยอยากรอนานมากหรอกครับ ถ้าเจอไฟล์เกิน 30 MB เป็นผมก็ไม่โหลดแล้วครับ ใหญ่อิบอาย :(<br>นี้ขนาดยังไม่รวม Data กับ Cache ในแอพอีกนะครับ ถ้าเป็น App Game ค่อยว่าไปอย่าง</p><h4>มาเริ่มรู้จักก่อนดีกว่าครับว่า WebP คืออะไร ?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7Zw9_irSQc80R-mO8lCeWA.png" /><figcaption>ตัวอย่างรูป Convert PNG เป็น WebP</figcaption></figure><h3><strong>WebP คืออะไร</strong></h3><p>เป็นฟอร์แมตรูปภาพแบบ <a href="http://en.wikipedia.org/wiki/Lossy_compression">lossy</a> แต่มีการบีบอัดที่เหนือกว่าไฟล์ JPEG หรือ PNG เพราะกูเกิลใช้ VP8 มาบีบอัดรูปภาพ ทำให้ได้ภาพที่มีขนาดเล็กกว่า JPEG ที่คุณภาพใกล้เคียง</p><p>WebP มันมาได้พักใหญ่ๆแล้วละ (เกิดก่อน Vector Drawable เสียอีก) แต่คนใช้นั้นน้อยเสียเหลือเกิน ถ้าเทียบกับไฟล์ SVG</p><blockquote>WebP VS Png Sample : <br><a href="https://developers.google.com/speed/webp/gallery?csw=1">https://developers.google.com/speed/webp/gallery?csw=1</a></blockquote><h3>WebP มี 2 รูปแบบ</h3><ol><li>lossy (WebP)</li><li>lossless encoding (transparent WebP)</li></ol><blockquote>Sample ระหว่าง lossy กับ lossless ของ WebP<br><a href="https://developers.google.com/speed/webp/gallery2">https://developers.google.com/speed/webp/gallery2</a></blockquote><h3>Support Android Version ?</h3><ul><li>WebP รองรับขั้นต่ำที่ Android 4.0 (API level 14) ขึ้นไป</li><li>transparent WebP รองรับที่ Android 4.3 (API level 18) ขึ้นไป</li></ul><h3>เทียบกับไฟล์ SVG อันไหนดีกว่า ?</h3><p>ต้องบอกก่อนเลยว่า ใช้งานคนละรูปแบบกันมากกว่า<br>ในขณะที่ SVG ออกแบบมาเพื่อไม่ให้ต้องใส่ไฟล์รูปหลายขนาด <br>(mdph ,hdpi ,xhdpi ,xxhdpi, xxxhdpi) ซึ่งเป็นสาเหตุที่ทำให้ไฟล์ APK ใหญ่ขึ้น<br>ส่วนตัวแล้ว ถ้าเป็นไฟล์ภาพขนาดใหญ่ๆ ส่วนใหญ่ก็จะใช้ WebP <br>แต่ถ้าเป็น icon แบบง่ายๆ อย่าง Material Icon ที่มีขนาดเล็กๆ ไม่ใหญ่ ก็จะใช้เป็น SVG</p><blockquote>Guide : Recommend ขนาดไฟล์ภาพ <strong>SVG</strong> ไว้ไม่ให้เกิน <strong>200 x 200</strong> dp</blockquote><h3><strong>ใช้ Tool ตัวไหนในการ Convert</strong></h3><p>Android Studio 2.3 มี Tool ในการ Convert เป็น WebP ให้เรียบร้อยแล้ว !! <br>สบาย ~~~~~~ !!</p><h3>ขั้นตอนการทำ WebP ใน Android Studio</h3><p>Android Studio สามารถ Convert ไฟล์ PNG, JPG, BMP, หรือ static GIF images เป็น WebP ได้</p><ol><li>เลือกรูปที่จะ Convert ใน Drawable / mipmap คลิ๊กขวา แล้วเลือก <br>Convert to WebP…</li></ol><blockquote><strong>สามารถเลือกได้มากกว่า 1 รูปนะ</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*crF_g5Q8QNDms0fK0SBYrg.png" /></figure><p>2. จะขึ้น Dialog ซึ่ง Default settings จะขึ้นอยู่กับว่าเราเลือก minSdkVersion ของ Project ไว้ที่เท่าไหร่</p><p>- <strong>Encoding quality</strong> คือ จำกัดคุณภาพให้เหลือกี่ % จากรูปดังเดิม</p><p>- <strong>Skips files where the encoded result is larger than the original</strong> หมายความว่า ถ้าแปลงเป็น WebP แล้วมันได้ขนาดไฟล์ที่ใหญ่กว่าเดิม มันจะไม่ทำไฟล์พวกนั้นให้</p><blockquote><strong>ไฟล์ภาพ 9 patch ไม่สามารถแปลงเป็น WebP ได้นะครับ</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*7nPsJNAYxpA--0Or2Y8CJw.png" /><figcaption>Dialog Convert to WebP…</figcaption></figure><p>3. ถ้าติ๊ก Preview จะขึ้นหน้าต่างเพื่อเปรียบเทียบขนาดของภาพ จะแบ่งเป็น 3 ช่อง ช่อง 1 คือ รูป Original ที่เลือกไว้ของเรา <br>ช่อง 2 คือความแตกต่างระหว่าง Original และ WEBP<br>ช่อง 3 คือ WebP ที่แปลงร่างสำเร็จแล้ว !!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z1mLOJw-wTLD335cKaqE7Q.png" /><figcaption>หน้าต่าง Preview WebP</figcaption></figure><p>จะเห็นได้ว่า ขนาดไฟล์นั้นจาก <strong>158.2 KB เหลือเพียง 9.9 KB !!! ลด 10 เท่ากว่าๆ</strong><br>ความแตกต่างของภาพนั้นดูออกยากมาก เอาจริงๆ สายตาของคนดูไม่ออกหรอกครับว่ามันแตกต่างกัน มากน้อยเพียงใด</p><p>คราวนี้ขอลองอะไรเล่นๆดูบ้าง ถ้าลองใส่ Quality ให้เลือกซัก 0% จะเกิดอะไรขึ้น !!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SU41690_3b7ZwS4LvIUgGA.png" /><figcaption>ลอง Set Quality เป็น 0% ดู</figcaption></figure><p>อันนี้สายตาคนเริ่มเห็นความแตกต่างละ แต่ขนาดไฟล์นั้นลดลงเหลือ จาก <br>158.2 KB &gt; 1.1 KB ซึ่งเยอะมาก !!!!!!</p><p>อันนี้ก็คงแล้วแต่งานละครับว่า อยากให้ภาพละเอียดมากน้อยเพียงใด แต่เอาจริงๆไม่มีใครตั้ง 0% หรอกครับ อย่างน้อยถ้ารูปที่ไม่ค่อยชัด หรือภาพที่มีแต่ Background ปรับซัก 10 - 20% ก็กำลังดีละครับ</p><blockquote><strong><em>Note:</em></strong><em>ถ้า Set Quality เป็น 100% และ </em><em>minSdkVersion เป็น level 18 <br>Android Studioจะเป็นการ convert เป็น lossless encoding โดยอัตโนมัติทันที</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F4gwPiohcrMJblbUvnjTjw.png" /></figure><p>แค่นี้ก็ลดลงไปมากแล้ว ถ้า Project มีไฟล์รูปเยอะมากกกก สามารถุลดขนาดไฟล์ APK ได้มากขึ้นไปอีก (ดีไม่ดี อาจลดได้เกือบ 5–10 MB)</p><blockquote><strong><em>สามารถใช้ APK Analyze ก่อน เพื่อวิเคราะห์ดูขนาดไฟล์ APK ก่อนว่ามันใหญ่เพราะว่าอะไรก่อนนะครับ บางทีอาจจะไม่ได้ใหญ่ที่ขนาดรูป <br>แต่โดยส่วนใหญ่แล้วมันใหญ่เพราะไฟล์รูปแหละครับ</em></strong></blockquote><p>ก็ถือเป็น Blog สั้นๆ ความจริงมันก็มาได้พักใหญ่ๆแล้ว แต่คิดว่าคงมีอีกหลายคนไม่รู้ คงมีประโยชน์มากๆสำหรับทุกคน นะครับ :) เจอกัน Blog เรื่องหน้าครับ ~</p><p>Reference<br><a href="https://developers.google.com/speed/webp/">https://developers.google.com/speed/webp/</a><br><a href="https://developer.android.com/studio/write/convert-webp.html">https://developer.android.com/studio/write/convert-webp.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac7cddd9941b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/23perspective/android-reduce-download-size-with-webp-images-ac7cddd9941b">[Android] Reduce Download Size with WebP Images :)</a> was originally published in <a href="https://medium.com/23perspective">2 3 Perspective</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Android] Privacy Policy violation]]></title>
            <link>https://medium.com/@ult3mate/android-privacy-policy-violation-1ccc6de05d12?source=rss-31ca54a28384------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ccc6de05d12</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[policy]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Pattadon Adhipanyasarij]]></dc:creator>
            <pubDate>Wed, 08 Feb 2017 07:25:39 GMT</pubDate>
            <atom:updated>2018-05-09T14:09:23.337Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eaJ1DnjCiGB_LSjoVPGgWg.jpeg" /></figure><p>เป็นอีกเรื่องที่สำคัญ (เนื่องจากพึ่งโดนไปไม่นานนี่เอง)<br>ผมเชื่อว่าหลายๆคน ไม่ได้ตั้งใจให้เกิดเรื่องแบบนี้ขึ้นหรอก (ก็ไม่ได้ตั้งใจจริงๆ) และเชื่อว่ามีอีกหลายคน ไม่ได้สนใจเรื่อง Policy ของ Google Play ด้วย</p><p>่เรื่องนี้เป็นเรื่องที่สำคัญมากๆเรื่องนึงสำหรับ Android Developer เองด้วย ดังนั้น Android Developer ควรรับรู้เรื่องพวกนี้ไว้ด้วยนะครับ</p><p>จากประสบการณ์ที่โดนมากับตัว (แบบไม่ได้ตั้งใจ จริงๆนะ T_T )<br>หลักๆตอนนี้ที่โดนมีกรณีเหล่านี้</p><h3>กรณีที่คุณโดน Warning หรือ Reject (Removal)</h3><p>ความหมายง่ายๆ Submit หรือ Update ไม่ผ่านนั้นแหละ<br>App ยังคงอยู่นะ แต่เป็น Version ก่อนหน้านี้ ไม่ใช่ Version ที่ Update ล่าสุด</p><p>Developer สามารถ Update แอพได้ต่อ แต่คุณต้องแก้สิ่งที่ทาง Google Play แจ้งมา (ปกติจะแจ้งมาทาง Email ) และผ่าน Store Listing ของ Google Play Developer Console ซึ่งเค้าจะก็แนบ Link สาเหตุที่คุณโดน และวิธีแก้ไข</p><p>ตัวอย่างที่ Google ส่งมาในเมล</p><blockquote>We have determined that your app <strong>STILL</strong> enables background playing of YouTube videos, which is a violation of the <a href="https://developers.google.com/youtube/terms">YouTube API Terms of Service</a>:</blockquote><blockquote>“Your API Client will not, and You will not encourage or create functionality for Your users or other third parties to: (8) separate, isolate, or modify the audio or video components of any YouTube audiovisual content made available through the YouTube API;”</blockquote><p>อันนี้คือ Video Youtube เล่นบน Background (ซ่อนแอปก็ยังเล่นอยู่) ซึ่งมันผิดกฎของ Youtube API (สารภาพตรงๆ แก้ประมาณ 3 รอบถึงจะผ่านได้ -0-)</p><p>อีก Case ที่เคยโดนคือ ScreenShot ซึ่งตัวผมเองก็งงนะ Submit ครั้งแรกผ่านมานานมากกกกกก แต่พึ่งมาโดน ScreenShot เนื่องจากไปละเมิดลิขสิทธิ์เค้า =-=</p><blockquote><strong>unauthorized use of copyrighted content in your promotional screenshots</strong>. If you submitted an update, the previous version of your app is still live on Google Play</blockquote><p>ผมก็เลยทำการจัดการเอารูปออกชั่วคราวไปก่อนเพื่อให้ Update ผ่าน <br>มันก็เป็นวิธีที่ไม่ดีหรอก แต่ตอนนั้นต้องรีบใช้ เลยทำแบบนี้ไป ซึ่งวิธีที่ถูกต้องจริงๆเนี้ย คือ ส่งคำร้องหา Google พร้อมเอกสารว่า เรามีสิทธิ์ใช้ Content เหล่านั้นโดนไม่ละเมิดลิทธิ์</p><p>อีกหลาย Case ที่เจอก็อย่างเช่น SSL Security , Icon App ไปเหมือนกับ App Google Map ตรงนี้ถ้าว่างๆจะเขียนเล่าให้ฟังนะครับ</p><h3>กรณีที่คุณโดน Suspended</h3><p>App โดนแบนเพราะทำผิดกฎและเงื่อนไขของ Google Play และทาง Google เองก็จะส่ง mail มาบอกว่าผิดกฎข้อไหน ตรงนี้อยากให้ Developer ระวังหน่อยครับ เพราะถ้าโดน Suspended อีกครั้ง Account Play Store นั้นจะโดนแบนถาวรไปเลย แถมสมัครใหม่ด้วย Account นั้นไม่ได้ด้วยนะ !!(NO~~~~~~~~~~~~~~~~~~~~~~~ 25$ RIP)</p><p>ตัวอย่างที่เคยโดนมากับตัว</p><blockquote>After review, [Appname], [Package NAme], has been suspended and removed from Google Play as a policy strike because it violates the <strong>impersonation</strong> policy.</blockquote><blockquote><strong>Next Steps</strong></blockquote><blockquote>Read through the <a href="https://play.google.com/about/spam.html#impersonation-intellectual-property:impersonation">Impersonation</a> article for more details and examples of policy violations.</blockquote><blockquote>Make sure your app is compliant with the <a href="https://play.google.com/about/spam.html#impersonation-intellectual-property">Impersonation and Intellectual Property</a> policy and all other policies listed in the<a href="https://play.google.com/about/developer-content-policy.html">Developer Program Policies</a>. Remember additional enforcement could occur if there are further policy issues with your apps.</blockquote><blockquote><a href="https://play.google.com/apps/publish">Sign in to your Developer Console</a> and submit the policy compliant app using a new package name and a new app name.</blockquote><blockquote><strong>What if I have permission to use the content?</strong></blockquote><blockquote><a href="https://support.google.com/googleplay/android-developer/troubleshooter/2993242">Contact our support team</a> to provide a justification for its use. Justification may include providing proof that you are authorized to use the content in your app or some other legal justification.</blockquote><blockquote>Additional suspensions of any nature may result in the termination of your developer account, and investigation and possible termination of related Google accounts. If your account is terminated, payments will cease and Google may recover the proceeds of any past sales and/or the cost of any associated fees (such as chargebacks and transaction fees) from you.</blockquote><blockquote>If you’ve reviewed the policy and feel this suspension may have been in error, please reach out to our <a href="https://support.google.com/googleplay/android-developer/troubleshooter/2993242">policy support team</a>. One of my colleagues will get back to you within 2 business days.</blockquote><blockquote>Regards,</blockquote><blockquote>The Google Play Review Team</blockquote><p>ประสบการณ์ตรงๆเลยครับ สารภาพตรงๆของ Developer อย่างผมเลยนะครับ ไม่ได้ตั้งใจให้โดนแบนเลย และ Google เองไม่ได้ตรวจครั้งแรกที่ Submit แต่ดันไปเจอตอน update ประมาณครั้งที่ 8 โดนปุ๊บ ถึงกับสติแตก =[]=</p><p>ตรงจุดนี้ ใจเย็นๆไว้นะครับ ถ้าคุณคิดว่า App ที่คุณทำไม่ได้ผิดกฎตามที่บอก สามารถขอคำอุทรณ์ได้ตาม Link นี้ครับ</p><p><a href="https://support.google.com/googleplay/android-developer/contact/appappeals">https://support.google.com/googleplay/android-developer/contact/appappeals</a></p><p>มันจะมีกฎบางข้อที่คุณต้องหา เอกสารรับรอง หรือหลักฐานอะไรก็ได้ที่ทำให้ทาง Google รู้ว่าคุณมีสิทธิ์ใช้รูปภาพ , icon และ Content เหล่านี้</p><p><strong>-Intellectual property (</strong>ทรัพย์สินทางปัญญา<strong>)<br>-impersonation or deceptive behavior (</strong>การปลอมแปลงเป็นบุคคลอื่น<strong>)</strong></p><p>โดยการส่งตรวจ Google จะใช้เวลาประมาณ 1 วันในการ Review <br>(ตอนส่งใช้แค่ 1 วันจริงๆ แต่ตอนขอเอกสารเนี้ย ราวๆ 2 สัปดาห์ lol)</p><p>อีกกรณีที่เคยโดนคือทำ App ที่กรอกรหัสประชาชน แต่ดันไม่ได้ทำ Policy ของ App ไว้ ดังนั้นก็ควรระวังจุดนี้ไว้ด้วย</p><p>ถ้า Account โดนแบน แต่เราไม่ได้ทำอะไรผิดจริงๆ Google มันตรวจผิดพลาด หรือเหตุผลอะไรก็ตาม ก็สามารถเอา Account คืนได้เช่นกันครับ</p><p><a href="https://support.google.com/googleplay/android-developer/contact/accountappeals?hl=en">https://support.google.com/googleplay/android-developer/contact/accountappeals?hl=en</a></p><p><strong><em>สรุปว่า </em></strong>Android Developer ควรอ่าน Policy ไว้คร่าวๆก่อนที่จะทำ App เพื่อป้องกันไม่ให้เกิดเหตุการณ์แบบนี้ขึ้น (กรณีเลวร้ายสุดคือ Account โดนแบน นี้แหละ)</p><p>อ่านได้ทีนี้เลยครับ</p><p><a href="http://play.google.com/about/developer-distribution-agreement.html">https://play.google.com/about/developer-distribution-agreement.html</a><br><a href="https://play.google.com/about/android-developer-policies.html">https://play.google.com/about/android-developer-policies.html</a> <a href="https://play.google.com/about/developer-content-policy/">https://play.google.com/about/developer-content-policy/</a></p><h3><strong><em>แต่สิ่งที่สำคัญที่สุดในหัวข้อเรื่องนี้คือ Privacy Policy Url บน GooglePlay ครับ</em></strong></h3><p>ล่าสุดทาง Google ได้ออกข้อบังคับใหม่ โดย Application ที่ใช้ Permission ที่เกี่ยวข้องกับ User โดยตรง ทาง Google จะให้ใส่ Privacy Policy ในแอพ แม้จะเป็นภายในแอพหรือนอกแอพก็ตาม</p><p>บน Google Play Developer Console เคยเห็นแบบนี้กันมั้ยเอ่ย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/1*nTNZfXYmgRf_aGd7S3t7Cw.png" /></figure><p>ระวังไว้ ถ้าปล่อยไว้แบบนี้ซักระยะ Google จะส่ง Mail แบบนี้มาครับ</p><blockquote><strong>Policy issue</strong>: Google Play requires developers to provide a valid privacy policy when the app requests or handles sensitive user or device information. Your app requests sensitive permissions (e.g. camera, microphone, accounts, contacts, or phone) or user data, but does not include a valid privacy policy.</blockquote><blockquote><strong>Action required:</strong> Include a link to a valid privacy policy on your app’s Store Listing page and within your app. You can find more information in our <a href="https://support.google.com/googleplay/android-developer/answer/113469#privacy">help center</a>.</blockquote><blockquote>Alternatively, you may opt-out of this requirement by removing any requests for sensitive permissions or user data.</blockquote><blockquote>If you have additional apps in your catalog, please make sure they are compliant with our <a href="https://play.google.com/about/privacy-security/personal-sensitive/">Prominent Disclosure</a> requirements.</blockquote><blockquote>Please resolve this issue by <strong>March 15, 2017</strong>, or administrative action will be taken to limit the visibility of your app, up to and including removal from the Play Store. Thanks for helping us provide a clear and transparent experience for Google Play users.</blockquote><blockquote>Regards,</blockquote><blockquote>The Google Play Team</blockquote><p>ถ้าไม่ใส่ Privacy Policy ก่อนวันที่ 15 มีนาคม 2017 App นี้จะโดนเอาออกจาก PlayStore ทันทีนะ เรื่องวันที่ จากเท่าที่อ่าน ไม่ใช่ทุกแอพบนโลกที่จะโดนตรวจในวันที่ 15 มีนาคม 2017 นะ แต่ส่วนใหญ่จะโดนกันในวันที่ 15 มีนาคม</p><p>ดังนั้นทุก Application ที่มีการใช้ Permission camera, microphone, accounts, contacts, หรือ phone ควรใส่ Privacy Policy ของ Google Play ไว้ด้วยนะครับ</p><p>ส่วนเรื่องการเขียน Privacy Policy นั้นก็ต้องเขียนให้ครอบคลุมที่ Permission ที่เราใช้ด้วยนะ (ตรงจุดนี้ก็ยังไม่แน่ใจเกี่ยวกับการตรวจเรื่อง Policy ของ Google เหมือนกัน)</p><blockquote>ก็เป็นบทความสั้นๆนะครับ สำหรับเรื่องที่สำคัญอย่างมากของการทำ Application เอาจริงๆไม่ใช่แค่ Android หรอกครับ iOS , Web ก็มีครับ<br>ดังนั้น ก่อนที่จะทำแอพอะไร ตรวจสอบไว้ก่อนก็ดีครับผม :P</blockquote><p>ส่วนใครมีอะไรที่พบเจอต่างจากกระผม สามารถแชร์มารับฟังกันได้นะครับ</p><blockquote><strong>Update 1/08/2017</strong><br>ณ ตอนนี้ Google Play Developer Console บังคับใส่ Privacy Policy URL แล้วนะครับผม :) ในกรณีที่ขอ Permission บางอย่าง</blockquote><p><strong>Update 9/05/2018</strong></p><p>App ที่ไม่ได้ใส่ link policy ใน console ณ. ตอนนี้ ผมโดนไปแล้วหลายแอป ซึ่ง app ได้ถูก removal ออกไป วิธีแก้คือใส่ link policy ใน Google play จากนั้น submit update 1 รอบ รอซักพัก แอปของคุณจะกลับมาอยู่บน Play Store เหมือนเดิมครับ แต่ถ้าโชคร้ายำปผิดเรื่องอื่นอีก ก็คงต้องจัดการตามที่ mail ที่ Google ส่งมาละครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ccc6de05d12" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>