tag:blogger.com,1999:blog-73034545357185007042024-03-05T13:19:09.165+07:00Tutorial Android - Android StudioBlog Tutorial Android Bagi Pemulagwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.comBlogger57125tag:blogger.com,1999:blog-7303454535718500704.post-29466580276788785182022-01-17T22:18:00.003+07:002022-01-17T22:32:52.940+07:00Membuat Game Android Tic Tac Toe Menggunakan Java<p><span style="font-family: georgia;">Selamat malam sobat koding sekalian! Lama sekali tidak posting karena kesibukan yang menumpuk. Pada postingan ini saya akan berbagi sebuah game sederhana, yaitu game <b>Tic Tac Toe</b> atau silang-bulat-silang dalam versi bahasa Indonesia, menggunakan Android Studio dengan bahasa Java. Bagi yang belum tahu bagaimana cara bermain game ini bisa melihat di <a href="https://id.wikipedia.org/wiki/Silang-bulat-silang" target="_blank">Wikipedia</a>.<span></span></span></p><a name='more'></a><p></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhyKKnuojn173IBvWmTTwK5HIgZWZutGBsVjU-7RWFi43v9qDFgOmbqaFTjYFf_GaA4V1HknCXiKJfdDKszY9eB7P15g2e5v23L6Ci6RilQImjyR2aU43Jq6Ty0ytk19iYe076o7UHI2YszGWPpUBjT3rnBtwu_tEaIUlpuYWkPqxkTtqHrDmkqZrEY=s1199" style="margin-left: auto; margin-right: auto;"><img alt="Making android Tic Tac Toe game" border="0" data-original-height="1199" data-original-width="1080" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEhyKKnuojn173IBvWmTTwK5HIgZWZutGBsVjU-7RWFi43v9qDFgOmbqaFTjYFf_GaA4V1HknCXiKJfdDKszY9eB7P15g2e5v23L6Ci6RilQImjyR2aU43Jq6Ty0ytk19iYe076o7UHI2YszGWPpUBjT3rnBtwu_tEaIUlpuYWkPqxkTtqHrDmkqZrEY=w288-h320" title="Tic Tac Toe" width="288" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #38761d;"><b>Gambar 1</b></span> : Permainan Tic Tac Toe</td></tr></tbody></table><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Baik, tidak berlama-lama kita mulai langsung dengan membuat sebuah project empty activity baru pada android studio, dalam contoh ini saya berikan nama Tic Tac Toe (</span><i style="font-family: georgia;">com.gwnbs.tictactoe</i><span style="font-family: georgia;">). Karena ini sederhana, kita tidak menambahkan apapun di luar android studio, alias android studio apa adanya.</span></p><p><span style="font-family: georgia;">Kita langsung menambahkan komponen-komponen xml di dalam <b>activity_main.xml</b>, sembilan tombol (Button) utama untuk bermain game Tic Tac Toe dan beberapa komponen tambahan. Seperti berikut isinya :</span></p><span style="font-family: georgia;"><b>activity_main.xml</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/playerOne"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Player One"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/playerOneScore"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="0"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/playerOne"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/playerOne"</span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/playerTwo"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Player Two"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/playerTwoScore"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="0"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/playerTwo"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/playerTwo"</span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/playerStatus"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textAlignment</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/playerOneScore"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_0"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/playerStatus"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@+id/btn_1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/btn_0"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@+id/btn_0"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@id/btn_2"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_2"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/btn_1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@+id/btn_1"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_3"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@+id/btn_4"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/btn_0"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_4"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/btn_3"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@+id/btn_3"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@id/btn_5"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_5"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/btn_4"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@+id/btn_4"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@+id/btn_1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/btn_3"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_7"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/btn_6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@+id/btn_6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@id/btn_8"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/btn_8"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="135dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="60sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#413F43"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/btn_7"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@+id/btn_7"</span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/resetGame"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="60dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Reset Game"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="#ffffff"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="#E1470D"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/btn_6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><span style="font-family: georgia;"><br /></span><div><span style="font-family: georgia;">Pada file diatas kita memiliki 5 komponen TextView untuk update skor, 9 Button untuk bermain dan 1 Button untuk mereset permainan. Lanjut pada koding java di <b>MainActivity.java</b>, tidak perlu panjang lebar berikut isi selengkapnya dari file ini.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.tictactoe</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.annotation.</span><span style="color: #9e880d;">SuppressLint</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.DialogInterface</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.graphics.Color</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Button</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AlertDialog</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span><span style="color: #0033b3;">implements </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Global Variable<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">playerOneScore</span>, <span style="color: #871094;">playerTwoScore</span>, <span style="color: #871094;">playerStatus</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">Button</span>[] <span style="color: #871094;">buttons </span>= <span style="color: #0033b3;">new </span>Button[<span style="color: #1750eb;">9</span>];<br /> <span style="color: #0033b3;">private </span><span style="color: black;">Button </span><span style="color: #871094;">resetGame</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">playerOneScoreCount</span>, <span style="color: #871094;">playerTwoScoreCount</span>, <span style="color: #871094;">rountCount</span>;<br /> <span style="color: #0033b3;">private boolean </span><span style="color: #871094;">activePlayer</span>;<br /> <span style="color: #0033b3;">private boolean </span><span style="color: #871094;">chooseSign </span>= <span style="color: #0033b3;">true</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">String </span><span style="color: #871094;">playerOneSign</span>, <span style="color: #871094;">playerTwoSign</span>;<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//P1 => 0, P2 => 1, empty => 2<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">int</span>[] <span style="color: #871094;">gameState </span>= {<span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">2</span>};<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Posisi menang (mendatar menurun dan diagonal)<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">int</span>[][] <span style="color: #871094;">winningPositions </span>= {<br /> {<span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">1</span>, <span style="color: #1750eb;">2</span>}, {<span style="color: #1750eb;">3</span>, <span style="color: #1750eb;">4</span>, <span style="color: #1750eb;">5</span>}, {<span style="color: #1750eb;">6</span>, <span style="color: #1750eb;">7</span>, <span style="color: #1750eb;">8</span>},<br /> {<span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">3</span>, <span style="color: #1750eb;">6</span>}, {<span style="color: #1750eb;">1</span>, <span style="color: #1750eb;">4</span>, <span style="color: #1750eb;">7</span>}, {<span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">5</span>, <span style="color: #1750eb;">8</span>},<br /> {<span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">4</span>, <span style="color: #1750eb;">8</span>}, {<span style="color: #1750eb;">2</span>, <span style="color: #1750eb;">4</span>, <span style="color: #1750eb;">6</span>}<br /> };<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//inisialiasi id<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">playerOneScore </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">playerOneScore</span>);<br /> <span style="color: #871094;">playerTwoScore </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">playerTwoScore</span>);<br /> <span style="color: #871094;">playerStatus </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">playerStatus</span>);<br /> <span style="color: #871094;">resetGame </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">resetGame</span>);<br /><br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">chooseSign</span>) dialogChooseSide();<br /> <span style="color: #0033b3;">else </span>startGame();<br /><br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">dialogChooseSide</span>() {<br /> <span style="color: black;">AlertDialog</span>.<span style="color: black;">Builder chooseSideDialog </span>= <span style="color: #0033b3;">new </span><span style="color: black;">AlertDialog</span>.Builder(<span style="color: #0033b3;">this</span>);<br /> <span style="color: black;">chooseSideDialog</span>.setCancelable(<span style="color: #0033b3;">false</span>);<br /> <span style="color: black;">chooseSideDialog</span>.setTitle(<span style="color: #067d17;">"Choose your side"</span>);<br /> <span style="color: black;">chooseSideDialog</span>.setPositiveButton(<span style="color: #067d17;">"X"</span>, (dialogInterface, i) -><br /> readyToPlay(dialogInterface, <span style="color: #067d17;">"X"</span>, <span style="color: #067d17;">"O"</span>));<br /> <span style="color: black;">chooseSideDialog</span>.setNegativeButton(<span style="color: #067d17;">"O"</span>, (dialogInterface, i) -><br /> readyToPlay(dialogInterface, <span style="color: #067d17;">"O"</span>, <span style="color: #067d17;">"X"</span>));<br /> <span style="color: black;">chooseSideDialog</span>.create().show();<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">readyToPlay</span>(<span style="color: black;">DialogInterface </span>dialogInterface, <span style="color: black;">String </span>signOne, <span style="color: black;">String </span>signTwo) {<br /> dialogInterface.dismiss();<br /> <span style="color: #871094;">playerOneSign </span>= signOne;<br /> <span style="color: #871094;">playerTwoSign </span>= signTwo;<br /> <span style="color: #871094;">chooseSign </span>= <span style="color: #0033b3;">false</span>;<br /> startGame();<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">startGame</span>() {<br /> <span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int </span>i = <span style="color: #1750eb;">0</span>; i < <span style="color: #871094;">buttons</span>.<span style="color: #871094;">length</span>; i++) {<br /> <span style="color: black;">String buttonID </span>= <span style="color: #067d17;">"btn_" </span>+ i;<br /> <span style="color: #0033b3;">int </span><span style="color: black;">resourceID </span>= getResources().getIdentifier(<span style="color: black;">buttonID</span>, <span style="color: #067d17;">"id"</span>, getPackageName());<br /> <span style="color: #871094;">buttons</span>[i] = findViewById(<span style="color: black;">resourceID</span>);<br /> <span style="color: #871094;">buttons</span>[i].setOnClickListener(<span style="color: #0033b3;">this</span>);<br /> }<br /><br /> <span style="color: #871094;">rountCount </span>= <span style="color: #1750eb;">0</span>;<br /> <span style="color: #871094;">playerOneScoreCount </span>= <span style="color: #1750eb;">0</span>;<br /> <span style="color: #871094;">playerTwoScoreCount </span>= <span style="color: #1750eb;">0</span>;<br /> <span style="color: #871094;">activePlayer </span>= <span style="color: #0033b3;">true</span>;<br /> }<br /><br /> <span style="color: #9e880d;">@SuppressLint</span>(<span style="color: #067d17;">"SetTextI18n"</span>)<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #0033b3;">if </span>(!((<span style="color: black;">Button</span>) v).getText().toString().equals(<span style="color: #067d17;">""</span>)) {<br /> <span style="color: #0033b3;">return</span>;<br /> }<br /> <span style="color: black;">String buttonID </span>= v.getResources().getResourceEntryName(v.getId());<br /> <span style="color: #0033b3;">int </span><span style="color: black;">gameStatePointer </span>= <span style="color: black;">Integer</span>.<span style="font-style: italic;">parseInt</span>(<span style="color: black;">buttonID</span>.substring(<span style="color: black;">buttonID</span>.length() - <span style="color: #1750eb;">1</span><span style="color: #8c8c8c; font-style: italic;">//buttonID.length()<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>));<br /><br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">activePlayer</span>) {<br /> ((<span style="color: black;">Button</span>) v).setText(<span style="color: #871094;">playerOneSign</span>);<br /> ((<span style="color: black;">Button</span>) v).setTextColor(<span style="color: black;">Color</span>.<span style="font-style: italic;">parseColor</span>(<span style="color: #067d17;">"#FFC34A"</span>));<br /> <span style="color: #871094;">gameState</span>[<span style="color: black;">gameStatePointer</span>] = <span style="color: #1750eb;">0</span>;<br /> } <span style="color: #0033b3;">else </span>{<br /> ((<span style="color: black;">Button</span>) v).setText(<span style="color: #871094;">playerTwoSign</span>);<br /> ((<span style="color: black;">Button</span>) v).setTextColor(<span style="color: black;">Color</span>.<span style="font-style: italic;">parseColor</span>(<span style="color: #067d17;">"#70FFEA"</span>));<br /> <span style="color: #871094;">gameState</span>[<span style="color: black;">gameStatePointer</span>] = <span style="color: #1750eb;">1</span>;<br /> }<br /> <span style="color: #871094;">rountCount</span>++;<br /><br /> <span style="color: #0033b3;">if </span>(checkWinner()) {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">activePlayer</span>) {<br /> <span style="color: #871094;">playerOneScoreCount</span>++;<br /> updatePlayerScore();<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(<span style="color: #0033b3;">this</span>, <span style="color: #067d17;">"Player One Won!"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">playerTwoScoreCount</span>++;<br /> updatePlayerScore();<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(<span style="color: #0033b3;">this</span>, <span style="color: #067d17;">"Player Two Won!"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> playAgain();<br /> } <span style="color: #0033b3;">else if </span>(<span style="color: #871094;">rountCount </span>== <span style="color: #1750eb;">9</span>) {<br /> playAgain();<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(<span style="color: #0033b3;">this</span>, <span style="color: #067d17;">"No winner!"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">activePlayer </span>= !<span style="color: #871094;">activePlayer</span>;<br /> }<br /><br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">playerOneScoreCount </span>> <span style="color: #871094;">playerTwoScoreCount</span>) {<br /> <span style="color: #871094;">playerStatus</span>.setText(<span style="color: #067d17;">"Player One is Winning!"</span>);<br /> } <span style="color: #0033b3;">else if </span>(<span style="color: #871094;">playerTwoScoreCount </span>> <span style="color: #871094;">playerOneScoreCount</span>) {<br /> <span style="color: #871094;">playerStatus</span>.setText(<span style="color: #067d17;">"Player Two is Winning!"</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">playerStatus</span>.setText(<span style="color: #067d17;">""</span>);<br /> }<br /><br /> <span style="color: #871094;">resetGame</span>.setOnClickListener(v1 -> {<br /> playAgain();<br /> <span style="color: #871094;">playerOneScoreCount </span>= <span style="color: #1750eb;">0</span>;<br /> <span style="color: #871094;">playerTwoScoreCount </span>= <span style="color: #1750eb;">0</span>;<br /> <span style="color: #871094;">playerStatus</span>.setText(<span style="color: #067d17;">""</span>);<br /> updatePlayerScore();<br /> dialogChooseSide();<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">public boolean </span><span style="color: #00627a;">checkWinner</span>() {<br /> <span style="color: #0033b3;">boolean </span>winnerResult = <span style="color: #0033b3;">false</span>;<br /> <span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int</span>[] <span style="color: black;">winningPosition </span>: <span style="color: #871094;">winningPositions</span>) {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">gameState</span>[<span style="color: black;">winningPosition</span>[<span style="color: #1750eb;">0</span>]] == <span style="color: #871094;">gameState</span>[<span style="color: black;">winningPosition</span>[<span style="color: #1750eb;">1</span>]] &&<br /> <span style="color: #871094;">gameState</span>[<span style="color: black;">winningPosition</span>[<span style="color: #1750eb;">1</span>]] == <span style="color: #871094;">gameState</span>[<span style="color: black;">winningPosition</span>[<span style="color: #1750eb;">2</span>]] && <span style="color: #871094;">gameState</span>[<span style="color: black;">winningPosition</span>[<span style="color: #1750eb;">0</span>]] != <span style="color: #1750eb;">2</span>) {<br /> winnerResult = <span style="color: #0033b3;">true</span>;<br /> <span style="color: #0033b3;">break</span>;<br /> }<br /> }<br /> <span style="color: #0033b3;">return </span>winnerResult;<br /> }<br /><br /> <span style="color: #9e880d;">@SuppressLint</span>(<span style="color: #067d17;">"SetTextI18n"</span>)<br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">updatePlayerScore</span>() {<br /> <span style="color: #871094;">playerOneScore</span>.setText(<span style="color: black;">Integer</span>.<span style="font-style: italic;">toString</span>(<span style="color: #871094;">playerOneScoreCount</span>));<br /> <span style="color: #871094;">playerTwoScore</span>.setText(<span style="color: black;">Integer</span>.<span style="font-style: italic;">toString</span>(<span style="color: #871094;">playerTwoScoreCount</span>));<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">playAgain</span>() {<br /> <span style="color: #871094;">rountCount </span>= <span style="color: #1750eb;">0</span>;<br /> <span style="color: #871094;">activePlayer </span>= <span style="color: #0033b3;">true</span>;<br /><br /> <span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int </span>i = <span style="color: #1750eb;">0</span>; i < <span style="color: #871094;">buttons</span>.<span style="color: #871094;">length</span>; i++) {<br /> <span style="color: #871094;">gameState</span>[i] = <span style="color: #1750eb;">2</span>;<br /> <span style="color: #871094;">buttons</span>[i].setEnabled(<span style="color: #0033b3;">false</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">i2 </span>= i;<br /> <span style="color: #871094;">buttons</span>[i].animate().alpha(<span style="color: #1750eb;">0f</span>).setDuration(<span style="color: #1750eb;">500</span>).withEndAction(() -> {<br /> <span style="color: #871094;">buttons</span>[<span style="color: #851691;">i2</span>].setText(<span style="color: #067d17;">""</span>);<br /> <span style="color: #871094;">buttons</span>[<span style="color: #851691;">i2</span>].animate().alpha(<span style="color: #1750eb;">1f</span>).setDuration(<span style="color: #1750eb;">1000</span>).withEndAction(() -><br /> <span style="color: #871094;">buttons</span>[<span style="color: #851691;">i2</span>].setEnabled(<span style="color: #0033b3;">true</span>)).start();<br /> }<br /> ).start();<br /> }<br /> }<br />}</pre></div><br /><span style="font-family: georgia;"><br /></span><div><span style="font-family: georgia;">Untuk app selengkapnya dapat dilihat pada github saya di <a href="https://github.com/DaltrayNababan/TicTacToe">https://github.com/DaltrayNababan/TicTacToe</a> </span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Saya kira begitu saja tidak perlu panjang-panjang, jika ada yang ingin ditanyakan atau dijelasakan bisa tinggalkan komen atau kirimkan saya email. Terima kasih.</span></div></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-29394086817429115732021-06-23T16:36:00.004+07:002021-06-23T16:50:03.298+07:00Cara Membagikan Project Android ke Github<span style="font-family: Arima Madurai;">Pada tutorial ini, saya ingin berbagi mengenai langkah-langkah membagikan proyek Android ke <b>Github</b>. <b>Github </b>adalah platform berbasis web yang digunakan untuk kontrol versi. <b>Git </b>menyederhanakan proses bekerja dengan orang lain dan memudahkan untuk berkolaborasi dalam proyek.</span><div><span style="font-family: Arima Madurai;"><br /></span></div><div><span style="font-family: Arima Madurai;">Baik, tentunya kita harus memiliki akun <b>Github </b>terlebih dahulu, jika Anda belum memilikinya, silahkan untuk membuatnya terlebih dahulu di <a href="https://github.com/signup"><b>https://github.com/signup</b></a>. </span><span style="font-family: "Arima Madurai";">Kemudian juga Anda harus menginstal software </span><b style="font-family: "Arima Madurai";">Git </b><span style="font-family: "Arima Madurai";">di komputer Anda, silahkan mengunjungi </span><a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git" style="font-family: "Arima Madurai";"><b>https://git-scm.com/book/en/v2/Getting-Started-Installing-Git</b></a><span style="font-family: "Arima Madurai";">, ikuti langkah-langkahnya dalam penginstalan.<span><a name='more'></a></span></span><div><div><span style="font-family: Arima Madurai;"><br /></span></div><div><span style="font-family: Arima Madurai;">Dalam contoh ini saya membuat sebuah project baru dengan package <i>com.gwnbs.sharegithub</i>, karena ini hanya contoh cara membagikan project ke github, saya tidak menambahkan apa-apa di dalam project contoh ini.</span></div></div></div><div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<span style="font-family: Arima Madurai;"><br /></span></div><h3 style="text-align: left;"><span style="color: #6aa84f; font-family: Arima Madurai;"><b>Membagi Project ke Github</b></span></h3><div><span style="font-family: Arima Madurai;">Pada tab navigasi Android Studio, pilih <b><i>VCS </i></b>kemudian <b><i>Enable Version Control Integration</i></b>, selanjutnya pada menu dropdown yang ada pada dialog yang muncul pilih <b><i>Git</i></b>. Bila berhasil akan muncul pemberitahuan pada Event Log dengan pesan "<i>Created Git Repository in</i> .....(path project)". </span><span style="font-family: "Arima Madurai";">Jika sebelumnya Anda sudah melakukannya, pilihan Enable Version Control Integration tidak akan tersedia lagi pada menu VCS. Berikut gambar panduannya :</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDNhhO6IM9jhqGAud64Kp03R5o6lOJ_95r3j7C8kSgxlL9NIPSL4nBS01o9VW6N4UtTk-zcggd7GvvrMXxLYPiT1LJJstTiyyGB7pjYTUuhtkngOgkvqJCO2EifIiF9Wsk3XanKfWLmms/s1366/Screenshot+%252867%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Enabling Git version control integration" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDNhhO6IM9jhqGAud64Kp03R5o6lOJ_95r3j7C8kSgxlL9NIPSL4nBS01o9VW6N4UtTk-zcggd7GvvrMXxLYPiT1LJJstTiyyGB7pjYTUuhtkngOgkvqJCO2EifIiF9Wsk3XanKfWLmms/w400-h225/Screenshot+%252867%2529.png" title="Share Android Project to Github" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 1</b></span>: Enabling Git Version Control</td></tr></tbody></table><br /></div><div class="separator" style="clear: both; text-align: center;"><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB9t96QAJKF_PXNeUuXAdPNhpQmiPfLimotIaVk18fVPxo0NTFtHEvrM9DN6lI-iuuyonJ4mp-hiKiFbyzPadXNhc8n1qMg8QEwJiEXSX977lfUE5gNgyBF4GqPOFclQhfbdXUp7fjL4Y/s1366/Screenshot+%252869%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Enabling Git version control" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB9t96QAJKF_PXNeUuXAdPNhpQmiPfLimotIaVk18fVPxo0NTFtHEvrM9DN6lI-iuuyonJ4mp-hiKiFbyzPadXNhc8n1qMg8QEwJiEXSX977lfUE5gNgyBF4GqPOFclQhfbdXUp7fjL4Y/w400-h225/Screenshot+%252869%2529.png" title="Share Android Project to Github" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 2</b></span>: Enabling Git Version Control</td></tr></tbody></table></div><br /><div>
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: Arima Madurai;">Setelah langkah diatas, kembali lagi ke <b><i>VCS </i></b>kemudian <b><i>Import into Version Control</i></b> dan pilih <b><i>Share Project on Github</i></b>, akan muncul dialog untuk login menghubungkan akun Github Anda. Silahkan masukkan email dan password akun Github Anda lalu klik <b>Log In</b>. </span></div><div><span style="font-family: Arima Madurai;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKba_RgYnm-b-sVE59RWk2qXhHIafCayjGGlIEoEdOUMZc7E_pczEk-P-aH4l4hf0marjxyKb4-U3urwV4U7lllM132LsY1JG6FP40hUECRDqgk_AjoJRacCIEKL2I9cmZW_F3wSbZ5_o/s1366/Screenshot+%252870%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="steps how to share project to github" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKba_RgYnm-b-sVE59RWk2qXhHIafCayjGGlIEoEdOUMZc7E_pczEk-P-aH4l4hf0marjxyKb4-U3urwV4U7lllM132LsY1JG6FP40hUECRDqgk_AjoJRacCIEKL2I9cmZW_F3wSbZ5_o/w400-h225/Screenshot+%252870%2529.png" title="Share project to github" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 3</b></span>: Share Project on Github</td></tr></tbody></table><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEji67wKssZCJexhSLOhXTAmIsDpwglbxz-LjftVP1bw66LUXtIhXSQ587SvceKG6T_SyFhA41cqHl6vmdg5eaoe-bE2lAIQ6YodWldbbVOIJ1Oa1rcpLaa4oh8cd9k3lb4IFz8AwO_n8So/s1366/Screenshot+%252871%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Connecting github account to project" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEji67wKssZCJexhSLOhXTAmIsDpwglbxz-LjftVP1bw66LUXtIhXSQ587SvceKG6T_SyFhA41cqHl6vmdg5eaoe-bE2lAIQ6YodWldbbVOIJ1Oa1rcpLaa4oh8cd9k3lb4IFz8AwO_n8So/w400-h225/Screenshot+%252871%2529.png" title="Connect github account to Android Studio" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 4</b></span>: Log In Form akun Github</td></tr></tbody></table><div><span style="font-family: Arima Madurai;"><br />Jika kesulitan login menggunakan email dan password, Anda dapat login dengan menggunakan Token dengan mengklik "<b>Use Token</b>", kemudian pada dialog Token form yang muncul klik <b>Generate</b>, maka Anda akan diarahkan ke Developers Settings akun Github Anda pada browser. Scroll saja langsung halaman ke paling bawah dan klik "<b>Generate Token</b>". Setelah berhasil, salin token lalu tempelkan pada form Token tahap sebelumnya pada Android Strudio Anda.</span></div><div><span style="font-family: Arima Madurai;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIFI8_8gR_IMiJXxn6GYnzH4Q7NvnYodQivttkHwEZj5jSyV_6o1RC3K0Bwi7GSYmYvXVuKU5X3-Tvz6voaNYahOiRKJOScgaE-yYMlGqcGFUWa7GPokHFf7IY7R5TNIz2y2Gc6k4UUrY/s1366/Screenshot+%252872%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Generate a token to connect github to project" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIFI8_8gR_IMiJXxn6GYnzH4Q7NvnYodQivttkHwEZj5jSyV_6o1RC3K0Bwi7GSYmYvXVuKU5X3-Tvz6voaNYahOiRKJOScgaE-yYMlGqcGFUWa7GPokHFf7IY7R5TNIz2y2Gc6k4UUrY/w400-h225/Screenshot+%252872%2529_LI.jpg" title="Connecting github account using token" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 5</b></span>: Log In Form akun Github menggunakan Token</td></tr></tbody></table><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6PqC6D-LL886hONCosA_McWfEsO5ZJoFqIKCIgeS_NftdQJwW4s2KJwEWsAF5ARifSTrfyQDsRpUeVc30NZ5Dm06Xh1kMOgg-S-F8I7REl0ONfoz8Sfzq3fPF5ZaKmDcYf58lYFy7pcY/s1360/Screenshot+%252875%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Generate new toke acces on Github Settings" border="0" data-original-height="735" data-original-width="1360" height="216" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6PqC6D-LL886hONCosA_McWfEsO5ZJoFqIKCIgeS_NftdQJwW4s2KJwEWsAF5ARifSTrfyQDsRpUeVc30NZ5Dm06Xh1kMOgg-S-F8I7REl0ONfoz8Sfzq3fPF5ZaKmDcYf58lYFy7pcY/w400-h216/Screenshot+%252875%2529.png" title="Generate Github Token" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 6</b></span>: Membuat Token Access Github baru</td></tr></tbody></table><span style="font-family: Arima Madurai;"><br /></span></div><div>
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: Arima Madurai;">Setelah Log In baik menggunakan email ataupun token berhasil, dialog berikutnya yang muncul adalah untuk menentukan nama Repository, akses Repository, remote dan deskripsi akan muncul. Pada <b>Repository name</b> silahkan isi dengan nama yang kalian inginkan misal dicontoh ini saya beri <i>ShareGithub</i>, lalu silahkan centang <b>private </b>jika kalian tidak ingin project nya dapat diakses publik, kemudian tentukan nama untuk <b>remote </b>dan <b>deskripsi</b>. Klik <b>Share </b>jika sudah.</span></div><div><span style="font-family: Arima Madurai;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhioRUgxzxjM7fyE2YXvY7B8cDv3mF9dWFrLlwdRRE_BGKdz4_B2DHKUaV3-70DqJRnsTHlK5RAZB05udRKHk7LwO3_yLoDGuP1tDfB8u9m4oTI9aNDYszOvhe8TT6sIffkquMnteyN_TA/s1366/Screenshot+%252874%2529.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhioRUgxzxjM7fyE2YXvY7B8cDv3mF9dWFrLlwdRRE_BGKdz4_B2DHKUaV3-70DqJRnsTHlK5RAZB05udRKHk7LwO3_yLoDGuP1tDfB8u9m4oTI9aNDYszOvhe8TT6sIffkquMnteyN_TA/w400-h225/Screenshot+%252874%2529.png" title="Determine repository attribute" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 7</b></span>: Menentukan atribut Repository project</td></tr></tbody></table><br /><span style="font-family: Arima Madurai;">Berikutnya adalah menentukan pesan commit dan file-file apa saja yang ingin di bagikan ke dalam repository github. Seacra default, seluruh file pada project akan tercentang, silahkan di hilangkan centang pada file yang kalian tidak ingin di ikut sertakan. Kemudian tentukan commit message dan klik tombol <b>Add</b>.</span></div><div><span style="font-family: Arima Madurai;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirKoDBevB53c4FecneWggR_nLpxjjyBxt1AqFGvytDFXsX1wu_twWCkW5z74iCrL3me3Rw-AwmADOl2JTDx5YGo6PWoaK6KE_Wd7irkln2fWxuT0eNH93Sp-ykXpaJnXi9Z94-iJvJ5Eg/s1366/Screenshot+%252876%2529.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirKoDBevB53c4FecneWggR_nLpxjjyBxt1AqFGvytDFXsX1wu_twWCkW5z74iCrL3me3Rw-AwmADOl2JTDx5YGo6PWoaK6KE_Wd7irkln2fWxuT0eNH93Sp-ykXpaJnXi9Z94-iJvJ5Eg/w400-h225/Screenshot+%252876%2529.png" title="Share project files to github" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 8</b></span>: Menentukan file-file yang ingin dibagikan.</td></tr></tbody></table><br /><span style="font-family: Arima Madurai;">Jika berhasil, akan muncul pesan <i>"Successfully shared project on GitHub: ShareGithub</i></span><span style="font-family: "Arima Madurai";"><i>"</i> pada event log. Silahkan di klik pada pesan tersebut untuk mengarah ke repository project pada akun github Anda.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><h3 style="text-align: left;"><span style="font-family: "Arima Madurai";"><span style="color: #6aa84f;">Memperbaharui Repository Project</span></span></h3><div><span style="font-family: "Arima Madurai";">Ketika kita melakukan perubahan pada project, misalnya menambahkan file atau memodifikasi file yang ada, repository project pada akun github tidak secara otomatis diperbaharui. Ketika Anda memodifikasi sebuah file baik mengurangi atau menambahkan koding, nama file tersebut akan ditandai dengan warna tertentu (warna biru di Android Studio saya) yang artinya file tersebut perlu di commit dan di push ke repository untuk diperbaharui.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><span style="font-family: "Arima Madurai";">Contoh berikut saya membuat sebuah activity baru dengan nama <b>AnotherActivity</b>, yang sebelumnya hanya ada MainActivity. Kemudian setelah activity dibuat, muncul dialog yang menanyakan apakah saya akan menambahkannya ke Git Repository.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj98K8cgDegfrjIDBppeF73HhWQwJ33yGpMkqs-cjlc3D347eBS49z8rfs39QhrO9xKL91qfg6dxxRugyIIzwYxkiZXUzp9cl6gUbiYkbIS0FSc38flzLNABni0jRPxMiGRY_gY9OdkR0/s1366/Screenshot+%252879%2529.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj98K8cgDegfrjIDBppeF73HhWQwJ33yGpMkqs-cjlc3D347eBS49z8rfs39QhrO9xKL91qfg6dxxRugyIIzwYxkiZXUzp9cl6gUbiYkbIS0FSc38flzLNABni0jRPxMiGRY_gY9OdkR0/w400-h225/Screenshot+%252879%2529.png" title="Add new file to Git" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 9</b></span>: Menambahkan file baru ke Git.</td></tr></tbody></table><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: "Arima Madurai";">Klik centang pada "<i>Remember, don't ask again</i>" jika Anda tidak ingin dialog seperti diatas muncul lagi setiap kali membuat file baru di project, yang artinya secara otomatis file baru akan ditambahkan atau tidak ditambahkan ke Git tanpa konfirmasi lagi. Kemudian klik <b>Add</b> untuk menambahkan atau <b>Cancel </b>untuk tidak.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><span style="font-family: "Arima Madurai";">Jika ditambahkan, maka nama file baru tersebut akan diwarnai (warna hijau di Android Studio saya) </span><span style="font-family: "Arima Madurai";">yang artinya file tersebut perlu di commit dan di push ke repository untuk diperbaharui.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><span style="font-family: "Arima Madurai";">Untuk memperbaharui repository github project Anda, klik ikon <b>Commit </b>yang ada dibagian sudut kanan atas maka dialog dengan daftar file baru dan file yang dimodifikasi akan ditampilkan. Tentukan pesan commit lalu klik tombol <b>Commit</b>. Dalam contoh ini saya memodifikasi MainActivity dan menambahkan file AnotherActivity.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5VAG1SBwfHSyQY5uFp9kyrFCIZdgH39dgN8mz4S0BDJzkwHLGcSHdCEA3TpEI4Dkyp-pcIUqR9rNJXsaGOWzk0Z_CPip_iGzR4b7Qpbvb-xyTv121Bv7iJSbRgEfbTi46P6AUXWqmqCo/s1366/Screenshot+%252881%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Commit and push icon location on Android Studio" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5VAG1SBwfHSyQY5uFp9kyrFCIZdgH39dgN8mz4S0BDJzkwHLGcSHdCEA3TpEI4Dkyp-pcIUqR9rNJXsaGOWzk0Z_CPip_iGzR4b7Qpbvb-xyTv121Bv7iJSbRgEfbTi46P6AUXWqmqCo/w400-h225/Screenshot+%252881%2529.png" title="Commit and Push Git" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 10</b></span>: Letak icon Commit dan Push Git</td></tr></tbody></table><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwIHhJr1F-m6wnQsGxjZ1e9L4gcQDtKcTQDxwi_Zv7DOmMfli7ZSGgz9VYMvjH9Y3rPDd06kfB0HrXyynbQREiMZ3fuymdFjWvXXO_B8kteGeUse-snbf5vEf_c34jCs4vw6jBsZY_GdQ/s1366/Screenshot+%252880%2529.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwIHhJr1F-m6wnQsGxjZ1e9L4gcQDtKcTQDxwi_Zv7DOmMfli7ZSGgz9VYMvjH9Y3rPDd06kfB0HrXyynbQREiMZ3fuymdFjWvXXO_B8kteGeUse-snbf5vEf_c34jCs4vw6jBsZY_GdQ/w400-h225/Screenshot+%252880%2529.png" title="Commiting new file to Git" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;"><b>Gambar 11</b></span>: Commit file baru dan file yang dimodifikasi.</td></tr></tbody></table><br /><span style="font-family: "Arima Madurai";">Jika Anda medapatkan dialog warning saat melakukan Commit, silahkan di review dengan menekan tombol <b>Review </b>jika memang perlu, namun jika Anda tahu warning itu bukanlah masalah silahkan lanjut commit dengan menekan tombol <b>Commit</b>.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><span style="font-family: "Arima Madurai";">Setelah proses Commit sukses, terakhir adalah <b>Push </b>dengan menekan ikon Push yang terletak tepat disebelah kanan ikon Commit, seperti yang terlihat pada panduan gambar 10. Silahkan cek repository github project Anda. Untuk halaman github repository contoh ini dapat dilihat <b><a href="https://github.com/DaltrayNababan/ShareGithub" target="_blank">DISINI</a></b>.</span></div><div><span style="font-family: "Arima Madurai";"><br /></span></div><div><span style="font-family: "Arima Madurai";">Baik, sekian dan terimakasih. Semoga dapat membantu, jika tidak silahkan disampaikan permasalahan yang sobat alami, melalui komentar atau media lain yang disediakan.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-62827883100639273312021-04-21T22:14:00.001+07:002021-04-21T22:37:56.715+07:00Menggunakan Pemutar Video Youtube di Aplikasi Android<p><span style="font-family: georgia;">Dalam tutorial ini kita akan membahas bagaimana cara menambahkan atau menggunakan pemutar video <b>Youtube </b>ke dalam aplikasi Android. Disini kita akan menggunakan 2 cara; cara pertama adalah dengan menggunakan API Youtube resmi (agak sedikit ribet bagi yang merasa), dan cara kedua dengan menggunakan library pemutar video Youtube dari seseorang yang juga bekerja untuk Google (versi simpel).<span></span></span></p><a name='more'></a><span style="font-family: georgia;"><br /></span><p></p><h3 style="text-align: left;"><span style="font-family: georgia;"><b><span style="color: #38761d;">1. API Youtube resmi</span></b></span></h3><span style="font-family: georgia;">Untuk dapat menggunakan API Youtube resmi, kita harus memiliki yang namanya API Key yang bisa didapatkan dari <b><a href="https://console.developers.google.com/" target="_blank">Google Cloud Platform</a></b>. Untuk bisa membuat API Key, terlebih dahulu kita perlu membuat sebuah project atau project yang sudah di Google Cloud Platform. Jika sebelumnya Anda pernah menggunakan <b>Firebase Analytics</b> pada aplikasi Anda, Anda bisa menggunkan project tersebut untuk mendapatkan API Key.<br /><br />Nah sekarang silahkan kunjungi link Google Cloud Platform tersebut. Pada gambar-gambar dibawah ini mendemokan tahap-tahap dalam membuat project baru untuk mendapatkan API Key. Setelah Anda membukanya, silahkan masuk ke akun google Anda terlebih dahulu jika belum masuk, kemudian klik <b>CREATE PROJECT</b> seperti terlihat pada gambar dibawah ini.</span><div><span style="font-family: georgia;"><br /></span><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4unJqAuAZ6MrFFVy1e3P3cqIcCW6sRTJ05gt-tuHBkHMr7-qPq5UEEkMp8ZeXyf-z6ylJG_RV1rvm-xqapz9-Y76RsO2cOoFEi5IVGsfCifZmlacqXK5iwhx19h6sgPS2RDpqW_bVgwA/s1366/Screenshot+%252857%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Creating google cloud project" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4unJqAuAZ6MrFFVy1e3P3cqIcCW6sRTJ05gt-tuHBkHMr7-qPq5UEEkMp8ZeXyf-z6ylJG_RV1rvm-xqapz9-Y76RsO2cOoFEi5IVGsfCifZmlacqXK5iwhx19h6sgPS2RDpqW_bVgwA/w400-h225/Screenshot+%252857%2529_LI.jpg" title="Google Cloud Project" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;">Gambar 1</span> : Tahap pertama dalam membuat project Google Cloud baru.</td></tr></tbody></table><br /><br /><span style="font-family: georgia;">Kemudian tentukan nama untuk projectnya, untuk location bisa biarkan saja lalu klik tombol <b>CREATE</b>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeaC5WZc9rSj3Ufm6i0JhQobH10Vwu7rWv7hq3ZNln5huy9hpO_jZealrCXusDo_H72ItXq9rdSNN4Gaq7XftTV-2rao2i1xgAIfcoTD9IOHesapGq2L-AV7A9R_K9bIDoPmk9OXNaFLw/s1366/Screenshot+%252858%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Creating google cloud console project" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeaC5WZc9rSj3Ufm6i0JhQobH10Vwu7rWv7hq3ZNln5huy9hpO_jZealrCXusDo_H72ItXq9rdSNN4Gaq7XftTV-2rao2i1xgAIfcoTD9IOHesapGq2L-AV7A9R_K9bIDoPmk9OXNaFLw/w400-h225/Screenshot+%252858%2529_LI.jpg" title="Google Cloud Project" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;">Gambar 2</span> : Tahap kedua dalam membuat project Google Cloud baru.</td></tr></tbody></table><br /><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Setelah itu, kita akan dibawa ke dashboard project baru Google Cloud. Pada menu vertical yang tersedia, klik <b>Credentials</b> (dengan ikon kunci disebelah kiri), lalu akan muncul halaman credentials. Kemudian klik <b>CREATE CREDENTIALS</b> (dengan ikon plus disebelah kiri) lalu akan muncul menu dialog, pada menu dialog pilih API Key (paling atas). Lengkapnya seperti gambar berikut.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdTDLVAPLFw_G0_gJtzECIFr3SiuLhxcx7Q0pEvLfFFfB_S9bUCMfQXPQqIa25O48Fe7uDTGYoSRCEWMa7b5PYegxE2EP23WJgy2iOHUB2WOOEL6qxqm0k6RCG_lPL3piSH0cJSNPqmhM/s1366/Screenshot+%252862%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="How to create an API Key on Google Cloud Project" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdTDLVAPLFw_G0_gJtzECIFr3SiuLhxcx7Q0pEvLfFFfB_S9bUCMfQXPQqIa25O48Fe7uDTGYoSRCEWMa7b5PYegxE2EP23WJgy2iOHUB2WOOEL6qxqm0k6RCG_lPL3piSH0cJSNPqmhM/w400-h225/Screenshot+%252862%2529_LI.jpg" title="Google Cloud API Key" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;">Gambar 3</span> : Tahap pertama dalam membuat API Key.</td></tr></tbody></table><br /><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Setelah tahap pada gambar 3 diatas, segera akan muncul dialog baru lagi yang berisikan API Key Anda. Silahkan di copy API Key nya.</span></div><div><span style="font-family: georgia;"><br /></span></div><br />
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-6475664550624774"
data-ad-slot="3360046960"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHf3u8jXy4bPDji6hyphenhyphenakdWAWnHinWIbz2NvKqjgxJtHpCROEiWKALGUjlZKd0E3SAjnlMCteVPGt211FGBmi6R4ajeh__3nF7prsZ5m1IW1i9PTPK-dwIQmitvT18izoR31tzQAS7KfeE/s1366/Screenshot+%252863%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="1366" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHf3u8jXy4bPDji6hyphenhyphenakdWAWnHinWIbz2NvKqjgxJtHpCROEiWKALGUjlZKd0E3SAjnlMCteVPGt211FGBmi6R4ajeh__3nF7prsZ5m1IW1i9PTPK-dwIQmitvT18izoR31tzQAS7KfeE/s320/Screenshot+%252863%2529_LI.jpg" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;">Gambar 4</span> : API Key berhasil dibuat.</td></tr></tbody></table><br /></div><div><br /></div><div><span style="font-family: georgia;">Selanjutnya adalah mengunduh <a href="https://developers.google.com/youtube/android/player/downloads" target="_blank"><b>Youtube Android Player API</b></a> nya. File ini dikemas dalam bentuk arsip zip. Setelah kalian mengunduhnya, silahkan diekstrak file <b><i>YoutubeAndroidPlayerApi.jar</i></b> nya (terletak di dalam folder libs pada file zip tersebut).</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWAhXCIbFS53AmM5j2IqNGa1WH96zFLds4zaF-1embHkyxtz1ufv4xF6TDNL9obwQlfUcvvDO49waB2CCyhb1Uf0bR2PF-HDMM7RZ_WvKvlbES5HVYZCLAaO5bjMzsoifMLiMiB9CG9tE/s1366/Screenshot+%252864%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Download link for YoutubeAndroidPlayer" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWAhXCIbFS53AmM5j2IqNGa1WH96zFLds4zaF-1embHkyxtz1ufv4xF6TDNL9obwQlfUcvvDO49waB2CCyhb1Uf0bR2PF-HDMM7RZ_WvKvlbES5HVYZCLAaO5bjMzsoifMLiMiB9CG9tE/w400-h225/Screenshot+%252864%2529_LI.jpg" title="YoutubeAndroidPlayer.jar" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;">Gambar 5</span> : Link unduh Youtube Android Player API.</td></tr></tbody></table><br /><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Sekarang kita masuk ke Project aplikasi Android. Project tutorial ini nama paketnya adalah </span><i style="font-family: georgia;">com.gwnbs.youtubeplayer</i><span style="font-family: georgia;">. Disini saya akan mendemokan beberapa cara dalam implementasinya, mulai dari yang tersimpel hingga beberapa variasi.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Pertama buat project Aplikasi kalian terlihat sebagai Project, kemudian luaskan folder utama, luaskan lagi folder <b><i>app </i></b>dan akan muncul folder <i><b>libs</b></i>. Copy <i><b>YouTubeAndroidPlayerApi.jar</b></i> yang didownload tadi lalu pastekan ke dalam folder <b><i>libs</i></b>. Akan terlihat seperti gambar berikut.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ1gTiannczd7NJ89Q5or5STdB_ZljmS40cXw-9Vb3Duza4p4AP0lNZGCxEIcVCUvj5P32zPrhIQMJU5R5723IKrd2ELu3DW7MqiRmIkAdtgfocHcHUqiAE-r3i9-uq2YCv5_6RpfiIgA/s1366/Screenshot+%252865%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Where to place youtube android player api" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ1gTiannczd7NJ89Q5or5STdB_ZljmS40cXw-9Vb3Duza4p4AP0lNZGCxEIcVCUvj5P32zPrhIQMJU5R5723IKrd2ELu3DW7MqiRmIkAdtgfocHcHUqiAE-r3i9-uq2YCv5_6RpfiIgA/w400-h225/Screenshot+%252865%2529_LI.jpg" title="Placing YouTubeAndroidPlayerApi" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #6aa84f;">Gambar 6</span>: Folder penempatan YouTubeAndroidPlayerApi.jar.</td></tr></tbody></table><br /><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Silahkan kembalikan view project Anda ke view Android. Berikutnya adalah mengimplementasi file YouTubeAndroidPlayerApi tersebut ke dalam file build.gradle(:app) dengan menambahkannya ke dependencies. </span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>build.gradle(Module: app)</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;">plugins <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>id <span style="color: #067d17;">'com.android.application'<br /></span><span style="font-weight: bold;">}<br /></span>android <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>compileSdkVersion <span style="color: #1750eb;">30<br /></span><span style="color: #1750eb;"> </span>buildToolsVersion <span style="color: #067d17;">"30.0.2"<br /></span><span style="color: #067d17;"><br /></span><span style="color: #067d17;"> </span>defaultConfig <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>applicationId <span style="color: #067d17;">"com.gwnbs.youtubeplayer"<br /></span><span style="color: #067d17;"> </span>minSdkVersion <span style="color: #1750eb;">21<br /></span><span style="color: #1750eb;"> </span>targetSdkVersion <span style="color: #1750eb;">30<br /></span><span style="color: #1750eb;"> </span>versionCode <span style="color: #1750eb;">1<br /></span><span style="color: #1750eb;"> </span>versionName <span style="color: #067d17;">"1.0"<br /></span><span style="color: #067d17;"> </span>testInstrumentationRunner <span style="color: #067d17;">"androidx.test.runner.AndroidJUnitRunner"<br /></span><span style="color: #067d17;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;"> </span>buildTypes <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>release <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>minifyEnabled <span style="color: #0033b3;">false<br /></span><span style="color: #0033b3;"> </span>proguardFiles getDefaultProguardFile(<span style="color: #067d17;">'proguard-android-optimize.txt'</span>), <span style="color: #067d17;">'proguard-rules.pro'<br /></span><span style="color: #067d17;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"> }<br /></span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span>dependencies <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>implementation <span style="color: #067d17;">'androidx.appcompat:appcompat:1.2.0'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'com.google.android.material:material:1.3.0'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'androidx.constraintlayout:constraintlayout:2.0.4'<br /></span><span style="color: #067d17;"> </span>testImplementation <span style="color: #067d17;">'junit:junit:4.13.2'<br /></span><span style="color: #067d17;"> </span>androidTestImplementation <span style="color: #067d17;">'androidx.test.ext:junit:1.1.2'<br /></span><span style="color: #067d17;"> </span>androidTestImplementation <span style="color: #067d17;">'androidx.test.espresso:espresso-core:3.3.0'<br /></span><span style="color: #067d17;"><br /></span><span style="color: #067d17;"> </span><span style="color: #8c8c8c; font-style: italic;">//Implementasi YoutubePlayerApi<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>implementation files(<span style="color: #067d17;">'libs/YouTubeAndroidPlayerApi.jar'</span>)<br /><span style="font-weight: bold;">}</span></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Kemudian beralih ke <b>AndroidManifest.xml</b> untuk menambahkan izin penggunaan internet. Disini juga kita perlu menambahkan <i>configChanges </i>ke activity kita, tujuannya adalah supaya saat layar ponsel terotasi, activity aplikasi kita tidak termusnahkan (yang artinya activity akan di create ulang saat layar ponsel berganti-ganti orientasi).</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Jika kita tidak ingin activity kita musnah saat orientasi ponsel berubah-ubah, kita dapat menambahkan <i>configChanges </i>pada setiap activity yang kita inginkan tidak musnah saat berganti orientasi.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>AndroidManifest.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">manifest </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">package</span><span style="color: #067d17;">="com.gwnbs.youtubeplayer"</span>><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Izin penggunaan internet --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">uses-permission </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="android.permission.INTERNET"</span>/><br /><br /> <<span style="color: #0033b3;">application<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:allowBackup</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:icon</span><span style="color: #067d17;">="@mipmap/ic_launcher"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:label</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:roundIcon</span><span style="color: #067d17;">="@mipmap/ic_launcher_round"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:supportsRtl</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:theme</span><span style="color: #067d17;">="@style/Theme.YoutubePlayer"</span>><br /><br /> <<span style="color: #0033b3;">activity </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">=".MainActivity"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:configChanges</span><span style="color: #067d17;">="orientation|screenSize"</span>> <span style="color: #8c8c8c; font-style: italic;"><!-- Config Changes untuk activity Main--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">intent-filter</span>><br /> <<span style="color: #0033b3;">action </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="android.intent.action.MAIN" </span>/><br /><br /> <<span style="color: #0033b3;">category </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="android.intent.category.LAUNCHER" </span>/><br /> </<span style="color: #0033b3;">intent-filter</span>><br /> </<span style="color: #0033b3;">activity</span>><br /> </<span style="color: #0033b3;">application</span>><br /><br /></<span style="color: #0033b3;">manifest</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Kita mulai dengan cara yang paling sederhana yaitu saat activity dibuka, Youtube Player langsung memutar sebuah video. Pertama tambahkan kompenen YoutubePlayerView kedalam layout activity, dalam contoh ini <b>activity_main.xml</b>, seperti berikut.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_main.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">com.google.android.youtube.player.YouTubePlayerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/youtubePlayer"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><br />
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-6475664550624774"
data-ad-slot="1348961299"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><div><span style="font-family: georgia;"><br /></span></div>Kemudian di <b>MainActivity.java</b>, inisialisai terlebih dahulu API Key yang dibuat sebelumnya menjadi sebuah fixed String, atau bisa juga langsung kalian sertakan saat menginisialisasi YouTubePlayerView. Saat inisialisai YouTubePlayerView berhasil, didalam override onInitializationSuccess kita memanggil <i><span style="color: #38761d;">youTubePlayer.loadVideo(String videoKey);</span></i> untuk segera memutar video saat inisialisasi berhasil.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Nah, untuk mendapatkan <b>video key</b> dari sebuah video, silahkan kunjungi <a href="http://youtube.com">youtube.com</a> melalui browser, lalu putar sebuah video. Kemudian pada kolom link atau pencarian browser kalian akan terlihat misalnya seperti ini : <span style="color: #3d85c6;">https://www.youtube.com/watch?v=<u>giWIr7U1deA</u></span> .</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Pada link video youtube diatas, video key nya adalah "<b>giWIr7U1deA</b>". Pada intinya, setiap video key dari sebuah video di youtube adalah semua karakter-karakter terakhir setelah simbol sama dengan (=).</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Namun perlu diperhatikan juga misalnya kalian sebelumnya sudah menonton sebuah video di youtube dan tidak selesai maka saat kalian membuka dan menonton nya kembali, kalian akan secara otomatis melanjutkan menonton video tersebut dimulai dari durasi terakhir kalian meninggalkan video tersebut, sehingga video key akan mendapat tambahan karakter, yakni simbol "<b>&t=</b>" diikuti durasi yang kalian tinggalkan. Pastikan kalian tidak mengikutsertakan tambahan karakter tersebut saat ingin mengcopy video key.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.youtubeplayer</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.annotation.</span><span style="color: #9e880d;">SuppressLint</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubeBaseActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubeInitializationResult</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubePlayer</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubePlayerView</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">YouTubeBaseActivity </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Silahkan ganti dengan API Key kalian. Api Key dibawah ini tidak valid<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">API_KEY </span>= <span style="color: #067d17;">"AIzaSyAguheyxnJUqoiskxmndgfjdjwFHKdbXYE"</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: black;">YouTubePlayerView youTubePlayerView </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">youtubePlayer</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//inisialisasi YouTubePlayerView<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">youTubePlayerView</span>.initialize(<span style="color: #871094; font-style: italic;">API_KEY</span>, <span style="color: #0033b3;">new </span><span style="color: black;">YouTubePlayer</span>.<span style="color: black;">OnInitializedListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationSuccess</span>(<span style="color: black;">YouTubePlayer</span>.<span style="color: black;">Provider </span>provider, <span style="color: black;">YouTubePlayer </span>youTubePlayer, <span style="color: #0033b3;">boolean </span>b) {<br /> <span style="color: #8c8c8c; font-style: italic;">//Memutar video<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>youTubePlayer.loadVideo(<span style="color: #067d17;">"giWIr7U1deA"</span>);<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationFailure</span>(<span style="color: black;">YouTubePlayer</span>.<span style="color: black;">Provider </span>provider, <span style="color: black;">YouTubeInitializationResult </span>youTubeInitializationResult) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Failed to initialize. Please check something!"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Cara diatas adalah cara sederhana memutar langsung sebuah video. Nah berikutnya memutar banyak video, artinya kita memiliki sebuah daftar video yang mana ketika sebuah video selesai, video selanjutnya secara otomatis akan diputar. Disini kita memerlukan object List untuk membuat daftar video key kita. Pada contoh berikut, saya menggunakan object Array yang kemudian di konversi menjadi List.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Jika sebelumnya kita memanggil <i><span style="color: #38761d;">youTubePlayer.loadVideo(String videoKey);</span></i>, sekarang kita memanggil <i><span style="color: #38761d;">youTubePlayer.loadVideos(List<String> videoKeys);</span></i>. Disini saya sudah mengcopy 10 video key dari youtube.com. Sehingga MainActivity.java akan menjadi seperti berikut.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.youtubeplayer</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubeBaseActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubeInitializationResult</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubePlayer</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubePlayerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.Arrays</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">YouTubeBaseActivity </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Silahkan ganti dengan API Key kalian. Api Key dibawah ini tidak valid<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">API_KEY </span>= <span style="color: #067d17;">"AIzaSyAguheyxnJUqoiskxmndgfjdjwFHKdbXYE"</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: black;">YouTubePlayerView youTubePlayerView </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">youtubePlayer</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//inisialisai YouTubePlayerView<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">youTubePlayerView</span>.initialize(<span style="color: #871094; font-style: italic;">API_KEY</span>, <span style="color: #0033b3;">new </span><span style="color: black;">YouTubePlayer</span>.<span style="color: black;">OnInitializedListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationSuccess</span>(<span style="color: black;">YouTubePlayer</span>.<span style="color: black;">Provider </span>provider, <span style="color: black;">YouTubePlayer </span>youTubePlayer, <span style="color: #0033b3;">boolean </span>b) {<br /> <span style="color: #8c8c8c; font-style: italic;">//Memutar banyak video otomatis<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>youTubePlayer.loadVideos(<span style="color: black;">Arrays</span>.<span style="font-style: italic;">asList</span>(<span style="color: #871094;">videosKey</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationFailure</span>(<span style="color: black;">YouTubePlayer</span>.<span style="color: black;">Provider </span>provider, <span style="color: black;">YouTubeInitializationResult </span>youTubeInitializationResult) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Failed to initialize. Please check something!"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">String </span>[] <span style="color: #871094;">videosKey </span>= {<span style="color: #067d17;">"giWIr7U1deA"</span>, <span style="color: #067d17;">"Fp9pNPdNwjI"</span>, <span style="color: #067d17;">"nW948Va-l10"</span>, <span style="color: #067d17;">"ojGHE581lsM"</span>,<br /> <span style="color: #067d17;">"4fFMlolMo_Q"</span>, <span style="color: #067d17;">"TcMBFSGVi1c"</span>, <span style="color: #067d17;">"6ZfuNTqbHE8"</span>, <span style="color: #067d17;">"eOrNdBpGMv8"</span>, <span style="color: #067d17;">"ue80QwXMRHg"</span>, <span style="color: #067d17;">"tmeOjFno6Do"</span>};<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><br />
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-6475664550624774"
data-ad-slot="8611345975"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><div><span style="font-family: georgia;"><br /></span></div>Berikutnya kita lebih mengkostuminasi, misalnya kita memutar videonya pada sebuah event listener atau onClickListener. Tentunya activity_main.xml disini memerlukan tambahan komponen. Berikut ini saya menambahkan beberapa view dan layout. Juga sebuah LinearLayout kosong yang nantinya kita akan menambahkan view kedalamnya melalui MainActivity.java, <i>mengapa view tidak langsung ditambahkan di activity_main.xml?</i> Jawabannya karena yang akan kita tambahkan adalah 10 TextView, karena 10 itu termasuk jumlah yang banyak, maka sebaiknya kita menambahkan nya menggunakan java loop supaya lebih simpel kodingannya. Baik, berikut activity_main.xml..</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_main.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">com.google.android.youtube.player.YouTubePlayerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/youtubePlayer"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutPlay"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="top"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/youtubePlayer"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textPlay"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="0.6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="?selectableItemBackground"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:drawablePadding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Play Video"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:drawableStartCompat</span><span style="color: #067d17;">="@drawable/ic_play" </span>/><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textChooseVideo"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="?selectableItemBackground"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Pilih video"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:drawableEndCompat</span><span style="color: #067d17;">="@drawable/ic_drop" </span>/><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutVideoList"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Pada MainActivity.java, kita harus membuat variabel global object <b><i>YouTubePlayer</i></b> supaya dapat diakses nantinya diluar inisialisai <b><i>YouTubePlayerView</i></b>. Saat inisialisai YouTubePlayerView berhasil, kita membuat argumen menyamakan di dalamnya untuk variabel YouTubePlayer. Berikut dibawah ini lengkapnya, sudah saya sematkan java comment pada setiap kode/metode nya.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.youtubeplayer</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.annotation.</span><span style="color: #9e880d;">SuppressLint</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.text.TextUtils</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.util.TypedValue</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.LinearLayout</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.core.content.ContextCompat</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubeBaseActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubeInitializationResult</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubePlayer</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.youtube.player.YouTubePlayerView</span>;<br /><br /><span style="color: #9e880d;">@SuppressLint</span>(<span style="color: #067d17;">"SetTextI18n"</span>)<br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">YouTubeBaseActivity </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Silahkan ganti dengan API Key kalian. Api Key dibawah ini tidak valid<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">API_KEY </span>= <span style="color: #067d17;">"AIzaSyAguheyxnJUqoiskxmndgfjdjwFHKdbXYE"</span>;<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/*Variabel global untuk YouTubePlayer.<br /></span><span style="color: #8c8c8c; font-style: italic;"> Saat inisialisai YouTubePlayerView berhasil maka variabel ini tidak lagi null dan kita bisa mengaksesnya<br /></span><span style="color: #8c8c8c; font-style: italic;"> melalui event listener*/<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private </span><span style="color: black;">YouTubePlayer </span><span style="color: #871094;">player</span>;<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">textPlay</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">LinearLayout </span><span style="color: #871094;">layoutPlay</span>, <span style="color: #871094;">layoutVideoList</span>;<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Variabel string yang nilainya ditentukan nanti saat 10 Textview yang dibuat menggunakan loop di klik.<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private </span><span style="color: black;">String </span><span style="color: #871094;">videoKeySelected </span>= <span style="color: #067d17;">""</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Inisialisai view dengan ID<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">YouTubePlayerView youTubePlayerView </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">youtubePlayer</span>);<br /> <span style="color: #871094;">textPlay </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textPlay</span>);<br /> <span style="color: #871094;">layoutVideoList </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">layoutVideoList</span>);<br /> <span style="color: #871094;">layoutPlay </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">layoutPlay</span>);<br /> <span style="color: black;">TextView textChooseVideo </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textChooseVideo</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Inisialisasi YouTubePlayerView<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">youTubePlayerView</span>.initialize(<span style="color: #871094; font-style: italic;">API_KEY</span>, <span style="color: #0033b3;">new </span><span style="color: black;">YouTubePlayer</span>.<span style="color: black;">OnInitializedListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationSuccess</span>(<span style="color: black;">YouTubePlayer</span>.<span style="color: black;">Provider </span>provider, <span style="color: black;">YouTubePlayer </span>youTubePlayer, <span style="color: #0033b3;">boolean </span>b) {<br /> <span style="color: #8c8c8c; font-style: italic;">//Argumen untuk membuat player tidak lagi null.<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">player </span>= youTubePlayer;<br /> <span style="color: #871094;">layoutPlay</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> }<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationFailure</span>(<span style="color: black;">YouTubePlayer</span>.<span style="color: black;">Provider </span>provider, <span style="color: black;">YouTubeInitializationResult </span>youTubeInitializationResult) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Failed to initialize. Please check something!"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> setVideoList();<br /><br /> <span style="color: black;">textChooseVideo</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #871094;">layoutVideoList</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> }<br /> });<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Metode klik untuk memutar video<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">textPlay</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #8c8c8c; font-style: italic;">//TextUtils.isEmpty untuk mendeteksi null dan kosong, untuk menghindari aplikasi crash NullPointerException<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">if </span>(<span style="color: black;">TextUtils</span>.<span style="font-style: italic;">isEmpty</span>(<span style="color: #871094;">videoKeySelected</span>)) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Pilih video untuk dimainkan"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> <span style="color: #0033b3;">return</span>;<br /> }<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">player</span>.isPlaying()) {<br /> <span style="color: #871094;">player</span>.pause();<br /> }<br /> <span style="color: #871094;">player</span>.loadVideo(<span style="color: #871094;">videoKeySelected</span>);<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">String </span>[] <span style="color: #871094;">videosKey </span>= {<span style="color: #067d17;">"giWIr7U1deA"</span>, <span style="color: #067d17;">"Fp9pNPdNwjI"</span>, <span style="color: #067d17;">"nW948Va-l10"</span>, <span style="color: #067d17;">"ojGHE581lsM"</span>,<br /> <span style="color: #067d17;">"4fFMlolMo_Q"</span>, <span style="color: #067d17;">"TcMBFSGVi1c"</span>, <span style="color: #067d17;">"6ZfuNTqbHE8"</span>, <span style="color: #067d17;">"eOrNdBpGMv8"</span>, <span style="color: #067d17;">"ue80QwXMRHg"</span>, <span style="color: #067d17;">"tmeOjFno6Do"</span>};<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setVideoList</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">//Java loop menambahkan 10 Textview ke dalam linearlayout kosong<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int </span>i = <span style="color: #1750eb;">0</span>; i < <span style="color: #1750eb;">10</span>; i++) {<br /> <span style="color: black;">TextView textVideo </span>= <span style="color: #0033b3;">new </span>TextView(<span style="color: #0033b3;">this</span>);<br /> <span style="color: black;">LinearLayout</span>.<span style="color: black;">LayoutParams params </span>= <span style="color: #0033b3;">new </span><span style="color: black;">LinearLayout</span>.LayoutParams(<span style="color: black;">LinearLayout</span>.<span style="color: black;">LayoutParams</span>.<span style="color: #871094; font-style: italic;">MATCH_PARENT</span>,<br /> <span style="color: black;">LinearLayout</span>.<span style="color: black;">LayoutParams</span>.<span style="color: #871094; font-style: italic;">WRAP_CONTENT</span>);<br /> <span style="color: black;">params</span>.setMargins(<span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">10</span>, <span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">10</span>);<br /> <span style="color: black;">textVideo</span>.setLayoutParams(<span style="color: black;">params</span>);<br /> <span style="color: black;">textVideo</span>.setTextSize(<span style="color: black;">TypedValue</span>.<span style="color: #871094; font-style: italic;">COMPLEX_UNIT_SP</span>, <span style="color: #1750eb;">18</span>);<br /> <span style="color: black;">textVideo</span>.setTextColor(<span style="color: black;">ContextCompat</span>.<span style="font-style: italic;">getColor</span>(getApplicationContext(), <span style="color: black;">R</span>.<span style="color: black;">color</span>.<span style="color: #871094; font-style: italic;">black</span>));<br /> <span style="color: black;">textVideo</span>.setPadding(<span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">5</span>, <span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">5</span>);<br /> <span style="color: #871094;">layoutVideoList</span>.addView(<span style="color: black;">textVideo</span>);<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Membuat variabel ke-10 textview sehingga lebih mudah diakses untuk menentukan teks dan klik listener<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">TextView textVideo1 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">0</span>);<br /> <span style="color: black;">TextView textVideo2 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">1</span>);<br /> <span style="color: black;">TextView textVideo3 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">2</span>);<br /> <span style="color: black;">TextView textVideo4 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">3</span>);<br /> <span style="color: black;">TextView textVideo5 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">4</span>);<br /> <span style="color: black;">TextView textVideo6 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">5</span>);<br /> <span style="color: black;">TextView textVideo7 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">6</span>);<br /> <span style="color: black;">TextView textVideo8 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">7</span>);<br /> <span style="color: black;">TextView textVideo9 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">8</span>);<br /> <span style="color: black;">TextView textVideo10 </span>= (<span style="color: black;">TextView</span>) <span style="color: #871094;">layoutVideoList</span>.getChildAt(<span style="color: #1750eb;">9</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menentukan text untuk ke 10 textview<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">textVideo1</span>.setText(<span style="color: #067d17;">"SangChi Trailer"</span>);<br /> <span style="color: black;">textVideo2</span>.setText(<span style="color: #067d17;">"Black Widow Trailer"</span>);<br /> <span style="color: black;">textVideo3</span>.setText(<span style="color: #067d17;">"Loki Trailer"</span>);<br /> <span style="color: black;">textVideo4</span>.setText(<span style="color: #067d17;">"The Falcon & The Winter Soldier Trailer"</span>);<br /> <span style="color: black;">textVideo5</span>.setText(<span style="color: #067d17;">"Wanda vs Agata Harkness"</span>);<br /> <span style="color: black;">textVideo6</span>.setText(<span style="color: #067d17;">"Avengers: Endgame Trailer"</span>);<br /> <span style="color: black;">textVideo7</span>.setText(<span style="color: #067d17;">"Avengers: Infinity War Trailer"</span>);<br /> <span style="color: black;">textVideo8</span>.setText(<span style="color: #067d17;">"The Avengers Trailer"</span>);<br /> <span style="color: black;">textVideo9</span>.setText(<span style="color: #067d17;">"Thor: Ragnarok Trailer"</span>);<br /> <span style="color: black;">textVideo10</span>.setText(<span style="color: #067d17;">"The Avengers: Age of Ultron"</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//menentukan klik listener untuk ke-10 Textview<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>onTextVideoClicked(<span style="color: black;">textVideo1</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">0</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo2</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">1</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo3</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">2</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo4</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">3</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo5</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">4</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo6</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">5</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo7</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">6</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo8</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">7</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo9</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">8</span>]);<br /> onTextVideoClicked(<span style="color: black;">textVideo10</span>, <span style="color: #871094;">videosKey</span>[<span style="color: #1750eb;">9</span>]);<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Metode klik untuk ke-10 Textview yang dibuat menggunakan loop diatas<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">onTextVideoClicked</span>(<span style="color: #0033b3;">final </span><span style="color: black;">TextView </span>textVideo, <span style="color: #0033b3;">final </span><span style="color: black;">String </span>videoKey) {<br /> textVideo.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #871094;">layoutVideoList</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">videoKeySelected </span>= <span style="color: #851691;">videoKey</span>;<br /> <span style="color: #871094;">textPlay</span>.setText(<span style="color: #067d17;">"Play " </span>+ <span style="color: #851691;">textVideo</span>.getText().toString());<br /> }<br /> });<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/eR4K274PmP0" width="320" youtube-src-id="eR4K274PmP0"></iframe></div><br /></div><div><br /></div></span><h3 style="text-align: left;"><span style="color: #38761d; font-family: georgia;"><b>2. Menggunakan Library</b></span></h3></div><div><span style="font-family: georgia;">Cara ini sangat simpel tentunya, tidak membutuhkan API Key. Cukup dengan menambahkan library ke build.gradle app maka pemutar video Youtube pun siap untuk di implementasi. Library ini memenuhi syarat dan ketentuan Youtube API jadi kalian tidak perlu ragu jika ingin menggunakannya. Baik, tambahkan library berikut ke dalam dependencies pada file build.gradle(:app) dan sync project.<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;">implementation <span style="color: #067d17;">'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5'</span></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Lalu tambahkan komponennya di dalam layout activity.</span></div></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_main.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/youtubePlayer"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:showYouTubeButton</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:showFullScreenButton</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:showSeekBar</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Kemudian MainActivity.java akan terlihat seperti ini :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.youtubeplayer</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: black;">YouTubePlayerView youTubePlayerView </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">youtubePlayer</span>);<br /> getLifecycle().addObserver(<span style="color: black;">youTubePlayerView</span>);<br /><br /> <span style="color: black;">youTubePlayerView</span>.addYouTubePlayerListener(<span style="color: #0033b3;">new </span><span style="color: black;">AbstractYouTubePlayerListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onReady</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">YouTubePlayer </span>youTubePlayer) {<br /> <span style="color: #0033b3;">super</span>.onReady(youTubePlayer);<br /> youTubePlayer.loadVideo(<span style="color: #067d17;">"giWIr7U1deA"</span>, <span style="color: #1750eb;">0</span>);<br /> }<br /> });<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Untuk mengenal lebih jauh mengenai library ini, kalian bisa mengunjungi halaman Github dari project library ini <b><a href="https://github.com/PierfrancescoSoffritti/android-youtube-player" target="_blank">DISINI</a></b>. Jika misalnya Anda ingin videonya diputar pada event listener, Anda bisa mengkuti cara seperti penerapan API Youtube resmi diatas, karena class utama dalam library ini dengan API Youtube resmi tidak berbeda seperti misalnya <b>YouTubePlayerView </b>dan <b>YouTubePlayer</b>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baiklah, sekian dan terima kasih. Jika ada pertanyaan silahkan disampaikan melalui kolom komentar atau bisa hubungi melalui kontak yang tersedia.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-27373999060708178572021-04-18T00:46:00.001+07:002021-04-18T00:46:45.417+07:00Menambah dan Menghapus (Banyak) Item Pada RecyclerView Secara Dinamis<p><span style="font-family: georgia;">Karena postingan ini dikerjakan pada malam hari, maka selamat malam kepada sobat Androiders sekalian! Dalam postingan ini kita akan membahas mengenai menambah dan menghapus item pada RecyclerView secara dinamis, karena dinamis memiliki pengertian yang cukup luas, dinamis yang dimaksud pada postingan ini adalah semacam berulang-ulang atau berubah-ubah. Menghapus disini akan di implementasikan menghapus satu-persatu dan menghapus banyak sekaligus.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;">Dalam tutorial ini konsepnya adalah sebuah daftar barang, terdiri dari nama barang, jumlah dan deskripsi. Kita juga akan menggunakan SQLite Database untuk penyimpanan data. Kurang lebih seperti aplikasi catatan, shopping list dan sebagainnya yang sejenis.</span></p><p><span style="font-family: georgia;"><br /></span></p><p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifSD0n6FsVSEYW8JG61KZ3s_RYj_wwFfa2nCdl-y1TKBdS_RyF_uqZfGE9Rh0yf3ujo3cZYT6X4C1ztcjRPbvISJsk9Rr9N8e2u1QCPxYcL1C8NEnpnyyz870TIY3I5-fPc10eR5V6C8o/s1366/barang_java.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Delete and adding item to RecyclerView using SQL Database" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifSD0n6FsVSEYW8JG61KZ3s_RYj_wwFfa2nCdl-y1TKBdS_RyF_uqZfGE9Rh0yf3ujo3cZYT6X4C1ztcjRPbvISJsk9Rr9N8e2u1QCPxYcL1C8NEnpnyyz870TIY3I5-fPc10eR5V6C8o/w320-h180/barang_java.png" title="Delete Multiple Item RecyclerView" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b><span style="color: #38761d;">Gambar 1</span></b> : Folder-folder dan file-file project.</td></tr></tbody></table><span style="font-family: georgia;"><br /></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/fnBYlOP-8_Q" width="320" youtube-src-id="fnBYlOP-8_Q"></iframe></div><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Baik, mari kita mulai dengan mempersiapkan sebuah project baru. Dalam tutorial ini package projectnya adalah <i>com.gwnbs.rvadddelete</i>. Kita mulai dari activity_main.xml, isinya sebagai berikut :</span></p><p><span style="font-family: georgia;"><b>activity_main.xml</b> :<br /></span></p><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textJudul"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="#4E4E4E"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Daftar Sesuatu"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvList"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textJudul" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textAddItem"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_view"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Add"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:drawableStartCompat</span><span style="color: #067d17;">="@drawable/ic_add"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textDeleteItems"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_view"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:drawableStartCompat</span><span style="color: #067d17;">="@drawable/ic_delete"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textKosong"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Daftar kosong. Klik tombol Add untuk menambah daftar."<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Sebelumnya, berikut beberapa resource drawable yang kita gunakan dalam tutorial ini, yaitu beberapa background drawable dan icon. Silahkan di copy ke folder drawable jika mengikuti tutorial ini.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>background_input.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/white"</span>/><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="5dp"</span>/><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><br /><b>background_selected.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#E53935"</span>/><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="10dp"</span>/><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><br /><b>background_view.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">ripple </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/teal_200"</span>><br /> <<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">shape </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/teal_700"</span>/><br /> <<span style="color: #0033b3;">stroke </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="1dp"</span>/><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="10dp"</span>/><br /> </<span style="color: #0033b3;">shape</span>><br /> </<span style="color: #0033b3;">item</span>><br /></<span style="color: #0033b3;">ripple</span>></pre></div><br /><br /><b>ic_add.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="?attr/colorControlNormal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><br /><br /><b>ic_delete.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="24dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="24dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="#EC3434"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M15,16h4v2h-4zM15,8h7v2h-7zM15,12h6v2h-6zM3,18c0,1.1 0.9,2 2,2h6c1.1,0 2,-0.9 2,-2L13,8L3,8v10zM14,5h-3l-1,-1L6,4L5,5L2,5v2h12z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Berikutnya kita membuat java class model getter dan setter untuk nama barang, jumlah barang dan deskripsi barang. Didalam class ini kita juga memvariabelkan sebuah boolean dengan akses public yang nantinya akan kita gunakan untuk menandai item pada recyclerview untuk dihapus. Lalu integer id yang akan digunakan nantinya untuk menghapus item dari database. Id ini nilainya ditentukan otomatis oleh database saat kita memasukkan data ke database, jadi kita tidak menentukan nilai untuk id.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>Barang.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.rvadddelete</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">Barang </span>{<br /><br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">id</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">String </span><span style="color: #871094;">namaBarang</span>, <span style="color: #871094;">jumlahBarang</span>, <span style="color: #871094;">deskripsiBarang</span>;<br /> <span style="color: #0033b3;">public boolean </span><span style="color: #871094;">isBarangSelected </span>= <span style="color: #0033b3;">false</span>;<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getId</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">id</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">setId</span>(<span style="color: #0033b3;">int </span>id) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">id </span>= id;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getNamaBarang</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">namaBarang</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">setNamaBarang</span>(<span style="color: black;">String </span>namaBarang) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">namaBarang </span>= namaBarang;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getJumlahBarang</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">jumlahBarang</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">setJumlahBarang</span>(<span style="color: black;">String </span>jumlahBarang) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">jumlahBarang </span>= jumlahBarang;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getDeskripsiBarang</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">deskripsiBarang</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">setDeskripsiBarang</span>(<span style="color: black;">String </span>deskripsiBarang) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">deskripsiBarang </span>= deskripsiBarang;<br /> }<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Kemudian kita membuat sebuah java interface. Interface ini gunanya supaya kita dapat mengimplementasikan koding pada activity yang mana harusnya ditulis pada class adapter, terutama untuk event listener.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>BarangListener.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.rvadddelete</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public interface </span><span style="color: black;">BarangListener </span>{<br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">checkBarang</span>(<span style="color: #0033b3;">int </span>totalItem, <span style="color: #0033b3;">int </span>id);<br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">selectedBarangs</span>(<span style="color: black;">List</span><<span style="color: black;">Barang</span>> barangs);<br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">onSelected</span>(<span style="color: black;">Boolean </span>isSelected);<br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">onBarangClicked</span>(<span style="color: black;">String </span>message);<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div>Pada kode <b>BarangListener</b> diatas, metode <i>checkBarang </i>kita gunakan sebagai pendengar untuk mengirim jumlah item dan id dari item pada daftar dari adapter ke activity, <i>selectedBarangs </i>sebagai pendengar item yang ditandai, <i>onSelected </i>sebagai pendengar apakah sedang ada item yang ditandai atau tidak, lalu <i>onBarangClicked </i>sebagai pendengar saat item di klik.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Selanjutnya sebuah layout resource file sebagai container/wadah untuk item-item. Selain 3 komponen TextView untuk nama, jumlah dan deskripsi barang, di dalam layout ini kita juga menyisipkan sebuah ImagView untuk menghapus item secara tunggal.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>item_list_container.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_view"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="top"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutDetailBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@+id/imageDelete"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textNamaBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textDeskripsiBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.7"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="16sp" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textJumlahBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/layoutDetailBarang" </span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageDelete"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_input"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@null"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_delete"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textJumlahBarang" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Berikutnya kelas Adapter. Selain kelas list <b>Barang.java</b>, interface <b>BarangListener.java</b> juga kita masukkan kedalam konstruktor, supaya dapat diakses melalui activity atau <b>MainActivity.java</b> pada tutorial ini. Sehingga pada setiap metode klik yaitu klik listener untuk ImageView untuk menghapus item secara tunggal, lalu klik listener pada item (klik biasa dan long klik) kita cukup mengisikan variabel <b>BarangListener</b> diikuti metode yang diperlukan seperti yang sudah dibahas sebelumnya diatas.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Metode long klik pada item kita pakai untuk menandai item-item, sedangkan metode klik biasa/pendek pada item hanya untuk menampilkan pesan toast nama barang, namun disini kita membuat kondisi <i>if else</i> yaitu klik pendek akan mengikuti fungsi long klik jika terdapat item yang terpilih. </span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>ListBarangAdapter.java</b> : <br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.rvadddelete</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">ListBarangAdapter </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">Adapter</span><<span style="color: black;">ListBarangAdapter</span>.<span style="color: black;">ListBarangViewHolder</span>> {<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">Barang</span>> <span style="color: #871094;">barangs</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">BarangListener </span><span style="color: #871094;">listener</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">ListBarangAdapter</span>(<span style="color: black;">List</span><<span style="color: black;">Barang</span>> barangs, <span style="color: black;">BarangListener </span>listener) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">barangs </span>= barangs;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">listener </span>= listener;<br /> }<br /><br /> <span style="color: #9e880d;">@NonNull<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">ListBarangViewHolder </span><span style="color: #00627a;">onCreateViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ViewGroup </span>parent, <span style="color: #0033b3;">int </span>viewType) {<br /> <span style="color: #0033b3;">return new </span>ListBarangViewHolder(<span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(parent.getContext())<br /> .inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">item_list_container</span>, parent, <span style="color: #0033b3;">false</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBindViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ListBarangViewHolder </span>holder, <span style="color: #0033b3;">int </span>position) {<br /> holder.bindBarang(<span style="color: #871094;">barangs</span>.get(position));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public int </span><span style="color: #00627a;">getItemCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">barangs</span>.size();<br /> }<br /><br /> <span style="color: #0033b3;">class </span><span style="color: black;">ListBarangViewHolder </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">ViewHolder </span>{<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">TextView </span><span style="color: #871094;">textNamaBarang</span>, <span style="color: #871094;">textJumlahBarang</span>, <span style="color: #871094;">textDeskripsiBarang</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">ImageView </span><span style="color: #871094;">imageDelete</span>;<br /><br /> <span style="color: #00627a;">ListBarangViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">super</span>(itemView);<br /> <span style="color: #871094;">textNamaBarang </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textNamaBarang</span>);<br /> <span style="color: #871094;">textJumlahBarang </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textJumlahBarang</span>);<br /> <span style="color: #871094;">textDeskripsiBarang </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textDeskripsiBarang</span>);<br /> <span style="color: #871094;">imageDelete </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageDelete</span>);<br /> }<br /><br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">bindBarang</span>(<span style="color: #0033b3;">final </span><span style="color: black;">Barang </span>barang) {<br /> <span style="color: #871094;">textNamaBarang</span>.setText(barang.getNamaBarang());<br /> <span style="color: #871094;">textJumlahBarang</span>.setText(barang.getJumlahBarang());<br /> <span style="color: #871094;">textDeskripsiBarang</span>.setText(barang.getDeskripsiBarang());<br /> <span style="color: #871094;">imageDelete</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #871094;">barangs</span>.remove(<span style="color: #851691;">barang</span>);<br /> <span style="color: #871094;">itemView</span>.animate().translationX(<span style="color: #1750eb;">1200</span>).setDuration(<span style="color: #1750eb;">500</span>).withEndAction(<span style="color: #0033b3;">new </span><span style="color: black;">Runnable</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">run</span>() {<br /> notifyItemRemoved(getAdapterPosition());<br /> notifyItemRangeChanged(getAdapterPosition(), getItemCount());<br /> <span style="color: #871094;">listener</span>.checkBarang(<span style="color: #871094;">barangs</span>.size(), <span style="color: #851691;">barang</span>.getId());<br /> }<br /> }).start();<br /> }<br /> });<br /><br /> <span style="color: #871094;">itemView</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #0033b3;">if </span>(selectedBarangs().size() > <span style="color: #1750eb;">0</span>) {<br /> isSelected(<span style="color: #851691;">barang</span>, <span style="color: #871094;">itemView</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">listener</span>.onBarangClicked(<span style="color: #851691;">barang</span>.getNamaBarang() + <span style="color: #067d17;">" di klik"</span>);<br /> }<br /> }<br /> });<br /><br /> <span style="color: #871094;">itemView</span>.setOnLongClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnLongClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public boolean </span><span style="color: #00627a;">onLongClick</span>(<span style="color: black;">View </span>v) {<br /> isSelected(<span style="color: #851691;">barang</span>, <span style="color: #871094;">itemView</span>);<br /> <span style="color: #0033b3;">return true</span>;<br /> }<br /> });<br /><br /> <span style="color: #0033b3;">if </span>(barang.<span style="color: #871094;">isBarangSelected</span>) {<br /> <span style="color: #871094;">itemView</span>.setBackgroundResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">background_selected</span>);<br /> <span style="color: #871094;">listener</span>.onSelected(<span style="color: #0033b3;">true</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">itemView</span>.setBackgroundResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">background_view</span>);<br /> <span style="color: #0033b3;">if </span>(selectedBarangs().size() == <span style="color: #1750eb;">0</span>) {<br /> <span style="color: #871094;">listener</span>.onSelected(<span style="color: #0033b3;">false</span>);<br /> }<br /> }<br /> }<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">isSelected</span>(<span style="color: black;">Barang </span>barang, <span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">if </span>(!barang.<span style="color: #871094;">isBarangSelected</span>) {<br /> barang.<span style="color: #871094;">isBarangSelected </span>= <span style="color: #0033b3;">true</span>;<br /> itemView.setBackgroundResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">background_selected</span>);<br /> <span style="color: #871094;">listener</span>.onSelected(<span style="color: #0033b3;">true</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> barang.<span style="color: #871094;">isBarangSelected </span>= <span style="color: #0033b3;">false</span>;<br /> itemView.setBackgroundResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">background_view</span>);<br /> <span style="color: #0033b3;">if </span>(selectedBarangs().size() == <span style="color: #1750eb;">0</span>) {<br /> <span style="color: #871094;">listener</span>.onSelected(<span style="color: #0033b3;">false</span>);<br /> }<br /> }<br /> <span style="color: #871094;">listener</span>.selectedBarangs(selectedBarangs());<br /> }<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">List</span><<span style="color: black;">Barang</span>> <span style="color: #00627a;">selectedBarangs</span>() {<br /> <span style="color: black;">List</span><<span style="color: black;">Barang</span>> <span style="color: black;">selectedBarangs </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">for </span>(<span style="color: black;">Barang barang </span>: <span style="color: #871094;">barangs</span>) {<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">barang</span>.<span style="color: #871094;">isBarangSelected</span>) {<br /> <span style="color: black;">selectedBarangs</span>.add(<span style="color: black;">barang</span>);<br /> }<br /> }<br /> <span style="color: #0033b3;">return </span><span style="color: black;">selectedBarangs</span>;<br /> }<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Selanjutnya kita membuat kelas penghubung atau pengatur SQLite Database. Seperti biasaya dalam membuat kelas semacam ini, kita menentukan nama dari database, versinya, lalu membuat tabel dengan kolom-kolom nya.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>DatabaseManager.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.rvadddelete</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.ContentValues</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Context</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.database.Cursor</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.database.sqlite.SQLiteDatabase</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.database.sqlite.SQLiteOpenHelper</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.text.TextUtils</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Nullable</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.Collections</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">DatabaseManager </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">String </span><span style="color: #871094;">DB_NAME </span>= <span style="color: #067d17;">"barang.db"</span>;<br /> <span style="color: #0033b3;">private final int </span><span style="color: #871094;">DB_VERSION </span>= <span style="color: #1750eb;">1</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">SQLiteDatabase </span><span style="color: #871094;">sqLiteDatabase</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">DatabaseHelper </span><span style="color: #871094;">databaseHelper</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">Context </span><span style="color: #871094;">context</span>;<br /><br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">TABLE_BARANG </span>= <span style="color: #067d17;">"Barang"</span>;<br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">ID_BARANG </span>= <span style="color: #067d17;">"ID"</span>;<br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">NAMA_BARANG </span>= <span style="color: #067d17;">"Nama_barang"</span>;<br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">JUMLAH_BARANG </span>= <span style="color: #067d17;">"Jumlah_barang"</span>;<br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">DESKRIPSI_BARANG </span>= <span style="color: #067d17;">"Deskripsi_barang"</span>;<br /><br /> <span style="color: #0033b3;">private static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">CREATE_TABLE_BARANG </span>= <span style="color: #067d17;">" create table " </span>+ <span style="color: #871094; font-style: italic;">TABLE_BARANG </span>+ <span style="color: #067d17;">"(" </span>+ <span style="color: #871094; font-style: italic;">ID_BARANG<br /></span><span style="color: #871094; font-style: italic;"> </span>+ <span style="color: #067d17;">" integer primary key, " </span>+ <span style="color: #871094; font-style: italic;">NAMA_BARANG </span>+ <span style="color: #067d17;">" TEXT, " </span>+ <span style="color: #871094; font-style: italic;">JUMLAH_BARANG </span>+ <span style="color: #067d17;">" TEXT, "<br /></span><span style="color: #067d17;"> </span>+ <span style="color: #871094; font-style: italic;">DESKRIPSI_BARANG </span>+ <span style="color: #067d17;">" TEXT);"</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">DatabaseManager</span>(<span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">context </span>= context;<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">open</span>() {<br /> <span style="color: #871094;">databaseHelper </span>= <span style="color: #0033b3;">new </span>DatabaseHelper(<span style="color: #871094;">context</span>);<br /> <span style="color: #871094;">sqLiteDatabase </span>= <span style="color: #871094;">databaseHelper</span>.getWritableDatabase();<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">close</span>() {<br /> <span style="color: #871094;">databaseHelper</span>.close();<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">tambahBarang</span>(<span style="color: black;">Barang </span>barang) {<br /> <span style="color: black;">ContentValues cv </span>= <span style="color: #0033b3;">new </span>ContentValues();<br /> <span style="color: black;">cv</span>.put(<span style="color: #871094; font-style: italic;">NAMA_BARANG</span>, barang.getNamaBarang());<br /> <span style="color: black;">cv</span>.put(<span style="color: #871094; font-style: italic;">JUMLAH_BARANG</span>, barang.getJumlahBarang());<br /> <span style="color: black;">cv</span>.put(<span style="color: #871094; font-style: italic;">DESKRIPSI_BARANG</span>, barang.getDeskripsiBarang());<br /> <span style="color: #871094;">sqLiteDatabase</span>.insert(<span style="color: #871094; font-style: italic;">TABLE_BARANG</span>, <span style="color: #0033b3;">null</span>, <span style="color: black;">cv</span>);<br /> }<br /><br /> <span style="color: #0033b3;">public void </span><span style="color: #00627a;">hapusBarang</span>(<span style="color: #0033b3;">int </span>id) {<br /> <span style="color: #871094;">sqLiteDatabase</span>.delete(<span style="color: #871094; font-style: italic;">TABLE_BARANG</span>, <span style="color: #871094; font-style: italic;">ID_BARANG </span>+ <span style="color: #067d17;">"=" </span>+ id, <span style="color: #0033b3;">null</span>);<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">Cursor </span><span style="color: #00627a;">getDaftarFromDatabase</span>() {<br /> <span style="color: black;">String </span>[] <span style="color: black;">columns </span>= <span style="color: #0033b3;">new </span>String[]{<span style="color: #871094; font-style: italic;">ID_BARANG</span>, <span style="color: #871094; font-style: italic;">NAMA_BARANG</span>, <span style="color: #871094; font-style: italic;">JUMLAH_BARANG</span>, <span style="color: #871094; font-style: italic;">DESKRIPSI_BARANG</span>};<br /> <span style="color: black;">Cursor c </span>= <span style="color: #871094;">sqLiteDatabase</span>.query(<span style="color: #871094; font-style: italic;">TABLE_BARANG</span>, <span style="color: black;">columns</span>, <span style="color: #0033b3;">null</span>, <span style="color: #0033b3;">null</span>,<br /> <span style="color: #0033b3;">null</span>, <span style="color: #0033b3;">null</span>, <span style="color: #0033b3;">null</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">c </span>!=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: black;">c</span>.moveToFirst();<br /> }<br /> <span style="color: #0033b3;">return </span><span style="color: black;">c</span>;<br /> }<br /><br /> <span style="color: #0033b3;">class </span><span style="color: black;">DatabaseHelper </span><span style="color: #0033b3;">extends </span><span style="color: black;">SQLiteOpenHelper </span>{<br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">DatabaseHelper</span>(<span style="color: #9e880d;">@Nullable </span><span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">super</span>(context, <span style="color: #871094;">DB_NAME</span>, <span style="color: #0033b3;">null</span>, <span style="color: #871094;">DB_VERSION</span>);<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">SQLiteDatabase </span>db) {<br /> db.execSQL(<span style="color: #871094; font-style: italic;">CREATE_TABLE_BARANG</span>);<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onUpgrade</span>(<span style="color: black;">SQLiteDatabase </span>db, <span style="color: #0033b3;">int </span>oldVersion, <span style="color: #0033b3;">int </span>newVersion) {<br /> db.execSQL(<span style="color: #067d17;">"DROP TABLE IF EXISTS " </span>+ <span style="color: #871094; font-style: italic;">TABLE_BARANG</span>);<br /> }<br /> }<br /><br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div>Pada kelas <b>DatabaseManager </b>diatas, kita menentukan 4 kolom untuk id barang, nama barang, jumlah barang dan deskripsinya. Saat memasukkan data ke database yaitu metode <b>tambahBarang </b>diatas, kita tidak perlu menentukan id barang karena akan secara otomatis ditentukan oleh database itu sendiri menggunakan digit berurutan mulai dr angka 1.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Untuk menghapus item secara tunggal ataupun banyak sekaligus, kita menggunakan id tersebut. Tambahannya untuk menghapus banyak item, kita perlu menggunakan java loop untuk melipat gandakan metode penghapusan sesuai dengan jumlah item yang dipilih.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, berikutnya kita buat lagi sebuah layout resource file yang akan di inflate menggunakan AlertDialog saat ingin menambahkan item RecyclerView.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>dialog_add.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"</span>><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_view"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textJudulDialog"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:drawablePadding</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Tambah Item"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:drawableEndCompat</span><span style="color: #067d17;">="@drawable/ic_add"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">EditText<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/inputNamaBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.8"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_input"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:hint</span><span style="color: #067d17;">="Nama barang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:imeOptions</span><span style="color: #067d17;">="actionNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:importantForAutofill</span><span style="color: #067d17;">="no"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:inputType</span><span style="color: #067d17;">="textCapWords"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="18sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toStartOf</span><span style="color: #067d17;">="@+id/inputJumlahBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textJudulDialog" </span>><br /><br /> <<span style="color: #0033b3;">requestFocus</span>/><br /> </<span style="color: #0033b3;">EditText</span>><br /><br /> <<span style="color: #0033b3;">EditText<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/inputJumlahBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="80dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.8"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_input"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:hint</span><span style="color: #067d17;">="Jumlah"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:imeOptions</span><span style="color: #067d17;">="actionNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:importantForAutofill</span><span style="color: #067d17;">="no"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:inputType</span><span style="color: #067d17;">="number"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="18sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textJudulDialog" </span>/><br /><br /> <<span style="color: #0033b3;">EditText<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/inputDeskripsiBarang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.8"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/background_input"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="top"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:hint</span><span style="color: #067d17;">="Deskripsi barang"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:imeOptions</span><span style="color: #067d17;">="actionDone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:importantForAutofill</span><span style="color: #067d17;">="no"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:inputType</span><span style="color: #067d17;">="textCapSentences|textMultiLine"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:minHeight</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="18sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/inputNamaBarang" </span>/><br /><br /> <<span style="color: #0033b3;">com.google.android.material.button.MaterialButton<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/buttonAdd"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="TAMBAH"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:backgroundTint</span><span style="color: #067d17;">="@color/teal_200"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/inputDeskripsiBarang" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Terakhir kita implementasi di <b>MainActivity.java</b>. Supaya lebih simpel dalam penerapan koding yg dikirimkan <b>BarangListener.java</b> melalui adapter, kita implements kelas MainActivity ini dengan kelas BarangListener.java setelah extends AppCompatActivity, lalu kita overriding metode-metode yang ada pada BarangListener.java.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Lalu pada setiap metode override dari <b>BarangListener.java</b>, kita sisipkan koding-koding yang diperlukan. Selengkapnya seperti dibawah ini yang sudah disisipkan java comment pada setiap metode / event.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.rvadddelete</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AlertDialog</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.annotation.</span><span style="color: #9e880d;">SuppressLint</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.DialogInterface</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.database.Cursor</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.graphics.drawable.ColorDrawable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.Gravity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.EditText</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.material.button.MaterialButton</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span><span style="color: #0033b3;">implements </span><span style="color: black;">BarangListener </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/*Variabel global supaya dapat diakses dari metode atau kelas mana saja yang masih berada<br /></span><span style="color: #8c8c8c; font-style: italic;"> didalam kelas MainActivity dan tentunya yang bukan static */<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">Barang</span>> <span style="color: #871094;">barangs </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ListBarangAdapter </span><span style="color: #871094;">listBarangAdapter</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">textKosong</span>, <span style="color: #871094;">textAddItem</span>, <span style="color: #871094;">textDeleteItems</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">RecyclerView </span><span style="color: #871094;">rvList</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">List</span><<span style="color: black;">Barang</span>> <span style="color: #871094;">selectedBarangs</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">DatabaseManager </span><span style="color: #871094;">manager</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Inisialisai View menggunakan id<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">textAddItem </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textAddItem</span>);<br /> <span style="color: #871094;">textKosong </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textKosong</span>);<br /> <span style="color: #871094;">textDeleteItems </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textDeleteItems</span>);<br /> <span style="color: #871094;">rvList </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvList</span>);<br /><br /> <span style="color: #871094;">manager </span>= <span style="color: #0033b3;">new </span>DatabaseManager(getApplicationContext());<br /><br /> <span style="color: #871094;">listBarangAdapter </span>= <span style="color: #0033b3;">new </span>ListBarangAdapter(<span style="color: #871094;">barangs</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">rvList</span>.setAdapter(<span style="color: #871094;">listBarangAdapter</span>);<br /><br /> <span style="color: #871094;">textAddItem</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #871094;">textAddItem</span>.animate().translationX(<span style="color: #1750eb;">500</span>).setDuration(<span style="color: #1750eb;">300</span>).withEndAction(<span style="color: #0033b3;">new </span><span style="color: black;">Runnable</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">run</span>() {<br /> <span style="color: #871094;">textAddItem</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> dialogTambahItem();<br /> }<br /> }).start();<br /> }<br /> });<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Klik listener menghapus banyak item<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">textDeleteItems</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #871094;">manager</span>.open();<br /> <span style="color: #8c8c8c; font-style: italic;">//loop menggandakan jumlah penghapusan data sebanyak jumlah item yangg terpilih.<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int </span>i = <span style="color: #1750eb;">0</span>; i < <span style="color: #871094;">selectedBarangs</span>.size(); i++) {<br /> <span style="color: #871094;">manager</span>.hapusBarang(<span style="color: #871094;">selectedBarangs</span>.get(i).getId());<br /> }<br /> <span style="color: #871094;">manager</span>.close();<br /> onSelected(<span style="color: #0033b3;">false</span>);<br /> getBarangFromDatabase();<br /> }<br /> });<br /><br /> getBarangFromDatabase();<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Memunculkan dialog untuk menambah item.<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">dialogTambahItem</span>() {<br /> <span style="color: black;">AlertDialog</span>.<span style="color: black;">Builder b </span>= <span style="color: #0033b3;">new </span><span style="color: black;">AlertDialog</span>.Builder(<span style="color: #0033b3;">this</span>);<br /> <span style="color: black;">View view </span>= <span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(<span style="color: #0033b3;">this</span>).inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">dialog_add</span>, <span style="color: #0033b3;">null</span>);<br /> <span style="color: #0033b3;">final </span><span style="color: black;">EditText inputNamaBarang </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">inputNamaBarang</span>);<br /> <span style="color: #0033b3;">final </span><span style="color: black;">EditText inputJumlahBarang </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">inputJumlahBarang</span>);<br /> <span style="color: #0033b3;">final </span><span style="color: black;">EditText inputDeskripsiBarang </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">inputDeskripsiBarang</span>);<br /> <span style="color: black;">MaterialButton buttonAdd </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">buttonAdd</span>);<br /> <span style="color: black;">b</span>.setView(<span style="color: black;">view</span>);<br /> <span style="color: #0033b3;">final </span><span style="color: black;">AlertDialog dialogTambah </span>= <span style="color: black;">b</span>.create();<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">dialogTambah</span>.getWindow() !=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: black;">dialogTambah</span>.getWindow().setBackgroundDrawable(<span style="color: #0033b3;">new </span>ColorDrawable(<span style="color: #1750eb;">0</span>));<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Klik listener untuk menambahkan data ke database<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">buttonAdd</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: black;">String namaBarang </span>= <span style="color: #851691;">inputNamaBarang</span>.getText().toString();<br /> <span style="color: black;">String jumlahBarang </span>= <span style="color: #851691;">inputJumlahBarang</span>.getText().toString();<br /> <span style="color: black;">String deskripsiBarang </span>= <span style="color: #851691;">inputDeskripsiBarang</span>.getText().toString();<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/* Saat nama barang tidak ditentukan alias kosong maka tidak dimasukkan ke database<br /></span><span style="color: #8c8c8c; font-style: italic;"> alias dikembalikan dengan pesan toast */<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">if </span>(<span style="color: black;">namaBarang</span>.isEmpty()) {<br /> toast(<span style="color: #067d17;">"Tulis nama barang"</span>);<br /> <span style="color: #0033b3;">return</span>;<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/* Saat jumlah barang tidak ditentukan alias kosong maka tidak dimasukkan ke database<br /></span><span style="color: #8c8c8c; font-style: italic;"> alias dikembalikan dengan pesan toast */<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">if </span>(<span style="color: black;">jumlahBarang</span>.isEmpty()) {<br /> toast(<span style="color: #067d17;">"Jumlahnya berapa?"</span>);<br /> <span style="color: #0033b3;">return</span>;<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/* Saat deskripsi barang tidak ditentukan alias kosong maka tidak dimasukkan ke database<br /></span><span style="color: #8c8c8c; font-style: italic;"> alias dikembalikan dengan pesan toast */<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">if </span>(<span style="color: black;">deskripsiBarang</span>.isEmpty()) {<br /> toast(<span style="color: #067d17;">"Deskripsikan barangnya"</span>);<br /> <span style="color: #0033b3;">return</span>;<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/*Jika ketiga kondisi diatas dilewati alias tidak ada yang kosong maka data dimasukkan<br /></span><span style="color: #8c8c8c; font-style: italic;"> ke database*/<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #851691;">dialogTambah</span>.dismiss();<br /> <span style="color: #871094;">manager</span>.open();<br /> <span style="color: black;">Barang barang </span>= <span style="color: #0033b3;">new </span>Barang();<br /> <span style="color: black;">barang</span>.setNamaBarang(<span style="color: black;">namaBarang</span>);<br /> <span style="color: black;">barang</span>.setJumlahBarang(<span style="color: black;">jumlahBarang</span>);<br /> <span style="color: black;">barang</span>.setDeskripsiBarang(<span style="color: black;">deskripsiBarang</span>);<br /> <span style="color: #871094;">manager</span>.tambahBarang(<span style="color: black;">barang</span>);<br /> <span style="color: #871094;">manager</span>.close();<br /> toast(<span style="color: black;">namaBarang </span>+ <span style="color: #067d17;">" dengan jumlah " </span>+ <span style="color: black;">jumlahBarang </span>+ <span style="color: #067d17;">" ditambahkan ke daftar."</span>);<br /> getBarangFromDatabase();<br /> }<br /> });<br /> <span style="color: black;">dialogTambah</span>.setOnDismissListener(<span style="color: #0033b3;">new </span><span style="color: black;">DialogInterface</span>.<span style="color: black;">OnDismissListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onDismiss</span>(<span style="color: black;">DialogInterface </span>dialog) {<br /> <span style="color: #871094;">textAddItem</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #871094;">textAddItem</span>.animate().translationX(<span style="color: #1750eb;">0</span>).setDuration(<span style="color: #1750eb;">300</span>).start();<br /> }<br /> });<br /> <span style="color: black;">dialogTambah</span>.show();<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Mendapatkan data dari database<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">getBarangFromDatabase</span>() {<br /> <span style="color: #871094;">barangs</span>.clear();<br /> <span style="color: #871094;">rvList</span>.setAdapter(<span style="color: #0033b3;">null</span>);<br /> <span style="color: #871094;">listBarangAdapter </span>= <span style="color: #0033b3;">new </span>ListBarangAdapter(<span style="color: #871094;">barangs</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">rvList</span>.setAdapter(<span style="color: #871094;">listBarangAdapter</span>);<br /> <span style="color: #871094;">manager</span>.open();<br /> <span style="color: black;">Cursor c </span>= <span style="color: #871094;">manager</span>.getDaftarFromDatabase();<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">c</span>.getCount() > <span style="color: #1750eb;">0</span>) {<br /> <span style="color: #0033b3;">int </span><span style="color: black;">count </span>= <span style="color: #871094;">barangs</span>.size();<br /> <span style="color: #0033b3;">while </span>(!<span style="color: black;">c</span>.isAfterLast()) {<br /> <span style="color: black;">Barang barang </span>= <span style="color: #0033b3;">new </span>Barang();<br /> <span style="color: black;">barang</span>.setId(<span style="color: black;">c</span>.getInt(<span style="color: black;">c</span>.getColumnIndexOrThrow(<span style="color: black;">DatabaseManager</span>.<span style="color: #871094; font-style: italic;">ID_BARANG</span>)));<br /> <span style="color: black;">barang</span>.setNamaBarang(<span style="color: black;">c</span>.getString(<span style="color: black;">c</span>.getColumnIndexOrThrow(<span style="color: black;">DatabaseManager</span>.<span style="color: #871094; font-style: italic;">NAMA_BARANG</span>)));<br /> <span style="color: black;">barang</span>.setJumlahBarang(<span style="color: black;">c</span>.getString(<span style="color: black;">c</span>.getColumnIndexOrThrow(<span style="color: black;">DatabaseManager</span>.<span style="color: #871094; font-style: italic;">JUMLAH_BARANG</span>)));<br /> <span style="color: black;">barang</span>.setDeskripsiBarang(<span style="color: black;">c</span>.getString(<span style="color: black;">c</span>.getColumnIndexOrThrow(<span style="color: black;">DatabaseManager</span>.<span style="color: #871094; font-style: italic;">DESKRIPSI_BARANG</span>)));<br /> <span style="color: #871094;">barangs</span>.add(<span style="color: black;">barang</span>);<br /> <span style="color: black;">c</span>.moveToNext();<br /> }<br /> <span style="color: #871094;">listBarangAdapter</span>.notifyItemRangeInserted(<span style="color: black;">count</span>, <span style="color: #871094;">barangs</span>.size());<br /> }<br /> checkDaftarBarang(<span style="color: #871094;">barangs</span>.size());<br /> <span style="color: #871094;">manager</span>.close();<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/*Metode menyembunyikan / menampilkan TextView yang berada di tengah layar.<br /></span><span style="color: #8c8c8c; font-style: italic;"> Jika tidak ada item maka ditampilkan, dan sebaliknya. */<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">checkDaftarBarang</span>(<span style="color: #0033b3;">int </span>totalItem) {<br /> <span style="color: #0033b3;">if </span>(totalItem > <span style="color: #1750eb;">0</span>) {<br /> <span style="color: #871094;">textKosong</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">textKosong</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> }<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/*Metode pesan toast sehingga tidak perlu berulang-ulang memanggil kelas Toast<br /></span><span style="color: #8c8c8c; font-style: italic;"> yang juga memperpendek kodingan tentunya.*/<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">toast</span>(<span style="color: black;">String </span>message) {<br /> <span style="color: black;">Toast toast </span>= <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), message, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>);<br /> <span style="color: black;">toast</span>.setGravity(<span style="color: black;">Gravity</span>.<span style="color: #871094; font-style: italic;">CENTER</span>, <span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">0</span>);<br /> <span style="color: black;">toast</span>.show();<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menghapus item secara tunggal kemudian memeriksa jumlah item yang tersisa.<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">checkBarang</span>(<span style="color: #0033b3;">int </span>totalItem, <span style="color: #0033b3;">int </span>id) {<br /> checkDaftarBarang(totalItem);<br /> <span style="color: #871094;">manager</span>.open();<br /> <span style="color: #871094;">manager</span>.hapusBarang(id);<br /> <span style="color: #871094;">manager</span>.close();<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Mengetahui berapa jumlah item yang dipilih.<br /></span><span style="color: #8c8c8c; font-style: italic;"> //Menyamakan data list selectedBarangs dengan data list item yang dipilih<br /></span><span style="color: #8c8c8c; font-style: italic;"> /*sehingga nantinya bisa menghapus banyak item menggunakan selectedBarangs melalui java loop<br /></span><span style="color: #8c8c8c; font-style: italic;"> * yang diimplementasikan di dalam onCreate*/<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@SuppressLint</span>(<span style="color: #067d17;">"SetTextI18n"</span>)<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">selectedBarangs</span>(<span style="color: black;">List</span><<span style="color: black;">Barang</span>> barangs) {<br /> <span style="color: #871094;">selectedBarangs </span>= barangs;<br /> <span style="color: #871094;">textDeleteItems</span>.setText(<span style="color: #067d17;">"Hapus " </span>+ barangs.size() + <span style="color: #067d17;">" dipilih"</span>);<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">/*Saat tidak ada item yang terpilih, boolean ini akan false. Penentuan nilai boolean ini sudah dilakukan<br /></span><span style="color: #8c8c8c; font-style: italic;"> melalui adapter, juga melalui klik listener hapus banyak item yang terdapat didalam onCreate namun<br /></span><span style="color: #8c8c8c; font-style: italic;"> nilainya hanya false.*/<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onSelected</span>(<span style="color: black;">Boolean </span>isSelected) {<br /> <span style="color: #0033b3;">if </span>(isSelected) {<br /> <span style="color: #871094;">textDeleteItems</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">textDeleteItems</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> }<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menampilkan pesan toast saat item di klik (klik pendek / setOnClickListener)<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBarangClicked</span>(<span style="color: black;">String </span>message) {<br /> toast(message);<br /> }<br />}</pre></div><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, terimakasih atas waktu dan kunjungannya. Jika ada yang kurang berkenan mohon di maafkan, jika ada yang salah dan ada masalah silahkan diposting melalui kolom komentar.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com2tag:blogger.com,1999:blog-7303454535718500704.post-90946485878871130072021-02-21T16:07:00.003+07:002021-07-10T11:00:32.902+07:00Aplikasi Movie & TV Show Menggunakan API TMDB (Part 3: Kelanjutan dari part 2)<h3 style="text-align: left;"><span style="font-family: georgia;"><b>UPDATE !</b> </span>10 Juli 2021. Penambahan repositori Github, dapat dilihat di <a href="https://github.com/DaltrayNababan/MovieTracker" style="font-family: georgia;">https://github.com/DaltrayNababan/MovieTracker</a></h3><p><span style="font-family: georgia;"><br /></span></p><p><span style="font-family: georgia;">Untuk kembali ke tutorial bagian 2 silahkan </span><b style="font-family: georgia;"><a href="https://www.gwnbs.com/2021/02/aplikasi-movie-tv-show-menggunakan-api-themoviedb-part2.html" target="">KLIK DISINI</a></b><span style="font-family: georgia;">. </span></p><p><span style="font-family: georgia;">Pada tutorial bagian 2, terakhir kita membuat 2 buah layout resource files untuk layout movie dan tv, selanjutnya kedua layout tersebut akan kita include ke <b>activity_main.xml</b>. Berikut isi dari activity_main.xml.<span></span></span></p><a name='more'></a><p></p><span style="font-family: georgia;"><b>activity_main.xml</b> :<br /></span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutActionBar"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/color_actionbar"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textAppTitle"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageSearch"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_oval"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_search" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> <<span style="color: #0033b3;">androidx.core.widget.NestedScrollView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/scrollView"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/layoutActionBar"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutContent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">include </span><span style="color: #174ad4;">layout</span><span style="color: #067d17;">="@layout/layout_movie" </span>/><br /><br /> <<span style="color: #0033b3;">View<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="1dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/black" </span>/><br /><br /> <<span style="color: #0033b3;">include </span><span style="color: #174ad4;">layout</span><span style="color: #067d17;">="@layout/layout_tv" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.3"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:autoLink</span><span style="color: #067d17;">="web"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/text_info"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="18sp" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">androidx.core.widget.NestedScrollView</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Selanjutnya kita implementasi di <b>MainActivity.java</b> untuk mulai menampilkan <i>Most Popular Movies, Now Playing Movies, Upcoming Movies, Top Rated Movies, Most Popular TV Shows, Top Rated TV Shows dan On Air TV Shows</i>.</span><p></p><div><span style="font-family: georgia;">Isi file ini cukup panjang, mencapai hampir 500 lines termasuk import class. Class / library Retrofit kita gunakan disini untuk memperoleh data movie dan tv dari database TMDB, juga nantinya pada aktivitas pencarian dan aktivitas rincian. Karena isi class ini panjang, maka box coding area saya buat mode scrolling.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 700px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.graphics.drawable.ColorDrawable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.inputmethod.EditorInfo</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.EditText</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ProgressBar</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.RadioButton</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.RadioGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AlertDialog</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Call</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Callback</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Response</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Retrofit</span>;<br /><br /><span style="color: #8c8c8c; font-style: italic;">/**<br /></span><span style="color: #8c8c8c; font-style: italic;"> *Created by : Daltray Nababan<br /></span><span style="color: #8c8c8c; font-style: italic;"> * site : https://www.gwnbs.com<br /></span><span style="color: #8c8c8c; font-style: italic;"> * email : birwinecheese@gmail.com<br /></span><span style="color: #8c8c8c; font-style: italic;"> */<br /></span><span style="color: #8c8c8c; font-style: italic;"><br /></span><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">ApiService </span><span style="color: #871094;">apiService</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">RecyclerView </span><span style="color: #871094;">rvMoviePopular</span>, <span style="color: #871094;">rvMovieNowPlaying</span>, <span style="color: #871094;">rvMovieTopRated</span>, <span style="color: #871094;">rvMovieUpcoming</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ProgressBar </span><span style="color: #871094;">loadingMoviePopular</span>, <span style="color: #871094;">loadingMovieNowPlaying</span>, <span style="color: #871094;">loadingMovieTopRated</span>, <span style="color: #871094;">loadingMovieUpcoming</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ProgressBar </span><span style="color: #871094;">loadingMainMoviePopular</span>, <span style="color: #871094;">loadingMainMovieNowPlaying</span>, <span style="color: #871094;">loadingMainMovieTopRated</span>, <span style="color: #871094;">loadingMainMovieUpcoming</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">MovieAdapter </span><span style="color: #871094;">moviePopularAdapter</span>, <span style="color: #871094;">movieNowPlayingAdapter</span>, <span style="color: #871094;">movieUpcomingAdapter</span>, <span style="color: #871094;">movieTopRatedAdapter</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">moviePopularResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">movieNowPlayingResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">movieUpcomingResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">movieTopRatedResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">currentPageMoviePopular </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">currentPageMovieNowPlaying </span>= <span style="color: #1750eb;">1</span>,<br /> <span style="color: #871094;">currentPageMovieUpcoming </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">currentPageMovieTopRated </span>= <span style="color: #1750eb;">1</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">totalPagesMoviePopular </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">totalPagesMovieNowPlaying </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">totalPagesMovieUpcoming </span>=<span style="color: #1750eb;">1</span>,<br /> <span style="color: #871094;">totalPagesMovieTopRated </span>= <span style="color: #1750eb;">1</span>;<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">RecyclerView </span><span style="color: #871094;">rvTvPopular</span>, <span style="color: #871094;">rvTvTopRated</span>, <span style="color: #871094;">rvTvOnAir</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ProgressBar </span><span style="color: #871094;">loadingTvPopular</span>, <span style="color: #871094;">loadingTvTopRated</span>, <span style="color: #871094;">loadingTvOnAir</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ProgressBar </span><span style="color: #871094;">loadingMainTvPopular</span>, <span style="color: #871094;">loadingMainTvTopRated</span>, <span style="color: #871094;">loadingMainTvOnAir</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">TVAdapter </span><span style="color: #871094;">tvPopularAdapter</span>, <span style="color: #871094;">tvTopRatedAdapter</span>, <span style="color: #871094;">tvOnAirAdapter</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #871094;">tvPopularResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #871094;">tvTopRatedResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #871094;">tvOnAirResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">currentPageTVPopular </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">currentPageTVTopRated </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">currentPageTVOnAir </span>= <span style="color: #1750eb;">1</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">totalPagesTVPopular </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">totalPagesTVTopRated </span>= <span style="color: #1750eb;">1</span>, <span style="color: #871094;">totalPagesTVOnAir </span>= <span style="color: #1750eb;">1</span>;<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Silahkan diganti dengan API Key TMDB Anda<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">MYAPI_KEY </span>= <span style="color: #067d17;">"api_key_anda"</span>;<br /> <br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">LANGUAGE </span>= <span style="color: #067d17;">"en-US"</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">String </span><span style="color: #871094;">searchType </span>= <span style="color: #0033b3;">null</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: black;">Retrofit retrofit </span>= <span style="color: black;">ApiClient</span>.<span style="font-style: italic;">getClient</span>();<br /> <span style="color: #871094;">apiService </span>= <span style="color: black;">retrofit</span>.create(<span style="color: black;">ApiService</span>.<span style="color: #0033b3;">class</span>);<br /><br /> setPopularMovies();<br /> setNowPlayingMovies();<br /> setUpcomingMovies();<br /> setTopRatedMovies();<br /><br /> setPopularTV();<br /> setTopRatedTV();<br /> setOnAirTV();<br /><br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageSearch</span>).setOnClickListener(v -> dialogSearch());<br /><br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setPopularMovies</span>() {<br /> <span style="color: #871094;">rvMoviePopular </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvMoviePopular</span>);<br /> <span style="color: #871094;">moviePopularAdapter </span>= <span style="color: #0033b3;">new </span>MovieAdapter(<span style="color: #871094;">moviePopularResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingMoviePopular </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMoviePopular</span>);<br /> <span style="color: #871094;">loadingMainMoviePopular </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMainMoviePopular</span>);<br /> <span style="color: #871094;">loadingMainMoviePopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> getPopularMovies();<br /> <span style="color: #871094;">rvMoviePopular</span>.setAdapter(<span style="color: #871094;">moviePopularAdapter</span>);<br /><br /> <span style="color: #871094;">rvMoviePopular</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvMoviePopular</span>.canScrollHorizontally(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #871094;">loadingMoviePopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPageMoviePopular </span><= <span style="color: #871094;">totalPagesMoviePopular</span>) {<br /> <span style="color: #871094;">currentPageMoviePopular </span>+= <span style="color: #1750eb;">1</span>;<br /> getPopularMovies();<br /> }<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">getPopularMovies</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getPopularMovies(<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: #871094; font-style: italic;">LANGUAGE</span>, <span style="color: #871094;">currentPageMoviePopular</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">MovieRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">MovieRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">totalPagesMoviePopular </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">if </span>(response.body().getResults() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingMainMoviePopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMoviePopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">moviePopularResults</span>.size();<br /> <span style="color: #871094;">moviePopularResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">moviePopularAdapter</span>.notifyItemChanged(<span style="color: black;">oldCount</span>, <span style="color: #871094;">moviePopularResults</span>.size());<br /> }<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingMainMoviePopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat popular movies"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setNowPlayingMovies</span>() {<br /> <span style="color: #871094;">rvMovieNowPlaying </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvMovieNowPlaying</span>);<br /> <span style="color: #871094;">movieNowPlayingAdapter </span>= <span style="color: #0033b3;">new </span>MovieAdapter(<span style="color: #871094;">movieNowPlayingResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingMovieNowPlaying </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMovieNowPlaying</span>);<br /> <span style="color: #871094;">loadingMainMovieNowPlaying </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMainMovieNowPlaying</span>);<br /> <span style="color: #871094;">loadingMainMovieNowPlaying</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> getNowPlayingMovies();<br /> <span style="color: #871094;">rvMovieNowPlaying</span>.setAdapter(<span style="color: #871094;">movieNowPlayingAdapter</span>);<br /><br /> <span style="color: #871094;">rvMovieNowPlaying</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvMovieNowPlaying</span>.canScrollHorizontally(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #871094;">loadingMovieNowPlaying</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPageMovieNowPlaying </span><= <span style="color: #871094;">totalPagesMovieNowPlaying</span>) {<br /> <span style="color: #871094;">currentPageMovieNowPlaying </span>+= <span style="color: #1750eb;">1</span>;<br /> getNowPlayingMovies();<br /> }<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">getNowPlayingMovies</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getNowPlayingMovies(<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: #871094; font-style: italic;">LANGUAGE</span>, <span style="color: #871094;">currentPageMovieNowPlaying</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">MovieRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">MovieRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">totalPagesMovieNowPlaying </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">if </span>(response.body().getResults() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingMainMovieNowPlaying</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMovieNowPlaying</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">movieNowPlayingResults</span>.size();<br /> <span style="color: #871094;">movieNowPlayingResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">movieNowPlayingAdapter</span>.notifyItemChanged(<span style="color: black;">oldCount</span>, <span style="color: #871094;">movieNowPlayingResults</span>.size());<br /> }<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingMainMovieNowPlaying</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat now playing movies"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setUpcomingMovies</span>() {<br /> <span style="color: #871094;">rvMovieUpcoming </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvUpcomingMovie</span>);<br /> <span style="color: #871094;">movieUpcomingAdapter </span>= <span style="color: #0033b3;">new </span>MovieAdapter(<span style="color: #871094;">movieUpcomingResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingMovieUpcoming </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMovieUpcoming</span>);<br /> <span style="color: #871094;">loadingMainMovieUpcoming </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMainMovieUpcoming</span>);<br /> <span style="color: #871094;">loadingMainMovieUpcoming</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> getUpcomingMovies();<br /> <span style="color: #871094;">rvMovieUpcoming</span>.setAdapter(<span style="color: #871094;">movieUpcomingAdapter</span>);<br /><br /> <span style="color: #871094;">rvMovieUpcoming</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvMovieUpcoming</span>.canScrollHorizontally(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #871094;">loadingMovieUpcoming</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPageMovieUpcoming </span><= <span style="color: #871094;">totalPagesMovieUpcoming</span>) {<br /> <span style="color: #871094;">currentPageMovieUpcoming </span>+= <span style="color: #1750eb;">1</span>;<br /> getUpcomingMovies();<br /> }<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">getUpcomingMovies</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getUpcomingMovies(<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: #871094; font-style: italic;">LANGUAGE</span>, <span style="color: #871094;">currentPageMovieUpcoming</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">MovieRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">MovieRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">totalPagesMovieUpcoming </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">if </span>(response.body().getResults() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingMainMovieUpcoming</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMovieUpcoming</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">movieUpcomingResults</span>.size();<br /> <span style="color: #871094;">movieUpcomingResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">movieUpcomingAdapter</span>.notifyItemChanged(<span style="color: black;">oldCount</span>, <span style="color: #871094;">movieUpcomingResults</span>.size());<br /> }<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingMainMovieUpcoming</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat upcoming movies"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setTopRatedMovies</span>() {<br /> <span style="color: #871094;">rvMovieTopRated </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvMovieTopRated</span>);<br /> <span style="color: #871094;">movieTopRatedAdapter </span>= <span style="color: #0033b3;">new </span>MovieAdapter(<span style="color: #871094;">movieTopRatedResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingMovieTopRated </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMovieTopRated</span>);<br /> <span style="color: #871094;">loadingMainMovieTopRated </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMainMovieTopRated</span>);<br /> <span style="color: #871094;">loadingMainMovieTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> getTopRatedMovies();<br /> <span style="color: #871094;">rvMovieTopRated</span>.setAdapter(<span style="color: #871094;">movieTopRatedAdapter</span>);<br /><br /> <span style="color: #871094;">rvMovieTopRated</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvMovieTopRated</span>.canScrollHorizontally(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #871094;">loadingMovieTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPageMovieTopRated </span><= <span style="color: #871094;">totalPagesMovieTopRated</span>) {<br /> <span style="color: #871094;">currentPageMovieTopRated </span>+= <span style="color: #1750eb;">1</span>;<br /> getTopRatedMovies();<br /> }<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">getTopRatedMovies</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getTopRatedMovies(<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: #871094; font-style: italic;">LANGUAGE</span>, <span style="color: #871094;">currentPageMovieTopRated</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">MovieRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">MovieRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">totalPagesMovieTopRated </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">if </span>(response.body().getResults() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingMainMovieTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMovieTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">movieTopRatedResults</span>.size();<br /> <span style="color: #871094;">movieTopRatedResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">movieTopRatedAdapter</span>.notifyItemChanged(<span style="color: black;">oldCount</span>, <span style="color: #871094;">movieTopRatedResults</span>.size());<br /> }<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingMainMovieTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat top rated movies"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setPopularTV</span>() {<br /> <span style="color: #871094;">rvTvPopular </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvTvPopular</span>);<br /> <span style="color: #871094;">tvPopularAdapter </span>= <span style="color: #0033b3;">new </span>TVAdapter(<span style="color: #871094;">tvPopularResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingTvPopular </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingTvPopular</span>);<br /> <span style="color: #871094;">loadingMainTvPopular </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMainTvPopular</span>);<br /> <span style="color: #871094;">loadingMainTvPopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> getPopularTV();<br /> <span style="color: #871094;">rvTvPopular</span>.setAdapter(<span style="color: #871094;">tvPopularAdapter</span>);<br /><br /> <span style="color: #871094;">rvTvPopular</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvTvPopular</span>.canScrollHorizontally(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #871094;">loadingTvPopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPageTVPopular </span><= <span style="color: #871094;">totalPagesTVPopular</span>) {<br /> <span style="color: #871094;">currentPageTVPopular </span>+= <span style="color: #1750eb;">1</span>;<br /> getPopularTV();<br /> }<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">getPopularTV</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getTvPopular(<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: #871094; font-style: italic;">LANGUAGE</span>, <span style="color: #871094;">currentPageTVPopular</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">TVRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">TVRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">totalPagesTVPopular </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">if </span>(response.body().getResults() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingMainTvPopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingTvPopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">tvPopularResults</span>.size();<br /> <span style="color: #871094;">tvPopularResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">tvPopularAdapter</span>.notifyItemChanged(<span style="color: black;">oldCount</span>, <span style="color: #871094;">tvPopularResults</span>.size());<br /> }<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingMainTvPopular</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat popular tv shows"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setTopRatedTV</span>() {<br /> <span style="color: #871094;">rvTvTopRated </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvTvTopRated</span>);<br /> <span style="color: #871094;">tvTopRatedAdapter </span>= <span style="color: #0033b3;">new </span>TVAdapter(<span style="color: #871094;">tvTopRatedResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingTvTopRated </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingTvTopRated</span>);<br /> <span style="color: #871094;">loadingMainTvTopRated </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMainTvTopRated</span>);<br /> <span style="color: #871094;">loadingMainTvTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> getTopRatedTV();<br /> <span style="color: #871094;">rvTvTopRated</span>.setAdapter(<span style="color: #871094;">tvTopRatedAdapter</span>);<br /><br /> <span style="color: #871094;">rvTvTopRated</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvTvTopRated</span>.canScrollHorizontally(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #871094;">loadingTvTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPageTVTopRated </span><= <span style="color: #871094;">totalPagesTVTopRated</span>) {<br /> <span style="color: #871094;">currentPageTVTopRated </span>+= <span style="color: #1750eb;">1</span>;<br /> getTopRatedTV();<br /> }<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">getTopRatedTV</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getTvTopRated(<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: #871094; font-style: italic;">LANGUAGE</span>, <span style="color: #871094;">currentPageTVTopRated</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">TVRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">TVRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">totalPagesTVTopRated </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">if </span>(response.body().getResults() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingTvTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMainTvTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">tvTopRatedResults</span>.size();<br /> <span style="color: #871094;">tvTopRatedResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">tvTopRatedAdapter</span>.notifyItemChanged(<span style="color: black;">oldCount</span>, <span style="color: #871094;">tvTopRatedResults</span>.size());<br /> }<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingMainTvTopRated</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat top rated tv shows"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setOnAirTV</span>() {<br /> <span style="color: #871094;">rvTvOnAir </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvTvOnAir</span>);<br /> <span style="color: #871094;">tvOnAirAdapter </span>= <span style="color: #0033b3;">new </span>TVAdapter(<span style="color: #871094;">tvOnAirResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingTvOnAir </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingTvOnAir</span>);<br /> <span style="color: #871094;">loadingMainTvOnAir </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMainTvOnAir</span>);<br /> <span style="color: #871094;">loadingMainTvOnAir</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> getOnAirTV();<br /> <span style="color: #871094;">rvTvOnAir</span>.setAdapter(<span style="color: #871094;">tvOnAirAdapter</span>);<br /><br /> <span style="color: #871094;">rvTvOnAir</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvTvOnAir</span>.canScrollHorizontally(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #871094;">loadingTvOnAir</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPageTVOnAir </span><= <span style="color: #871094;">totalPagesTVOnAir</span>) {<br /> <span style="color: #871094;">currentPageTVOnAir </span>+= <span style="color: #1750eb;">1</span>;<br /> getOnAirTV();<br /> }<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">getOnAirTV</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getTvOnAir(<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: #871094; font-style: italic;">LANGUAGE</span>, <span style="color: #871094;">currentPageTVOnAir</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">TVRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">TVRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">totalPagesTVOnAir </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">if </span>(response.body().getResults() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingMainTvOnAir</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingTvOnAir</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">tvOnAirResults</span>.size();<br /> <span style="color: #871094;">tvOnAirResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">tvOnAirAdapter</span>.notifyItemChanged(<span style="color: black;">oldCount</span>, <span style="color: #871094;">tvOnAirResults</span>.size());<br /> }<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingMainTvOnAir</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat on air tv shows"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">dialogSearch</span>() {<br /> <span style="color: black;">AlertDialog</span>.<span style="color: black;">Builder builder </span>= <span style="color: #0033b3;">new </span><span style="color: black;">AlertDialog</span>.Builder(<span style="color: #0033b3;">this</span>);<br /> <span style="color: black;">LayoutInflater inflater </span>= getLayoutInflater();<br /> <span style="color: black;">View v </span>= <span style="color: black;">inflater</span>.inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">dialog_search</span>, <span style="color: #0033b3;">null</span>);<br /><br /> <span style="color: black;">EditText inputSearch </span>= <span style="color: black;">v</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">inputSearch</span>);<br /> <span style="color: black;">ImageView imageDoSearch </span>= <span style="color: black;">v</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageDoSearch</span>);<br /> <span style="color: black;">RadioGroup radioGroup </span>= <span style="color: black;">v</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">radioGroup</span>);<br /> <span style="color: black;">RadioButton radioButtonMovie </span>= <span style="color: black;">v</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">radioButtonMovie</span>);<br /> <span style="color: black;">RadioButton radioButtonTv </span>= <span style="color: black;">v</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">radioButtonTv</span>);<br /><br /> <span style="color: black;">builder</span>.setView(<span style="color: black;">v</span>);<br /> <span style="color: black;">AlertDialog dialogSearch </span>= <span style="color: black;">builder</span>.create();<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">dialogSearch</span>.getWindow() != <span style="color: #0033b3;">null</span>)<br /> <span style="color: black;">dialogSearch</span>.getWindow().setBackgroundDrawable(<span style="color: #0033b3;">new </span>ColorDrawable(<span style="color: #1750eb;">0</span>));<br /><br /> <span style="color: black;">radioGroup</span>.setOnCheckedChangeListener((group, checkedId) -> {<br /> <span style="color: #0033b3;">if </span>(checkedId == <span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">radioButtonMovie</span>) {<br /> <span style="color: #871094;">searchType </span>= <span style="color: #851691;">radioButtonMovie</span>.getText().toString();<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">searchType </span>= <span style="color: #851691;">radioButtonTv</span>.getText().toString();<br /> }<br /> });<br /> <span style="color: black;">imageDoSearch</span>.setOnClickListener(view -> doSearch(<span style="color: #851691;">inputSearch</span>.getText().toString()));<br /><br /> <span style="color: black;">inputSearch</span>.setOnEditorActionListener((v1, actionId, event) -> {<br /> <span style="color: #0033b3;">if </span>(actionId == <span style="color: black;">EditorInfo</span>.<span style="color: #871094; font-style: italic;">IME_ACTION_GO</span>) {<br /> doSearch(<span style="color: #851691;">inputSearch</span>.getText().toString());<br /> }<br /> <span style="color: #0033b3;">return false</span>;<br /> });<br /><br /> <span style="color: black;">dialogSearch</span>.show();<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">doSearch</span>(<span style="color: black;">String </span>query) {<br /> <span style="color: #0033b3;">if </span>(query.isEmpty()) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Tidak ada huruf terdeteksi"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> <span style="color: #0033b3;">return</span>;<br /> }<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">searchType </span>== <span style="color: #0033b3;">null</span>) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Harap pilih antara tipe movie dan tv"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> <span style="color: #0033b3;">return</span>;<br /> }<br /> <span style="color: black;">Intent i </span>= <span style="color: #0033b3;">new </span>Intent(<span style="color: black;">MainActivity</span>.<span style="color: #0033b3;">this</span>, <span style="color: black;">SearchActivity</span>.<span style="color: #0033b3;">class</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"tipe"</span>, <span style="color: #871094;">searchType</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"searchFor"</span>, query);<br /> startActivity(<span style="color: black;">i</span>);<br /> }<br />}</pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="8611345975" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Silahkan untuk mengganti nilai String MYAPI_KEY dengan API Key TMDB Anda. Karena disini kita sudah menggunakan Java versi 1.8 sehingga untuk eksekusi metode listener seperti click tidak lagi diperlukan override public onClick(View v), semua metode listener dapat digantikan dengan yang namanya "<i><b>Lambda Expression</b></i> (->)".</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Pada code MainActivity diatas, Anda akan mendapatkan error untuk <b>dialog_search.xml</b> dan Intent menuju <b><i>SearchActivity </i></b>class karena memang layout dan activity nya belum kita buat, silahkan di abaikan untuk sementara karena kita akan membuat terlebih dahulu activity details. Silahkan buat empty activity baru dengan nama <b>DetailsActivity.java</b> dan layoutnya <b>activity_details.xml</b>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Disini kita akan menampilkan beberapa details mengenai movie / tv, sekitar 16 nilai rincian termasuk gambar poster dan gambar backdrop. Details untuk movie dan tv berada dalam satu activity saja yakni DetailsActivity. Disini kita menggunakan statement untuk bergantian antara details movie atau tv, kita melacak nya dengan mengirimkan nilai Intent extra berbeda. Baik, berikut isi file layout dan java class ini.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_details.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 500px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".DetailsActivity"</span>><br /><br /> <<span style="color: #0033b3;">FrameLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.1"</span>><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imagePosterBack"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone" </span>/><br /><br /> </<span style="color: #0033b3;">FrameLayout</span>><br /><br /> <<span style="color: #0033b3;">androidx.core.widget.NestedScrollView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/scrollView"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"</span>><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"</span>><br /><br /> <<span style="color: #0033b3;">com.makeramen.roundedimageview.RoundedImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageBackdrop"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="255dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="1dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:riv_corner_radius</span><span style="color: #067d17;">="4dp" </span>/><br /><br /> <<span style="color: #0033b3;">com.makeramen.roundedimageview.RoundedImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imagePoster"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="150dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="220dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/imageBackdrop"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/imageBackdrop"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:riv_corner_radius</span><span style="color: #067d17;">="6dp" </span>/><br /><br /> <<span style="color: #0033b3;">View<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_view_gradient"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@id/imagePoster"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/imagePoster"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/imagePoster"</span>/><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutMainDetails"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toEndOf</span><span style="color: #067d17;">="@id/imagePoster"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/imageBackdrop"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textTitle"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="30sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textRuntime"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textReleaseDate"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutOtherDetails"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/layoutMainDetails"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textOverview"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="18sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textlanguage"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textStatus"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textBudgetOrSeasons"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textRevenueOrEpisodes"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textPopularity"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textTagline"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textVoteCount"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textVoteAverage"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textHomepage"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="2dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:autoLink</span><span style="color: #067d17;">="web"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:includeFontPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /> </<span style="color: #0033b3;">androidx.core.widget.NestedScrollView</span>><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingDetails"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="70dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="70dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageBackDetails"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_back"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_oval"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><br /><b>DetailsActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 500px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ProgressBar</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.core.text.HtmlCompat</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.core.widget.NestedScrollView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.makeramen.roundedimageview.RoundedImageView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.text.NumberFormat</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.Arrays</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.Locale</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Call</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Callback</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Response</span>;<br /><br /><span style="color: #8c8c8c; font-style: italic;">/**<br /></span><span style="color: #8c8c8c; font-style: italic;"> *Created by : Daltray Nababan<br /></span><span style="color: #8c8c8c; font-style: italic;"> * site : https://www.gwnbs.com<br /></span><span style="color: #8c8c8c; font-style: italic;"> * email : birwinecheese@gmail.com<br /></span><span style="color: #8c8c8c; font-style: italic;"> */<br /></span><span style="color: #8c8c8c; font-style: italic;"><br /></span><span style="color: #0033b3;">public class </span><span style="color: black;">DetailsActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">RoundedImageView </span><span style="color: #871094;">imageBackdrop</span>, <span style="color: #871094;">imagePoster</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ImageView </span><span style="color: #871094;">imagePosterBack</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">textTitle</span>, <span style="color: #871094;">textRuntime</span>, <span style="color: #871094;">textReleaseDate</span>, <span style="color: #871094;">textOverview</span>, <span style="color: #871094;">textlanguage</span>, <span style="color: #871094;">textStatus</span>, <span style="color: #871094;">textBudgetOrSeasons</span>, <span style="color: #871094;">textRevenueOrEpisodes</span>,<br /> <span style="color: #871094;">textPopularity</span>, <span style="color: #871094;">textTagline</span>, <span style="color: #871094;">textVoteCount</span>, <span style="color: #871094;">textVoteAverage</span>, <span style="color: #871094;">textHomepage</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ProgressBar </span><span style="color: #871094;">loadingDetails</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">NestedScrollView </span><span style="color: #871094;">scrollView</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">id</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ApiService </span><span style="color: #871094;">apiService</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_details</span>);<br /><br /> <span style="color: #871094;">imagePosterBack </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imagePosterBack</span>);<br /> <span style="color: #871094;">imageBackdrop </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageBackdrop</span>);<br /> <span style="color: #871094;">imagePoster </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imagePoster</span>);<br /> <span style="color: #871094;">textTitle </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textTitle</span>);<br /> <span style="color: #871094;">textRuntime </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textRuntime</span>);<br /> <span style="color: #871094;">textReleaseDate </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textReleaseDate</span>);<br /> <span style="color: #871094;">textOverview </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textOverview</span>);<br /> <span style="color: #871094;">textlanguage </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textlanguage</span>);<br /> <span style="color: #871094;">textStatus </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textStatus</span>);<br /> <span style="color: #871094;">textBudgetOrSeasons </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textBudgetOrSeasons</span>);<br /> <span style="color: #871094;">textRevenueOrEpisodes </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textRevenueOrEpisodes</span>);<br /> <span style="color: #871094;">textPopularity </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textPopularity</span>);<br /> <span style="color: #871094;">textTagline </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textTagline</span>);<br /> <span style="color: #871094;">textVoteCount </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textVoteCount</span>);<br /> <span style="color: #871094;">textVoteAverage </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textVoteAverage</span>);<br /> <span style="color: #871094;">textHomepage </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textHomepage</span>);<br /> <span style="color: #871094;">loadingDetails </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingDetails</span>);<br /> <span style="color: #871094;">scrollView </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">scrollView</span>);<br /><br /> <span style="color: #871094;">apiService </span>= <span style="color: black;">ApiClient</span>.<span style="font-style: italic;">getClient</span>().create(<span style="color: black;">ApiService</span>.<span style="color: #0033b3;">class</span>);<br /><br /> <span style="color: #871094;">id </span>= getIntent().getIntExtra(<span style="color: #067d17;">"id"</span>, <span style="color: #1750eb;">0</span>);<br /> <span style="color: black;">String tipe </span>= getIntent().getStringExtra(<span style="color: #067d17;">"tipe"</span>);<br /> <span style="color: #0033b3;">switch </span>(<span style="color: black;">tipe</span>) {<br /> <span style="color: #0033b3;">case </span><span style="color: #067d17;">"movie"</span>:<br /> setMovieDetails();<br /> <span style="color: #0033b3;">break</span>;<br /> <span style="color: #0033b3;">case </span><span style="color: #067d17;">"tv"</span>:<br /> setTVDetails();<br /> <span style="color: #0033b3;">break</span>;<br /> }<br /><br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageBackDetails</span>).setOnClickListener(v -> onBackPressed());<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setMovieDetails</span>() {<br /> <span style="color: #871094;">loadingDetails</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieDetails</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getMovieDetails(<span style="color: black;">String</span>.<span style="font-style: italic;">valueOf</span>(<span style="color: #871094;">id</span>), <span style="color: black;">MainActivity</span>.<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">MovieDetails</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieDetails</span>> call, <span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">MovieDetails</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingDetails</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">scrollView</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #871094;">imagePosterBack</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imagePosterBack</span>, response.body().getPosterPath());<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setBackdropURL</span>(<span style="color: #871094;">imageBackdrop</span>, response.body().getBackdropPath());<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imagePoster</span>, response.body().getPosterPath());<br /> <span style="color: #871094;">textTitle</span>.setText(response.body().getTitle());<br /> setHTMLText(<span style="color: #871094;">textRuntime</span>, <span style="color: #067d17;">"Runtime"</span>, response.body().getRuntime() + <span style="color: #067d17;">" minutes"</span>);<br /> setHTMLText(<span style="color: #871094;">textReleaseDate</span>, <span style="color: #067d17;">"Released on"</span>, response.body().getReleaseDate());<br /> <span style="color: #871094;">textOverview</span>.setText(response.body().getOverview());<br /> setHTMLText(<span style="color: #871094;">textlanguage</span>, <span style="color: #067d17;">"Language"</span>, response.body().getLanguage());<br /> setHTMLText(<span style="color: #871094;">textStatus</span>, <span style="color: #067d17;">"Status"</span>, response.body().getStatus());<br /><br /> <span style="color: black;">Double budget </span>= <span style="color: black;">Double</span>.<span style="font-style: italic;">parseDouble</span>(response.body().getBudget());<br /> <span style="color: black;">String budgetFormatted </span>= <span style="color: black;">NumberFormat</span>.<span style="font-style: italic;">getCurrencyInstance</span>(<span style="color: black;">Locale</span>.<span style="color: #871094; font-style: italic;">US</span>).format(<span style="color: black;">budget</span>);<br /> setHTMLText(<span style="color: #871094;">textBudgetOrSeasons</span>, <span style="color: #067d17;">"Budget"</span>, <span style="color: black;">budgetFormatted</span>);<br /><br /> <span style="color: black;">Double revenue </span>= <span style="color: black;">Double</span>.<span style="font-style: italic;">parseDouble</span>(response.body().getRevenue());<br /> <span style="color: black;">String revenueFormatted </span>= <span style="color: black;">NumberFormat</span>.<span style="font-style: italic;">getCurrencyInstance</span>(<span style="color: black;">Locale</span>.<span style="color: #871094; font-style: italic;">US</span>).format(<span style="color: black;">revenue</span>);<br /> setHTMLText(<span style="color: #871094;">textRevenueOrEpisodes</span>, <span style="color: #067d17;">"Revenue"</span>, <span style="color: black;">revenueFormatted</span>);<br /><br /> setHTMLText(<span style="color: #871094;">textPopularity</span>, <span style="color: #067d17;">"Popularity"</span>, response.body().getPopularity());<br /> setHTMLText(<span style="color: #871094;">textTagline</span>, <span style="color: #067d17;">"Tagline"</span>, response.body().getTagline());<br /> setHTMLText(<span style="color: #871094;">textVoteCount</span>, <span style="color: #067d17;">"Vote Count"</span>, response.body().getVoteAverage());<br /> setHTMLText(<span style="color: #871094;">textVoteAverage</span>, <span style="color: #067d17;">"Vote Average"</span>, response.body().getVoteCount());<br /> setHTMLText(<span style="color: #871094;">textHomepage</span>, <span style="color: #067d17;">"Homepage"</span>, response.body().getHomepage());<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieDetails</span>> call, <span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingDetails</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat details dari " </span>+ <span style="color: #871094;">id</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setTVDetails</span>() {<br /> <span style="color: #871094;">loadingDetails</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: black;">Call</span><<span style="color: black;">TVDetails</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.getTvDetails(<span style="color: black;">String</span>.<span style="font-style: italic;">valueOf</span>(<span style="color: #871094;">id</span>), <span style="color: black;">MainActivity</span>.<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">TVDetails</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVDetails</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">TVDetails</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">loadingDetails</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">scrollView</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #871094;">imagePosterBack</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imagePosterBack</span>, response.body().getPosterPath());<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setBackdropURL</span>(<span style="color: #871094;">imageBackdrop</span>, response.body().getBackdropPath());<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imagePoster</span>, response.body().getPosterPath());<br /> <span style="color: #871094;">textTitle</span>.setText(response.body().getName());<br /><br /> <span style="color: black;">String </span>runtime = <span style="color: black;">Arrays</span>.<span style="font-style: italic;">toString</span>(response.body().getEpisodeRuntime());<br /> runtime = runtime.replace(<span style="color: #067d17;">"["</span>, <span style="color: #067d17;">""</span>);<br /> runtime = runtime.replace(<span style="color: #067d17;">"]"</span>, <span style="color: #067d17;">""</span>);<br /> runtime = runtime.replace(<span style="color: #067d17;">","</span>, <span style="color: #067d17;">" &"</span>);<br /> setHTMLText(<span style="color: #871094;">textRuntime</span>, <span style="color: #067d17;">"Runtime"</span>, runtime + <span style="color: #067d17;">" min"</span>);<br /><br /> <span style="color: #871094;">textReleaseDate</span>.setText(<span style="color: black;">HtmlCompat</span>.<span style="font-style: italic;">fromHtml</span>(<span style="color: #067d17;">"<font color='#0B6A10'>First air date</font> : " </span>+<br /> response.body().getFirstAirDate() + <span style="color: #067d17;">"<br/><font color='#0B6A10'>Last air date</font> : " </span>+<br /> response.body().getLastAirdate() , <span style="color: black;">HtmlCompat</span>.<span style="color: #871094; font-style: italic;">FROM_HTML_MODE_LEGACY</span>));<br /><br /> <span style="color: #871094;">textOverview</span>.setText(response.body().getOverview());<br /> setHTMLText(<span style="color: #871094;">textlanguage</span>, <span style="color: #067d17;">"Language"</span>, response.body().getOriginalLanguage());<br /> setHTMLText(<span style="color: #871094;">textStatus</span>, <span style="color: #067d17;">"Status"</span>, response.body().getStatus());<br /> setHTMLText(<span style="color: #871094;">textBudgetOrSeasons</span>, <span style="color: #067d17;">"Seasons"</span>, response.body().getNumberOfSeasons());<br /> setHTMLText(<span style="color: #871094;">textRevenueOrEpisodes</span>, <span style="color: #067d17;">"Episodes"</span>, response.body().getNumberOfEpisodes());<br /> setHTMLText(<span style="color: #871094;">textPopularity</span>, <span style="color: #067d17;">"Popularity"</span>, response.body().getPopularity());<br /> setHTMLText(<span style="color: #871094;">textTagline</span>, <span style="color: #067d17;">"Tagline"</span>, response.body().getTagline());<br /> setHTMLText(<span style="color: #871094;">textVoteCount</span>, <span style="color: #067d17;">"Vote Count"</span>, response.body().getVoteAverage());<br /> setHTMLText(<span style="color: #871094;">textVoteAverage</span>, <span style="color: #067d17;">"Vote Average"</span>, response.body().getVoteCount());<br /> setHTMLText(<span style="color: #871094;">textHomepage</span>, <span style="color: #067d17;">"Homepage"</span>, response.body().getHomepage());<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVDetails</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingDetails</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Terjadi kesalahan saat memuat details dari " </span>+ <span style="color: #871094;">id</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setHTMLText</span>(<span style="color: black;">TextView </span>tv, <span style="color: black;">String </span>stringColored, <span style="color: black;">String </span>stringValue) {<br /> tv.setText(<span style="color: black;">HtmlCompat</span>.<span style="font-style: italic;">fromHtml</span>(<span style="color: #067d17;">"<font color='#0B6A10'>" </span>+ stringColored + <span style="color: #067d17;">"</font> : " </span>+ stringValue,<br /> <span style="color: black;">HtmlCompat</span>.<span style="color: #871094; font-style: italic;">FROM_HTML_MODE_LEGACY</span>));<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Implementasi terakhir yang akan kita aplikasikan adalah pencarian movie dan tv. Untuk query pencarian dan tipe kita terapkan dalam sebuah dialog menggunakan AlertDialog pada MainActivity.java sebelumnya, kemudian kita kirimkan menggunakan Intent extra ke activity pencarian.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik silahkan buat terlebih dahulu sebuah resource file untuk dialog dengan nama <b>dialog_search.xml</b> kemudian buat empty activty baru dengan nama class <b>SearchActivity.java</b> dan layoutnya <b>activity_search.xml</b>. Hasil pencarian (item-item) akan kita tampilkan menggunakan RecyclerView dengan layout manager StaggeredGridLayout 4 kolom. Berikut selengkapnya :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>dialog_search.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_text_title"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/search_movie_or_tv_show"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp" </span>/><br /><br /> <<span style="color: #0033b3;">RadioGroup<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/radioGroup"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"</span>><br /><br /> <<span style="color: #0033b3;">RadioButton<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/radioButtonMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/movies"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">RadioButton<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/radioButtonTv"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/tv_shows"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> </<span style="color: #0033b3;">RadioGroup</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="1dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">EditText<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/inputSearch"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@null"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:hint</span><span style="color: #067d17;">="@string/enter_keyword"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:imeOptions</span><span style="color: #067d17;">="actionGo"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:importantForAutofill</span><span style="color: #067d17;">="no"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:inputType</span><span style="color: #067d17;">="text"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageDoSearch"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_oval"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_search" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><br /><b>activity_search.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".SearchActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textSearchQuery"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="60dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/color_actionbar"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageBack"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_back"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_oval"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/textSearchQuery"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/textSearchQuery"</span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvSearch"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textSearchQuery"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingSearch"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="70dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="70dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textNoResults"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/no_results"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMoreSearch"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><br /><b>SearchActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 500px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ProgressBar</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.core.text.HtmlCompat</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.StaggeredGridLayoutManager</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Call</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Callback</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Response</span>;<br /><br /><span style="color: #8c8c8c; font-style: italic;">/**<br /></span><span style="color: #8c8c8c; font-style: italic;"> *Created by : Daltray Nababan<br /></span><span style="color: #8c8c8c; font-style: italic;"> * site : https://www.gwnbs.com<br /></span><span style="color: #8c8c8c; font-style: italic;"> * email : birwinecheese@gmail.com<br /></span><span style="color: #8c8c8c; font-style: italic;"> */<br /></span><span style="color: #8c8c8c; font-style: italic;"><br /></span><span style="color: #0033b3;">public class </span><span style="color: black;">SearchActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">movieResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #871094;">TVResults </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: #0033b3;">private </span><span style="color: black;">String </span><span style="color: #871094;">query</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">RecyclerView </span><span style="color: #871094;">rvSearch</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">MovieSearchAdapter </span><span style="color: #871094;">movieAdapter</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">TVSearchAdapter </span><span style="color: #871094;">tvAdapter</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ApiService </span><span style="color: #871094;">apiService</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ProgressBar </span><span style="color: #871094;">loadingSearch</span>, <span style="color: #871094;">loadingMoreSearch</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">textNoResults</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">currentPage </span>= <span style="color: #1750eb;">1</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">totalPages </span>= <span style="color: #1750eb;">1</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_search</span>);<br /><br /> <span style="color: #871094;">loadingSearch </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingSearch</span>);<br /> <span style="color: #871094;">loadingMoreSearch </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">loadingMoreSearch</span>);<br /> <span style="color: #871094;">textNoResults </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textNoResults</span>);<br /> <span style="color: black;">TextView textSearchQuery </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textSearchQuery</span>);<br /> <span style="color: #871094;">rvSearch </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvSearch</span>);<br /> <span style="color: #871094;">rvSearch</span>.setLayoutManager(<span style="color: #0033b3;">new </span>StaggeredGridLayoutManager(<span style="color: #1750eb;">4</span>, <span style="color: black;">RecyclerView</span>.<span style="color: #871094; font-style: italic;">VERTICAL</span>));<br /> <span style="color: #871094;">movieAdapter </span>= <span style="color: #0033b3;">new </span>MovieSearchAdapter(<span style="color: #871094;">movieResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">tvAdapter </span>= <span style="color: #0033b3;">new </span>TVSearchAdapter(<span style="color: #871094;">TVResults</span>, <span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">loadingSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /><br /> <span style="color: #871094;">apiService </span>= <span style="color: black;">ApiClient</span>.<span style="font-style: italic;">getClient</span>().create(<span style="color: black;">ApiService</span>.<span style="color: #0033b3;">class</span>);<br /><br /> <span style="color: #871094;">query </span>= getIntent().getStringExtra(<span style="color: #067d17;">"searchFor"</span>);<br /> <span style="color: black;">textSearchQuery</span>.setText(<span style="color: black;">HtmlCompat</span>.<span style="font-style: italic;">fromHtml</span>(<span style="color: #067d17;">"You searched for : <b>" </span>+ <span style="color: #871094;">query </span>+ <span style="color: #067d17;">"</b>"</span>,<br /> <span style="color: black;">HtmlCompat</span>.<span style="color: #871094; font-style: italic;">FROM_HTML_MODE_LEGACY</span>));<br /> <span style="color: black;">String tipe </span>= getIntent().getStringExtra(<span style="color: #067d17;">"tipe"</span>);<br /> <span style="color: #0033b3;">switch </span>(<span style="color: black;">tipe</span>) {<br /> <span style="color: #0033b3;">case </span><span style="color: #067d17;">"Movies" </span>:<br /> searchForMovies();<br /> <span style="color: #871094;">rvSearch</span>.setAdapter(<span style="color: #871094;">movieAdapter</span>);<br /> <span style="color: #0033b3;">break</span>;<br /> <span style="color: #0033b3;">case </span><span style="color: #067d17;">"TV Shows" </span>:<br /> searchForTV();<br /> <span style="color: #871094;">rvSearch</span>.setAdapter(<span style="color: #871094;">tvAdapter</span>);<br /> <span style="color: #0033b3;">break</span>;<br /> }<br /><br /> <span style="color: #871094;">rvSearch</span>.addOnScrollListener(<span style="color: #0033b3;">new </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">OnScrollListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onScrolled</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RecyclerView </span>recyclerView, <span style="color: #0033b3;">int </span>dx, <span style="color: #0033b3;">int </span>dy) {<br /> <span style="color: #0033b3;">super</span>.onScrolled(recyclerView, dx, dy);<br /> <span style="color: #0033b3;">if </span>(!<span style="color: #871094;">rvSearch</span>.canScrollVertically(<span style="color: #1750eb;">1</span>)) {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">currentPage </span><= <span style="color: #871094;">totalPages</span>) {<br /> <span style="color: #871094;">loadingMoreSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #871094;">currentPage </span>+= <span style="color: #1750eb;">1</span>;<br /> <span style="color: #0033b3;">if </span>(<span style="color: #851691;">tipe</span>.equals(<span style="color: #067d17;">"Movies"</span>))<br /> searchForMovies();<br /> <span style="color: #0033b3;">else </span>searchForTV();<br /> }<br /> }<br /> }<br /> });<br /><br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageBack</span>).setOnClickListener(v -> onBackPressed());<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">searchForMovies</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.searchMovie(<span style="color: black;">MainActivity</span>.<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: black;">MainActivity</span>.<span style="color: #871094; font-style: italic;">LANGUAGE</span>,<br /> <span style="color: #871094;">query</span>, <span style="color: #871094;">currentPage</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">MovieRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">MovieRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #0033b3;">if </span>(response.body().getResults().size() > <span style="color: #1750eb;">0</span>) {<br /> <span style="color: #871094;">loadingSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMoreSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">totalPages </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">movieResults</span>.size();<br /> <span style="color: #871094;">movieResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">movieAdapter</span>.notifyItemRangeInserted(<span style="color: black;">oldCount</span>, <span style="color: #871094;">movieResults</span>.size());<br /> checkSize(response.body().getResults().size());<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">loadingSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMoreSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> }<br /> }<br /> }<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMoreSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">textNoResults</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #871094;">textNoResults</span>.setText(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">something_wrong</span>);<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">searchForTV</span>() {<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: black;">call </span>= <span style="color: #871094;">apiService</span>.searchTv(<span style="color: black;">MainActivity</span>.<span style="color: #871094; font-style: italic;">MYAPI_KEY</span>, <span style="color: black;">MainActivity</span>.<span style="color: #871094; font-style: italic;">LANGUAGE</span>,<br /> <span style="color: #871094;">query</span>, <span style="color: #871094;">currentPage</span>);<br /> <span style="color: black;">call</span>.enqueue(<span style="color: #0033b3;">new </span><span style="color: black;">Callback</span><<span style="color: black;">TVRespon</span>>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onResponse</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Response</span><<span style="color: black;">TVRespon</span>> response) {<br /> <span style="color: #0033b3;">if </span>(response.body() != <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #0033b3;">if </span>(response.body().getResults().size() > <span style="color: #1750eb;">0</span>) {<br /> <span style="color: #871094;">loadingSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMoreSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">totalPages </span>= response.body().getTotalPages();<br /> <span style="color: #0033b3;">int </span><span style="color: black;">oldCount </span>= <span style="color: #871094;">TVResults</span>.size();<br /> <span style="color: #871094;">TVResults</span>.addAll(response.body().getResults());<br /> <span style="color: #871094;">tvAdapter</span>.notifyItemRangeInserted(<span style="color: black;">oldCount</span>, <span style="color: #871094;">TVResults</span>.size());<br /> checkSize(response.body().getResults().size());<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">loadingSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMoreSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> }<br /> }<br /> }<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFailure</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> call,<span style="color: #9e880d;">@NonNull </span><span style="color: black;">Throwable </span>t) {<br /> <span style="color: #871094;">loadingSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">loadingMoreSearch</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> <span style="color: #871094;">textNoResults</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #871094;">textNoResults</span>.setText(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">something_wrong</span>);<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">checkSize</span>(<span style="color: #0033b3;">int </span>resulst) {<br /> <span style="color: #0033b3;">if </span>(resulst == <span style="color: #1750eb;">0</span>) {<br /> <span style="color: #871094;">textNoResults</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">textNoResults</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> }<br /> }<br />}</pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Perhatikan untuk import class, pastikan pada semua java class project Anda sama dengan yang ada di tutorial ini, karena bila salah import class bisa terjadi error dan bisa membuat Anda kebingungan. Hal ini dapat terjadi bila Anda tidak melakukan copy paste code dari postingan ini alias mengetik manual. Bila Anda copas code sudah dipastikan importnya sama dengan tutorial ini.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Nah terakhir tambahkan izi akses internet ke AndroidManifest.xml, kalau tidak apalikasi berpotensi crash dengan warning "akses ditolak, butuh internet permission".</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>AndroidManifest.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">manifest </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">package</span><span style="color: #067d17;">="com.gwnbs.movietracker"</span>><br /><br /> <<span style="color: #0033b3;">uses-permission </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="android.permission.INTERNET" </span>/><br /><br /> <<span style="color: #0033b3;">application<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:allowBackup</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:icon</span><span style="color: #067d17;">="@mipmap/ic_launcher"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:label</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:roundIcon</span><span style="color: #067d17;">="@mipmap/ic_launcher_round"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:supportsRtl</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:theme</span><span style="color: #067d17;">="@style/Theme.MovieTracker"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:ignore</span><span style="color: #067d17;">="AllowBackup"</span>><br /> <<span style="color: #0033b3;">activity </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">=".SearchActivity"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:screenOrientation</span><span style="color: #067d17;">="portrait"</span>/><br /> <<span style="color: #0033b3;">activity </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">=".DetailsActivity"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:screenOrientation</span><span style="color: #067d17;">="portrait"</span>/><br /> <<span style="color: #0033b3;">activity </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">=".MainActivity"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:screenOrientation</span><span style="color: #067d17;">="portrait"</span>><br /> <<span style="color: #0033b3;">intent-filter</span>><br /> <<span style="color: #0033b3;">action </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="android.intent.action.MAIN" </span>/><br /> <<span style="color: #0033b3;">category </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="android.intent.category.LAUNCHER" </span>/><br /> </<span style="color: #0033b3;">intent-filter</span>><br /> </<span style="color: #0033b3;">activity</span>><br /> </<span style="color: #0033b3;">application</span>><br /><br /></<span style="color: #0033b3;">manifest</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Nah, kita sudah clear. Bila Anda ingin menambah fitur-fitur lain, misalnya seperti video trailer movie / tv, daftar actor / actress, details actor / actress, movie / tv credit dan masih banyak lagi data lainnya yang bisa Anda request, silahkan mengunjungi <a href="https://developers.themoviedb.org/3/getting-started/introduction" target="_blank"><b>Dokumentasi API TMDB</b></a>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Dan jangan lupa juga, bila Anda bersungguh-sungguh ingin mengembangkan aplikasi menggunakan API TMDB untuk dipublikasikan, silahkan membaca syarat dan ketentuan yang diberlakukan oleh TMDB, jangan sampai nantinya API Key dihapus karena terindikasi melanggar, tentunya aplikasi Anda tidak akan dapat di akses lagi alias crash setiap kali dibuka. S&K nya bisa dilihat <b><a href="https://www.themoviedb.org/documentation/api/terms-of-use" target="_blank">DISINI</a></b>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Berikut screenshoots dari source dan resource project aplikasi ini, pastikan seluruh files ada dan sama.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEia79o2KiJyrTEAw7g9VCmGpvJRE3Hc3tCvYVnox4ExI_9K-B73AdjkYwIle70E8zarTPm-bGKRSBs0LQCFK7M4gxCgA60k5qLH8oeOI_gHiDA26eO8v2JGLWFFbDgd5tfP-pa5YC2B5Ig/s1366/Screenshot+%252852%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Source files name of Movies & TV Shows App" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEia79o2KiJyrTEAw7g9VCmGpvJRE3Hc3tCvYVnox4ExI_9K-B73AdjkYwIle70E8zarTPm-bGKRSBs0LQCFK7M4gxCgA60k5qLH8oeOI_gHiDA26eO8v2JGLWFFbDgd5tfP-pa5YC2B5Ig/w400-h225/Screenshot+%252852%2529.png" title="Android Movies & TV Shows App" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #38761d;"><b>Gambar 1</b></span> : Nama source files</td></tr></tbody></table><br /><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixAVsInUWbkju0ULPB5k7ZjIbu8Hp1aPLJSmv6x5nVRC2ef41uVg1Ghk0UfM9hQ2Qw9hGclwg7YwFdNUr_vjmMY0SLUWdmxA6mLESVo3wZKetxN8oFe1nbdGsc10yfasVKS7j1y7Yij7U/s1366/Screenshot+%252853%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Resources files name of Movies & TV Shows App" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixAVsInUWbkju0ULPB5k7ZjIbu8Hp1aPLJSmv6x5nVRC2ef41uVg1Ghk0UfM9hQ2Qw9hGclwg7YwFdNUr_vjmMY0SLUWdmxA6mLESVo3wZKetxN8oFe1nbdGsc10yfasVKS7j1y7Yij7U/w400-h225/Screenshot+%252853%2529.png" title="Android Movies & TV Shows App" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b><span style="color: #38761d;">Gambar 2</span></b> : Nama resource files</td></tr></tbody></table><br /><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baiklah, bila Anda mengalami kesulitan bisa di sampaikan ke saya melalui komentar atau kontak email untuk fast respon melalui form email di bilah samping blog ini. Atau surfing google untuk yang lebih fast respon. Mungkin dari Anda sebagian ada yang merasa kalau saya kurang bersahabat bila dilihat dari gaya postingan, sebenarnya tidak! Saya suka membantu apalagi memiliki minat yang sama.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Sekian dan terimaksih untuk waktu dan kunjungannya.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-85225388033249983622021-02-21T16:06:00.003+07:002021-07-10T11:01:20.417+07:00Aplikasi Movie & TV Show Menggunakan API TMDB (Part 2: Implementasi)<p><span style="font-family: georgia;"></span></p><h3><span style="font-family: georgia;"><b>UPDATE !</b> </span>10 Juli 2021. Penambahan repositori Github, dapat dilihat di <a href="https://github.com/DaltrayNababan/MovieTracker" style="font-family: georgia;">https://github.com/DaltrayNababan/MovieTracker</a></h3><p><span style="font-family: georgia;"><br /></span></p><p><span style="font-family: georgia;">Tutorial ini adalah bagian ke-2 dari <b>Aplikasi Movie & TV Shows Menggunakan API TMDB</b>. Bagi Anda yang ingin mengikuti tutorial ini namun belum melihat tutorial bagian 1, silahkan untuk terlebih dahulu melihatnya, <a href="https://www.gwnbs.com/2021/02/aplikasi-movie-tv-show-menggunakan-api-themoviedb-part1.html" target=""><b>Klik Disini</b></a>. Tutorial tersebut mengenai langkah-langkah mendapatkan <b>API Key</b> TMDB.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;">Sekarang mari kita buat sebuah project baru dengan minimum SDK 5.0 (21) dan layout empty activity. Pada tutorial ini, aplikasi diberi nama <b>Movie Tracker</b> dengan nama package <b><i>com.gwnbs.movietracker</i></b>. </span></p><p><span style="font-family: georgia;">Aplikasi ini akan memiliki fitur yakni menampilkan <i>Most Popular Movies, Now Playing Movies, Upcoming Movies, Top Rated Movies, Most Popular TV Shows, Top Rated TV Shows, On The Air TV Shows, rincian dari movie / tv show, dan pencarian movie / tv show</i>. Berikut video singkat penampakan dari aplikasi.</span></p><p><span style="font-family: georgia;"><br /></span></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="276" src="https://www.youtube.com/embed/Ni0xlOLRmxU" width="523" youtube-src-id="Ni0xlOLRmxU"></iframe></div><p></p><p><span style="font-family: georgia;"><br /></span></p><p><span style="font-family: georgia;">Disini kita menggunakan Java 8 atau 1.8, sehingga pada build.gradle(app) kita perlu melakukan konfigurasi tambahan. Baik, setelah project Anda sudah dibuat, pertama-tama buka file build.gradle(app) untuk melakukan konfigurasi dan menambahkan beberapa library yang dibutuhkan seperti : <b>Retrofit</b>, <b>Picasso </b>dan <b>RoundedImageView</b>. Pastikan file gradle Anda seperti dibawah ini :</span></p><span style="font-family: georgia;"><b>build.gradle(:app)</b> :<br /></span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;">plugins <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>id <span style="color: #067d17;">'com.android.application'<br /></span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span>android <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>compileSdkVersion <span style="color: #1750eb;">30<br /></span><span style="color: #1750eb;"> </span>buildToolsVersion <span style="color: #067d17;">"29.0.3"<br /></span><span style="color: #067d17;"><br /></span><span style="color: #067d17;"> </span>defaultConfig <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>applicationId <span style="color: #067d17;">"com.gwnbs.movietracker"<br /></span><span style="color: #067d17;"> </span>minSdkVersion <span style="color: #1750eb;">21<br /></span><span style="color: #1750eb;"> </span>targetSdkVersion <span style="color: #1750eb;">30<br /></span><span style="color: #1750eb;"> </span>versionCode <span style="color: #1750eb;">1<br /></span><span style="color: #1750eb;"> </span>versionName <span style="color: #067d17;">"1.0"<br /></span><span style="color: #067d17;"><br /></span><span style="color: #067d17;"> </span>testInstrumentationRunner <span style="color: #067d17;">"androidx.test.runner.AndroidJUnitRunner"<br /></span><span style="color: #067d17;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;"> </span>buildTypes <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>release <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>minifyEnabled <span style="color: #0033b3;">false<br /></span><span style="color: #0033b3;"> </span>proguardFiles getDefaultProguardFile(<span style="color: #067d17;">'proguard-android-optimize.txt'</span>), <span style="color: #067d17;">'proguard-rules.pro'<br /></span><span style="color: #067d17;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"> }<br /></span><span style="font-weight: bold;"> </span>compileOptions <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>sourceCompatibility <span style="color: black;">JavaVersion</span>.<span style="color: #871094; font-style: italic;">VERSION_1_8<br /></span><span style="color: #871094; font-style: italic;"> </span>targetCompatibility <span style="color: black;">JavaVersion</span>.<span style="color: #871094; font-style: italic;">VERSION_1_8<br /></span><span style="color: #871094; font-style: italic;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span>dependencies <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;"> </span><span style="color: #8c8c8c; font-style: italic;">//Library bawaan<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>implementation <span style="color: #067d17;">'androidx.appcompat:appcompat:1.2.0'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'com.google.android.material:material:1.3.0'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'androidx.constraintlayout:constraintlayout:2.0.4'<br /></span><span style="color: #067d17;"> </span>testImplementation <span style="color: #067d17;">'junit:junit:4.13.2'<br /></span><span style="color: #067d17;"> </span>androidTestImplementation <span style="color: #067d17;">'androidx.test.ext:junit:1.1.2'<br /></span><span style="color: #067d17;"> </span>androidTestImplementation <span style="color: #067d17;">'androidx.test.espresso:espresso-core:3.3.0'<br /></span><span style="color: #067d17;"><br /></span><span style="color: #067d17;"> </span><span style="color: #8c8c8c; font-style: italic;">//Library tambahan yang dibutuhkan<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>implementation <span style="color: #067d17;">'com.squareup.retrofit2:retrofit:2.9.0'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'com.squareup.retrofit2:converter-gson:2.9.0'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'com.squareup.picasso:picasso:2.71828'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'io.reactivex.rxjava2:rxandroid:2.0.1'<br /></span><span style="color: #067d17;"> </span>implementation <span style="color: #067d17;">'com.makeramen:roundedimageview:2.3.0'<br /></span><span style="font-weight: bold;">}</span></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Setelah itu lakukan sync gradle untuk mulai mengunduh library. Berikutnya resouce value yaitu <b>strings.xml</b>, <b>colors.xml</b> dan <b>themes.xml</b>. Saya harap Anda sudah menggunakan <b>Android Studio</b> versi terbaru saat ini yaitu 4.1.2 agar terhindar dari kendala-kendala, karena cukup banyak perubahan pada versi terbaru ini.</span><p></p><div><span style="font-family: georgia;">Untuk versi lama, resource value themes.xml adalah styles.xml. Pada Android Studio terbaru saat Anda membuat sebuah project baru, themes.xml ada 2 tipe yaitu night dan light. Silahkan hapus saja yang tipe night karena disini kita tidak menggunakannya. Berikut ketiga resource value tersebut.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>colors.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">resources</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="purple_200"</span>>#FFBB86FC</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="purple_500"</span>>#FF6200EE</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="purple_700"</span>>#FF3700B3</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="teal_200"</span>>#FF03DAC5</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="teal_700"</span>>#FF018786</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="black"</span>>#FF000000</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="white"</span>>#FFFFFFFF</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="color_actionbar"</span>>#E6E4E4</<span style="color: #0033b3;">color</span>><br /></<span style="color: #0033b3;">resources</span>></pre></div><br /><b>strings.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">resources</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="app_name"</span>>Movie Tracker | gwnbs.com</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="movies"</span>>Movies</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="most_popular_movies"</span>>Most Popular Movies</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="now_playing_movies"</span>>Now Playing Movies</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="upcoming_movies"</span>>Upcoming Movies</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="top_rated_movies"</span>>Top Rated Movies</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="tv_shows"</span>>TV Shows</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="most_popular_tv_shows"</span>>Most Popular TV Shows</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="top_rated_tv_shows"</span>>Top Rated TV Shows</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="on_the_air_tv_shows"</span>>On The Air TV Shows</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="text_info"</span>>Aplikasi ini sepenuhnya menggunakan API dari TMDB (www.themoviedb.org), namun tidak didukung oleh TMDB.</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="enter_keyword"</span>>Enter query</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="no_results"</span>>No results</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="something_wrong"</span>>Something wrong with connection</<span style="color: #0033b3;">string</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="search_movie_or_tv_show"</span>>Search Movie or TV Show</<span style="color: #0033b3;">string</span>><br /></<span style="color: #0033b3;">resources</span>></pre></div><br /><b>themes.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">resources </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Base application theme. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">style </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="Theme.MovieTracker" </span><span style="color: #174ad4;">parent</span><span style="color: #067d17;">="Theme.MaterialComponents.Light.NoActionBar"</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Primary brand color. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorPrimary"</span>>#454545</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorPrimaryVariant"</span>>#4A4141</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorOnPrimary"</span>>@color/white</<span style="color: #0033b3;">item</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Secondary brand color. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorSecondary"</span>>@color/teal_200</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorSecondaryVariant"</span>>@color/teal_700</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorOnSecondary"</span>>@color/black</<span style="color: #0033b3;">item</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Status bar color. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="android:statusBarColor" </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:targetApi</span><span style="color: #067d17;">="l"</span>>?attr/colorPrimaryVariant</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="android:fontFamily"</span>>@font/alegreya_medium</<span style="color: #0033b3;">item</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Customize your theme here. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span></<span style="color: #0033b3;">style</span>><br /></<span style="color: #0033b3;">resources</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Pada <b>themes.xml</b> diatas, untuk item <i>fontFamily</i>, saya mengunduhnya dari fonts.google.com. Silahkan untuk memilih font Anda sendiri, atau Anda bisa menghapus item fontFamily pada themes.xml diatas jika Anda belum mau terlalu ribet.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Berikutnya icon drawable dan background drawable. Untuk background drawable kita memilik 5 files xml dan vector drawable kita memilik 3 icon xml. Berikut file nya.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>bg_actionbar.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/color_actionbar"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><b>bg_item.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">ripple </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/purple_700"</span>><br /><br /> <<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">shape </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#ECEAEA"</span>/><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="10dp"</span>/><br /> </<span style="color: #0033b3;">shape</span>><br /> </<span style="color: #0033b3;">item</span>><br /><br /></<span style="color: #0033b3;">ripple</span>></pre></div><br /><b>bg_oval.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">ripple </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/purple_500"</span>><br /><br /> <<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">shape </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="oval"</span>><br /> <<span style="color: #0033b3;">stroke<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="1dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/black" </span>/><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/white" </span>/><br /> </<span style="color: #0033b3;">shape</span>><br /> </<span style="color: #0033b3;">item</span>><br /><br /></<span style="color: #0033b3;">ripple</span>></pre></div><br /><b>bg_text_title.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#D3D0D0"</span>/><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="15dp"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><b>bg_view_gradient.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">gradient </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:endColor</span><span style="color: #067d17;">="#FFFFFF"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:startColor</span><span style="color: #067d17;">="@android:color/transparent"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><b>ic_android.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="#1DA238"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M17.6,9.48l1.84,-3.18c0.16,-0.31 0.04,-0.69 -0.26,-0.85c-0.29,-0.15 -0.65,-0.06 -0.83,0.22l-1.88,3.24c-2.86,-1.21 -6.08,-1.21 -8.94,0L5.65,5.67c-0.19,-0.29 -0.58,-0.38 -0.87,-0.2C4.5,5.65 4.41,6.01 4.56,6.3L6.4,9.48C3.3,11.25 1.28,14.44 1,18h22C22.72,14.44 20.7,11.25 17.6,9.48zM7,15.25c-0.69,0 -1.25,-0.56 -1.25,-1.25c0,-0.69 0.56,-1.25 1.25,-1.25S8.25,13.31 8.25,14C8.25,14.69 7.69,15.25 7,15.25zM17,15.25c-0.69,0 -1.25,-0.56 -1.25,-1.25c0,-0.69 0.56,-1.25 1.25,-1.25s1.25,0.56 1.25,1.25C18.25,14.69 17.69,15.25 17,15.25z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><br /><b>ic_back.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="24dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="24dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:autoMirrored</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="#393939"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><br /><b>ic_search.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="#393939"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Tahap selanjutnya, buatlah 2 buah files java, sebuah java class dan sebuah java interface yang digunakan untuk mengirim permintaan <b>API </b>dari TMDB. Beri nama <b>ApiClient.java</b> dan <b>ApiService.java</b>. Berikut isinya.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>ApiClient.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Retrofit</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.converter.gson.GsonConverterFactory</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">ApiClient </span>{<br /><br /> <span style="color: #0033b3;">private static </span><span style="color: black;">Retrofit </span><span style="color: #871094; font-style: italic;">retrofit</span>;<br /><br /> <span style="color: #0033b3;">public static </span><span style="color: black;">Retrofit </span><span style="color: #00627a;">getClient</span>() {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094; font-style: italic;">retrofit </span>== <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094; font-style: italic;">retrofit </span>= <span style="color: #0033b3;">new </span><span style="color: black;">Retrofit</span>.Builder()<br /> .baseUrl(<span style="color: #067d17;">"https://api.themoviedb.org/3/"</span>)<br /> .addConverterFactory(<span style="color: black;">GsonConverterFactory</span>.<span style="font-style: italic;">create</span>())<br /> .build();<br /> }<br /> <span style="color: #0033b3;">return </span><span style="color: #871094; font-style: italic;">retrofit</span>;<br /> }<br /><br />}<br /></pre></div><br /><b>ApiService.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.Call</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.http.</span><span style="color: #9e880d;">GET</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.http.</span><span style="color: #9e880d;">Path</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">retrofit2.http.</span><span style="color: #9e880d;">Query</span>;<br /><br /><span style="color: #0033b3;">public interface </span><span style="color: black;">ApiService </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan Most Popular Movies<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"movie/popular"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: #00627a;">getPopularMovies</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /> <br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan Now Playing Movies<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"movie/now_playing"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: #00627a;">getNowPlayingMovies</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan Upcoming Movies<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"movie/upcoming"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: #00627a;">getUpcomingMovies</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan Top Rated Movies<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"movie/top_rated"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: #00627a;">getTopRatedMovies</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk pencarian Movie<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"search/movie"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieRespon</span>> <span style="color: #00627a;">searchMovie</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"query"</span>) <span style="color: black;">String </span>query, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan rincian / details movie<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"movie/{movie_id}"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">MovieDetails</span>> <span style="color: #00627a;">getMovieDetails</span>(<span style="color: #9e880d;">@Path</span>(<span style="color: #067d17;">"movie_id"</span>) <span style="color: black;">String </span>id, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan Most Popular TV Shows<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"tv/popular"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: #00627a;">getTvPopular</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan Top Rated TV Shows<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"tv/top_rated"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: #00627a;">getTvTopRated</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan On Air TV Shows<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"tv/on_the_air"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: #00627a;">getTvOnAir</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk menampilkan rincian TV Show<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"tv/{tv_id}"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">TVDetails</span>> <span style="color: #00627a;">getTvDetails</span>(<span style="color: #9e880d;">@Path</span>(<span style="color: #067d17;">"tv_id"</span>) <span style="color: black;">String </span>id, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Permintaan untuk pencarian TV Shows<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #9e880d;">@GET</span>(<span style="color: #067d17;">"search/tv"</span>)<br /> <span style="color: black;">Call</span><<span style="color: black;">TVRespon</span>> <span style="color: #00627a;">searchTv</span>(<span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"api_key"</span>) <span style="color: black;">String </span>apiKey, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"language"</span>) <span style="color: black;">String </span>language,<br /> <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"query"</span>) <span style="color: black;">String </span>query, <span style="color: #9e880d;">@Query</span>(<span style="color: #067d17;">"page"</span>) <span style="color: #0033b3;">int </span>page);<br /><br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Metode call-call diatas akan ditandai dengan error karena kita belum membuat class-class nya. Berikutnya buatlah beberapa java class, pertama buatlah sebuah class dengan nama <b>ImageAdapter.java</b>. Class ini berisi 2 metode static, digunakan untuk memuat poster dan backdrop gambar dari database TMDB ke aplikasi.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>ImageAdapter.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.squareup.picasso.Callback</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.squareup.picasso.Picasso</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">ImageAdapter </span>{<br /><br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">IMAGEBASE_URL </span>= <span style="color: #067d17;">"https://image.tmdb.org/t/p/w220_and_h330_face"</span>;<br /> <span style="color: #0033b3;">public static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">BACKDROPIMAGE_BASE_URL </span>= <span style="color: #067d17;">"https://image.tmdb.org/t/p/original"</span>;<br /><br /> <span style="color: #0033b3;">public static void </span><span style="color: #00627a;">setPosterURL</span>(<span style="color: black;">ImageView </span>imageView, <span style="color: black;">String </span>path) {<br /> <span style="color: #0033b3;">try </span>{<br /> imageView.setAlpha(<span style="color: #1750eb;">0f</span>);<br /> <span style="color: black;">Picasso</span>.<span style="font-style: italic;">get</span>().load(<span style="color: #871094; font-style: italic;">IMAGEBASE_URL </span>+ path).noFade().into(imageView, <span style="color: #0033b3;">new </span><span style="color: black;">Callback</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onSuccess</span>() {<br /> <span style="color: #851691;">imageView</span>.animate().setDuration(<span style="color: #1750eb;">500</span>).alpha(<span style="color: #1750eb;">1f</span>).start();<br /> }<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onError</span>(<span style="color: black;">Exception </span>e) {<br /> }<br /> });<br /> } <span style="color: #0033b3;">catch </span>(<span style="color: black;">Exception </span>ignored) {<br /> }<br /> }<br /><br /> <span style="color: #0033b3;">public static void </span><span style="color: #00627a;">setBackdropURL</span>(<span style="color: black;">ImageView </span>imageView, <span style="color: black;">String </span>path) {<br /> <span style="color: #0033b3;">try </span>{<br /> imageView.setAlpha(<span style="color: #1750eb;">0f</span>);<br /> <span style="color: black;">Picasso</span>.<span style="font-style: italic;">get</span>().load(<span style="color: #871094; font-style: italic;">BACKDROPIMAGE_BASE_URL </span>+ path).noFade().into(imageView, <span style="color: #0033b3;">new </span><span style="color: black;">Callback</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onSuccess</span>() {<br /> <span style="color: #851691;">imageView</span>.animate().setDuration(<span style="color: #1750eb;">500</span>).alpha(<span style="color: #1750eb;">1f</span>).start();<br /> }<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onError</span>(<span style="color: black;">Exception </span>e) {<br /> }<br /> });<br /> } <span style="color: #0033b3;">catch </span>(<span style="color: black;">Exception </span>ignored) {<br /> }<br /> }<br />}<br /></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Nah, sekarang buatlah 6 buah java files dengan nama : <b>MovieRespon.java</b>, <b>MovieResult.java</b>, <b>MovieDetails.java</b>, <b>TVRespon.java</b>, <b>TVResult.java</b> dan <b>TVDetails.java</b>. Class-class ini digunakan untuk getter (mendapatkan) nilai-nilai yang kita butuhkan dari database TMDB. Karena kita memuat data dengan streaming maka kita membutuhkan anotasi <b>Serialize</b>.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MovieRespon.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.gson.annotations.</span><span style="color: #9e880d;">SerializedName</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MovieRespon </span>{<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"page"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">page</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"results"</span>)<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">results </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"total_results"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">totalResults</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"total_pages"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">totalPages</span>;<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getPage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">page</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #00627a;">getResults</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">results</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getTotalResults</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">totalResults</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getTotalPages</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">totalPages</span>;<br /> }<br /><br />}<br /></pre></div><br /><b>MovieResult.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.gson.annotations.</span><span style="color: #9e880d;">SerializedName</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MovieResult </span>{<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"id"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">id</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"poster_path"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">posterPath</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"title"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">title</span>;<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getId</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">id</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getPosterPath</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">posterPath</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getTitle</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">title</span>;<br /> }<br />}<br /></pre></div><br /><b>MovieDetails.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Keep</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.gson.annotations.</span><span style="color: #9e880d;">SerializedName</span>;<br /><br /><span style="color: #9e880d;">@Keep<br /></span><span style="color: #0033b3;">public class </span><span style="color: black;">MovieDetails </span>{<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"title"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">title</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"homepage"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">homepage</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"overview"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">overview</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"runtime"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">runtime</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"poster_path"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">posterPath</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"vote_average"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">voteAverage</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"release_date"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">releaseDate</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"original_language"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">language</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"backdrop_path"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">backdropPath</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"status"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">status</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"budget"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">budget</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"revenue"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">revenue</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"popularity"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">popularity</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"tagline"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">tagline</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"vote_count"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">voteCount</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getStatus</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">status</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getHomepage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">homepage</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getOverview</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">overview</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getRuntime</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">runtime</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getPosterPath</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">posterPath</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getVoteAverage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">voteAverage</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getBackdropPath</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">backdropPath</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getReleaseDate</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">releaseDate</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getBudget</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">budget</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getRevenue</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">revenue</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getPopularity</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">popularity</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getTagline</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">tagline</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getVoteCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">voteCount</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getLanguage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">language</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getTitle</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">title</span>;<br /> }<br />}<br /></pre></div><br /><b>TVRespon.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.gson.annotations.</span><span style="color: #9e880d;">SerializedName</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">TVRespon </span>{<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"page"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">page</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"results"</span>)<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #871094;">results </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"total_results"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">totalResults</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"total_pages"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">totalPages</span>;<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getPage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">page</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #00627a;">getResults</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">results</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getTotalResults</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">totalResults</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getTotalPages</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">totalPages</span>;<br /> }<br /><br />}<br /></pre></div><br /><b>TVResult.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.gson.annotations.</span><span style="color: #9e880d;">SerializedName</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">TVResult </span>{<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"id"</span>)<br /> <span style="color: #0033b3;">int </span><span style="color: #871094;">id</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"poster_path"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">posterPath</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"original_name"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">name</span>;<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getId</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">id</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getPosterPath</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">posterPath</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getName</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">name</span>;<br /> }<br />}<br /></pre></div><br /><b>TVDetails.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Keep</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.gson.annotations.</span><span style="color: #9e880d;">SerializedName</span>;<br /><br /><span style="color: #9e880d;">@Keep<br /></span><span style="color: #0033b3;">public class </span><span style="color: black;">TVDetails </span>{<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"name"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">name</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"poster_path"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">posterPath</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"backdrop_path"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">backdropPath</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"episode_run_time"</span>)<br /> <span style="color: #0033b3;">int</span>[] <span style="color: #871094;">episodeRuntime</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"first_air_date"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">firstAirDate</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"last_air_date"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">lastAirdate</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"number_of_episodes"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">numberOfEpisodes</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"number_of_seasons"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">numberOfSeasons</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"original_language"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">originalLanguage</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"overview"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">overview</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"popularity"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">popularity</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"status"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">status</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"tagline"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">tagline</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"vote_average"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">voteAverage</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"vote_count"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">voteCount</span>;<br /><br /> <span style="color: #9e880d;">@SerializedName</span>(<span style="color: #067d17;">"homepage"</span>)<br /> <span style="color: black;">String </span><span style="color: #871094;">homepage</span>;<br /><br /> <span style="color: #0033b3;">public int</span>[] <span style="color: #00627a;">getEpisodeRuntime</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">episodeRuntime</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getBackdropPath</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">backdropPath</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getFirstAirDate</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">firstAirDate</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getLastAirdate</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">lastAirdate</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getName</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">name</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getNumberOfEpisodes</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">numberOfEpisodes</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getNumberOfSeasons</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">numberOfSeasons</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getOriginalLanguage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">originalLanguage</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getOverview</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">overview</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getPopularity</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">popularity</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getPosterPath</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">posterPath</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getStatus</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">status</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getTagline</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">tagline</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getVoteAverage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">voteAverage</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getVoteCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">voteCount</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getHomepage</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">homepage</span>;<br /> }<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Tentu disini kita akan menggunakan <b>RecyclerView </b>untuk memuat item-item yang kita request dalam daftar. Disini kita akan membutuhkan 4 buah java class adapter yaitu untuk daftar movie, daftar tv show, pencarian movie dan pencarian tv show. Namun sebelumnya buatlah terlebih dahulu 2 buah layout resouce file sebagai container dari item. Beri nama <b>item_container.xml</b> dan <b>item_container_search.xml</b>. Berikut kedua files ini.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>item_container.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">LinearLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_item"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"</span>><br /><br /> <<span style="color: #0033b3;">com.makeramen.roundedimageview.RoundedImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageItemPoster"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="85dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="120dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scaleType</span><span style="color: #067d17;">="centerCrop"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:riv_corner_radius</span><span style="color: #067d17;">="10dp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textItemName"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="85dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:lines</span><span style="color: #067d17;">="2"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="10sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:ignore</span><span style="color: #067d17;">="SmallSp" </span>/><br /><br /></<span style="color: #0033b3;">LinearLayout</span>></pre></div><br /><b>item_container_search.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">LinearLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"</span>><br /><br /> <<span style="color: #0033b3;">com.makeramen.roundedimageview.RoundedImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageItemPoster"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="150dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_item"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scaleType</span><span style="color: #067d17;">="centerCrop"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:riv_corner_radius</span><span style="color: #067d17;">="10dp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textItemName"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.6"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:lines</span><span style="color: #067d17;">="2"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="10sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:ignore</span><span style="color: #067d17;">="SmallSp" </span>/><br /><br /></<span style="color: #0033b3;">LinearLayout</span>></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Sekarang 4 buah file java adapter. Beri nama <b>MovieAdapter.java</b>, <b>MovieSearchAdapter.java</b>, <b>TVAdapter.java</b> dan <b>TVSearchAdapter.java</b>. Ke-4 adapter ini adalah adapter RecyclerView karena memang disini kita hanya memakai RecyclerView. Untuk class list, RecyclerView adalah yang terbaik saat ini.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MovieAdapter.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Context</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.text.TextUtils</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.makeramen.roundedimageview.RoundedImageView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MovieAdapter </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">Adapter</span><<span style="color: black;">MovieAdapter</span>.<span style="color: black;">MovieViewHolder</span>> {<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">movieResults</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">Context </span><span style="color: #871094;">context</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">MovieAdapter</span>(<span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> movieResults, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">movieResults </span>= movieResults;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">context </span>= context;<br /> }<br /><br /> <span style="color: #9e880d;">@NonNull<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">MovieAdapter</span>.<span style="color: black;">MovieViewHolder </span><span style="color: #00627a;">onCreateViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ViewGroup </span>parent, <span style="color: #0033b3;">int </span>viewType) {<br /> <span style="color: #0033b3;">return new </span>MovieViewHolder(<span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(parent.getContext())<br /> .inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">item_container</span>, parent, <span style="color: #0033b3;">false</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBindViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">MovieAdapter</span>.<span style="color: black;">MovieViewHolder </span>holder, <span style="color: #0033b3;">int </span>position) {<br /> holder.bindItem(<span style="color: #871094;">movieResults</span>.get(position), <span style="color: #871094;">context</span>);<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public int </span><span style="color: #00627a;">getItemCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">movieResults</span>.size();<br /> }<br /><br /> <span style="color: #0033b3;">static class </span><span style="color: black;">MovieViewHolder </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">ViewHolder </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">RoundedImageView </span><span style="color: #871094;">imageItemPoster</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">TextView </span><span style="color: #871094;">textItemName</span>;<br /><br /> <span style="color: #00627a;">MovieViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">super</span>(itemView);<br /><br /> <span style="color: #871094;">imageItemPoster </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageItemPoster</span>);<br /> <span style="color: #871094;">textItemName </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textItemName</span>);<br /> }<br /><br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">bindItem</span>(<span style="color: black;">MovieResult </span>movieResult, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">if </span>(!<span style="color: black;">TextUtils</span>.<span style="font-style: italic;">isEmpty</span>(movieResult.getPosterPath())) {<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imageItemPoster</span>, movieResult.getPosterPath());<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">imageItemPoster</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_android</span>);<br /> }<br /> <span style="color: #871094;">textItemName</span>.setText(movieResult.getTitle());<br /><br /> <span style="color: #871094;">itemView</span>.setOnClickListener(v -> {<br /> <span style="color: black;">Intent i </span>= <span style="color: #0033b3;">new </span>Intent(<span style="color: #851691;">context</span>, <span style="color: black;">DetailsActivity</span>.<span style="color: #0033b3;">class</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"tipe"</span>, <span style="color: #067d17;">"movie"</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"id"</span>, <span style="color: #851691;">movieResult</span>.getId());<br /> <span style="color: #851691;">context</span>.startActivity(<span style="color: black;">i</span>);<br /> });<br /> }<br /> }<br />}<br /></pre></div><br /><b>MovieSearchAdapter.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Context</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.text.TextUtils</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.makeramen.roundedimageview.RoundedImageView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MovieSearchAdapter </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">Adapter</span><<span style="color: black;">MovieSearchAdapter</span>.<span style="color: black;">MovieViewHolder</span>> {<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> <span style="color: #871094;">movieResults</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">Context </span><span style="color: #871094;">context</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">MovieSearchAdapter</span>(<span style="color: black;">List</span><<span style="color: black;">MovieResult</span>> movieResults, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">movieResults </span>= movieResults;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">context </span>= context;<br /> }<br /><br /> <span style="color: #9e880d;">@NonNull<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">MovieSearchAdapter</span>.<span style="color: black;">MovieViewHolder </span><span style="color: #00627a;">onCreateViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ViewGroup </span>parent, <span style="color: #0033b3;">int </span>viewType) {<br /> <span style="color: #0033b3;">return new </span>MovieViewHolder(<span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(parent.getContext())<br /> .inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">item_container_search</span>, parent, <span style="color: #0033b3;">false</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBindViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">MovieSearchAdapter</span>.<span style="color: black;">MovieViewHolder </span>holder, <span style="color: #0033b3;">int </span>position) {<br /> holder.bindItem(<span style="color: #871094;">movieResults</span>.get(position), <span style="color: #871094;">context</span>);<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public int </span><span style="color: #00627a;">getItemCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">movieResults</span>.size();<br /> }<br /><br /> <span style="color: #0033b3;">static class </span><span style="color: black;">MovieViewHolder </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">ViewHolder </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">RoundedImageView </span><span style="color: #871094;">imageItemPoster</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">TextView </span><span style="color: #871094;">textItemName</span>;<br /><br /> <span style="color: #00627a;">MovieViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">super</span>(itemView);<br /><br /> <span style="color: #871094;">imageItemPoster </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageItemPoster</span>);<br /> <span style="color: #871094;">textItemName </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textItemName</span>);<br /> }<br /><br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">bindItem</span>(<span style="color: black;">MovieResult </span>movieResult, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">if </span>(!<span style="color: black;">TextUtils</span>.<span style="font-style: italic;">isEmpty</span>(movieResult.getPosterPath())) {<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imageItemPoster</span>, movieResult.getPosterPath());<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">imageItemPoster</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_android</span>);<br /> }<br /> <span style="color: #871094;">textItemName</span>.setText(movieResult.getTitle());<br /><br /> <span style="color: #871094;">itemView</span>.setOnClickListener(v -> {<br /> <span style="color: black;">Intent i </span>= <span style="color: #0033b3;">new </span>Intent(<span style="color: #851691;">context</span>, <span style="color: black;">DetailsActivity</span>.<span style="color: #0033b3;">class</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"tipe"</span>, <span style="color: #067d17;">"movie"</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"id"</span>, <span style="color: #851691;">movieResult</span>.getId());<br /> <span style="color: #851691;">context</span>.startActivity(<span style="color: black;">i</span>);<br /> });<br /> }<br /> }<br />}<br /></pre></div><br /><b>TVAdapter.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Context</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.text.TextUtils</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.makeramen.roundedimageview.RoundedImageView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">TVAdapter </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">Adapter</span><<span style="color: black;">TVAdapter</span>.<span style="color: black;">TvViewHolder</span>> {<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #871094;">TVResults</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">Context </span><span style="color: #871094;">context</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">TVAdapter</span>(<span style="color: black;">List</span><<span style="color: black;">TVResult</span>> TVResults, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">TVResults </span>= TVResults;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">context </span>= context;<br /> }<br /><br /> <span style="color: #9e880d;">@NonNull<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">TvViewHolder </span><span style="color: #00627a;">onCreateViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ViewGroup </span>parent, <span style="color: #0033b3;">int </span>viewType) {<br /> <span style="color: #0033b3;">return new </span>TvViewHolder(<span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(parent.getContext())<br /> .inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">item_container</span>, parent, <span style="color: #0033b3;">false</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBindViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">TvViewHolder </span>holder, <span style="color: #0033b3;">int </span>position) {<br /> holder.bindItem(<span style="color: #871094;">TVResults</span>.get(position), <span style="color: #871094;">context</span>);<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public int </span><span style="color: #00627a;">getItemCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">TVResults</span>.size();<br /> }<br /><br /> <span style="color: #0033b3;">static class </span><span style="color: black;">TvViewHolder </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">ViewHolder </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">RoundedImageView </span><span style="color: #871094;">imageItemPoster</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">TextView </span><span style="color: #871094;">textItemName</span>;<br /><br /> <span style="color: #00627a;">TvViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">super</span>(itemView);<br /><br /> <span style="color: #871094;">imageItemPoster </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageItemPoster</span>);<br /> <span style="color: #871094;">textItemName </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textItemName</span>);<br /> }<br /><br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">bindItem</span>(<span style="color: black;">TVResult </span>tvResult, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">if </span>(!<span style="color: black;">TextUtils</span>.<span style="font-style: italic;">isEmpty</span>(tvResult.getPosterPath())) {<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imageItemPoster</span>, tvResult.getPosterPath());<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">imageItemPoster</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_android</span>);<br /> }<br /> <span style="color: #871094;">textItemName</span>.setText(tvResult.getName());<br /><br /> <span style="color: #871094;">itemView</span>.setOnClickListener(v -> {<br /> <span style="color: black;">Intent i </span>= <span style="color: #0033b3;">new </span>Intent(<span style="color: #851691;">context</span>, <span style="color: black;">DetailsActivity</span>.<span style="color: #0033b3;">class</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"tipe"</span>, <span style="color: #067d17;">"tv"</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"id"</span>, <span style="color: #851691;">tvResult</span>.getId());<br /> <span style="color: #851691;">context</span>.startActivity(<span style="color: black;">i</span>);<br /> });<br /> }<br /> }<br />}<br /></pre></div><br /><b>TVSearchAdapter.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.movietracker</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Context</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.text.TextUtils</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.makeramen.roundedimageview.RoundedImageView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">TVSearchAdapter </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">Adapter</span><<span style="color: black;">TVSearchAdapter</span>.<span style="color: black;">TvViewHolder</span>> {<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">TVResult</span>> <span style="color: #871094;">TVResults</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">Context </span><span style="color: #871094;">context</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">TVSearchAdapter</span>(<span style="color: black;">List</span><<span style="color: black;">TVResult</span>> TVResults, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">TVResults </span>= TVResults;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">context </span>= context;<br /> }<br /><br /> <span style="color: #9e880d;">@NonNull<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">TvViewHolder </span><span style="color: #00627a;">onCreateViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ViewGroup </span>parent, <span style="color: #0033b3;">int </span>viewType) {<br /> <span style="color: #0033b3;">return new </span>TvViewHolder(<span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(parent.getContext())<br /> .inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">item_container_search</span>, parent, <span style="color: #0033b3;">false</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBindViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">TvViewHolder </span>holder, <span style="color: #0033b3;">int </span>position) {<br /> holder.bindItem(<span style="color: #871094;">TVResults</span>.get(position), <span style="color: #871094;">context</span>);<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public int </span><span style="color: #00627a;">getItemCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">TVResults</span>.size();<br /> }<br /><br /> <span style="color: #0033b3;">static class </span><span style="color: black;">TvViewHolder </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">ViewHolder </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">RoundedImageView </span><span style="color: #871094;">imageItemPoster</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">TextView </span><span style="color: #871094;">textItemName</span>;<br /><br /> <span style="color: #00627a;">TvViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">super</span>(itemView);<br /><br /> <span style="color: #871094;">imageItemPoster </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageItemPoster</span>);<br /> <span style="color: #871094;">textItemName </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textItemName</span>);<br /> }<br /><br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">bindItem</span>(<span style="color: black;">TVResult </span>tvResult, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">if </span>(!<span style="color: black;">TextUtils</span>.<span style="font-style: italic;">isEmpty</span>(tvResult.getPosterPath())) {<br /> <span style="color: black;">ImageAdapter</span>.<span style="font-style: italic;">setPosterURL</span>(<span style="color: #871094;">imageItemPoster</span>, tvResult.getPosterPath());<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #871094;">imageItemPoster</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_android</span>);<br /> }<br /> <span style="color: #871094;">textItemName</span>.setText(tvResult.getName());<br /><br /> <span style="color: #871094;">itemView</span>.setOnClickListener(v -> {<br /> <span style="color: black;">Intent i </span>= <span style="color: #0033b3;">new </span>Intent(<span style="color: #851691;">context</span>, <span style="color: black;">DetailsActivity</span>.<span style="color: #0033b3;">class</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"tipe"</span>, <span style="color: #067d17;">"tv"</span>);<br /> <span style="color: black;">i</span>.putExtra(<span style="color: #067d17;">"id"</span>, <span style="color: #851691;">tvResult</span>.getId());<br /> <span style="color: #851691;">context</span>.startActivity(<span style="color: black;">i</span>);<br /> });<br /> }<br /> }<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Anda akan mendapatkan error untuk class <i><b>DetailsActivity.java</b></i> karena activity nya belum kita buat, silahkan diabaikan saja. Selanjutnya kita akan implementasi di <b>MainActivity</b>, tentunya pertama-tama dengan mendesain layout <b>activity_main.xml</b> terlebih dahulu. Supaya view di activity_main.xml tidak terlalu banyak, xml untuk movie dan tv kita pisahkan dalam resource berbeda yang nantinya kita include ke activity_main.xml.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, buatlah terlebih dahulu 2 layout resource files, berikan nama <b>layout_movie.xml</b> dan <b>layout_tv.xml</b> dan berikut isinya :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>layout_movie.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 350px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">LinearLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_text_title"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/movies"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/most_popular_movies"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/rvMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:overScrollMode</span><span style="color: #067d17;">="never"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textMoviePopular" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvMoviePopular" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMainMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/rvMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/rvMoviePopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvMoviePopular" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/now_playing_movies"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/rvMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:overScrollMode</span><span style="color: #067d17;">="never"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textMovieNowPlaying" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvMovieNowPlaying" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMainMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/rvMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/rvMovieNowPlaying"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvMovieNowPlaying" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textUpcomingMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/upcoming_movies"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/rvUpcomingMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvUpcomingMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:overScrollMode</span><span style="color: #067d17;">="never"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textUpcomingMovie" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMovieUpcoming"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvUpcomingMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvUpcomingMovie" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMainMovieUpcoming"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvUpcomingMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/rvUpcomingMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/rvUpcomingMovie"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvUpcomingMovie" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/top_rated_movies"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/rvMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:overScrollMode</span><span style="color: #067d17;">="never"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textMovieTopRated" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvMovieTopRated" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMainMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/rvMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/rvMovieTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvMovieTopRated" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /></<span style="color: #0033b3;">LinearLayout</span>></pre></div><br /><b>layout_tv.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 350px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">LinearLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_text_title"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/tv_shows"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/most_popular_tv_shows"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/rvTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:overScrollMode</span><span style="color: #067d17;">="never"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textTvPopular" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvTvPopular" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMainTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/rvTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/rvTvPopular"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvTvPopular" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/top_rated_tv_shows"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/rvTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:overScrollMode</span><span style="color: #067d17;">="never"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingStart</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textTvTopRated" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvTvTopRated" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/loadingMainTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/rvTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/rvTvTopRated"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvTvTopRated" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /> <<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/on_the_air_tv_shows"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/rvTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:clipToPadding</span><span style="color: #067d17;">="false"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:overScrollMode</span><span style="color: #067d17;">="never"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:paddingStart</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:paddingEnd</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textTvOnAir" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">android:id</span><span style="color: #067d17;">="@+id/loadingTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:layout_width</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:layout_height</span><span style="color: #067d17;">="25dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvTvOnAir" </span>/><br /><br /> <<span style="color: #0033b3;">ProgressBar<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">android:id</span><span style="color: #067d17;">="@+id/loadingMainTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">android:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/rvTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/rvTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/rvTvOnAir"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">app:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/rvTvOnAir" </span>/><br /><br /> </<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>><br /><br /></<span style="color: #0033b3;">LinearLayout</span>></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="8611345975" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Nampaknya postingan ini terlalu panjang, sebaiknya kita lanjutkan pada postingan berbeda. Silahkan <b><a href="https://www.gwnbs.com/2021/02/aplikasi-movie-tv-show-menggunakan-api-themoviedb-part3.html">KLIK DISINI</a></b>.</span>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-28309132382040212432021-02-21T16:05:00.003+07:002021-07-10T11:02:36.652+07:00Aplikasi Movie & TV Show Menggunakan API TMDB (Part 1 : Registrasi TMDB)<p><span style="font-family: georgia;">Tutorial ini kita akan membuat sebuah aplikasi pemantau <b>Movie </b>(film) dan <b>TV Show</b> (acara TV). Disini kita akan menggunakan <b>API </b>(<i>Application Programming Interface</i>) dari website <b>TMDB </b>(themoviedb.org) yang gratis sepenuhnya. Untuk dapat menggunakan API TMDB, kita harus memiliki <b>API Key</b> TMDB dan untuk mendapatkan API Key kita harus menjadi anggota TMDB terlebih dahulu.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;">Pada bagian ini, akan dijelaskan langkah-langkah mendaftar dan mendapatkan API Key. Pertama silahkan kunjungi website https://www.themoviedb.org, kemudian ganti bahasa ke dalam bahasa Indonesia karena dalam tutorial registrasi ini kita menggunkan bahasa Indonesia. Setelahnya, klik "<b>Bergabung dengan TMDb</b>" yang berada di bagian pojok atas kanan.</span></p><p><span style="font-family: georgia;">Pada form pendaftaran silahkan masukkan nama pengguna tanpa spasi, kemudian buat password Anda lalu konfirmasi password, form terakhir alamat email Anda kemudian klik tombol daftar. Berikut contoh gambarnya :</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1qqPVuLwxhFo6s5Bu6Roqnd-6Zh9hnz70mIN00zdj7wMNRNQCFSGZ0TG5UbaKLLoYBtC25P-b9hq5h4EBP90S_MaWnpX70Jv0M0EJU1xbYSa_zFET_gqm1FqDXySneaCX1UPl487QSJ8/s1366/Screenshot+%252842%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Registrasi themoviedb.org" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1qqPVuLwxhFo6s5Bu6Roqnd-6Zh9hnz70mIN00zdj7wMNRNQCFSGZ0TG5UbaKLLoYBtC25P-b9hq5h4EBP90S_MaWnpX70Jv0M0EJU1xbYSa_zFET_gqm1FqDXySneaCX1UPl487QSJ8/w400-h225/Screenshot+%252842%2529.png" title="Langkah-langkah mendaftar ke themoviedb.org" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b><span style="color: #38761d;">Gambar 1</span></b> : Form pendaftaran member themoviedb.org</td></tr></tbody></table><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Setelah proses diatas, selanjutnya Anda akan dibawa ke halaman yang meminta untuk mengkonfirmasi alamat email. Silahkan buka alamat untuk mengkonfirmasi. Setelah konfirmasi sukses, secara otomatis Anda akan di arahkan ke halaman dashboard profil TMDB, jika tidak, silahkan login secara manual.</span></p><p><span style="font-family: georgia;">Selanjutnya langsung saja akses <b>pengaturan </b>dengan mengklik icon profil Anda yang terletak dibagian pojok kanan atas. Seperti gambar berikut :</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIPIq1f73dzyMuNdp3aUQ6nUluEx2RnXCnJelue3vTUMZXwzf7G4iXEtJNC2ZV4gbRJs13SAtVfO0yFJ6QBWdfYW32igwQe8UEDXgC0cEXBhaOs3y6MEk5XmPPPzKVFVfV2XtzpJHsogQ/s1366/Screenshot+%252844%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Akses pengaturan TMDB" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIPIq1f73dzyMuNdp3aUQ6nUluEx2RnXCnJelue3vTUMZXwzf7G4iXEtJNC2ZV4gbRJs13SAtVfO0yFJ6QBWdfYW32igwQe8UEDXgC0cEXBhaOs3y6MEk5XmPPPzKVFVfV2XtzpJHsogQ/w400-h225/Screenshot+%252844%2529.png" title="Langkah membuat API Key themoviedb.org" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b><span style="color: #38761d;">Gambar 2</span></b> : Mengakses pengaturan TMDB</td></tr></tbody></table><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Berikutnya pada pilihan pengaturan yang ada pilih </span><b style="font-family: georgia;">API</b><span style="font-family: georgia;">, kemudian </span><b style="font-family: georgia;">pada pemintaan kunci API</b><span style="font-family: georgia;"> klik tombol </span><i style="font-family: georgia;"><b>click here</b></i><span style="font-family: georgia;">.</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlFzJRgmrnf5AYNMmWqianRcqpb64Oxd07uJHcDe4_Fv2kRXenmN8nPethIT-3eaPuG-fWwd8s-kAKtpgXL0lTz-wk5PWVZU8ma_SOqx-l9vhI6kxZgwE2QEWc6jEmmGCr-0aapw-fYQk/s1366/Screenshot+%252846%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="API Key TMDB" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlFzJRgmrnf5AYNMmWqianRcqpb64Oxd07uJHcDe4_Fv2kRXenmN8nPethIT-3eaPuG-fWwd8s-kAKtpgXL0lTz-wk5PWVZU8ma_SOqx-l9vhI6kxZgwE2QEWc6jEmmGCr-0aapw-fYQk/w400-h225/Screenshot+%252846%2529_LI.jpg" title="Langkah-langkah membuat API Key TMDB" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b><span style="color: #38761d;">Gambar 3 </span></b>: Langkah awal dalam membuat API Key</td></tr></tbody></table><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Walaupun kita sudah menyetel bahasa website ke dalam Bahasa Indonesia, namun sebagian konten website masih ada yang berbahasa inggris. Nah, selanjutnya akan muncul pertanyaan "What type of API key do you wish to register?", pilihan jawaban hanya 2 yaitu Developer dan Professional. Silahkan pilih salah satu yang sesuai dengan Anda, jika individual pilih developer, jika untuk perusahaan silahkan pilih profesional.</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjgj__AfrPJzir64efMXj8pyEN9RrYL8CvO_6PXG_qs8RwO7yQ_5AvUwQ6lyIIVq4l0Tg1MlaAPXiUgO_WC2xpfeAD_e31teTf__1pCbBQ7SP31RHjpr2Ol6hW5G-pM3V4wlvhFPIJ8_w/s1366/Screenshot+%252847%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="API Key TMDB" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjgj__AfrPJzir64efMXj8pyEN9RrYL8CvO_6PXG_qs8RwO7yQ_5AvUwQ6lyIIVq4l0Tg1MlaAPXiUgO_WC2xpfeAD_e31teTf__1pCbBQ7SP31RHjpr2Ol6hW5G-pM3V4wlvhFPIJ8_w/w400-h225/Screenshot+%252847%2529.png" title="Generating TMDB Api Key" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b><span style="color: #38761d;">Gambar 4</span></b> : Tahapan dalam membuat API Key</td></tr></tbody></table><p></p><p><span style="font-family: georgia;"><br /></span></p><p><span style="font-family: georgia;">Setelah proses diatas, silahkan dibaca syarat dan ketentuan yang muncul dengan seksama, jika melanggar, API Key Anda nantinya berpotensi dihapus. Tahapan berikutnya adalah mengisi form keterangan mengenai aplikasi yakni jenis aplikasi, nama aplikasi, url aplikasi, deskripsi aplikasi. Kemudian informasi pribadi Anda seperti nama depan dan belakang, alamat, nomor telepon, kode pos dan sebagainya.</span></p><p><span style="font-family: georgia;">Jika Anda tidak memiliki alamat url untuk aplikasi, Anda dapat membuat telebih dahulu blog gratis dari blogger atau wordpress atau apa saja. Seluruh form yang ada harus diisi, setelah semua diisi klik <b><i>Kirim</i></b>. Berikut contoh gambarnya :</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9TCStZRaj8QqS9QEp6HSbKzRpZV4nZTtOix3z5i-n-jlZnNfq8UekmjotVBQz34izzogwMPhpIUWcKExihx5a4hykuCw0w8X9yxL1wElRk81RcE9_DVHT2GvQYPCOryhoH9WNdAYS8Tg/s1366/Screenshot+%252850%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="TMDB API Key" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9TCStZRaj8QqS9QEp6HSbKzRpZV4nZTtOix3z5i-n-jlZnNfq8UekmjotVBQz34izzogwMPhpIUWcKExihx5a4hykuCw0w8X9yxL1wElRk81RcE9_DVHT2GvQYPCOryhoH9WNdAYS8Tg/w400-h225/Screenshot+%252850%2529.png" title="Form registrasi untuk mendapatkan API key TMDB" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #38761d;"><b>Gambar 5</b></span> : Mengisi formulir informasi aplikasi & informasi pribadi</td></tr></tbody></table><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Jika proses diatas berhasil, secara otomatis Anda akan diarahkan kembali ke halaman pengaturan namun kini pada bagian API sudah terdapat API Key Anda yang berada pada kolom </span><b style="font-family: georgia;">API Key (v3 auth)</b><span style="font-family: georgia;">, dan rincian-rincian mengenainya. Contoh API key TMDB misalnya : </span><i style="font-family: georgia;">abd73maki872ndhy46absk0098a</i><span style="font-family: georgia;">.</span></p><p><span style="font-family: georgia;">Silahkan di copy API Key tersebut, simpan ditempat yang mudah Anda akses. Atau langsung saja deklarasikan sebuah final String untuk API Key tersebut pada file java project Anda. Berikut gambar yang memperlihatkan API Key nya :</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0NmQ1s2Aq3VfMbvzBaVLthfQc5HEKdr0VQ3Pk4206xyX9ayG0ELDYKLoWkXQP0AWdM2j0MSS5kw3qb1EiVr-F3NMhcvAm469lWLWVwT18qtv_K2-nhpVD0zC1uKgzPjVgrkExV6XUd58/s1366/Screenshot+%252851%2529_LI.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Cara mendapatkan TMDB API Key" border="0" data-original-height="768" data-original-width="1366" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0NmQ1s2Aq3VfMbvzBaVLthfQc5HEKdr0VQ3Pk4206xyX9ayG0ELDYKLoWkXQP0AWdM2j0MSS5kw3qb1EiVr-F3NMhcvAm469lWLWVwT18qtv_K2-nhpVD0zC1uKgzPjVgrkExV6XUd58/w400-h225/Screenshot+%252851%2529_LI.jpg" title="TMDB API Key" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="color: #38761d;"><b>Gambar 6</b></span> : Wujud API Key TMDB</td></tr></tbody></table><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">API Key sudah ada, sekarang mari kita buat aplikasinya. Jika Anda hendak mengikuti keseluruhan tutorial ini, ada baiknya Anda menamakan seluruh yang ada pada project Anda nantinya sama dengan tutorial ini, misalnya mulai dari nama project, nama class, metode dan sebagainya untuk menghindari error, Anda dapat menggantinya nanti setelah semua selesai. Silahkan klik link di bawah ini untuk menuju ke postingan bagian berikutnya :</span></p><p><span style="font-family: georgia;"><b><a href="https://www.gwnbs.com/2021/02/aplikasi-movie-tv-show-menggunakan-api-themoviedb-part2.html">Aplikasi Movie & TV Show Menggunakan API TMDB (Part 2 : Implementasi)</a></b></span></p><h4 style="text-align: left;"><span style="font-family: georgia;"><b>UPDATE !</b> </span>10 Juli 2021. Penambahan repositori Github, dapat dilihat di <a href="https://github.com/DaltrayNababan/MovieTracker" style="font-family: georgia;">https://github.com/DaltrayNababan/MovieTracker</a></h4>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-50714914761684965172021-02-19T02:34:00.001+07:002021-02-22T18:41:07.181+07:00Contoh Halaman Onboarding Menggunakan ViewPager2<p><span style="font-family: georgia;"><b>OnBoarding </b>biasanya digunakan developers sebagai halaman awal bagi pengguna yang baru saja menginstall aplikasi, biasanya dipergunakan untuk menyambut kedatangan pengguna baru dan memberikan sedikit informasi awal mengenai aplikasi. Itu biasanya, namun banyak juga yang tidak seperti biasanya.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;">Disini kita akan menggunakan <b>ViewPager2 </b>dengan adapter <b>RecycleView</b>. Baik tanpa perlu basa-basi mari kita segera mulai.</span></p><p><span style="font-family: georgia;">Tentunya dengan membuat sebuah project terlebih dahulu atau project yang sudah ada. Karena dalam contoh ini menggunakan kelas <b>RippleDrawable </b>sehingga minimum target SDK nya adalah 5.0 atau <i>Lolipop</i>. Kegunaan RippleDrawable disini adalah untuk memberikan efek warna pada sebuah view clicklistener.</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/IrXmAibZhdw" width="320" youtube-src-id="IrXmAibZhdw"></iframe></div><br /><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Kemudian, jika Anda masih menggunakan <b>Android Studio</b> versi lama (mungkin dibawah 4.1), silahkan cek terlebih dahulu <i>build.gradle(app)</i> dan pastikan sudah terdapat library <b>Google Material</b> di dalamnya. Untuk versi diatas 4.1 library tersebut sudah di input otomatis saat membuat project baru. Berikut library nya :<br /></span></p><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;">implementation <span style="color: #067d17;">'com.google.android.material:material:1.3.0'</span></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Dalam contoh ini, kita menggunakan cukup banyak file drawable baik vector drawable ataupun background drawable. Berikut file-file dari folder drawable, silahkan di salin dan tempel ke dalam folder tersebut jika Anda mengikuti tutorial ini.</span><p></p><h4 style="text-align: left;"><span style="font-family: georgia;"><b><span style="color: #38761d;">Background Drawable</span></b> :</span></h4><div><span style="font-family: georgia;"><b>bg_button_main.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">ripple </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#FFFFFF"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">shape </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="10dp" </span>/><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#C0DCC1" </span>/><br /> </<span style="color: #0033b3;">shape</span>><br /> </<span style="color: #0033b3;">item</span>><br /><br /></<span style="color: #0033b3;">ripple</span>></pre></div><br /><b>bg_button_next.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">ripple </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#0CDCC7"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">shape </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="oval"</span>><br /> <<span style="color: #0033b3;">stroke<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="1dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#068F8E" </span>/><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#FFFFFF"</span>/><br /> </<span style="color: #0033b3;">shape</span>><br /> </<span style="color: #0033b3;">item</span>><br /><br /></<span style="color: #0033b3;">ripple</span>></pre></div><br /><b>bg_timer.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="15dp" </span>/><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#FFFFFF" </span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><b>bg_view.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="oval"</span>><br /><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#5E5858"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><b>bg_viewpager.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#DFDCE3"</span>/><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:bottomLeftRadius</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:bottomRightRadius</span><span style="color: #067d17;">="40dp"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><div><br /><b>indicator_active.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#068786"</span>/><br /> <<span style="color: #0033b3;">size </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="15dp"</span>/><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="20dp"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div></div><br /><b>indicator_inactive.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="oval"</span>><br /><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="#FFFFFF"</span>/><br /> <<span style="color: #0033b3;">size </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="15dp"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div></span><h4 style="text-align: left;"><span style="font-family: georgia;"><b><span style="color: #38761d;">Vector Drawable</span></b> :</span></h4></div><div><span style="font-family: georgia;"><b>ic_go.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:autoMirrored</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="#5175FF"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M13.49,5.48c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM9.89,19.38l1,-4.4 2.1,2v6h2v-7.5l-2.1,-2 0.6,-3c1.3,1.5 3.3,2.5 5.5,2.5v-2c-1.9,0 -3.5,-1 -4.3,-2.4l-1,-1.6c-0.4,-0.6 -1,-1 -1.7,-1 -0.3,0 -0.5,0.1 -0.8,0.1l-5.2,2.2v4.7h2v-3.4l1.8,-0.7 -1.6,8.1 -4.9,-1 -0.4,2 7,1.4z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><br /><b>ic_highfive.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="#5175FF"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M23,5.5V20c0,2.2 -1.8,4 -4,4h-7.3c-1.08,0 -2.1,-0.43 -2.85,-1.19L1,14.83c0,0 1.26,-1.23 1.3,-1.25c0.22,-0.19 0.49,-0.29 0.79,-0.29c0.22,0 0.42,0.06 0.6,0.16C3.73,13.46 8,15.91 8,15.91V4c0,-0.83 0.67,-1.5 1.5,-1.5S11,3.17 11,4v7h1V1.5C12,0.67 12.67,0 13.5,0S15,0.67 15,1.5V11h1V2.5C16,1.67 16.67,1 17.5,1S19,1.67 19,2.5V11h1V5.5C20,4.67 20.67,4 21.5,4S23,4.67 23,5.5z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><b><div><span style="font-family: georgia;"><b><br /></b></span></div>ic_next.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="24dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="24dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:autoMirrored</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="?attr/colorControlNormal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M5.88,4.12L13.76,12l-7.88,7.88L8,22l10,-10L8,2z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><b><div><span style="font-family: georgia;"><b><br /></b></span></div>ic_phone.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">vector </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="#5175FF"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportWidth</span><span style="color: #067d17;">="24"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:viewportHeight</span><span style="color: #067d17;">="24"</span>><br /> <<span style="color: #0033b3;">path<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fillColor</span><span style="color: #067d17;">="@android:color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:pathData</span><span style="color: #067d17;">="M16,1L8,1C6.34,1 5,2.34 5,4v16c0,1.66 1.34,3 3,3h8c1.66,0 3,-1.34 3,-3L19,4c0,-1.66 -1.34,-3 -3,-3zM14,21h-4v-1h4v1zM17.25,18L6.75,18L6.75,4h10.5v14z" </span>/><br /></<span style="color: #0033b3;">vector</span>><br /></pre></div><div><br /></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="8611345975" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Secara default saat membuat project, kita akan memiliki <b>activity_main.xml</b> dengan java class <b>MainActivity.java</b>. Activity launcher dalam contoh ini masih MainActivity.java, disini kita hanya mendeteksi apakah pengguna baru atau bukan menggunakan <i>SharedPreferences</i>. Jika pengguna baru, maka pengguna akan diarahkan ke <b>BoardingActivity.java </b>(activity ini akan kita buat nanti).</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, selanjutnya kita buat dahulu layout resource file untuk item adapter RecyclerView. Disini kita berikan nama <b>item_onboard.xml</b> yang berupa ImageView dan 2 TextView, dan berikut isi filenya :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>item_onboard.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"</span>><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageOnBoarding"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:adjustViewBounds</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"</span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textTitleBoarding"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="15dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"</span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textDescription"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="18sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"</span>/><br /><br /></<span style="color: #0033b3;">LinearLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Selanjutnya kita buat model class untuk item-itemnya, seperti biasanya dalam menggunakan RecyclerView class ini dipergunakan untuk menciptakan nilai-nilai untuk view-view pada item. Model class kita buat dengan nama <b>BoardingItemModel.java</b>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>BoardingItemModel.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.onboarding</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">BoardingItemModel </span>{<br /><br /> <span style="color: #0033b3;">private final int </span><span style="color: #871094;">imageBoarding</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">String </span><span style="color: #871094;">title</span>, <span style="color: #871094;">description</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">BoardingItemModel</span>(<span style="color: #0033b3;">int </span>imageBoarding, <span style="color: black;">String </span>title, <span style="color: black;">String </span>description) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">imageBoarding </span>= imageBoarding;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">title </span>= title;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">description </span>= description;<br /> }<br /><br /> <span style="color: #0033b3;">public int </span><span style="color: #00627a;">getImageBoarding</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">imageBoarding</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getTitle</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">title</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getDescription</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">description</span>;<br /> }<br />}<br /></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Nah sekarang mari kita buat activity baru yakni dengan layout <b>activity_boarding.xml</b> dan java class <b>BoardingActivity.java</b>. Setelah dibuat, kita tinggalkan sejenak dan beralih ke launcher activity terlebih dahulu. Untuk activity_main.xml, dalam contoh ini hanya memiliki sebuah view TextView untuk mereset data nantinya. Berikut isinya :</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_main.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textResetData"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_button_main"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Resed data"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintLeft_toLeftOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintRight_toRightOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Pada <b>MainActivity.java</b>, kita cek terlebih dahulu apakah pengguna baru atau bukan menggunakan boolean yang disimpan ke SharedPreferences. Nilai default boolean kita tentukan ke true, kemudian kita buat java statement booleannya. Di dalam statement true, saat pengguna terdeteksi adalah pengguna baru, maka kita arahkan ke <b>BoardingActivity.class</b> lalu kita setel nilai boolean menjadi false supaya pada saat pengguna membuka kembali aplikasi tidak terdeteksi sebagai pengguna baru.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivty.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.onboarding</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.SharedPreferences</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Kunci string boolean pengguna baru/bukan<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private static final </span><span style="color: black;">String </span><span style="color: #871094; font-style: italic;">FIRST_USER </span>= <span style="color: #067d17;">"firstTime"</span>;<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">SharedPreferences </span><span style="color: #871094;">preferences</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #871094;">preferences </span>= getSharedPreferences(<span style="color: #067d17;">"BOARDING"</span>, <span style="color: #871094; font-style: italic;">MODE_PRIVATE</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Cek pengguna baru atau bukan menggunakan boolean dengan nilai default true,<br /></span><span style="color: #8c8c8c; font-style: italic;"> //jika pengguna baru kita arahkan ke BoardingActivity<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">if </span>(<span style="color: #871094;">preferences</span>.getBoolean(<span style="color: #871094; font-style: italic;">FIRST_USER</span>, <span style="color: #0033b3;">true</span>)) {<br /> finish();<br /> startActivity(<span style="color: #0033b3;">new </span>Intent(<span style="color: #0033b3;">this</span>, <span style="color: black;">BoardingActivity</span>.<span style="color: #0033b3;">class</span>));<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Boolean kita simpan dengan nilai false<br /></span><span style="color: #8c8c8c; font-style: italic;"> //Supaya saat membuka kembali aplikasi pengguna tidak diarahkan lagi ke BoardingActivity<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">preferences</span>.edit().putBoolean(<span style="color: #871094; font-style: italic;">FIRST_USER</span>, <span style="color: #0033b3;">false</span>).apply();<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Mereset data SharedPreferences ke awal<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textResetData</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #871094;">preferences</span>.edit().clear().apply();<br /> finish();<br /> startActivity(getIntent());<br /> }<br /> });<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Sebelum kita melangkah ke BoardingActivity, hampir saja kita lupa membuat adapter. Disini adapter kita berikan nama <b>OnboardAdapter.java</b>. Adapter ini adalah adapter RecyclerView yang nanti akan digunakan untuk adapter ViewPager2. Berikut isinya :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>OnboardAdapter.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.onboarding</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">OnboardAdapter </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">Adapter</span><<span style="color: black;">OnboardAdapter</span>.<span style="color: black;">OnboardViewHolder</span>> {<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">BoardingItemModel</span>> <span style="color: #871094;">boardingItemModels</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">OnboardAdapter</span>(<span style="color: black;">List</span><<span style="color: black;">BoardingItemModel</span>> boardingItemModels) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">boardingItemModels </span>= boardingItemModels;<br /> }<br /><br /> <span style="color: #9e880d;">@NonNull<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">OnboardAdapter</span>.<span style="color: black;">OnboardViewHolder </span><span style="color: #00627a;">onCreateViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ViewGroup </span>parent, <span style="color: #0033b3;">int </span>viewType) {<br /> <span style="color: #0033b3;">return new </span>OnboardViewHolder(<span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(parent.getContext())<br /> .inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">item_onboard</span>, parent, <span style="color: #0033b3;">false</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBindViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">OnboardAdapter</span>.<span style="color: black;">OnboardViewHolder </span>holder, <span style="color: #0033b3;">int </span>position) {<br /> holder.bindOnboard(<span style="color: #871094;">boardingItemModels</span>.get(position));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public int </span><span style="color: #00627a;">getItemCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">boardingItemModels</span>.size();<br /> }<br /><br /> <span style="color: #0033b3;">static class </span><span style="color: black;">OnboardViewHolder </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">ViewHolder </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">TextView </span><span style="color: #871094;">textTitleBoarding</span>, <span style="color: #871094;">textDescription</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">ImageView </span><span style="color: #871094;">imageOnboarding</span>;<br /><br /> <span style="color: #00627a;">OnboardViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">super</span>(itemView);<br /><br /> <span style="color: #871094;">textTitleBoarding </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textTitleBoarding</span>);<br /> <span style="color: #871094;">textDescription </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textDescription</span>);<br /> <span style="color: #871094;">imageOnboarding </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageOnBoarding</span>);<br /><br /> }<br /><br /> <span style="color: #0033b3;">void </span><span style="color: #00627a;">bindOnboard</span>(<span style="color: black;">BoardingItemModel </span>boardingItemModel) {<br /> <span style="color: #871094;">imageOnboarding</span>.setImageResource(boardingItemModel.getImageBoarding());<br /> <span style="color: #871094;">textTitleBoarding</span>.setText(boardingItemModel.getTitle());<br /> <span style="color: #871094;">textDescription</span>.setText(boardingItemModel.getDescription());<br /> }<br /> }<br />}<br /></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Nah sekarang kita menuju ke topik inti yaitu <b>BoardingActivity</b>, pertama-tama tentunya dengan mengatur terlebih dahulu layout nya. Terdapat cukup banyak view didalam layout ini, sebenarnya sedikit jika bukan project tutorial.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Biasanya dalam project sungguhan saya bisa memiliki view berpululuh-puluh hingga ratusan yang bisa ber-efek bad performance, loading editor lelet. Ada baiknya kita membuat view langsung dari konteks menggunakan loop, atau list seperti recyclerview jika harus memiliki banyak view. </span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, berikut isi dari layout activity_boarding.xml :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_boarding.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".BoardingActivity"</span>><br /><br /> <<span style="color: #0033b3;">androidx.viewpager2.widget.ViewPager2<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/onBoardViewPager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_viewpager"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/viewCenterNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/indicatorsContainer"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textSkip"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="LEWAT"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">View<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_view"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/imageNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/imageNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/imageNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/imageNext" </span>/><br /><br /> <<span style="color: #0033b3;">View<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/viewCenterNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="1dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="1dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="@id/imageNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="@id/imageNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="@id/imageNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="@id/imageNext" </span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageNext"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="40dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_button_next"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="3dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_next"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toTopOf</span><span style="color: #067d17;">="@id/textGetStarted"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="@color/teal_700" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textGetStarted"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_button_main"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Mulai Sekarang!"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textCountDownTimer"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="150dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="150dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="100sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:visibility</span><span style="color: #067d17;">="gone"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_timer"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Terakhir <b>BoardingActivity.java</b>, di dalam class ini sudah diberikan sedikit penjelasan dalam bentuk java comment pada setiap metode-metode nya, apabila ada yang ingin ditanyakan bisa disampaikan melalui kolom komentar. Berikut isi selengkapnya :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>BoardingActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.onboarding</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Build</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.CountDownTimer</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.LinearLayout</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Nullable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.core.content.ContextCompat</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.viewpager2.widget.ViewPager2</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">BoardingActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Variable views<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private </span><span style="color: black;">ViewPager2 </span><span style="color: #871094;">onBoardViewPager</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">LinearLayout </span><span style="color: #871094;">indicatorsContainer</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">textCountDownTimer</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">ImageView </span><span style="color: #871094;">imageNext</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">CountDownTimer </span><span style="color: #871094;">countDownTimer</span>;<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">OnboardAdapter </span><span style="color: #871094;">onboardAdapter</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">BoardingItemModel</span>> <span style="color: #871094;">boardingItemModels </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: #9e880d;">@Nullable </span><span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_boarding</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Inisialisai id dari views<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">onBoardViewPager </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">onBoardViewPager</span>);<br /> <span style="color: #871094;">indicatorsContainer </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">indicatorsContainer</span>);<br /> <span style="color: #871094;">textCountDownTimer </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textCountDownTimer</span>);<br /> <span style="color: #871094;">imageNext </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageNext</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Memanggil metode void ke onCreate<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>setItemsOnboarding();<br /> setupIndicators();<br /> setCurrentIndicators(<span style="color: #1750eb;">0</span>);<br /><br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textSkip</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> gotoMainActivity();<br /> }<br /> });<br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textGetStarted</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> gotoMainActivity();<br /> }<br /> });<br /><br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setItemsOnboarding</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">//Menentukan nilai-nilai ImageView dan TextViews untuk item-item<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">boardingItemModels</span>.add(<span style="color: #0033b3;">new </span>BoardingItemModel(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_highfive</span>,<br /> <span style="color: #067d17;">"Selamat Datang!"</span>, <span style="color: #067d17;">"Hi, pengguna " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">MODEL<br /></span><span style="color: #871094; font-style: italic;"> </span>+ <span style="color: #067d17;">". Selamat datang! Terima kasih telah menggunakan aplikasi kami."</span>));<br /> <span style="color: #871094;">boardingItemModels</span>.add(<span style="color: #0033b3;">new </span>BoardingItemModel(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_phone</span>, <span style="color: #067d17;">"Khusus Untuk Smartphone Anda"</span>,<br /> <span style="color: #067d17;">"Aplikasi kami dirancang khusus untuk smartphone Anda, dijamin Anda tidak akan kecewa."</span>));<br /> <span style="color: #871094;">boardingItemModels</span>.add(<span style="color: #0033b3;">new </span>BoardingItemModel(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_go</span>, <span style="color: #067d17;">"Sudahkah Anda Siap?"</span>,<br /> <span style="color: #067d17;">"Jika Anda tidak mengklik tombol, dalam hitungan mundur selama 20 detik," </span>+<br /> <span style="color: #067d17;">" Anda akan kami bawa ke halaman utama aplikasi."</span>));<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menghubungkan viewPager ke adapter Recycleview<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">onboardAdapter </span>= <span style="color: #0033b3;">new </span>OnboardAdapter(<span style="color: #871094;">boardingItemModels</span>);<br /> <span style="color: #871094;">onBoardViewPager</span>.setAdapter(<span style="color: #871094;">onboardAdapter</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menyinkronkan halaman viewpager dengan indicators<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">onBoardViewPager</span>.registerOnPageChangeCallback(<span style="color: #0033b3;">new </span><span style="color: black;">ViewPager2</span>.<span style="color: black;">OnPageChangeCallback</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onPageSelected</span>(<span style="color: #0033b3;">int </span>position) {<br /> <span style="color: #0033b3;">super</span>.onPageSelected(position);<br /> setCurrentIndicators(position);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Jika halaman viewpager mencapai halaman terakhir<br /></span><span style="color: #8c8c8c; font-style: italic;"> //Kita setel penghitung waktu mundur untuk ke MainActivity<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">if </span>(position == <span style="color: #1750eb;">2</span>) {<br /> <span style="color: #871094;">textCountDownTimer</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">VISIBLE</span>);<br /> <span style="color: #871094;">textCountDownTimer</span>.animate().rotation(<span style="color: #1750eb;">360</span>).setDuration(<span style="color: #1750eb;">2000</span>).withEndAction(<span style="color: #0033b3;">new </span><span style="color: black;">Runnable</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">run</span>() {<br /> <span style="color: #871094;">textCountDownTimer</span>.setRotation(<span style="color: #1750eb;">0</span>);<br /> <span style="color: #871094;">countDownTimer </span>= <span style="color: #0033b3;">new </span><span style="color: black;">CountDownTimer</span>(<span style="color: #1750eb;">21000</span>, <span style="color: #1750eb;">1000</span>) {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onTick</span>(<span style="color: #0033b3;">long </span>millisUntilFinished) {<br /> <span style="color: #0033b3;">int </span><span style="color: black;">detik </span>= (<span style="color: #0033b3;">int</span>) (millisUntilFinished / <span style="color: #1750eb;">1000</span>);<br /> <span style="color: #871094;">textCountDownTimer</span>.setText(<span style="color: black;">String</span>.<span style="font-style: italic;">valueOf</span>(<span style="color: black;">detik</span>));<br /> }<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onFinish</span>() {<br /> gotoMainActivity();<br /> }<br /> }.start();<br /> }<br /> }).start();<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #8c8c8c; font-style: italic;">//Jika halaman di geser kehalaman sebelumnya sebelum waktu penghitung selesai<br /></span><span style="color: #8c8c8c; font-style: italic;"> //penghitung waktu mundur kita batalkan dan textviewnya kita hilangkan<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">if </span>(<span style="color: #871094;">countDownTimer </span>!=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">countDownTimer</span>.cancel();<br /> <span style="color: #871094;">countDownTimer </span>= <span style="color: #0033b3;">null</span>;<br /> }<br /> <span style="color: #871094;">textCountDownTimer</span>.setText(<span style="color: #067d17;">""</span>);<br /> <span style="color: #871094;">textCountDownTimer</span>.setVisibility(<span style="color: black;">View</span>.<span style="color: #871094; font-style: italic;">GONE</span>);<br /> }<br /> }<br /> });<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Mode scrolling viewpager<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">RecyclerView recyclerView </span>= (<span style="color: black;">RecyclerView</span>) <span style="color: #871094;">onBoardViewPager</span>.getChildAt(<span style="color: #1750eb;">0</span>);<br /> <span style="color: black;">recyclerView</span>.setOverScrollMode(<span style="color: black;">RecyclerView</span>.<span style="color: #871094; font-style: italic;">OVER_SCROLL_NEVER</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menyinkronkan icon next dengan viewPager<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">imageNext</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #0033b3;">int </span><span style="color: black;">items </span>= <span style="color: #871094;">onBoardViewPager</span>.getCurrentItem();<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">items </span>+ <span style="color: #1750eb;">1 </span>< <span style="color: #871094;">onboardAdapter</span>.getItemCount()) {<br /> <span style="color: #871094;">onBoardViewPager</span>.setCurrentItem(<span style="color: black;">items </span>+ <span style="color: #1750eb;">1</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> gotoMainActivity();<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menyetel indikator, dipanggil di dalam onCreate<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">setupIndicators</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">//Membuat image array sebanyak jumlah halaman viewpager<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">ImageView</span>[] <span style="color: black;">indicators </span>= <span style="color: #0033b3;">new </span>ImageView[<span style="color: #871094;">onboardAdapter</span>.getItemCount()];<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Layoutparams untuk menyetel margin<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">LinearLayout</span>.<span style="color: black;">LayoutParams layoutParams </span>= <span style="color: #0033b3;">new </span><span style="color: black;">LinearLayout</span>.LayoutParams(<span style="color: black;">LinearLayout</span>.<span style="color: black;">LayoutParams</span>.<span style="color: #871094; font-style: italic;">WRAP_CONTENT</span>,<br /> <span style="color: black;">LinearLayout</span>.<span style="color: black;">LayoutParams</span>.<span style="color: #871094; font-style: italic;">WRAP_CONTENT</span>);<br /> <span style="color: black;">layoutParams</span>.setMargins(<span style="color: #1750eb;">0</span>, <span style="color: #1750eb;">8</span>, <span style="color: #1750eb;">5</span>, <span style="color: #1750eb;">8</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//java loop untuk menentukan indikator dan menyetel nilai default background indikatornya<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int </span>i = <span style="color: #1750eb;">0</span>; i < <span style="color: black;">indicators</span>.<span style="color: #871094;">length</span>; i++) {<br /> <span style="color: black;">indicators</span>[i] = <span style="color: #0033b3;">new </span>ImageView(<span style="color: #0033b3;">this</span>);<br /> <span style="color: black;">indicators</span>[i].setImageDrawable(<span style="color: black;">ContextCompat</span>.<span style="font-style: italic;">getDrawable</span>(getApplicationContext(),<br /> <span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">indicator_inactive</span>));<br /> <span style="color: black;">indicators</span>[i].setLayoutParams(<span style="color: black;">layoutParams</span>);<br /> <span style="color: #871094;">indicatorsContainer</span>.addView(<span style="color: black;">indicators</span>[i]);<br /> }<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Menyetel indikator saat halaman di scroll<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">setCurrentIndicators</span>(<span style="color: #0033b3;">int </span>position) {<br /> <span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int </span>i = <span style="color: #1750eb;">0</span>; i < <span style="color: #871094;">indicatorsContainer</span>.getChildCount(); i++) {<br /> <span style="color: black;">ImageView imageView </span>= (<span style="color: black;">ImageView</span>) <span style="color: #871094;">indicatorsContainer</span>.getChildAt(i);<br /> <span style="color: #0033b3;">if </span>(i == position) {<br /> <span style="color: black;">imageView</span>.setImageDrawable(<span style="color: black;">ContextCompat</span>.<span style="font-style: italic;">getDrawable</span>(getApplicationContext(),<br /> <span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">indicator_active</span>));<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: black;">imageView</span>.setImageDrawable(<span style="color: black;">ContextCompat</span>.<span style="font-style: italic;">getDrawable</span>(getApplicationContext(),<br /> <span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">indicator_inactive</span>));<br /> }<br /> }<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Intent ke MainActivity<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">gotoMainActivity</span>() {<br /> finish();<br /> startActivity(<span style="color: #0033b3;">new </span>Intent(<span style="color: #0033b3;">this</span>, <span style="color: black;">MainActivity</span>.<span style="color: #0033b3;">class</span>));<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">countDownTimer</span>!=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">countDownTimer</span>.cancel();<br /> <span style="color: #871094;">countDownTimer </span>= <span style="color: #0033b3;">null</span>;<br /> }<br /> }<br /><br />}<br /></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Sekian dan terima kasih atas waktu dan kunjungannya.</span>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-64053798346198832052021-02-10T14:58:00.003+07:002021-02-22T18:41:22.345+07:00Android Drag Drop Views Menggunakan Linearlayout<span style="font-family: georgia;">Disini kita akan membahas <b>Drag Drop Views / Widget</b> yang ada di dalam sebuah Linearlayout, dengan menggunakan kelas kustom yang memperpanjang Linearlayout. Kode di dalam kelas kustom ini cukup panjang, namun saya sudah membuatnya menjadi sebuah library, sehingga Anda hanya perlu mengunduh library tersebut kedalam project Anda.<span><a name='more'></a></span></span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Namun bila Anda ingin membuatnya menjadi kelas Anda sendiri, silahkan klik <a href="https://github.com/daltray/DragLinearLayout/blob/main/mylibrary/src/main/java/com/gwnbs/library/GwnbsDragLinear.java" target="_blank">link ini</a> untuk melihat / menyalin nya. Kelas ini bukan murni kreasi saya, saya hanya menambah dan merubah / memperbaharui beberapa kode yang sudah usang. Berikut video dari tutorial project ini :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/wBhj80iAMP8" width="320" youtube-src-id="wBhj80iAMP8"></iframe></div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, pertama mari kita persiapkan sebuah project dengan minimum SDK 21 (Lolipop). Pada contoh ini nama projectnya adalah <b>Drag Drop</b> dengan nama paket <i>com.gwnbs.dragdrop</i>. Kemudian untuk mengunduh library nya, buka <b>build.gradle(Project)</b> lalu tambahkan <i>maven { url '<span style="color: #6aa84f;">https://jitpack.io</span>' }</i> kedalam repositories allprojects :<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9pt;">allprojects <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>repositories <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>google()<br /> jcenter()<br /> maven <span style="font-weight: bold;">{ </span>url <span style="color: #067d17;">'https://jitpack.io' </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"> }<br /></span><span style="font-weight: bold;">}</span></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Selanjutnya buka <b>build.gradle(Module)</b> dan tambahkan implementasi library berikut ke dalam dependencies kemudian sync project untuk mulai mengunduh library :<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9pt;">implementation <span style="color: #067d17;">'com.github.daltray:DragLinearLayout:1.1.0'</span></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Library ini mendukung baik orientasi vertical maupun horizontal. Dalam contoh ini kita akan menggunakan vertical. Views yang dapat di drag dan drop adalah views child yang berada satu tingkat didalam parent layout. Analoginya misalnya saya punya sepuluh anak, kesepuluh anak saya ini bisa saling drag drop, namun cucu-cucu saya (anak-anak dari anak-anak saya) tidak bisa di drag drop, mereka akan mengikut orang tuanya yang di drag drop.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Untuk memulai menggunakan library ini, pada activity_main.xml panggil dengan tag <b><com.gwnbs.library.GwnbsDragLinear</b> , tentukan atribut-atribut dan tambahkan child-child view. Seperti contoh dibawah ini memiliki 6 child view dan 3 grand child view.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_main.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout <br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textGuide"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:alpha</span><span style="color: #067d17;">="0.7"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="#F6F1F1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/teks_guide"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="18sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Parent Layout --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">com.gwnbs.library.GwnbsDragLinear<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/dragDrop"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/textGuide"</span>><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Child view 1--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:autoLink</span><span style="color: #067d17;">="web"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_margin"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/blog_gwnbs"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Child view 2--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_margin"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_android" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Child view 3--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_margin"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/bahasa_java"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Child view 4--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_margin"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/subscribe"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Child view 5--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_margin"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_java" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Child view 6--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_margin"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- GrandChild view 1--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_android" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- GrandChild view 2--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_java" </span>/><br /><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- GrandChild view 3--><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="@string/android_java"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">com.gwnbs.library.GwnbsDragLinear</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Untuk kode di konteks <b>MainActivity.java</b> kita hanya perlu beberapa baris kode saja untuk menyetel view-view dapat di drag drop, karena memang sudah diimplementasikan di dalam kelas yang ada pada library.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Pertama dengan menginisialisasi variabel dan id dari kelas <b>GwnbsDragLinear</b>, kemudian menggunakan loop untuk menghitung jumlah child view, terakhir memanggil metode <i>setViewDraggable</i>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.dragdrop</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.gwnbs.library.GwnbsDragLinear</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Inisialisasi variabel dan id class GwnbsDragLinear<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">GwnbsDragLinear dragDrop </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">dragDrop</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Java loop untuk menghitung jumlah child view didalam GwnbsDragLinear<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">for </span>(<span style="color: #0033b3;">int </span>i = <span style="color: #1750eb;">0</span>; i < <span style="color: black;">dragDrop</span>.getChildCount(); i++) {<br /> <span style="color: black;">View child </span>= <span style="color: black;">dragDrop</span>.getChildAt(i);<br /> <span style="color: #8c8c8c; font-style: italic;">//Menyetel views dapat diseret menggunakan setViewDraggable<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">dragDrop</span>.setViewDraggable(<span style="color: black;">child</span>, <span style="color: black;">child</span>);<br /> }<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Berikut isi dari <b>bg_margin.xml</b> drawable yang digunakan sebagai background untuk child-child view diatas :<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: "Courier New", monospace; font-size: 9pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="5dp"</span>/><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/white"</span>/><br /> <<span style="color: #0033b3;">stroke </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:width</span><span style="color: #067d17;">="1dp"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Nah jika Anda ingin memodifikasi, baik menambahkan kode / metode ataupun merubah kode, silahkan klik link yang sudah ditautkan diatas sebelumnya untuk menyalin kode. Pertama buat sebuah Java Class dengan nama <b>GwnbsDragLinear </b>(Anda dapat merubahnya nanti dengan cara <i>Refactor > Rename</i> setelah seluruh kode di pastekan), lalu pastekan seluruh kode yang Anda salin dari link tersebut.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, sekian tutorial ini. Terimakasih atas waktu dan kunjungannya. Jika ada pertanyaan silahkan disampaikan di komentar.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-6027476368657704732021-01-23T04:20:00.002+07:002021-02-22T18:41:43.009+07:00Cara (terbaru) Pasang Iklan Banner, Interstitial, dan RewardedAd AdMob<p><span style="font-family: georgia;">Tatkala membuka file gradle app, sering kita menjumpai implementasi library di sorot berwarna (jika kita mengaktifkan inspeksi) yang mana menginstruksikan kita untuk melakukan pembaharuan library. Setelah melakukan pembaharuan sebuah library, ada saja peringatan mengenai class atau metode yang di <i><span style="color: #ffa400;">deprecated </span></i>(usang) yang tentunya bukanlah hal yang baik jika kita tidak mengganti class atau metode yang di deprecated tersebut.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;">Pada postingan-postingan terdahulu blog ini sudah membahas cara menerapkan iklan Banner, Interstitial dan Rewarded AdMob secara terpisah, dan disini kita akan membahas ulang ketiga jenis iklan tersebut sekaligus karena cara implementasi untuk Interstitial dan Rewarded pada postingan terdahulu sudah di deprecated, dan untuk Banner masih sama.</span></p><p><span style="font-family: georgia;"><b>Deprecated </b>bukan berarti class, metode atau kode tersebut tidak bisa dipakai lagi, masih tetap dapat dipakai namun tidak disarankan, biasanya karena berbahaya atau ada alternatif lain yang lebih baik.</span></p><p><span style="font-family: georgia;">Kita akan mulai dari iklan banner, namun sebelumnya import terlebih dahulu library Google AdMob SDK terbaru ke dalam file gradle app lalu sync gradle menggunakan internet:<br /></span></p><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;">implementation <span style="color: #067d17;">'com.google.android.gms:play-services-ads:19.7.0'</span></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Kemudian juga meta data ID Aplikasi di dalam tag <application> pada file AndroidManifest.xml. contoh ID Aplikasi berikut adalah sampel ID Aplikasi resmi Google AdMob. Jika Anda ingin lebih detil dan presisi lagi tentang bagaimana cara mendapatkannya, menempatkannya dan lain sebagainya, Anda bisa melihat postingan-postingan terdahulu berikut : <a href="https://www.gwnbs.com/2020/06/cara-memasang-iklan-interstital-admob-di-android-studio.html" target="_blank">Cara Memasang Iklan Interstitial AdMob</a>, <a href="https://www.gwnbs.com/2020/06/cara-pasang-iklan-banner-admob-android-studio.html" target="_blank">Cara Memasang Iklan Banner AdMob</a>, dan <a href="https://www.gwnbs.com/2020/09/cara-memasang-iklan-video-reward-admob.html" target="_blank">Cara Memasang Iklan Video Reward AdMob</a>.</span><div><span style="font-family: georgia;"><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">meta-data<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="com.google.android.gms.ads.APPLICATION_ID"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:value</span><span style="color: #067d17;">="ca-app-pub-3940256099942544~3347511713"</span>/></pre></div></span><p></p></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<h3 style="text-align: left;"><span style="color: #38761d; font-family: times; font-size: medium;"><b><u>IKLAN BANNER</u></b></span></h3><span style="font-family: georgia;">Penerapan iklan banner merupakan yang termudah dan tersimpel diantara lainnya, kita hanya butuh sebuah komponen view AdView pada layout.xml, kemudian beberapa baris kode pada konteks (pada contoh ini <b>MainActivity.java</b>) iklan siap ditampilkan. Atau kita juga bisa mengimplementasikan seluruhnya pada konteks tanpa perlu membuat komponen AdView pada layout. Disini akan kita bahas keduanya.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kita mulai dari menempatkan komponen AdView pada layout <b>activity_mai.xml</b>, dengan ukuran iklan "BANNER" (320x50). Ukuran lain yang bisa Anda gunakan selain BANNER adalah : LARGE_BANNER (320x100), MEDIUM_RECTANGLE (300x250), FULL_BANNER (468x60), LEADERBOARD (728x90), dan SMART_BANNER (Screen width x 32|50|90).<br /><br /><b>activity_main.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Hello World!"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="30sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintLeft_toLeftOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintRight_toRightOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /><br /> <<span style="color: #0033b3;">com.google.android.gms.ads.AdView<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">ads</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/adView"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">ads</span><span style="color: #174ad4;">:adSize</span><span style="color: #067d17;">="BANNER"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">ads</span><span style="color: #174ad4;">:adUnitId</span><span style="color: #067d17;">="ca-app-pub-3940256099942544/6300978111"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Kemudian pada <b>MainActivity.java</b>, pertama dengan inisialisasi MobileAds SDK, setelah itu variabel dan inisialisai AdView, lalu memuat iklan dengan mengirim permintaan ke jaringan AdMob dengan AdRequest. Kita bisa menambahkan opsi listener untuk adView tersebut untuk mengetahui apakah iklan berhasil dimuat atau gagal.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Didalam listener, metode yang paling kita butuhkan ialah <i>onAdLoaded </i>dan <i>onAdFailedToLoad</i>. Kita bisa menempatkan aksi didalam metode-metode tersebut. Misalnya didalam onAdLoaded (saat iklan berhasil dimuat) kita menampilkan pesan toast. Di dalam onAdFailedToLoad (iklan gagal dimuat) kita menempatkan pesan toast, saat iklan gagal dimuat kita bisa mengirim lagi permintaan iklan ke jaringan AdMob.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Jika Anda mengirim permintaan iklan kembali didalam metode <i>onAdFailedToLoad</i>, pastikan Anda membatasi jumlah permintaan, misalnya sebanyak 3 atau 4 kali. Anda bisa menggunakan integer untuk membatasi jumlah pemintaan ulang. Hal ini untuk menghindari permintaan iklan yang terus-menerus gagal dalam situasi seperti konektivitas jaringan terbatas.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Setelah penyiapan iklan banner selesai, kemudian kita juga perlu men destroy (menghancurkan) AdView dari aktifitas yang ditinggalkan pengguna aplikasi dengan meng override metode <b>onDestroy</b>. Metode onDestroy adalah metode untuk aktifitas yang sudah hancur. Berikut kode selengkapnya :<br /><br /><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.admobads</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdListener</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdRequest</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.LoadAdError</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.MobileAds</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.InitializationStatus</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.OnInitializationCompleteListener</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">AdView </span><span style="color: #871094;">adView</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">AdRequest </span><span style="color: #871094;">adRequest</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">loadBannerAgain</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: black;">MobileAds</span>.<span style="font-style: italic;">initialize</span>(<span style="color: #0033b3;">this</span>, <span style="color: #0033b3;">new </span><span style="color: black;">OnInitializationCompleteListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationComplete</span>(<span style="color: black;">InitializationStatus </span>initializationStatus) {<br /> }<br /> });<br /><br /> <span style="color: #871094;">adView </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">adView</span>);<br /> <span style="color: #871094;">adRequest </span>= <span style="color: #0033b3;">new </span><span style="color: black;">AdRequest</span>.Builder().build();<br /> <span style="color: #871094;">adView</span>.loadAd(<span style="color: #871094;">adRequest</span>);<br /><br /> <span style="color: #871094;">adView</span>.setAdListener(<span style="color: #0033b3;">new </span><span style="color: black;">AdListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdLoaded</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">//Jika iklan berhasil dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan berhasil dimuat"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdFailedToLoad</span>(<span style="color: black;">LoadAdError </span>adError) {<br /> <span style="color: #8c8c8c; font-style: italic;">//Saat iklan gagal dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan gagal dimuat, kode error : " </span>+ adError.getMessage(),<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Mengirim permintaan kembali sebanyak 3 kali.<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">loadBannerAgain </span>+= <span style="color: #1750eb;">1</span>;<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">loadBannerAgain </span>== <span style="color: #1750eb;">1 </span>|| <span style="color: #871094;">loadBannerAgain </span>== <span style="color: #1750eb;">2 </span>|| <span style="color: #871094;">loadBannerAgain </span>== <span style="color: #1750eb;">3</span>) {<br /> <span style="color: #871094;">adView</span>.loadAd(<span style="color: #871094;">adRequest</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Permintaan ulang " </span>+ <span style="color: #871094;">loadBannerAgain</span>, <br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Sudah 3 kali request tapi tidak muncul juga. Saya stop request"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdClicked</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">//Saat user mengklik iklan<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Anda telah mengklik iklan. Terimakasih"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onDestroy</span>() {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">adView </span>!=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">adView</span>.destroy();<br /> }<br /> <span style="color: #0033b3;">super</span>.onDestroy();<br /> }<br />}</pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Bagi Anda mungkin yang belum mengerti dan bertanya-tanya mengenai <span style="color: #800180;">if(adView !=null)</span> didalam metode <b>onDestroy </b>diatas, artinya adalah : <b>null </b>sendiri jika diartikan ke bahasa Indonesia adalah kosong / tidak ada, lalu maksud dari simbol "<b>!=</b>" berarti tidak. Jika diartikan kedalam bahasa percakapan maka "<i>jika adview tidak kosong</i>", artinya adview berhasil dimuat dan ditampilkan. Kita perlu membuat boolean seperti itu untuk menghindari crash.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Coba saja misalnya Anda langsung saja membuat <span style="color: #800180;">adView.destroy</span> tanpa boolean if, maka saat iklan gagal dimuat dan tampil (berarti adview nya null / kosong), kemudian Anda menekan tombol back atau tombol lain untuk pergi dari aktifitas tersebut maka aplikasi akan crash, dan jika Anda melihat di log cat penyebabnya adalah <b>NullPointerException</b>, yang artinya compiler tidak menemukan object tersebut.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Nah, pada <b>onDestroy </b>diatas, kita memberi instruksi compiler untuk menghancurkan adView jika adView nya tidak kosong, dan jika adView nya kosong atau tidak ada, compiler tidak melakukan apa-apa.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Sekarang kita implementasi AdView keseluruhan melalui konteks <b>MainActivity.java</b>. Pertama kita hilangkan dulu komponen AdView pada <b>activity_main.xml</b> diatas, lalu kita gantikan dengan sebuah layout, dalam contoh ini LinearLayout. Layout ini nantinya yang akan kita gunakan untuk menambahkan AdView melalui MainActivity.<br /><br /><b>activity_main.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Hello World!"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="30sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintLeft_toLeftOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintRight_toRightOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutBannerAds"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"</span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Selanjutnya pada <b>MainActivity </b>kita hanya merubah dan menambah beberapa kode saja. Kode yang kita gantikan adalah inisialisai <i>findViewById </i>menjadi <i>new AdView</i>, kemudian jika tadinya kita menyetel ukuran dan unit ID melalui activity_main.xml, disini kita menyetelnya dengan menggunakan <i>setAdSize </i>dan <i>setAdUnitId</i>. Setelah itu inisialisai LinearLayout lalu menambahkan adView kedalamnya.<br /><br /><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.admobads</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.LinearLayout</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdListener</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdRequest</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdSize</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.LoadAdError</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.MobileAds</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.InitializationStatus</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.OnInitializationCompleteListener</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">AdView </span><span style="color: #871094;">adView</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">AdRequest </span><span style="color: #871094;">adRequest</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">loadBannerAgain</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: black;">MobileAds</span>.<span style="font-style: italic;">initialize</span>(<span style="color: #0033b3;">this</span>, <span style="color: #0033b3;">new </span><span style="color: black;">OnInitializationCompleteListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationComplete</span>(<span style="color: black;">InitializationStatus </span>initializationStatus) {<br /> }<br /> });<br /><br /> <span style="color: #871094;">adView </span>= <span style="color: #0033b3;">new </span>AdView(<span style="color: #0033b3;">this</span>);<br /> <span style="color: #871094;">adRequest </span>= <span style="color: #0033b3;">new </span><span style="color: black;">AdRequest</span>.Builder().build();<br /> <span style="color: #871094;">adView</span>.setAdSize(<span style="color: black;">AdSize</span>.<span style="color: #871094; font-style: italic;">BANNER</span>);<br /> <span style="color: #871094;">adView</span>.setAdUnitId(<span style="color: #067d17;">"ca-app-pub-3940256099942544/6300978111"</span>);<br /><br /> <span style="color: black;">LinearLayout layoutBannerAds </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">layoutBannerAds</span>);<br /> <span style="color: black;">layoutBannerAds</span>.addView(<span style="color: #871094;">adView</span>);<br /> <span style="color: #871094;">adView</span>.loadAd(<span style="color: #871094;">adRequest</span>);<br /><br /> <span style="color: #871094;">adView</span>.setAdListener(<span style="color: #0033b3;">new </span><span style="color: black;">AdListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdLoaded</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">//Jika iklan berhasil dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan berhasil dimuat"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdFailedToLoad</span>(<span style="color: black;">LoadAdError </span>adError) {<br /> <span style="color: #8c8c8c; font-style: italic;">//Saat iklan gagal dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan gagal dimuat, kode error : " </span>+ adError.getMessage(),<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Mengirim permintaan kembali sebanyak 3 kali.<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">loadBannerAgain </span>+= <span style="color: #1750eb;">1</span>;<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">loadBannerAgain </span>== <span style="color: #1750eb;">1 </span>|| <span style="color: #871094;">loadBannerAgain </span>== <span style="color: #1750eb;">2 </span>|| <span style="color: #871094;">loadBannerAgain </span>== <span style="color: #1750eb;">3</span>) {<br /> <span style="color: #871094;">adView</span>.loadAd(<span style="color: #871094;">adRequest</span>);<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Permintaan ulang " </span>+ <span style="color: #871094;">loadBannerAgain</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Sudah 3 kali request tapi tidak muncul juga. Saya stop request"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdClicked</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">//Saat user mengklik iklan<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Anda telah mengklik iklan. Terimakasih"</span>,<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onDestroy</span>() {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">adView </span>!=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">adView</span>.destroy();<br /> }<br /> <span style="color: #0033b3;">super</span>.onDestroy();<br /> }<br />}</pre></div><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/REHkMRVpv3g" width="320" youtube-src-id="REHkMRVpv3g"></iframe></div><span style="font-family: georgia;"><br /></span></div>
<h3 style="text-align: left;"><span style="color: #38761d; font-family: times; font-size: medium;"><b><u>IKLAN INTERSTITIAL</u></b></span></h3><span style="font-family: georgia;">
Pada library SDK Google AdMob terbaru ini, penerapan interstitial banyak mengalami perubahan. Mulai dari cara memuat iklan yang sekarang melalui Interstitial static (dulunya melalui inisialisai interstitial), lalu listener yang sekarang menggunakan ContentCallback, dan cara menampilkan iklan yang sekarang menggunakan boolean null.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Masih seperti yang dahulu, implementasi Interstitial sepenuhnya melalui konteks, kita hanya perlu menyediakan komponen tombol melalui layout untuk menampilkan iklan. Biasanya interstitial diterapkan developers pada transisi dari satu aktivitas ke aktivitas lainnya, artinya iklan muncul sebelum pengguna sampai ke aktifitas yang dituju. Atau pada game saat sebuah level permainan selesai, iklan muncul sebelum menuju ke level berikutnya.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Penerapan kemana pengguna akan dibawa setelah iklan ditutup adalah pada metode ContentCallback yaitu <i>onAdDismissedFullScreenContent</i>. Dahulu sebelum di deprecated bernama <i>onAdClosed</i> dengan metode listener sama dengan iklan banner.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, misalnya di <b>activity_main.xml</b> kita memiliki tombol untuk menampilkan iklan.<br /><br /><b>activity_main.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/buttonAd"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Show Ad"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="30sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintLeft_toLeftOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintRight_toRightOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Iklan Interstitial ini sifatnya setelah iklan berhasil dimuat dan berhasil dimunculkan, kita perlu mengirim permintaan baru lagi ke jaringan AdMob untuk iklan selanjutnya. Istilahnya "sekali pakai". Tidak seperti Banner yang berganti iklan secara otomatis.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Nah, di <b>MainActivity</b>, kita menerapkan permintaan iklan di dalam sebuah metode private, lalu metode ini kita panggil di onCreate untuk memuat iklan di awal saat aktifitas dibuka, dan saat iklan di ditutup oleh pengguna yaitu pada <i>onAdDissmisedFullScreenContent</i>, metode ini kita panggil lagi untuk memuat iklan selanjutnya.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, pertama-tama dengan membuat global variabel class Interstitial, kemudian kita buat metode private untuk mengirim permintaan iklan dengan Interstitial static. Di dalam metode mengirim permintaan iklan ini, pada metode <i>onAdLoaded</i>, kita perlu menyamakan global varibel Interstitial dengan argumen variabel Interstitial yang ada dalam onAdLoaded.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Selanjutnya pada <i>onAdFailedToLoad</i>, kita perlu menyetel global varibel Interstitial tersebut ke null.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kemudian metode menampilkan iklan yaitu pada tombol buttonAd, kita harus membuat boolean "<i>jika interstitial tidak kosong</i>" <b>if (interstitial !=null)</b> terlebih dahulu, kemudian didalamnya disematkan <b>interstitial.show(MainActivity.this)</b> diikuti listener yaitu <i>setOnFullScreenContentCallback</i>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Di dalam listener, pada <i>onAdShowedFullScreenContent</i>, variabel interstitial harus disetel lagi ke null supaya iklan yang sama tidak ada lagi. Lalu pada <i>onAdDismissedFullScreenContent </i>kita panggil lagi metode private permintaan iklan untuk memuat iklan baru. Selengkapnya terlihat seperti ini :<br /><br /><b>MainActivity.java</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.admobads</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdError</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdRequest</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.FullScreenContentCallback</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.LoadAdError</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.MobileAds</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.InitializationStatus</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.OnInitializationCompleteListener</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.interstitial.InterstitialAd</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">InterstitialAd </span><span style="color: #871094;">interstitialAd</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: black;">MobileAds</span>.<span style="font-style: italic;">initialize</span>(<span style="color: #0033b3;">this</span>, <span style="color: #0033b3;">new </span><span style="color: black;">OnInitializationCompleteListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationComplete</span>(<span style="color: black;">InitializationStatus </span>initializationStatus) {<br /> }<br /> });<br /><br /> loadInterstitial();<br /><br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">buttonAd</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">interstitialAd </span>!=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">interstitialAd</span>.show(<span style="color: black;">MainActivity</span>.<span style="color: #0033b3;">this</span>);<br /><br /> <span style="color: #871094;">interstitialAd</span>.setFullScreenContentCallback(<span style="color: #0033b3;">new </span><span style="color: black;">FullScreenContentCallback</span>(){<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdDismissedFullScreenContent</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan ditutup<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>loadInterstitial();<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan ditutup"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdFailedToShowFullScreenContent</span>(<span style="color: black;">AdError </span>adError) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan gagal muncul<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan gagal muncul"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdShowedFullScreenContent</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">// saat iklan sudah muncul<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">interstitialAd </span>= <span style="color: #0033b3;">null</span>;<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan sedang tampil"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">loadInterstitial</span>() {<br /> <span style="color: black;">InterstitialAd</span>.<span style="font-style: italic;">load</span>(<span style="color: #0033b3;">this</span>, <span style="color: #067d17;">"ca-app-pub-3940256099942544/1033173712"</span>, <span style="color: #0033b3;">new </span><span style="color: black;">AdRequest</span>.Builder().build(),<br /> <span style="color: #0033b3;">new </span><span style="color: black;">InterstitialAdLoadCallback</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdLoaded</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">InterstitialAd </span>interstitial) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan berhasil dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">interstitialAd </span>= interstitial;<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan berhasil dimuat"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdFailedToLoad</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">LoadAdError </span>loadAdError) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan gagal dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">interstitialAd </span>= <span style="color: #0033b3;">null</span>;<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan gagal dimuat. Kode gagal : " </span>+ loadAdError.getMessage(),<br /> <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Jika Anda hendak menuju ke aktivitas lain saat iklan ditutup, Anda hanya perlu menambahkan <b>startActivity(new Intent(MainActivity.this, namaActivityAnda.class));</b> di dalam <i>onAdDismissedFullScreenContent</i>. Dan juga Anda perlu menambahkannya didalam <i>onAdFailedToLoad </i>pada metode permintaan iklan jika memang fungsi utama tombol Anda adalah ke aktivitas lain. Karena tentu tidak lucu saat iklan berhasil dimuat dan tampil pengguna di arahkan ke aktivitas lain tersebut saat iklan ditutup namun tidak saat iklan gagal dimuat.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Lalu jika Anda hendak menampilkan iklannya pada tombol back perangkat, Anda override saja <b>onBackPressed </b>lalu pindahkan semua kode yang ada didalam metode klik tombol buttonAd ke dalam onBackPressed, seperti dibawah ini.<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #9e880d;">@Override<br /></span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBackPressed</span>() {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">interstitialAd </span>!=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">interstitialAd</span>.show(<span style="color: black;">MainActivity</span>.<span style="color: #0033b3;">this</span>);<br /><br /> <span style="color: #871094;">interstitialAd</span>.setFullScreenContentCallback(<span style="color: #0033b3;">new </span><span style="color: black;">FullScreenContentCallback</span>(){<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdDismissedFullScreenContent</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan ditutup<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>loadInterstitial();<br /> <span style="color: black;">MainActivity</span>.<span style="color: #0033b3;">super</span>.onBackPressed();<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan ditutup"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdFailedToShowFullScreenContent</span>(<span style="color: black;">AdError </span>adError) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan gagal muncul<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan gagal muncul"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdShowedFullScreenContent</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">// saat iklan sudah muncul<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">interstitialAd </span>= <span style="color: #0033b3;">null</span>;<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan sedang tampil"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: #0033b3;">super</span>.onBackPressed();<br /> }<br />}</pre></div></span></div><h3 style="text-align: left;"><span style="color: #38761d; font-family: times; font-size: medium;"><b><u><br /></u></b></span></h3><div><span style="color: #38761d; font-family: times; font-size: medium;"><b><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/j5ki5ur46g8" width="320" youtube-src-id="j5ki5ur46g8"></iframe></div><br /></b></span></div><h3 style="text-align: left;"><span style="color: #38761d; font-family: times; font-size: medium;"><b><u>IKLAN REWARDED</u></b></span></h3><span style="font-family: georgia;">Jenis iklan ini hanya video. Penerapan iklan reward ini sama persis dengan penerapan Interstitial, bedanya hanya ada pada reward atau hadiah. Sesuai namanya, harus ada hadiah yang didapatkan pengguna setelah penyelesaian menonton iklan, hadiah tentu tergantuang sang developer, entah itu Anda akan memberi misalnya dalam game yaitu koin tambahan, uang, nyawa tambahan, akses premium dan lain sebagainya.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Jadi untuk penjelasan selain metode hadiah nanti, sama persis dengan Interstitial sehingga tidak perlu dijelaskan kembali.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Nah, dicontoh ini kita terapkan yang simpel saja, yaitu memberikan hadiah bonus uang 100 dollar kepada user. Kita akan menyimpan nilai uang pengguna menggunakan SharedPreferences. </span><span style="font-family: georgia;">Penentuan dan pemberian hadiah diterapkan didalam metode <i>onUserEarnedReward </i>yang di override bersamaan saat menampilkan iklan yaitu :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="color: #800180; font-family: georgia;">rewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() {</span></div><div><span style="color: #800180; font-family: georgia;">//disini penentuan hadiah</span></div><div><span style="color: #800180; font-family: georgia;">});</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, kita mulai dari layout yaitu sebuah TextView untuk menampilkan nilai uang dan sebuah tombol untuk menampilkan iklan. Berikut isinya :<br /><br /><b>activity_main.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textDollar"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="0"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="35sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:drawableStartCompat</span><span style="color: #067d17;">="@drawable/ic_dollar"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/buttonBonus"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Bonus $100"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="30sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintLeft_toLeftOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintRight_toRightOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Seperti yang sudah disampaikan diatas bahwa cara mengirim permintaan iklan, menampilkan iklan untuk rewarded sama persis dengan Interstitial. Nah berikut isi selengkapnya pada MainActivity.java.<br /><br /><b>MainActivity.java</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.admobads</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.SharedPreferences</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdError</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.AdRequest</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.FullScreenContentCallback</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.LoadAdError</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.MobileAds</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.OnUserEarnedRewardListener</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.InitializationStatus</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.initialization.OnInitializationCompleteListener</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.rewarded.RewardItem</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.rewarded.RewardedAd</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">com.google.android.gms.ads.rewarded.RewardedAdLoadCallback</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">RewardedAd </span><span style="color: #871094;">rewardedAd</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">textDollar</span>;<br /> <span style="color: #0033b3;">private </span><span style="color: black;">SharedPreferences </span><span style="color: #871094;">preferences</span>;<br /> <span style="color: #0033b3;">private int </span><span style="color: #871094;">hadiah</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #871094;">textDollar </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textDollar</span>);<br /> <span style="color: #871094;">preferences </span>= getSharedPreferences(<span style="color: #067d17;">"REWARD"</span>, <span style="color: #871094; font-style: italic;">MODE_PRIVATE</span>);<br /> <span style="color: #871094;">hadiah </span>= <span style="color: #871094;">preferences</span>.getInt(<span style="color: #067d17;">"hadiah"</span>, <span style="color: #1750eb;">0</span>);<br /> <span style="color: #871094;">textDollar</span>.setText(<span style="color: black;">String</span>.<span style="font-style: italic;">valueOf</span>(<span style="color: #871094;">hadiah</span>));<br /><br /> <span style="color: black;">MobileAds</span>.<span style="font-style: italic;">initialize</span>(<span style="color: #0033b3;">this</span>, <span style="color: #0033b3;">new </span><span style="color: black;">OnInitializationCompleteListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onInitializationComplete</span>(<span style="color: black;">InitializationStatus </span>initializationStatus) {<br /> }<br /> });<br /><br /> loadRewarded();<br /><br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">buttonBonus</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>v) {<br /> <span style="color: #0033b3;">if </span>(<span style="color: #871094;">rewardedAd </span>!= <span style="color: #0033b3;">null</span>) {<br /> <span style="color: #871094;">rewardedAd</span>.show(<span style="color: black;">MainActivity</span>.<span style="color: #0033b3;">this</span>, <span style="color: #0033b3;">new </span><span style="color: black;">OnUserEarnedRewardListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onUserEarnedReward</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RewardItem </span>rewardItem) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Memberi hadiah kepada pengguna saat iklan selesai ditonton<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Anda dapat $100 bonus"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> <span style="color: #871094;">hadiah </span>+= <span style="color: #1750eb;">100</span>;<br /> <span style="color: black;">SharedPreferences</span>.<span style="color: black;">Editor editor </span>= <span style="color: #871094;">preferences</span>.edit();<br /> <span style="color: black;">editor</span>.putInt(<span style="color: #067d17;">"hadiah"</span>, <span style="color: #871094;">hadiah</span>);<br /> <span style="color: black;">editor</span>.apply();<br /> <span style="color: #871094;">textDollar</span>.setText(<span style="color: black;">String</span>.<span style="font-style: italic;">valueOf</span>(<span style="color: #871094;">hadiah</span>));<br /> }<br /> });<br /><br /> <span style="color: #871094;">rewardedAd</span>.setFullScreenContentCallback(<span style="color: #0033b3;">new </span><span style="color: black;">FullScreenContentCallback</span>(){<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdShowedFullScreenContent</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan sedang tampil<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">rewardedAd </span>= <span style="color: #0033b3;">null</span>;<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan sedang tampil"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdFailedToShowFullScreenContent</span>(<span style="color: black;">AdError </span>adError) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan gagal ditampilkan<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan gagal ditampilkan"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdDismissedFullScreenContent</span>() {<br /> <span style="color: #8c8c8c; font-style: italic;">// Saat iklan ditutup<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>loadRewarded();<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan ditutup"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> } <span style="color: #0033b3;">else </span>{<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan null"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">loadRewarded</span>() {<br /> <span style="color: black;">RewardedAd</span>.<span style="font-style: italic;">load</span>(<span style="color: #0033b3;">this</span>, <span style="color: #067d17;">"ca-app-pub-3940256099942544/5224354917"</span>,<br /> <span style="color: #0033b3;">new </span><span style="color: black;">AdRequest</span>.Builder().build(), <span style="color: #0033b3;">new </span><span style="color: black;">RewardedAdLoadCallback</span>(){<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdFailedToLoad</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">LoadAdError </span>loadAdError) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Iklan gagal dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">rewardedAd </span>= <span style="color: #0033b3;">null</span>;<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan gagal dimuat"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onAdLoaded</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">RewardedAd </span>reward) {<br /> <span style="color: #8c8c8c; font-style: italic;">// Iklan berhasil dimuat<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #871094;">rewardedAd </span>= reward;<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #067d17;">"Iklan berhasil dimuat"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/VfZu5t00tLI" width="320" youtube-src-id="VfZu5t00tLI"></iframe></div><br /><div><br /></div><div>Baik, terimakasih atas kunjungan dan waktunya. Jika ada pertanyaan silahkan diposting dikolom komentar.</div></span>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com2tag:blogger.com,1999:blog-7303454535718500704.post-9493567774722403672020-12-18T13:57:00.002+07:002021-02-22T18:41:59.936+07:00Cara Mendapatkan Informasi Sistem Dari Perangkat Android<span style="font-family: georgia;">Tak jarang pada sebuah aplikasi yang kita gunakan kita mendapati informasi mengenai perangkat Android yang kita gunakan, misalnya merek, pembuat, model, versi android, dan lain sebagainya dari perangkat android kita.<span><a name='more'></a></span></span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Pada kesempatan ini gwnbs akan berbagi mengenai cara bagaimana kodingan untuk mendapatkan informasi tersebut, caranya lumayan sederhana dan mudah, tentunya bagi Anda yang belum tahu caranya ya, kalau sudah tahu berarti Anda sudah tahu dan tidak perlu dilanjutkan.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQJNaJH7D6F8-C6D0nX4aWAU_FEd3KVC-xGGf7qQ-Tbkiz_iKtKqd4snnNR0VQF5o9iIfBwDhkEpCwWFRffyZp-nhNjAj7HIa8kJ81Bdy0_l7cf2Q1_duXWZ-tqM757I_PaMbl5Sff9uY/s1920/Screenshot_20201218-134748.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Cara mendapatkan informasi perangkat android" border="0" data-original-height="1920" data-original-width="1080" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQJNaJH7D6F8-C6D0nX4aWAU_FEd3KVC-xGGf7qQ-Tbkiz_iKtKqd4snnNR0VQF5o9iIfBwDhkEpCwWFRffyZp-nhNjAj7HIa8kJ81Bdy0_l7cf2Q1_duXWZ-tqM757I_PaMbl5Sff9uY/w180-h320/Screenshot_20201218-134748.jpg" title="System Information" width="180" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Gambar 1 : Memperlihatkan informasi dari perangkat</td></tr></tbody></table><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Disini kita akan menampilkan beberapa informasi mengenai perangkat Android yang menggunakan aplikasi kita. Misalnya di </span><b style="font-family: georgia;">activity_main.xml</b><span style="font-family: georgia;"> kita memiliki sebuah tombol yang akan memunculkan informasi mengenai perangkat melalui dialog.</span></div><div><span style="font-family: georgia;"><br /><b>activity_main.xml</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/buttonInfo"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Show Information"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintLeft_toLeftOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintRight_toRightOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent" </span>/><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Karena kita disini memunculkan informasi melalui <b>AlertDialog </b>custom, maka kita membutuhkan layout untuk di inflate. Berikut layout custom untuk alert dialog tersebut, dengan nama <b>layout_dialog.xml</b>.<br /><br /><b>layout_dialog.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Device Information"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textDeviceInformation"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textCloseDialog"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_gravity</span><span style="color: #067d17;">="end"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingTop</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Close"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textAllCaps</span><span style="color: #067d17;">="true"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/teal_700"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="22sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Di dalam layout diatas kita memiliki 3 <b>TextView</b>. TextView pertama hanya sebagai judul dari dialog, TextView kedua yang nantinya akan kita setel nilainya untuk menampilkan informasi mengenai perangkat, dan TextView terakhir sebagai tombol untuk menutup dialog.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Pada contoh ini kita akan menyetel nilai TextView informasi perangkat menggunakan mode <b>HTML </b>supaya warna teks dapat di kustominasi. Namun juga nantintya akan diberikan penyetelan TextView secara normal.</span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><div><span style="font-family: georgia;">Untuk mendapatkan informasi, kita memanggil kelas <b>Build </b>diikuti nama dari informasi yang diinginkan. Misalnya : Build.VERSION.<i><span style="color: #800180;">SDK_INT</span></i> untuk mendapatkan informasi mengenai versi SDK dari perangkat Android.<br /><br /><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.systeminformation</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.graphics.drawable.ColorDrawable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Build</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AlertDialog</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.core.text.HtmlCompat</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//metode klik tombol untuk memunculkan dialog<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">buttonInfo</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> dialogDeviceInformation();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//Memunculkan dialog<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">dialogDeviceInformation</span>() {<br /> <span style="color: black;">AlertDialog</span>.<span style="color: black;">Builder builder </span>= <span style="color: #0033b3;">new </span><span style="color: black;">AlertDialog</span>.Builder(<span style="color: #0033b3;">this</span>);<br /> <span style="color: black;">LayoutInflater inflater </span>= <span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(<span style="color: #0033b3;">this</span>);<br /> <span style="color: black;">View view </span>= <span style="color: black;">inflater</span>.inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">layout_dialog</span>, <span style="color: #0033b3;">null</span>);<br /> <span style="color: black;">TextView textDeviceInformation </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textDeviceInformation</span>);<br /> <span style="color: black;">builder</span>.setView(<span style="color: black;">view</span>);<br /><span style="color: #8c8c8c; font-style: italic;"><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span>setTextToDeviceInformation(<span style="color: black;">textDeviceInformation</span>);<br /><br /> <span style="color: #0033b3;">final </span><span style="color: black;">AlertDialog dialogInformation </span>= <span style="color: black;">builder</span>.create();<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">dialogInformation</span>.getWindow() !=<span style="color: #0033b3;">null</span>) {<br /> <span style="color: black;">dialogInformation</span>.getWindow().setBackgroundDrawable(<span style="color: #0033b3;">new </span>ColorDrawable(<span style="color: #1750eb;">0</span>));<br /> }<br /><br /> <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textCloseDialog</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> <span style="color: #851691;">dialogInformation</span>.dismiss();<br /> }<br /> });<br /><br /> <span style="color: black;">dialogInformation</span>.show();<br /> }<br /><br /> <span style="color: #8c8c8c; font-style: italic;">//metode private menyetel nilai textview dengan mode HTML<br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;">private void </span><span style="color: #00627a;">setTextToDeviceInformation</span>(<span style="color: black;">TextView </span>textDeviceInformation) {<br /> textDeviceInformation.setText(<span style="color: black;">HtmlCompat</span>.<span style="font-style: italic;">fromHtml</span>(<span style="color: #067d17;">"<font color=green>Model</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">MODEL </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>ID</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">ID </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Manufacturer</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">MANUFACTURER </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Brand</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">BRAND </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Type</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">TYPE </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>User</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">USER </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Base</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION_CODES</span>.<span style="color: #871094; font-style: italic;">BASE </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Incremental</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION</span>.<span style="color: #871094; font-style: italic;">INCREMENTAL </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Android SDK</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION</span>.<span style="color: #871094; font-style: italic;">SDK_INT </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Board</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">BOARD </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Host</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">HOST </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Fingerprint</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">FINGERPRINT </span>+<br /> <span style="color: #067d17;">"<br/><font color=green>Android Version</font> : " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION</span>.<span style="color: #871094; font-style: italic;">RELEASE</span>,<br /> <span style="color: black;">HtmlCompat</span>.<span style="color: #871094; font-style: italic;">FROM_HTML_MODE_LEGACY</span>));<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Jika Anda ingin menggunakan mode menyetel teks secara biasa, silahkan gantikan metode private menyetel nilai string TextView diatas menjadi kode dibawah seperti dibawah ini :<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">private void </span><span style="color: #00627a;">setTextToDeviceInformation</span>(<span style="color: black;">TextView </span>textDeviceInformation) {<br /> textDeviceInformation.setText(<span style="color: #067d17;">"Model : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">MODEL </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"ID : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">ID </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Manufacturer : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">MANUFACTURER </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Brand : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">BRAND </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Type : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">TYPE </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"User : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">USER </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Base : " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION_CODES</span>.<span style="color: #871094; font-style: italic;">BASE </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Incremental : " </span>+ <span style="color: #067d17;">" " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION</span>.<span style="color: #871094; font-style: italic;">INCREMENTAL </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Android SDK : " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION</span>.<span style="color: #871094; font-style: italic;">SDK_INT </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Board : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">BOARD </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Host : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">HOST </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Fingerprint : " </span>+ <span style="color: black;">Build</span>.<span style="color: #871094; font-style: italic;">FINGERPRINT </span>+ <span style="color: #067d17;">"</span><span style="color: #0037a6;">\n</span><span style="color: #067d17;">" </span>+<br /> <span style="color: #067d17;">"Android Version : " </span>+ <span style="color: black;">Build</span>.<span style="color: black;">VERSION</span>.<span style="color: #871094; font-style: italic;">RELEASE</span>);<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Informasi diatas hanya sebagian, masih banyak lagi informasi yang bisa kita dapatkan. Baik, sekian informasi sederhana ini. Terimakasih untuk waktu dan kunjungannya, jika ada pertanyaan silahkan diposting dikomentar.</span></div></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-27508641838871488312020-12-17T18:56:00.002+07:002021-02-22T18:42:16.696+07:00Contoh Sederhana Dalam Menerapkan Fragment<p><span style="font-family: georgia;"><i>Sebuah <b>Fragment </b>mewakili bagian yang dapat digunakan kembali dari antarmuka aplikasi. Fragment mendefinisikan dan mengelola tata letaknya sendiri, memiliki siklus prosesnya sendiri, dan dapat menangani peristiwa masukannya sendiri. Fragment tidak dapat hidup sendiri melainkan harus dihosting oleh aktivitas atau fragment lain. Hierarki tampilan fragment menjadi bagian dari, atau dilampirkan ke hierarki tampilan host.<span></span></i></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;">Dalam tutorial ini akan dicontohkan penerapannya secara sederhana. Kita akan memiliki 4 Fragment dalam sebuah activity. Disini kita memiliki komponen atau view yang sama pada setiap layout fragment, sehingga kita tidak perlu membuat layout untuk masing-masing fragment, cukup satu saja.</span></p><p><span style="font-family: georgia;"><br /></span></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/q--whH17pxc" width="320" youtube-src-id="q--whH17pxc"></iframe></div><br /><p><span style="font-family: georgia;">Tentunya mari memulai dengan membuat sebuah project baru, dalam contoh ini kita menggunakan tema </span><b style="font-family: georgia;"><i>NoActionBar</i></b><span style="font-family: georgia;">. Pada </span><b style="font-family: georgia;">activity_main.xml</b><span style="font-family: georgia;">, akan terdapat sebuah tombol yang berupa ImageView yang nantinya akan menampilkan </span><b style="font-family: georgia;">PopUpMenu </b><span style="font-family: georgia;">untuk navigasi antar fragment.</span></p><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="8611345975" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;"><b>activity_main.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">LinearLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutHeader"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/teal_700"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textTitle"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="0dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_weight</span><span style="color: #067d17;">="1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Android Fragments"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp" </span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageMenu"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="30dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="?selectableItemBackground"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:src</span><span style="color: #067d17;">="@drawable/ic_menu"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:tint</span><span style="color: #067d17;">="@color/white" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> <<span style="color: #0033b3;">FrameLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutFragment"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent" </span>/><br /><br /></<span style="color: #0033b3;">LinearLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Pada layout diatas, kita memiliki sebuah <b>Framelayout </b>dengan id nya <i>layoutFragment</i>. Framelayout tersebut nantinya yang akan digantikan oleh fragment. Saat kita bernavigasi ke fragment 1, maka layout fragment 1 akan menggantikan <b>Framelayout </b>tersebut, begitupun untuk ketiga fragment lainnya.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, sekarang kita persiapkan sebuah layout untuk ke-empat fragment tersebut. Disini kita buat dengan nama <b>fragment_layout.xml</b>, yang mana berisi 3 buah komponen view : TextView, ImageView, dan Button. Seperti yang sudah dikatakan diatas, karena kita memiliki view yang sama pada semua fragment sehingga kita tidak perlu membuat layout baru untuk setiap fragment, cukup satu untuk semua.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Namun seandainya kita akan memiliki view yang berbeda atau view dengan jumlah yang berbeda pada setiap fragment, tentunya kita perlu membuat layout nya berbeda-beda juga, atau bisa saja dengan layout yang sama namun menambahkan view secara terprogram melalui file java class dari fragment. Tentunya akan lebih sedikit merepotkan daripada membuat layout berbeda.<br /><br /><b>fragment_layout.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">LinearLayout </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="20dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/textView"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Contoh Fragment"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="25sp" </span>/><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/imageView"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name" </span>/><br /><br /> <<span style="color: #0033b3;">Button<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/button"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="sans-serif-black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Button Click"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/black"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp" </span>/><br /><br /></<span style="color: #0033b3;">LinearLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Selanjutnya kita membuat 4 file java class yang memperluas (extends) kelas Fragment. Disini kita buat dengan nama : <b>Fragment1</b>, <b>Fragment2</b>, <b>Fragment3</b>, dan <b>Fragment4</b>. Pada sebuah activity kita menggunakan argumen <i>setContentView </i>untuk menautkan konteks dengan layout, namum pada fragment kita menggunakan object <i>View </i>dengan <i>LayoutInflater </i>untuk menautkan ke layout, sama seperti kelas adapter ataupun kelas lainnya jika ingin menginflate layout.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Untuk keempat Fragment diatas isi kodingannya sama, disini kita hanya merubah nilai dari ketiga komponen view nya. Berikut ke-empat <b>Fragment </b>tersebut :<br /><br /><b>Fragment1 </b>:<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.contohfragment</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Button</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Nullable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.fragment.app.Fragment</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">Fragment1 </span><span style="color: #0033b3;">extends </span><span style="color: black;">Fragment </span>{<br /><br /> <span style="color: #9e880d;">@Nullable<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">View </span><span style="color: #00627a;">onCreateView</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">LayoutInflater </span>inflater, <span style="color: #9e880d;">@Nullable </span><span style="color: black;">ViewGroup </span>container,<br /> <span style="color: #9e880d;">@Nullable </span><span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: black;">View view </span>= inflater.inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">fragment_layout</span>, container, <span style="color: #0033b3;">false</span>);<br /><br /> <span style="color: black;">TextView textView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textView</span>);<br /> <span style="color: black;">Button button </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">button</span>);<br /> <span style="color: black;">ImageView imageView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageView</span>);<br /><br /> <span style="color: black;">imageView</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_1</span>);<br /> <span style="color: black;">textView</span>.setText(<span style="color: #067d17;">"Ini adalah fragment 1"</span>);<br /> <span style="color: black;">button</span>.setText(<span style="color: #067d17;">"Click 1"</span>);<br /><br /> <span style="color: black;">button</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(requireActivity(), <span style="color: #067d17;">"You're on Fragment 1"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> <span style="color: #0033b3;">return </span><span style="color: black;">view</span>;<br /> }<br />}<br /></pre></div><br /><b>Fragment2 </b>:<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.contohfragment</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Button</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Nullable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.fragment.app.Fragment</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">Fragment2 </span><span style="color: #0033b3;">extends </span><span style="color: black;">Fragment </span>{<br /><br /> <span style="color: #9e880d;">@Nullable<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">View </span><span style="color: #00627a;">onCreateView</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">LayoutInflater </span>inflater, <span style="color: #9e880d;">@Nullable </span><span style="color: black;">ViewGroup </span>container,<br /> <span style="color: #9e880d;">@Nullable </span><span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: black;">View view </span>= inflater.inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">fragment_layout</span>, container, <span style="color: #0033b3;">false</span>);<br /><br /> <span style="color: black;">TextView textView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textView</span>);<br /> <span style="color: black;">Button button </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">button</span>);<br /> <span style="color: black;">ImageView imageView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageView</span>);<br /><br /> <span style="color: black;">imageView</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_2</span>);<br /> <span style="color: black;">textView</span>.setText(<span style="color: #067d17;">"Ini adalah fragment 2"</span>);<br /> <span style="color: black;">button</span>.setText(<span style="color: #067d17;">"Click 2"</span>);<br /><br /> <span style="color: black;">button</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(requireActivity(), <span style="color: #067d17;">"You're on Fragment 2"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> <span style="color: #0033b3;">return </span><span style="color: black;">view</span>;<br /> }<br />}<br /></pre></div><br /><b>Fragment3 </b>:<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.contohfragment</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Button</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Nullable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.fragment.app.Fragment</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">Fragment3 </span><span style="color: #0033b3;">extends </span><span style="color: black;">Fragment </span>{<br /><br /> <span style="color: #9e880d;">@Nullable<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">View </span><span style="color: #00627a;">onCreateView</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">LayoutInflater </span>inflater, <span style="color: #9e880d;">@Nullable </span><span style="color: black;">ViewGroup </span>container,<br /> <span style="color: #9e880d;">@Nullable </span><span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: black;">View view </span>= inflater.inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">fragment_layout</span>, container, <span style="color: #0033b3;">false</span>);<br /><br /> <span style="color: black;">TextView textView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textView</span>);<br /> <span style="color: black;">Button button </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">button</span>);<br /> <span style="color: black;">ImageView imageView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageView</span>);<br /><br /> <span style="color: black;">imageView</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_3</span>);<br /> <span style="color: black;">textView</span>.setText(<span style="color: #067d17;">"Ini adalah fragment 3"</span>);<br /> <span style="color: black;">button</span>.setText(<span style="color: #067d17;">"Click 3"</span>);<br /><br /> <span style="color: black;">button</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(requireActivity(), <span style="color: #067d17;">"You're on Fragment 3"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> <span style="color: #0033b3;">return </span><span style="color: black;">view</span>;<br /> }<br />}<br /></pre></div><br /><b>Fragment4 </b>:<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.contohfragment</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Button</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.Toast</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">Nullable</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.fragment.app.Fragment</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">Fragment4 </span><span style="color: #0033b3;">extends </span><span style="color: black;">Fragment </span>{<br /><br /> <span style="color: #9e880d;">@Nullable<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">View </span><span style="color: #00627a;">onCreateView</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">LayoutInflater </span>inflater, <span style="color: #9e880d;">@Nullable </span><span style="color: black;">ViewGroup </span>container,<br /> <span style="color: #9e880d;">@Nullable </span><span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: black;">View view </span>= inflater.inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">fragment_layout</span>, container, <span style="color: #0033b3;">false</span>);<br /><br /> <span style="color: black;">TextView textView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textView</span>);<br /> <span style="color: black;">Button button </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">button</span>);<br /> <span style="color: black;">ImageView imageView </span>= <span style="color: black;">view</span>.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageView</span>);<br /><br /> <span style="color: black;">imageView</span>.setImageResource(<span style="color: black;">R</span>.<span style="color: black;">drawable</span>.<span style="color: #871094; font-style: italic;">ic_4</span>);<br /> <span style="color: black;">textView</span>.setText(<span style="color: #067d17;">"Ini adalah fragment 4"</span>);<br /> <span style="color: black;">button</span>.setText(<span style="color: #067d17;">"Click 4"</span>);<br /><br /> <span style="color: black;">button</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> <span style="color: black;">Toast</span>.<span style="font-style: italic;">makeText</span>(requireActivity(), <span style="color: #067d17;">"You're on Fragment 4"</span>, <span style="color: black;">Toast</span>.<span style="color: #871094; font-style: italic;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> <span style="color: #0033b3;">return </span><span style="color: black;">view</span>;<br /> }<br />}<br /></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Selanjutnya kita membuat file resource <b>menu</b>, pada contoh ini yaitu <b>menu_fragment.xml</b>, untuk di inflate nantinya menggunakan <b>PopUpMenu</b>, yang berisi 4 item untuk bernavigasi antar fragment. Jika Anda belum tahu cara membuat resource menu, caranya klik kanan pada folder <i><b>res > New > Android Resource File</b></i>, kemudian pada resource type pilih menu. <br /><br /><b>menu_fragment.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">menu </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"</span>><br /><br /> <<span style="color: #0033b3;">item<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/menuFragment1"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:title</span><span style="color: #067d17;">="Fragment 1"</span>/><br /><br /> <<span style="color: #0033b3;">item<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/menuFragment2"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:title</span><span style="color: #067d17;">="Fragment 2"</span>/><br /><br /> <<span style="color: #0033b3;">item<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/menuFragment3"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:title</span><span style="color: #067d17;">="Fragment 3"</span>/><br /><br /> <<span style="color: #0033b3;">item<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/menuFragment4"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:title</span><span style="color: #067d17;">="Fragment 4"</span>/><br /><br /></<span style="color: #0033b3;">menu</span>></pre></div></span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Terakhir di <b>MainActivity.java</b>, pertama-tama kita buat sebuah metode private untuk berganti dari satu fragment ke fragment lainnya. Di dalam metode ini kita berikan 2 argumen yaitu Fragment dan String. Saat berganti fragment, disini kita juga merubah nilai string dari <b>TextView </b>pada actionBar, sehingga kita menggunakan argumen String supaya mudah. Kelas yang diperlukan untuk dapat berganti antar fragment yaitu <i>FragmentManager </i>dan <i>FragmentTransaction</i>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kemudian didalam onCreate kita menentukan metode klik listener untuk tombol yang ada di actionBar, yang mana didalam metode klik tersebut kita memanggil <b>PopUpMenu</b>, lalu menentukan kondisi berdasarkan <i>id </i>dari item PopUpMenu tersebut. Di setiap kondisi kita memanggil metode private yang sudah dibuat sebelumnya dengan <b>Fragment </b>(Fragment1, Fragment2, Fragment3, dan Fragment4) dan nilai String yang berbeda.</span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;"><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.contohfragment</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.fragment.app.Fragment</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.fragment.app.FragmentManager</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.fragment.app.FragmentTransaction</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.MenuItem</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.PopupMenu</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #0033b3;">private </span><span style="color: black;">TextView </span><span style="color: #871094;">textTitle</span>;<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> <span style="color: #871094;">textTitle </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">textTitle</span>);<br /><br /> findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">imageMenu</span>).setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> <span style="color: black;">PopupMenu popupMenu </span>= <span style="color: #0033b3;">new </span>PopupMenu(<span style="color: black;">MainActivity</span>.<span style="color: #0033b3;">this</span>, view);<br /> <span style="color: black;">popupMenu</span>.inflate(<span style="color: black;">R</span>.<span style="color: black;">menu</span>.<span style="color: #871094; font-style: italic;">menu_fragment</span>);<br /> <span style="color: black;">popupMenu</span>.setOnMenuItemClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">PopupMenu</span>.<span style="color: black;">OnMenuItemClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public boolean </span><span style="color: #00627a;">onMenuItemClick</span>(<span style="color: black;">MenuItem </span>menuItem) {<br /> <span style="color: #0033b3;">int </span><span style="color: black;">id </span>= menuItem.getItemId();<br /> <span style="color: #0033b3;">if </span>(<span style="color: black;">id </span>== <span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">menuFragment1</span>) {<br /> replaceFragment(<span style="color: #0033b3;">new </span>Fragment1(), <span style="color: #067d17;">"Fragment 1"</span>);<br /> } <span style="color: #0033b3;">else if </span>(<span style="color: black;">id </span>== <span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">menuFragment2</span>) {<br /> replaceFragment(<span style="color: #0033b3;">new </span>Fragment2(), <span style="color: #067d17;">"Fragment 2"</span>);<br /> } <span style="color: #0033b3;">else if </span>(<span style="color: black;">id </span>== <span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">menuFragment3</span>) {<br /> replaceFragment(<span style="color: #0033b3;">new </span>Fragment3(), <span style="color: #067d17;">"Fragment 3"</span>);<br /> } <span style="color: #0033b3;">else </span>{<br /> replaceFragment(<span style="color: #0033b3;">new </span>Fragment4(), <span style="color: #067d17;">"Fragment 4"</span>);<br /> }<br /> <span style="color: #0033b3;">return true</span>;<br /> }<br /> });<br /> <span style="color: black;">popupMenu</span>.show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">replaceFragment</span>(<span style="color: black;">Fragment </span>fragment, <span style="color: black;">String </span>title) {<br /> <span style="color: black;">FragmentManager manager </span>= getSupportFragmentManager();<br /> <span style="color: black;">FragmentTransaction transaction </span>= <span style="color: black;">manager</span>.beginTransaction();<br /> <span style="color: black;">transaction</span>.replace(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">layoutFragment</span>, fragment);<br /> <span style="color: black;">transaction</span>.setTransition(<span style="color: black;">FragmentTransaction</span>.<span style="color: #871094; font-style: italic;">TRANSIT_FRAGMENT_OPEN</span>);<br /> <span style="color: black;">transaction</span>.commit();<br /><br /> <span style="color: #871094;">textTitle</span>.setText(title);<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Untuk gambar-gambar yang ada dalam project contoh ini, semua dibuat melalui Vector Asset.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, sekian untuk tutorial ini. Terimakasih atas waktu dan kunjungannya, jika ada kekurangan dan kesalahan mohon dimaafkan, jika ada yang ingin disampaikan silahkan diposting dikomentar.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-33330418575017571782020-12-12T17:39:00.004+07:002021-02-22T18:42:33.804+07:00Contoh RecyclerView Dengan Orientasi Horizontal<p><span style="font-family: georgia;">Tutorial kali ini akan mencontohkan penerapan <b>RecyclerView </b>dengan orientasi dan scroll <b>horizontal </b>(mendatar). Disini kita akan memiliki 3 buah komponen RecyclerView dalam satu layout (<b>activity_main.xml</b>) yang dalam contoh ini adalah kategori film. RecyclerView pertama untuk kategori <i>Film Terlaris</i>, RecyclerView kedua untuk kategori <i>Film Terbaru</i> dan RecyclerView ketiga untuk kategori <i>Film Indonesia</i>.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;"><br /></span></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/8mUdu0zCYvg" width="320" youtube-src-id="8mUdu0zCYvg"></iframe></div><br /><p><span style="font-family: georgia;">Setiap item pada RecyclerView terdapat 2 buah view yaitu ImageView untuk poster dari film, dan TextView untuk judul film. Kemudian untuk klik event pada item akan membawa ke halaman details dari film yakni </span><b style="font-family: georgia;">DetailsActivity.java</b><span style="font-family: georgia;">. Details berisi 3 view yaitu poster film, judul film, dan deskripsi film, sehingga untuk model data getter dan konstruktor nya kita memerlukan 3 object dan semuanya adalah </span><b style="font-family: georgia;">String</b><span style="font-family: georgia;">.</span></p><p><span style="font-family: georgia;">Loh kok gambar pakai <b>String</b>, bukan <b>int</b>? Karena disini kita akan memuat gambarnya menggunakan sambungan Internet dengan bantuan library <b>Picasso</b>, jadi tidak perlu memasukkan file gambar ke project. Link gambar diambil dari situs <a href="https://www.themoviedb.org/" target="_blank">The Movie Database</a>. Berikut contoh link gambar dari situs tersebut : <a href="https://image.tmdb.org/t/p/w220_and_h330_face/eitRZXfbw6rO0CfP3lPaGgK63qr.jpg" target="_blank">Klik disini</a>.</span></p><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Dari contoh link tersebut, karakter-karakter setelah simbol slash terakhir dengan ekstensi .jpg yang akan kita input saat menentukan data item link gambar dari film. Sedangkan karakter-karakter yang berada sebelum simbol slash terakhir akan kita inisialisai sehingga tidak perlu berulang-ulang memasukkan link yang sama pada setiap item nantinya.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Lalu untuk details dari film (<b>DetailsActivity.java</b>), data-data dari film (link gambar, judul, deskripsi) akan dikirimkan menggunakan intent yang penerapannya dilakukan di dalam file adapter dari RecyclerView. Deskripsi film kita tempatkan di resource <b>strings.xml</b>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, mari kita mulai dari mengimport library <b>RecyclerView </b>dan <b>Picasso</b>. Jika Anda menggunkan <b>Android Studio</b> 4.1.1 atau versi yang lebih baru, library RecyclerView sudah otomatis ada dalam project. Masukkan kedua library dibawah ini ke dalam dependencies <b>build.gradle(Module: app)</b>, kemudian sync project (jangan lupa internet on).<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;">implementation <span style="color: #067d17;">"androidx.recyclerview:recyclerview:1.1.0"<br /></span>implementation <span style="color: #067d17;">'com.squareup.picasso:picasso:2.71828'</span></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Sebelum melangkah ke layout/konteks, buat terlebih dahulu 2 Drawable Resource File yang akan digunakan sebagai background dari layout nya nanti. Bagi Anda yang belum tahu akses membuatnya, klik kanan pada folder <b><i>drawable > New > Drawable Resource File</i></b>, isikan nama seperti dibawah ini. Berikut kedua background Drawable tersebut :<br /><br /><b>bg_rv.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="8dp"</span>/><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/blackishLight"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div><br /><b>bg_container.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">shape </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:shape</span><span style="color: #067d17;">="rectangle"</span>><br /><br /> <<span style="color: #0033b3;">corners </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:radius</span><span style="color: #067d17;">="8dp"</span>/><br /> <<span style="color: #0033b3;">solid </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:color</span><span style="color: #067d17;">="@color/black"</span>/><br /><br /></<span style="color: #0033b3;">shape</span>></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Kedua file diatas hanya berbeda pada warna saja, selebihnya sama. Dan berikut untuk resource value yakni <b>colors.xml</b>, <b>strings.xml</b> dan <b>themes.xml</b>. Pada Android Studio 4.1.1 ini, nama default resource tema bukan lagi<b> styles.xml</b>, melainkan <b>themes.xml</b>. Seperti yang sudah disampaikan diatas, untuk deskripsi dari film kita tempatkan di resource value strings.xml.<br /><br /><b>colors.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">resources</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="purple_200"</span>>#FFBB86FC</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="purple_500"</span>>#FF6200EE</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="purple_700"</span>>#FF3700B3</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="teal_200"</span>>#FF03DAC5</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="teal_700"</span>>#FF018786</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="black"</span>>#FF000000</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="blackishRed"</span>>#323030</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="white"</span>>#FFFFFFFF</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="whiteDark"</span>>#ECE8E8</<span style="color: #0033b3;">color</span>><br /> <<span style="color: #0033b3;">color </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="blackishLight"</span>>#3D3D3D</<span style="color: #0033b3;">color</span>><br /></<span style="color: #0033b3;">resources</span>></pre></div><br /><b>strings.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 300px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">resources</span>><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="app_name"</span>>Horizontal RecyclerView</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="avengers_endgame"</span>>Avengers: Endgame adalah film pahlawan super Amerika 2019 yang didasarkan pada tim<br /> superhero Avengers dari Marvel Comics, diproduksi oleh Marvel Studios dan didistribusikan oleh Walt Disney Studios<br /> Motion Pictures. Ini adalah sekuel The Avengers 2012, film 2015 Avengers: Age of Ultron dan film 2018<br /> Avengers: Infinity War, dan film kedua puluh dua di Marvel Cinematic Universe (MCU).\n\nFilm ini disutradarai oleh<br /> Anthony dan Joe Russo, yang ditulis oleh Christopher Markus dan Stephen McFeely, dan menampilkan pemeran ansambel<br /> termasuk Robert Downey Jr, Chris Evans, Mark Ruffalo, Chris Hemsworth, Scarlett Johansson, Jeremy Renner,<br /> Don Cheadle, Don Cheadle, Paul Rudd, Brie Larson, Karen Gillan, Danai Gurira, Benedict Wong, Jon Favreau,<br /> Bradley Cooper, Gwyneth Paltrow, dan Josh Brolin. Dalam film itu, anggota Avengers yang masih hidup dan sekutu<br /> mereka berusaha untuk membalikkan kerusakan yang disebabkan oleh Thanos dalam Infinity War.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="avatar"</span>>Avatar, dikenal sebagai James Cameron\'s Avatar, adalah film fiksi ilmiah petualangan<br /> Amerika Serikat tahun 2009 yang disutradarai oleh James Cameron dan diproduseri oleh James Cameron dan Jon Landau.<br /> \n\nNaskah film ini ditulis oleh James Cameron. Film ini dibintangi oleh Sam Worthington, Zoe Saldana, Stephen Lang,<br /> Michelle Rodriguez dan Sigourney Weaver.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="titanic"</span>>Titanic adalah sebuah film epik, roman, dan bencana Amerika Serikat produksi tahun 1997 yang<br /> diskenarioi sekaligus disutradarai oleh James Cameron. \n\nFilm ini bercerita tentang kisah cinta antara Jack dan Rose<br /> (diperankan oleh Leonardo DiCaprio dan Kate Winslet) yang berasal dari status sosial berbeda di atas kapal<br /> RMS Titanic yang tenggelam dalam pelayaran perdananya pada tanggal 15 April 1912.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="star_wars"</span>>Star Wars: The Force Awakens adalah sebuah film opera luar angkasa Amerika Serikat yang<br /> disutradarai oleh J. J. Abrams. \n\nFilm ini merupakan yang ketujuh dari serial film Star Wars dan dibintangi oleh<br /> Harrison Ford, Mark Hamill, Carrie Fisher, Adam Driver, Daisy Ridley, John Boyega, Oscar Isaac, Lupita Nyong\'o,<br /> Andy Serkis, Domhnall Gleeson, Anthony Daniels, Peter Mayhew, dan Max von Sydow. Kisah cerita film ini terjadi<br /> sekitar 30 tahun setelah peristiwa di dalam film Return of the Jedi (1983).</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="avengers_infinity_war"</span>>Avengers: Infinity War adalah film pahlawan super Amerika 2018 yang didasarkan<br /> pada tim superhero Marvel Comics the Avengers, diproduksi oleh Marvel Studios dan didistribusikan oleh<br /> Walt Disney Studios Motion Pictures. Ini adalah sekuel The Avengers 2012 dan Avengers: Age of Ultron 2015,<br /> dan film kesembilan belas di Marvel Cinematic Universe (MCU). \n\nFilm ini disutradarai oleh Anthony dan Joe Russo,<br /> ditulis oleh Christopher Markus dan Stephen McFeely, dan menampilkan pemeran ansambel termasuk Robert Downey Jr,<br /> Chris Hemsworth, Mark Ruffalo, Chris Evans, Scarlett Johansson, Benedict Cumberbatch, Don Cheadle, Tom Holland,<br /> Chadwick Boseman, Paul Bettany, Elizabeth Olsen, Anthony Mackie, Sebastian Stan, Danai Gurira, Letitia Wright,<br /> Dave Bautista, Zoe Saldana, Josh Brolin, dan Chris Pratt. Dalam film tersebut, Avengers dan Guardians of the<br /> Galaxy berupaya mencegah Thanos dari mengumpulkan enam Batu Infinity yang sangat kuat sebagai bagian dari<br /> upayanya untuk membunuh setengah dari seluruh kehidupan di alam semesta. \n\nFilm ini diumumkan pada Oktober 2014<br /> sebagai Avengers: Infinity War - part 1. The Russo brother datang untuk mengarahkan pada April 2015, dan sebulan<br /> kemudian Markus dan McFeely menandatangani untuk menulis naskah untuk film tersebut, yang mendapat inspirasi<br /> dari Buku komik Jim Starlin 1991, The Infinity Gauntlet, dan komik-komik Jonathan Hickman 2013, Infinity. \n\nPada 2016,<br /> Marvel memperpendek gelar menjadi Avengers: Infinity War. Pembuatan film dimulai pada Januari 2017 di<br /> Pinewood Atlanta Studios di Fayette County, Georgia, dengan pemain besar yang sebagian besar terdiri dari aktor<br /> yang mengulang peran mereka dari film MCU sebelumnya, termasuk Brolin sebagai Thanos. Dengan perkiraan<br /> anggaran $ 316-400 juta, ini adalah salah satu film termahal yang pernah dibuat.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="jurassic_world"</span>>Jurassic World adalah sebuah film petualangan fiksi ilmiah 3D dari Amerika Serikat<br /> yang disutradarai oleh Colin Trevorrow. \n\nFilm ini merupakan film keempat dari serial film Jurassic Park yang<br /> dimulai tahun 1993 dan dibuat oleh Steven Spielberg dan kali ini, Steven menjadi produser film ini.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="the_lion_king"</span>>The Lion King adalah sebuah film petualangan drama Amerika Serikat yang diproduksi oleh<br /> Walt Disney Pictures, ditulis oleh Jeff Nathanson, dan disutradarai oleh Jon Favreau. Film tersebut adalah sebuah<br /> remake CGI fotorealistis dari film tahun 1994 bernama sama yang dianimasikan secara tradisional karya Disney.<br /> \n\nPara pengisi suaranya adalah Donald Glover, Seth Rogen, Chiwetel Ejiofor, Billy Eichner, John Oliver,<br /> Keegan-Michael Key, Beyoncé Knowles-Carter, dan James Earl Jones.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="jiu_jitsu"</span>>Every six years, an ancient order of jiu-jitsu fighters joins forces to battle a vicious<br /> race of alien invaders. But when a celebrated war hero goes down in defeat, the fate of the planet and mankind<br /> hangs in the balance.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="fatman"</span>>A rowdy, unorthodox Santa Claus is fighting to save his declining business. Meanwhile, Billy,<br /> a neglected and precocious 12 year old, hires a hit man to kill Santa after receiving a lump of<br /> coal in his stocking.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="the_dalton_gang"</span>>When their brother Frank is killed by an outlaw, brothers Bob Dalton, Emmett Dalton<br /> and Gray Dalton join their local sheriff\'s department. When they are cheated by the law, they turn to crime,<br /> robbing trains and anything else they can steal from over the course of two years in the early 1890\'s.<br /> Trying to out do Jesse James, they attempt to rob two banks at once in October of 1892, and things get ugly.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="tenet"</span>>Armed with only one word - Tenet - and fighting for the survival of the entire world,<br /> the Protagonist journeys through a twilight world of international espionage on a mission that will unfold in<br /> something beyond real time.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="rogue_city"</span>>Caught in the crosshairs of police corruption and Marseille’s warring gangs, a loyal<br /> cop must protect his squad by taking matters into his own hands.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="greenland"</span>>John Garrity, his estranged wife and their young son embark on a perilous journey to<br /> find sanctuary as a planet-killing comet hurtles toward Earth. Amid terrifying accounts of cities getting levelled,<br /> the Garrity\'s experience the best and worst in humanity. As the countdown to the global apocalypse approaches zero,<br /> their incredible trek culminates in a desperate and last-minute flight to a possible safe haven.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="the_raid"</span>>The Raid (sebelum diedarkan: Serbuan Maut) adalah film aksi seni bela diri dari Indonesia<br /> yang disutradarai oleh Gareth Evans dan dibintangi oleh Iko Uwais. Pertama kali dipublikasi pada Festival Film<br /> Internasional Toronto (Toronto International Film Festival, TIFF) 2011 sebagai film pembuka untuk kategori Midnight<br /> Madness, para kritikus dan penonton memuji film tersebut sebagai salah satu film aksi terbaik setelah<br /> bertahun-tahun sehingga memperoleh penghargaan The Cadillac People\'s Choice Midnight Madness Award[12].<br /> \n\nTerpilihnya film ini untuk diputar pada beberapa festival film internasional berikutnya, seperti Festival<br /> Film Internasional Dublin Jameson (Irlandia), Festival Film Glasgow (Skotlandia), Festival Film Sundance (Utah, AS),<br /> South by Southwest Film (SXSW, di Austin, Texas, AS), dan Festival Film Busan (Korea Selatan), menjadikannya<br /> sebagai film komersial produksi Indonesia pertama yang paling berhasil di tingkat dunia. \n\nFilm The Raid sebenarnya<br /> adalah ide lanjutan dari keseluruhan cerita yang diinginkan sutradara Gareth Evans. Proyek awalnya, Berandal,<br /> diumumkan tahun 2011 sebelum film "The Raid" dibuat pada tahun yang sama yaitu tahun 2011, tetapi baru dirilis<br /> pada 2014 dengan judul The Raid 2: Berandal.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="the_night_come_for_us"</span>>The Night Comes For Us (bahasa Indonesia: "Malam Datang Untuk Kita") adalah<br /> film aksi seru dari Indonesia yang disutradarai oleh Timo Tjahjanto dan dibintangi oleh Joe Taslim. Film ini tayang<br /> perdana pada Fantastic Fest pada tanggal 22 September 2018, yang kemudian ditayangkan melalui jaringan Netflix<br /> pada tanggal 19 Oktober 2018.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="headshot"</span>>Headshot adalah sebuah film laga Indonesia yang dirilis pada Desember 2016. Film ini<br /> disutradarai oleh Mo Brothers. Film ini menjadi film pertama Iko Uwais yang disutradarai oleh sutradara asal Indonesia.<br /> \n\nFilm ini mendapatkan empat nominasi pada Festival Film Indonesia 2016 dan berhasil memenangkan dua di antaranya.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="the_raid_2"</span>>The Raid 2: Berandal adalah film kejahatan seni bela diri dari Indonesia yang disutradarai oleh<br /> Gareth Evans dan dibintangi oleh Iko Uwais. Film ini adalah sekuel dari film The Raid. Film ini sebenarnya adalah<br /> proyek awal dari keseluruhan cerita The Raid yang diumumkan tahun 2011 sebelum prekuelnya, namun baru dirilis<br /> pada 2014.\n\nSetelah tayang perdana di Festival Film Sundance pada 21 Februari 2014, film The Raid 2: Berandal<br /> akhirnya diumumkan akan tayang serentak di Indonesia dan Amerika Serikat pada tanggal 28 Maret 2014. \n\nIko Uwais<br /> berperan kembali sebagai Rama, perwira pemula satuan senjata dan taktik khusus sekaligus seorang calon ayah.<br /> Selain itu film ini juga dibintangi Alex Abbad, Julie Estelle, Roy Marten, Tio Pakusadewo, Arifin Putra dan<br /> Cecep Arif Rahman. Aktor mancanegara dari Jepang seperti Ryuhei Matsuda, Kenichi Endo, dan Kazuki Kitamura juga<br /> ikut bergabung dalam film ini.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="soekarno"</span>>Soekarno adalah film drama Indonesia yang dirilis pada 11 Desember 2013. Film ini dibintangi oleh<br /> Ario Bayu dan Maudy Koesnaedi.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="ada_apa_dengan_cinta"</span>>Ada Apa dengan Cinta? adalah sebuah film percintaan Indonesia karya Rudi Soedjarwo<br /> yang dirilis pertama kali pada tanggal 7 Februari 2002 dan dibintangi Nicholas Saputra dan Dian Sastrowardoyo.<br /> \n\nPengambilan gambar dilakukan di beberapa lokasi di kecamatan Gambir, kecamatan Menteng, Kota Jakarta Pusat dan<br /> Kecamatan Kebon Jeruk.</<span style="color: #0033b3;">string</span>><br /><br /> <<span style="color: #0033b3;">string </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="dilan_1990"</span>>Dilan 1990 merupakan film drama Indonesia tahun 2018. Film ini diangkat dari novel Dilan:<br /> Dia adalah Dilanku Tahun 1990 karya Pidi Baiq dan dibintangi oleh Iqbaal Ramadhan dan Vanesha Prescilla. \n\nPara pemain<br /> pendukungnya antara lain Farhan, Ira Wibowo, Tike Priatnakusumah, dan personil grup idola JKT48, Adhisty Zara.<br /> Ridwan Kamil, yang saat rilis film menjabat sebagai Wali Kota Bandung, juga ikut bermain di film ini. Kakak Vanesha,<br /> Sissy Priscillia, menjadi narator film sekaligus suara dari Milea dewasa.</<span style="color: #0033b3;">string</span>><br /><br /></<span style="color: #0033b3;">resources</span>></pre></div><br /><b>themes.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">resources </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Base application theme. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">style </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="Theme.HorizontalRecyclerView" </span><span style="color: #174ad4;">parent</span><span style="color: #067d17;">="Theme.MaterialComponents.Light.DarkActionBar"</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Primary brand color. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorPrimary"</span>>@color/black</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorPrimaryVariant"</span>>@color/black</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorOnPrimary"</span>>@color/white</<span style="color: #0033b3;">item</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Secondary brand color. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorSecondary"</span>>@color/teal_200</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorSecondaryVariant"</span>>@color/teal_700</<span style="color: #0033b3;">item</span>><br /> <<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="colorOnSecondary"</span>>@color/black</<span style="color: #0033b3;">item</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Status bar color. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span><<span style="color: #0033b3;">item </span><span style="color: #174ad4;">name</span><span style="color: #067d17;">="android:statusBarColor" </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:targetApi</span><span style="color: #067d17;">="l"</span>>?attr/colorPrimaryVariant</<span style="color: #0033b3;">item</span>><br /> <span style="color: #8c8c8c; font-style: italic;"><!-- Customize your theme here. --><br /></span><span style="color: #8c8c8c; font-style: italic;"> </span></<span style="color: #0033b3;">style</span>><br /></<span style="color: #0033b3;">resources</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Sekarang kita menentukan isi layout <b>activity_main.xml</b>, seperti yang sudah dikatakan diatas, kita akan memiliki 3 komponen <b>RecyclerView</b>. Untuk memiliki RecyclerView dengan orientasi horizontal, kita hanya perlu menambahkan / merubah nilai dari atribut <span style="color: #800180;">android:orientation</span> dan <span style="color: #800180;">android:scrollbars</span> ke horizontal pada komponen RecyclerView.<br /><br /><b>activity_main.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/blackishRed"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".MainActivity"</span>><br /><br /> <<span style="color: #0033b3;">ScrollView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginStart</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginEnd</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutRvFilmTerlaris"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="50dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_rv"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Film Terlaris"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvTerlaris"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutRvFilmTerbaru"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_rv"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/layoutRvFilmTerlaris"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Film Terbaru"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvTerbaru"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/layoutRvFilmIndonesia"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_rv"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:gravity</span><span style="color: #067d17;">="center_vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toBottomOf</span><span style="color: #067d17;">="@id/layoutRvFilmTerbaru"</span>><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:text</span><span style="color: #067d17;">="Film Indonesia"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/rvIndonesia"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:scrollbars</span><span style="color: #067d17;">="horizontal"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layoutManager</span><span style="color: #067d17;">="androidx.recyclerview.widget.LinearLayoutManager" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">ScrollView</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div></div></span><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Selanjutnya sebuah layout resource file untuk menempatkan view-view dari atribut film, poster dan judul. Bagi Anda yang belum tahu akses membuat layout baru, klik kanan pada folder <b><i>layout > New > Layout Resource File</i></b>, berikan nama seperti dibawah ini :<br /><br /><b>film_container.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="110dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_margin</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@drawable/bg_container"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintBottom_toBottomOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintEnd_toEndOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintStart_toStartOf</span><span style="color: #067d17;">="parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/posterFilm"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="100dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="150dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="5dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/judulFilm"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="sans-serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:lines</span><span style="color: #067d17;">="2"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/whiteDark"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="14sp" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Meskipun disini kita memiliki 3 RecyclerView, namun untuk layout container item dan adapter kita hanya membutuhkan masing-masing satu. Layaknya moto dari Java, <b>WORA </b>- "<i>Write Once, Run Anywhere</i>".</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Baik, sebelum membuat model data dan adapter, terlebih dahulu buatlah sebuah activity baru, bagi Anda yang belum tahu akses membuatnya, Anda bisa klik kanan dimana saja kecuali pada Gradle Scripts, kemudian <b><i>New > Activity > Empty Activity</i></b>. Jangan lupa klik centang pada Generate Layout File. Berikut isi dari layout dan konteks java dari activity tersebut :<br /><br /><b>activity_details.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: #174ad4;">xml version</span><span style="color: #067d17;">="1.0" </span><span style="color: #174ad4;">encoding</span><span style="color: #067d17;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">android</span><span style="color: #067d17;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">app</span><span style="color: #067d17;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: #067d17;"> </span><span style="color: #174ad4;">xmlns:</span><span style="color: #871094;">tools</span><span style="color: #067d17;">="http://schemas.android.com/tools"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:background</span><span style="color: #067d17;">="@color/blackishRed"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">tools</span><span style="color: #174ad4;">:context</span><span style="color: #067d17;">=".DetailsActivity"</span>><br /><br /> <<span style="color: #0033b3;">ScrollView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">app</span><span style="color: #174ad4;">:layout_constraintTop_toTopOf</span><span style="color: #067d17;">="parent"</span>><br /><br /> <<span style="color: #0033b3;">LinearLayout<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="match_parent"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:orientation</span><span style="color: #067d17;">="vertical"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:padding</span><span style="color: #067d17;">="10dp"</span>><br /><br /> <<span style="color: #0033b3;">ImageView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/posterFilm"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="200dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="300dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginTop</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="10dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:contentDescription</span><span style="color: #067d17;">="@string/app_name" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/judulFilm"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_marginBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/white"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="20sp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textStyle</span><span style="color: #067d17;">="bold" </span>/><br /><br /> <<span style="color: #0033b3;">TextView<br /></span><span style="color: #0033b3;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:id</span><span style="color: #067d17;">="@+id/deskripsiFilm"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_width</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:layout_height</span><span style="color: #067d17;">="wrap_content"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:fontFamily</span><span style="color: #067d17;">="serif"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:paddingBottom</span><span style="color: #067d17;">="20dp"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textColor</span><span style="color: #067d17;">="@color/whiteDark"<br /></span><span style="color: #067d17;"> </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:textSize</span><span style="color: #067d17;">="16sp" </span>/><br /><br /> </<span style="color: #0033b3;">LinearLayout</span>><br /><br /> </<span style="color: #0033b3;">ScrollView</span>><br /><br /></<span style="color: #0033b3;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><br /><b>DetailsActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.horizontalrv</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.squareup.picasso.Picasso</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">DetailsActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_details</span>);<br /><br /> <span style="color: black;">ImageView posterFilm </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">posterFilm</span>);<br /> <span style="color: black;">TextView judulFilm </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">judulFilm</span>);<br /> <span style="color: black;">TextView deskripsiFilm </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">deskripsiFilm</span>);<br /><br /> <span style="color: black;">Picasso</span>.<span style="font-style: italic;">get</span>().load(getIntent().getStringExtra(<span style="color: #067d17;">"poster"</span>)).into(<span style="color: black;">posterFilm</span>);<br /> <span style="color: black;">judulFilm</span>.setText(getIntent().getStringExtra(<span style="color: #067d17;">"judul"</span>));<br /> <span style="color: black;">deskripsiFilm</span>.setText(getIntent().getStringExtra(<span style="color: #067d17;">"deskripsi"</span>));<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Berikutnya java class baru sebagai getter dan konstruktor dari view-view item Pada RecyclerView. Klik kanan pada folder <b><i>java > New > Java Class</i></b>, beri nama dan isikan seperti dibawah ini :<br /><br /><b>DataModel.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.horizontalrv</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">DataModel </span>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">String </span><span style="color: #871094;">posterFilm</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">String </span><span style="color: #871094;">judulFilm</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">String </span><span style="color: #871094;">deskripsiFilm</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">DataModel </span>(<span style="color: black;">String </span>posterFilm, <span style="color: black;">String </span>judulFilm, <span style="color: black;">String </span>deskripsiFilm) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">posterFilm </span>= posterFilm;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">judulFilm </span>= judulFilm;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">deskripsiFilm </span>= deskripsiFilm;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getPosterFilm</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">posterFilm</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getJudulFilm</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">judulFilm</span>;<br /> }<br /><br /> <span style="color: #0033b3;">public </span><span style="color: black;">String </span><span style="color: #00627a;">getDeskripsiFilm</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">deskripsiFilm</span>;<br /> }<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Sekarang java class baru untuk adapter <b>RecyclerView</b>. Tidak ada yang spesial di adapter ini, sama seperti adapter RecyclerView pada tutorial-tutorial RecyclerView sebelumnya. Bedanya mungkin disini kita menambahkan <b>Context </b>di konstruktor karena kita memang memerlukan context untuk mengirimkan intent dan menuju ke context <b>DetailsActivity.java</b>.<br /><br /><b>RvAdapter.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><pre style="font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.horizontalrv</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Context</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.content.Intent</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.LayoutInflater</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.View</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.view.ViewGroup</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.ImageView</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">android.widget.TextView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.annotation.</span><span style="color: #9e880d;">NonNull</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">com.squareup.picasso.Picasso</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">RvAdapter </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">Adapter</span><<span style="color: black;">RvAdapter</span>.<span style="color: black;">ItemFilmViewHolder</span>>{<br /><br /> <span style="color: #0033b3;">private final </span><span style="color: black;">List</span><<span style="color: black;">DataModel</span>> <span style="color: #871094;">dataModels</span>;<br /> <span style="color: #0033b3;">private final </span><span style="color: black;">Context </span><span style="color: #871094;">context</span>;<br /><br /> <span style="color: #0033b3;">public </span><span style="color: #00627a;">RvAdapter</span>(<span style="color: black;">List</span><<span style="color: black;">DataModel</span>> dataModels, <span style="color: black;">Context </span>context) {<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">dataModels </span>= dataModels;<br /> <span style="color: #0033b3;">this</span>.<span style="color: #871094;">context </span>= context;<br /> }<br /><br /> <span style="color: #9e880d;">@NonNull<br /></span><span style="color: #9e880d;"> @Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public </span><span style="color: black;">ItemFilmViewHolder </span><span style="color: #00627a;">onCreateViewHolder</span>(<span style="color: black;">ViewGroup </span>viewGroup, <span style="color: #0033b3;">int </span>viewType) {<br /> <span style="color: #0033b3;">return new </span>ItemFilmViewHolder(<span style="color: black;">LayoutInflater</span>.<span style="font-style: italic;">from</span>(viewGroup.getContext())<br /> .inflate(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">film_container</span>, viewGroup, <span style="color: #0033b3;">false</span>));<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onBindViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">ItemFilmViewHolder </span>holder, <span style="color: #0033b3;">int </span>position) {<br /> <span style="color: #0033b3;">final </span><span style="color: black;">DataModel dataModel </span>= <span style="color: #871094;">dataModels</span>.get(position);<br /><br /> <span style="color: #0033b3;">final </span><span style="color: black;">String baseURL </span>= <span style="color: #067d17;">"https://image.tmdb.org/t/p/w220_and_h330_face/"</span>;<br /> <span style="color: black;">Picasso</span>.<span style="font-style: italic;">get</span>().load(<span style="color: black;">baseURL </span>+ <span style="color: black;">dataModel</span>.getPosterFilm()).into(holder.<span style="color: #871094;">posterFilm</span>);<br /><br /> holder.<span style="color: #871094;">judulFilm</span>.setText(<span style="color: black;">dataModel</span>.getJudulFilm());<br /><br /> holder.<span style="color: #871094;">itemView</span>.setOnClickListener(<span style="color: #0033b3;">new </span><span style="color: black;">View</span>.<span style="color: black;">OnClickListener</span>() {<br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public void </span><span style="color: #00627a;">onClick</span>(<span style="color: black;">View </span>view) {<br /> <span style="color: black;">Intent intent </span>= <span style="color: #0033b3;">new </span>Intent(<span style="color: #871094;">context</span>, <span style="color: black;">DetailsActivity</span>.<span style="color: #0033b3;">class</span>);<br /> <span style="color: black;">intent</span>.putExtra(<span style="color: #067d17;">"poster"</span>, <span style="color: #851691;">baseURL </span>+ <span style="color: #851691;">dataModel</span>.getPosterFilm());<br /> <span style="color: black;">intent</span>.putExtra(<span style="color: #067d17;">"judul"</span>, <span style="color: #851691;">dataModel</span>.getJudulFilm());<br /> <span style="color: black;">intent</span>.putExtra(<span style="color: #067d17;">"deskripsi"</span>, <span style="color: #851691;">dataModel</span>.getDeskripsiFilm());<br /> <span style="color: #871094;">context</span>.startActivity(<span style="color: black;">intent</span>);<br /> }<br /> });<br /> }<br /><br /> <span style="color: #0033b3;">static class </span><span style="color: black;">ItemFilmViewHolder </span><span style="color: #0033b3;">extends </span><span style="color: black;">RecyclerView</span>.<span style="color: black;">ViewHolder </span>{<br /> <span style="color: black;">ImageView </span><span style="color: #871094;">posterFilm</span>;<br /> <span style="color: black;">TextView </span><span style="color: #871094;">judulFilm</span>;<br /><br /> <span style="color: #00627a;">ItemFilmViewHolder</span>(<span style="color: #9e880d;">@NonNull </span><span style="color: black;">View </span>itemView) {<br /> <span style="color: #0033b3;">super</span>(itemView);<br /> <span style="color: #871094;">posterFilm </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">posterFilm</span>);<br /> <span style="color: #871094;">judulFilm </span>= itemView.findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">judulFilm</span>);<br /> }<br /> }<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">public int </span><span style="color: #00627a;">getItemCount</span>() {<br /> <span style="color: #0033b3;">return </span><span style="color: #871094;">dataModels</span>.size();<br /> }<br />}<br /></pre></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Harap maklum jika bahasa ada campur-campur Inggris, kadang repot mengartikannya ke bahasa Indonesia. Ya, sebagai programmer tentunya kita harus terbiasa dengan Inggris karena memang semuanya bahasa Inggris. Baik, terakhir di <b>MainActivity.java</b>, seluruh data-data film kita tentukan dari konteks ini, masing-masing RecyclerView pada sebuah metode void private yang kemudian kita panggil di dalam <i>onCreate</i>.<br /><br /><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><pre style="font-family: Consolas, monospace; font-size: 9.8pt;"><span style="color: #0033b3;">package </span><span style="color: black;">com.gwnbs.horizontalrv</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.appcompat.app.AppCompatActivity</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">androidx.recyclerview.widget.RecyclerView</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">android.os.Bundle</span>;<br /><br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.ArrayList</span>;<br /><span style="color: #0033b3;">import </span><span style="color: black;">java.util.List</span>;<br /><br /><span style="color: #0033b3;">public class </span><span style="color: black;">MainActivity </span><span style="color: #0033b3;">extends </span><span style="color: black;">AppCompatActivity </span>{<br /><br /> <span style="color: #9e880d;">@Override<br /></span><span style="color: #9e880d;"> </span><span style="color: #0033b3;">protected void </span><span style="color: #00627a;">onCreate</span>(<span style="color: black;">Bundle </span>savedInstanceState) {<br /> <span style="color: #0033b3;">super</span>.onCreate(savedInstanceState);<br /> setContentView(<span style="color: black;">R</span>.<span style="color: black;">layout</span>.<span style="color: #871094; font-style: italic;">activity_main</span>);<br /><br /> setUpFilmTerlaris();<br /> setUpFilmTerbaru();<br /> setUpFilmIndonesia();<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setUpFilmTerlaris</span>() {<br /> <span style="color: black;">List</span><<span style="color: black;">DataModel</span>> <span style="color: black;">dataModelTerlaris </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: black;">RecyclerView rvTerlaris </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvTerlaris</span>);<br /> <span style="color: black;">rvTerlaris</span>.setHasFixedSize(<span style="color: #0033b3;">true</span>);<br /> <span style="color: black;">RvAdapter adapterTerlaris </span>= <span style="color: #0033b3;">new </span>RvAdapter(<span style="color: black;">dataModelTerlaris</span>, <span style="color: #0033b3;">this</span>);<br /><br /> <span style="color: black;">dataModelTerlaris</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"or06FN3Dka5tukK1e9sl16pB3iy.jpg"</span>, <span style="color: #067d17;">"Avengers: Endgame"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">avengers_endgame</span>)));<br /> <span style="color: black;">dataModelTerlaris</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"8Y7WrRK1iQHEX7UIftBeBMjPjWD.jpg"</span>, <span style="color: #067d17;">"Avatar"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">avatar</span>)));<br /> <span style="color: black;">dataModelTerlaris</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"AbuyfKTHDp7oHxpMKgtEaIZqoU9.jpg"</span>, <span style="color: #067d17;">"Titanic"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">titanic</span>)));<br /> <span style="color: black;">dataModelTerlaris</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"wqnLdwVXoBjKibFRR5U3y0aDUhs.jpg"</span>, <span style="color: #067d17;">"Star Wars: The Force Awakens"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">star_wars</span>)));<br /> <span style="color: black;">dataModelTerlaris</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"bxfCEo5a6t6gxUR9EjJLlqCe73i.jpg"</span>, <span style="color: #067d17;">"Avengers: Infinity War"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">avengers_infinity_war</span>)));<br /> <span style="color: black;">dataModelTerlaris</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"rhr4y79GpxQF9IsfJItRXVaoGs4.jpg"</span>, <span style="color: #067d17;">"Jurassic World"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">jurassic_world</span>)));<br /> <span style="color: black;">dataModelTerlaris</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"pSfwXxP94xktZKn3UaeVe6VdFZl.jpg"</span>, <span style="color: #067d17;">"The Lion King"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">the_lion_king</span>)));<br /><br /> <span style="color: black;">rvTerlaris</span>.setAdapter(<span style="color: black;">adapterTerlaris</span>);<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setUpFilmTerbaru</span>() {<br /> <span style="color: black;">List</span><<span style="color: black;">DataModel</span>> <span style="color: black;">dataModelTerbaru </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: black;">RecyclerView rvTerbaru </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvTerbaru</span>);<br /> <span style="color: black;">rvTerbaru</span>.setHasFixedSize(<span style="color: #0033b3;">true</span>);<br /> <span style="color: black;">RvAdapter adapterTerbaru </span>= <span style="color: #0033b3;">new </span>RvAdapter(<span style="color: black;">dataModelTerbaru</span>, <span style="color: #0033b3;">this</span>);<br /><br /> <span style="color: black;">dataModelTerbaru</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"eLT8Cu357VOwBVTitkmlDEg32Fs.jpg"</span>, <span style="color: #067d17;">"Jiu Jitsu"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">jiu_jitsu</span>)));<br /> <span style="color: black;">dataModelTerbaru</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"dL6hKVirJgpA2kIl2JOInY1Py1P.jpg"</span>, <span style="color: #067d17;">"Fatman"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">fatman</span>)));<br /> <span style="color: black;">dataModelTerbaru</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"6OeGqp18oZucUGziMIRNhLouZ75.jpg"</span>, <span style="color: #067d17;">"The Dalton Gang"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">the_dalton_gang</span>)));<br /> <span style="color: black;">dataModelTerbaru</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"k68nPLbIST6NP96JmTxmZijEvCA.jpg"</span>, <span style="color: #067d17;">"Tenet"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">tenet</span>)));<br /> <span style="color: black;">dataModelTerbaru</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"9HT9982bzgN5on1sLRmc1GMn6ZC.jpg"</span>, <span style="color: #067d17;">"Rogue City"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">rogue_city</span>)));<br /> <span style="color: black;">dataModelTerbaru</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"wVu2B58T61LAMiY68hxAA2NLcr9.jpg"</span>, <span style="color: #067d17;">"Greenland"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">greenland</span>)));<br /><br /> <span style="color: black;">rvTerbaru</span>.setAdapter(<span style="color: black;">adapterTerbaru</span>);<br /> }<br /><br /> <span style="color: #0033b3;">private void </span><span style="color: #00627a;">setUpFilmIndonesia</span>() {<br /> <span style="color: black;">List</span><<span style="color: black;">DataModel</span>> <span style="color: black;">dataModelIndonesia </span>= <span style="color: #0033b3;">new </span>ArrayList<>();<br /> <span style="color: black;">RecyclerView rvIndonesia </span>= findViewById(<span style="color: black;">R</span>.<span style="color: black;">id</span>.<span style="color: #871094; font-style: italic;">rvIndonesia</span>);<br /> <span style="color: black;">rvIndonesia</span>.setHasFixedSize(<span style="color: #0033b3;">true</span>);<br /> <span style="color: black;">RvAdapter adapterIndonesia </span>= <span style="color: #0033b3;">new </span>RvAdapter(<span style="color: black;">dataModelIndonesia</span>, <span style="color: #0033b3;">this</span>);<br /><br /> <span style="color: black;">dataModelIndonesia</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"Abnm1Ws3JH0ReCfEhLMPwPcMcGO.jpg"</span>, <span style="color: #067d17;">"The Raid"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">the_raid</span>)));<br /> <span style="color: black;">dataModelIndonesia</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"8lI1p5cPqgXN2qrKZrmI3mhKBfs.jpg"</span>, <span style="color: #067d17;">"The Night Come For Us"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">the_night_come_for_us</span>)));<br /> <span style="color: black;">dataModelIndonesia</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"lGamnocYl4omp1txOs21OjEmgux.jpg"</span>, <span style="color: #067d17;">"Headshot"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">headshot</span>)));<br /> <span style="color: black;">dataModelIndonesia</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"fwEML9xRIOQdNZQYv34kfcU8gE9.jpg"</span>, <span style="color: #067d17;">"The Raid 2"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">the_raid_2</span>)));<br /> <span style="color: black;">dataModelIndonesia</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"2DQhccMuqp3PtZLu2LQQjSXSNQV.jpg"</span>, <span style="color: #067d17;">"Soekarno"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">soekarno</span>)));<br /> <span style="color: black;">dataModelIndonesia</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"vhl4FQTkUnlGCCCjyNyQFYYgVGE.jpg"</span>, <span style="color: #067d17;">"Ada Apa Dengan Cinta"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">ada_apa_dengan_cinta</span>)));<br /> <span style="color: black;">dataModelIndonesia</span>.add(<span style="color: #0033b3;">new </span>DataModel(<span style="color: #067d17;">"eitRZXfbw6rO0CfP3lPaGgK63qr.jpg"</span>, <span style="color: #067d17;">"Dilan 1990"</span>,<br /> getString(<span style="color: black;">R</span>.<span style="color: black;">string</span>.<span style="color: #871094; font-style: italic;">dilan_1990</span>)));<br /><br /> <span style="color: black;">rvIndonesia</span>.setAdapter(<span style="color: black;">adapterIndonesia</span>);<br /> }<br />}</pre></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Dan jangan lupa untuk menambahkan Internet permission di <b>AndroidManifest.xml</b> sejajar dengan tag <application>, karena disini kita menggunakan internet untuk memuat gambar poster dari film.<br /><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 5px; text-align: left; width: auto;"><pre style="color: #080808; font-family: Consolas, monospace; font-size: 9.8pt;"><<span style="color: #0033b3;">uses-permission </span><span style="color: #871094;">android</span><span style="color: #174ad4;">:name</span><span style="color: #067d17;">="android.permission.INTERNET" </span>/></pre></div><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Sekian tutorial ini, dan terimakasih atas waktu dan kunjungannya. Jika ada yang ingin disampaikan, silahkan posting di komentar.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-37057074965778327602020-12-01T01:06:00.002+07:002021-02-22T18:42:51.002+07:00Menampilkan RecyclerView Dengan BottomSheetDialog Android<p><span style="font-family: georgia;">Untuk menampilkan <b>BottomSheetDialog </b>ini kita menggunakan library <span style="color: #800180;">com.google.android.material:material:1.2.1</span> dan <span style="color: #800180;">androidx.recyclerview:recyclerview:1.1.0</span> untuk <b>RecyclerView</b>. Pada tutorial kali ini, <b>BottomSheetDialog </b>akan menampilkan ViewGroup <b>RecyclerView </b>dengan item-tem artis (foto, nama, dan bio singkat ).<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;"><br /></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/C780gz3WLog" width="320" youtube-src-id="C780gz3WLog"></iframe></div><br /><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Baik, pertama-tama kita masukkan terlebih dahulu library <span style="color: #800180;">implementation 'com.google.android.material:material:1.2.1'</span> dan <span style="color: #800180;">implementation 'androidx.recyclerview:recyclerview:1.1.0'</span> kedalam dependencies pada file <i><b>build.gradle(Module:app)</b></i> kemudian sync gradle. Tentunya disini kita sudah memilik activity_main.xml sebagai default layout dan MainActivity.java sebagai default konteks saat membuat sebuah project baru.</span></p><p><span style="font-family: georgia;">Di dalam layout tersebut hanya akan ada sebuah tombol yang nantinya akan memunculkan <b>BottomSheetDialog</b>. Sekarang kita memerlukan sebuah layout file untuk menempatkan ViewGroup RecyclerView, juga sebuah layout sebagai custom layout item-item RecyclerView.</span></p><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Untuk kedua layout tersebut kita berikan nama <b>layout_bottomsheetdialog.xml</b> dan <b>item_container.xml</b>. Dan berikut isi dari ketiga layout file tersebut.<br /><br /><b>activity_main.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorPrimary"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/btnPress"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="PRESS TO SHOW"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="20sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorAccent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintBottom_toBottomOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintLeft_toLeftOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintRight_toRightOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="parent" </span>/><br /><br /></<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><br /><b>layout_bottomsheetdialog.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/artistContainer"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorPrimary"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="?actionBarSize"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutHeader"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorPrimaryDark"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center_vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="10dp"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_weight</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:ellipsize</span><span style="color: green; font-weight: bold;">="end"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:includeFontPadding</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Daftar Artist"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:maxLines</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="20sp"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/imageClose"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="4dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/ic_close"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="@color/colorText" </span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/bottomRv"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:clipToPadding</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:scrollbars</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layoutManager</span><span style="color: green; font-weight: bold;">="androidx.recyclerview.widget.LinearLayoutManager"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre></div><br /><b>item_container.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorPrimaryDark"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutRootContainer"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="10dp"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/picLogo"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="130dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="100dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:scaleType</span><span style="color: green; font-weight: bold;">="centerCrop"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorPrimaryDark"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="10dp"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:includeFontPadding</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Text Name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorAccent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="18sp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textBio"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:includeFontPadding</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Text Description"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="12sp" </span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Selanjutnya sebuah java class sebagai setter getter view-view pada item RecyclerView, dan sebuah java class adapter untuk RecyclerView. Untuk kedua file ini kita namakan <b>Artist.java</b> dan <b>RecyclerViewAdapter.java</b>. Berikut isi dari kedua file java ini :<br /><br /><b>Artist.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.bottomsheetdialog;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>Artist {<br /><br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">photo</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">name</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">bio</span>;<br /><br /> <span style="color: navy; font-weight: bold;">public </span>Artist (<span style="color: navy; font-weight: bold;">int </span>photo, String name, String bio) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">photo </span>= photo;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">name </span>= name;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">bio </span>= bio;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public int </span>getPhoto() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">photo</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public </span>String getName() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">name</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public </span>String getBio() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">bio</span>;<br /> }<br />}<br /></pre></div><br /><b>RecyclerViewAdapter.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.bottomsheetdialog;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.content.Context;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.Gravity;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.LayoutInflater;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.ViewGroup;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ImageView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.LinearLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.annotation.<span style="color: olive;">NonNull</span>;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.recyclerview.widget.RecyclerView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.List;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>RecyclerViewAdapter <span style="color: navy; font-weight: bold;">extends </span>RecyclerView.Adapter<RecyclerViewAdapter.ArtistViewHolder> {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>List<Artist> <span style="color: #660e7a; font-weight: bold;">artists</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>Context <span style="color: #660e7a; font-weight: bold;">context</span>;<br /><br /> <span style="color: navy; font-weight: bold;">public </span>RecyclerViewAdapter(List<Artist> artists, Context context) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">artists </span>= artists;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">context </span>= context;<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public int </span>getItemCount() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">artists</span>.size();<br /> }<br /><br /> <span style="color: olive;">@NonNull<br /></span><span style="color: olive;"> @Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public </span>ArtistViewHolder onCreateViewHolder(<span style="color: olive;">@NonNull </span>ViewGroup parent, <span style="color: navy; font-weight: bold;">int </span>viewType) {<br /> <span style="color: navy; font-weight: bold;">return new </span>ArtistViewHolder(LayoutInflater.<span style="font-style: italic;">from</span>(parent.getContext())<br /> .inflate(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">item_container</span>, parent, <span style="color: navy; font-weight: bold;">false</span>));<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onBindViewHolder(<span style="color: olive;">@NonNull </span><span style="color: navy; font-weight: bold;">final </span>ArtistViewHolder holder, <span style="color: navy; font-weight: bold;">int </span>position) {<br /> Artist artist = <span style="color: #660e7a; font-weight: bold;">artists</span>.get(position);<br /> holder.<span style="color: #660e7a; font-weight: bold;">picLogo</span>.setImageResource(artist.getPhoto());<br /> holder.<span style="color: #660e7a; font-weight: bold;">textName</span>.setText(artist.getName());<br /> holder.<span style="color: #660e7a; font-weight: bold;">textBio</span>.setText(artist.getBio());<br /><br /> holder.<span style="color: #660e7a; font-weight: bold;">layoutRootContainer</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View view) {<br /> Toast toast = Toast.<span style="font-style: italic;">makeText</span>(<span style="color: #660e7a; font-weight: bold;">context</span>, <span style="color: green; font-weight: bold;">"Anda menyukai " <br /></span><span style="color: green; font-weight: bold;"> </span>+ <span style="color: #660e7a;">holder</span>.<span style="color: #660e7a; font-weight: bold;">textName</span>.getText().toString(), Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_LONG</span>);<br /> toast.setGravity(Gravity.<span style="color: #660e7a; font-style: italic; font-weight: bold;">CENTER</span>,<span style="color: blue;">0</span>,<span style="color: blue;">0</span>);<br /> toast.show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">static class </span>ArtistViewHolder <span style="color: navy; font-weight: bold;">extends </span>RecyclerView.ViewHolder {<br /><br /> ImageView <span style="color: #660e7a; font-weight: bold;">picLogo</span>;<br /> TextView <span style="color: #660e7a; font-weight: bold;">textName</span>, <span style="color: #660e7a; font-weight: bold;">textBio</span>;<br /> LinearLayout <span style="color: #660e7a; font-weight: bold;">layoutRootContainer</span>;<br /><br /> ArtistViewHolder(View itemView) {<br /> <span style="color: navy; font-weight: bold;">super</span>(itemView);<br /> <span style="color: #660e7a; font-weight: bold;">picLogo </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">picLogo</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textName </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textName</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textBio </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textBio</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutRootContainer </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layoutRootContainer</span>);<br /> }<br /><br /> }<br /><br />}<br /></pre></div></span><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Terakhir di <b>MainActivity.java</b>, seluruh kode yang diperlukan untuk memunculkan <b>BottomSheetDialog </b>kita sematkan didalam metode klik listener pada tombol. Untuk item-item RecyclerView, dalam contoh ini hanya 6 item artis masing-masing dengan foto, nama, dan biography singkat.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Untuk foto dan bio singkat artis semua saya ambil dari <i>wikipedia</i>. Tombol dengan icon <b>X </b>pada <b>layoutbottomsheet_dialog.xml</b> kita gunakan untuk mendismiss atau menghilangkan <b>BottomSheetDialog</b>, dapat juga dilakukan dengan menarik kebawah layout_bottomsheetdialog nya.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kemudian kita memberikan klik listener untuk root layout pada <b>item_container.xml</b> yaitu memunculkan toast message saja. Penerapan klik listener ini dilakukan didalam class <b>RecyclerViewAdapter.java</b> yang sudah ditampilkan sebelumnya diatas. Baik, berikut isi dari MainActivity.java :<br /><br /><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.bottomsheetdialog;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.recyclerview.widget.RecyclerView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.material.bottomsheet.BottomSheetBehavior;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.material.bottomsheet.BottomSheetDialog;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.content.res.Resources;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.LayoutInflater;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.ViewGroup;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.FrameLayout;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.ArrayList;<br /><span style="color: navy; font-weight: bold;">import </span>java.util.List;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>BottomSheetDialog <span style="color: #660e7a; font-weight: bold;">bottomSheetDialog</span>;<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">btnPress</span>).setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View view) {<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">bottomSheetDialog </span>== <span style="color: navy; font-weight: bold;">null</span>) {<br /> <span style="color: #660e7a; font-weight: bold;">bottomSheetDialog </span>= <span style="color: navy; font-weight: bold;">new </span>BottomSheetDialog(MainActivity.<span style="color: navy; font-weight: bold;">this</span>);<br /> LayoutInflater inflater = LayoutInflater.<span style="font-style: italic;">from</span>(MainActivity.<span style="color: navy; font-weight: bold;">this</span>);<br /> View v = inflater.inflate(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layout_bottomsheetdialog</span>, (ViewGroup)<br /> findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">artistContainer</span>), <span style="color: navy; font-weight: bold;">false</span>);<br /> <span style="color: #660e7a; font-weight: bold;">bottomSheetDialog</span>.setContentView(v);<br /><br /> setRecyclerViewItem(v);<br /><br /> FrameLayout frameLayout = <span style="color: #660e7a; font-weight: bold;">bottomSheetDialog</span>.findViewById(com.google.android.material.R.id.<span style="color: #660e7a; font-style: italic;">design_bottom_sheet</span>);<br /> <span style="color: navy; font-weight: bold;">if </span>(frameLayout != <span style="color: navy; font-weight: bold;">null</span>) {<br /> BottomSheetBehavior<FrameLayout> bottomSheetBehavior = BottomSheetBehavior.<span style="font-style: italic;">from</span>(frameLayout);<br /> bottomSheetBehavior.setPeekHeight(Resources.<span style="font-style: italic;">getSystem</span>().getDisplayMetrics().<span style="color: #660e7a; font-weight: bold;">heightPixels</span>);<br /> bottomSheetBehavior.setState(BottomSheetBehavior.<span style="color: #660e7a; font-style: italic; font-weight: bold;">STATE_EXPANDED</span>);<br /> }<br /><br /> v.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">imageClose</span>).setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View view) {<br /> <span style="color: #660e7a; font-weight: bold;">bottomSheetDialog</span>.dismiss();<br /> }<br /> });<br /> }<br /> <span style="color: #660e7a; font-weight: bold;">bottomSheetDialog</span>.show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">private void </span>setRecyclerViewItem(View v) {<br /> RecyclerView bottomRecyclerView = v.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">bottomRv</span>);<br /> List<Artist> artists = <span style="color: navy; font-weight: bold;">new </span>ArrayList<>();<br /> artists.add(<span style="color: navy; font-weight: bold;">new </span>Artist(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">katty</span>, <span style="color: green; font-weight: bold;">"Katty Perry"</span>, <span style="color: green; font-weight: bold;">"Lahir 25 Oktober 1984, dikenal secara profesional " </span>+<br /> <span style="color: green; font-weight: bold;">"sebagai Katy Perry, adalah seorang penyanyi, penulis lagu dan juri televisi berkebangsaan Amerika Serikat."</span>));<br /> artists.add(<span style="color: navy; font-weight: bold;">new </span>Artist(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">downey</span>, <span style="color: green; font-weight: bold;">"Robert Downey Jr."</span>, <span style="color: green; font-weight: bold;">"Lahir 4 April 1965, adalah seorang aktor Amerika. " </span>+<br /> <span style="color: green; font-weight: bold;">"Kariernya sudah termasuk keberhasilan dan kepopuleran di masa mudanya, diikuti dengan penyalahgunaan zat terlarang " </span>+<br /> <span style="color: green; font-weight: bold;">"dan masalah hukum, serta kesuksesannya pada usia remaja."</span>));<br /> artists.add(<span style="color: navy; font-weight: bold;">new </span>Artist(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">sule</span>, <span style="color: green; font-weight: bold;">"Sutisna"</span>, <span style="color: green; font-weight: bold;">"Lahir di Cimahi, 15 November 1976; umur 44 tahun, adalah seorang " </span>+<br /> <span style="color: green; font-weight: bold;">"pelawak, pembawa acara, penyanyi, dan aktor berkebangsaan Indonesia. Ia dikenal karena kemampuannya membuat lelucon " </span>+<br /> <span style="color: green; font-weight: bold;">"spontan yang responsif dan kreatif."</span>));<br /> artists.add(<span style="color: navy; font-weight: bold;">new </span>Artist(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">islan</span>, <span style="color: green; font-weight: bold;">"Chelsea Elizabeth Islan"</span>, <span style="color: green; font-weight: bold;">"Lahir di Washington, D.C., 2 Juni 1995; umur 25 tahun, " </span>+<br /> <span style="color: green; font-weight: bold;">"merupakan seorang aktris berkebangsaan Indonesia. Dia telah bermain dalam beberapa film seperti Refrain, Street Society, Merry " </span>+<br /> <span style="color: green; font-weight: bold;">"Riana: Mimpi Sejuta Dolar dan Dibalik 98. Dia juga bermain di serial televisi berjudul Tetangga Masa Gitu?"</span>));<br /> artists.add(<span style="color: navy; font-weight: bold;">new </span>Artist(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">chris</span>, <span style="color: green; font-weight: bold;">"Chris Hemsworth"</span>, <span style="color: green; font-weight: bold;">"Lahir di Melbourne, Australia, 11 Agustus 1983; umur 37 tahun, " </span>+<br /> <span style="color: green; font-weight: bold;">"adalah aktor Australia. Ia dikenal atas perannya sebagai Thor dalam film-film Marvel Studios seperti Thor (2011), The Avengers (2012), " </span>+<br /> <span style="color: green; font-weight: bold;">"dan Thor: The Dark World (2013)"</span>));<br /> artists.add(<span style="color: navy; font-weight: bold;">new </span>Artist(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">agnez</span>, <span style="color: green; font-weight: bold;">"Agnes Monica Muljoto"</span>, <span style="color: green; font-weight: bold;">"Lahir di Jakarta, 1 Juli 1986; umur 34 tahun, atau yang sekarang " </span>+<br /> <span style="color: green; font-weight: bold;">"dikenal sebagai Agnez Mo, adalah seorang penyanyi dan artis berkebangsaan Indonesia. Ia memulai kariernya di industri hiburan pada usia " </span>+<br /> <span style="color: green; font-weight: bold;">"enam tahun sebagai seorang penyanyi cilik."</span>));<br /> RecyclerViewAdapter recyclerViewAdapter = <span style="color: navy; font-weight: bold;">new </span>RecyclerViewAdapter(artists, <span style="color: navy; font-weight: bold;">this</span>);<br /> bottomRecyclerView.setAdapter(recyclerViewAdapter);<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Berikut juga untuk warna-warna yang digunakan dalam contoh ini.<br /><br /><b>colors.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">resources</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimary"</span>>#141A31</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimaryDark"</span>>#081029</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorAccent"</span>>#0097FB</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorText"</span>>#C3CED3</<span style="color: navy; font-weight: bold;">color</span>><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Sekian turorial singkat ini. Terimakasih atas kunjungan dan waktunya, jika ada yang ingin disampaikan feel free untuk mempostingnya dikolom komentar.</span>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-5294443178382441892020-11-24T22:24:00.001+07:002021-02-22T18:43:39.451+07:00Cara Membuat Android App Bundle<p><span style="font-family: georgia;"><b>Android App Bundle</b> merupakan format publikasi resmi dari Android yang menawarkan cara efisien untuk membuat dan merilis aplikasi. Dengan <b>Android App Bundle</b>, ukuran unduh aplikasi dapat menjadi lebih kecil. Ukuran unduh sebuah aplikasi tentunya menjadi pertimbangan bagi sebagian besar orang sebelum mengunduh sebuah aplikasi.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;"><b>App Bundle</b> bekerja di <b>Google Playstore</b>, saya tidak tahu jika ada store lain yang mendukung penginstalan <b>App Bundle</b>. Mengenai <b>App Bundle</b> lebih dalam, silahkan mengunjungi situs resmi <b><a href="https://developer.android.com/platform/technology/app-bundle" target="_blank">Android Developer</a></b>, karena disini kita hanya pada praktek saja.</span></p><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="8611345975" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Untuk mencapai ukuran unduh aplikasi yang sekecil mungkin, kita bisa mengaktifkan penyusutan, obfuscation dan pengoptimalan source dan resource pada <b><i>build.gradle(Module: app)</i></b>. Pengaktifan ini akan menghapus resource yang tidak digunakan, menghapus dengan aman class (kolom, metode, atribut dan dependensi library) yang tidak digunakan, memperpendek nama-nama folder / class saat aplikasi di build (bangun).</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Tindakan tersebut juga akan mengamankan source kode aplikasi Anda dari pencuri-pencuri source code. Tambahkan <i>minifyEnabled </i>dan <i>shrinkResources </i>dengan nilai boolean true di dalam <i>buildTypes </i>pada file <b>build.gradle(Module: app)</b> kemudian sync gradle. Berikut contohnya :<br /><br /><b>build.gradle(Module: app)</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">apply <span style="color: green; font-weight: bold;">plugin</span>: <span style="color: green; font-weight: bold;">'com.android.application'<br /></span><span style="color: green; font-weight: bold;"><br /></span>android <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>compileSdkVersion <span style="color: blue;">29<br /></span><span style="color: blue;"> </span>buildToolsVersion <span style="color: green; font-weight: bold;">"29.0.3"<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span>defaultConfig <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>applicationId <span style="color: green; font-weight: bold;">"com.gwnbs.appbundle"<br /></span><span style="color: green; font-weight: bold;"> </span>minSdkVersion <span style="color: blue;">19<br /></span><span style="color: blue;"> </span>targetSdkVersion <span style="color: blue;">29<br /></span><span style="color: blue;"> </span>versionCode <span style="color: blue;">1<br /></span><span style="color: blue;"> </span>versionName <span style="color: green; font-weight: bold;">"1.0"<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span>testInstrumentationRunner <span style="color: green; font-weight: bold;">"androidx.test.runner.AndroidJUnitRunner"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;"> </span>buildTypes <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>release <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span><span style="color: grey; font-style: italic;">//Menyusutkan dan menghapus kode sumber<br /></span><span style="color: grey; font-style: italic;"> </span>minifyEnabled <span style="color: #000043; font-weight: bold;">true<br /></span><span style="color: #000043; font-weight: bold;"> </span>shrinkResources <span style="color: #000043; font-weight: bold;">true<br /></span><span style="color: #000043; font-weight: bold;"> </span>proguardFiles getDefaultProguardFile(<span style="color: green; font-weight: bold;">'proguard-android-optimize.txt'</span>), <span style="color: green; font-weight: bold;">'proguard-rules.pro'<br /></span><span style="color: green; font-weight: bold;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"> }<br /></span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span>dependencies <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>implementation fileTree(<span style="color: green; font-weight: bold;">dir</span>: <span style="color: green; font-weight: bold;">"libs"</span>, <span style="color: green; font-weight: bold;">include</span>: [<span style="color: green; font-weight: bold;">"*.jar"</span>])<br /> implementation <span style="color: green; font-weight: bold;">'androidx.appcompat:appcompat:1.2.0'<br /></span><span style="color: green; font-weight: bold;"> </span>implementation <span style="color: green; font-weight: bold;">'androidx.constraintlayout:constraintlayout:2.0.4'<br /></span><span style="color: green; font-weight: bold;"> </span>testImplementation <span style="color: green; font-weight: bold;">'junit:junit:4.13'<br /></span><span style="color: green; font-weight: bold;"> </span>androidTestImplementation <span style="color: green; font-weight: bold;">'androidx.test.ext:junit:1.1.2'<br /></span><span style="color: green; font-weight: bold;"> </span>androidTestImplementation <span style="color: green; font-weight: bold;">'androidx.test.espresso:espresso-core:3.3.0'<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="font-weight: bold;">}</span></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Untuk mempublikasikan aplikasi di Playstore, juga membutuhkan penandatangan aplikasi, disini kita akan membuat <b>Android App Bundle</b> dengan penandatanganan otomatis di Android Studio. </span><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><p><span style="font-family: georgia;">Pada pilihan tab yang tersedia, pilih <b><i>Build </i></b>> <b><i>Generate Signed Bundle / APK</i></b>. Seperti terlihat pada gambar berikut.</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfNj0fUYX38isv3MXgqzYAOsjy3d4andnPqDOahSOJj5Y49UjEn-wvWv9Y4RTnmGPLKkzoVjROiJ4bROE-k860kHJ5d2pWe7qmtBim9lwpFSDnndENE3nM1TRRKURYf5Zt-YXiu0uumw4/s1366/Screenshot+%252836%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Cara Membuat App Bundle Android" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfNj0fUYX38isv3MXgqzYAOsjy3d4andnPqDOahSOJj5Y49UjEn-wvWv9Y4RTnmGPLKkzoVjROiJ4bROE-k860kHJ5d2pWe7qmtBim9lwpFSDnndENE3nM1TRRKURYf5Zt-YXiu0uumw4/w320-h180/Screenshot+%252836%2529.png" title="App Bundle" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 1</b> : Langkah awal membuat App Bundle<br /></td></tr></tbody></table><span style="font-family: georgia;"></span><span style="font-family: georgia;"><br /></span><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kemudian pada jendela konfigurasi yang muncul terdapat 2 pilihan yakni Android App Bundle dan APK. Pilih <b>Android App Bundle</b><span> lalu klik </span><i><b>Next</b></i><span>. Selanjutnya pada jendela baru yang muncul, akan dihadapkan pada key store penandatanganan aplikasi. Pada pilihan <i>key store path</i>, klik tombol <b>C<i>reate new</i></b> yang berada tepat dibawahnya.</span></span></div><div><span style="font-family: georgia;"><span><br /></span></span></div><div><span style="font-family: georgia;"><span><br /></span></span></div><div><span style="font-family: georgia;"><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9EQ4kbuLj_BaYW9Ujlp0rLxNl-SmNQmfm99AiW5R-lYVOPP8NQzYeCCMNLy3-BYtOfaN2Go4_xETu-2yQkRhpLSORnnIRIaEG-cwsgiJco4kHxrZT6pwVSs0HnwCaXUMbS_IYwPePih0/s1366/Screenshot+%252838%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Membuat key store path" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9EQ4kbuLj_BaYW9Ujlp0rLxNl-SmNQmfm99AiW5R-lYVOPP8NQzYeCCMNLy3-BYtOfaN2Go4_xETu-2yQkRhpLSORnnIRIaEG-cwsgiJco4kHxrZT6pwVSs0HnwCaXUMbS_IYwPePih0/w320-h180/Screenshot+%252838%2529.png" title="App Bundle Android" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 2</b> : Membuat Key store path<br /></td></tr></tbody></table><br /><span><br /></span></span></div><div><span style="font-family: georgia;">Jendela berikutnya yang muncul adalah konfigurasi / penyetelan key store path baru. Pada kolom <i>key store path</i>, tentukan path untuk key store dan nama key store nya di komputer Anda, misalnya : C:\Users\user\namakeystore. Kemudian pada kolom <i>password </i>isikan password yang diinginkan lalu Confirm password.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Selanjutnya kolom <i>Alias</i>, tentukan sebuah nama alias untuk key storenya. Kemudian buat <i>password </i>lagi dan Confirm. Berikutnya <i>Validity</i>, tentukan berapa lama validitas dari key store. Terakhir menentukan atribut Certificate yaitu : First and Last Name, Organizational Unit, Organization, City or Locality, State or Province, dan Country Code.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Untuk Organizational Unit dan Organization bisa dikosongkan jika tidak ada. Berikut contoh gambar untuk pengisiannya :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvabSr1yjkkdZeo_OjUvMJvtY3OxhtIRcuGEshorQnoD6LsQN7WNLd-GpnC_CfNULaEH2nm02lEcPlOamonOocEqGzLvfhj0sQ9pKTP8O6vVXDOZBMMKPkqHa8fTOwrBSo9FN7qNONoZ4/s1366/Screenshot+%252834%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Membuat key store baru" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvabSr1yjkkdZeo_OjUvMJvtY3OxhtIRcuGEshorQnoD6LsQN7WNLd-GpnC_CfNULaEH2nm02lEcPlOamonOocEqGzLvfhj0sQ9pKTP8O6vVXDOZBMMKPkqHa8fTOwrBSo9FN7qNONoZ4/w320-h180/Screenshot+%252834%2529.png" title="Android App Bundle" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="font-family: georgia;"><b>Gambar 3</b> : Menentukan atribut key store</span><br /></td></tr></tbody></table><br /><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Setelah semua terisi, klik <i><b>OK</b></i>. Maka jendela akan tertutup dan kolom-kolom pada jendela sebelumnya pun akan secara otomatis terisi. Klik <b><i>Next </i></b>untuk menuju ke tahap berikutnya.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiwqCaoV1oSxp4m7LlF02EGLrI5rGCn6xmnspS-sqjhnIQ6VGtFECORc_YQvcgI7UsazsEsNGqqhhgZtErw7PXF4227sJT_RguZyxQUGzYMDHJmofdrFWSkpWS3fn6p8Ftng7-_co_0As/s1366/Screenshot+%252835%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="membuat android app bundle" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiwqCaoV1oSxp4m7LlF02EGLrI5rGCn6xmnspS-sqjhnIQ6VGtFECORc_YQvcgI7UsazsEsNGqqhhgZtErw7PXF4227sJT_RguZyxQUGzYMDHJmofdrFWSkpWS3fn6p8Ftng7-_co_0As/w320-h180/Screenshot+%252835%2529.png" title="App Bundle" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="font-family: georgia;"><b>Gambar 4</b> : Key store telah siap</span><br /></td></tr></tbody></table><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><p><span style="font-family: georgia;">Selanjutnya pilih <i>release </i>pada jendela baru yang muncul lalu klik <b><i>Finish</i></b>. Tunggu hingga proses building selesai. Setelah proses selesai, dipojok kanan bawah Event Log pada Android Studio akan muncul pesan yang menginfokan bahwa pembuatan <b>App Bundle</b> sukses. Klik <i>locate </i>pada pesan tersebut untuk melihat keberadaan aplikasi di komputer Anda.</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_Li34BEGF1tAYUpEDySFD-79Ytx7Of84Uvgpqc31dFYHXFU6EJ4oscvc-FcuXeOP78tSViAOoa0K2YuxWeYx6mjEL3FS8Zt2MHkCm3AwfjZk2yDXwmUIrhdqU7ioFflt0tKqJVzR-6Ag/s1366/Screenshot+%252839%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="App bundle android release type" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_Li34BEGF1tAYUpEDySFD-79Ytx7Of84Uvgpqc31dFYHXFU6EJ4oscvc-FcuXeOP78tSViAOoa0K2YuxWeYx6mjEL3FS8Zt2MHkCm3AwfjZk2yDXwmUIrhdqU7ioFflt0tKqJVzR-6Ag/w320-h180/Screenshot+%252839%2529.png" title="Android App Bundle" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="font-family: georgia;"><b>Gambar 5</b> : App Bundle tipe release</span><br /></td></tr></tbody></table><br /><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlknaSgj_mmI0-AtROFHbDDIyZmPo_fAm-9xf_GlDfessJpEsFTmoI8GYVtV3rTDCxMxf-bVyHhPCtuH85-z_3cOOYwkfH7tz5_I8tNlmuWk6CfB9Ic79bro8JJs8EmXLnJCyv3zbB-uc/s1366/Screenshot+%252840%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="event log android studio" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlknaSgj_mmI0-AtROFHbDDIyZmPo_fAm-9xf_GlDfessJpEsFTmoI8GYVtV3rTDCxMxf-bVyHhPCtuH85-z_3cOOYwkfH7tz5_I8tNlmuWk6CfB9Ic79bro8JJs8EmXLnJCyv3zbB-uc/w320-h180/Screenshot+%252840%2529.png" title="android studio" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="font-family: georgia;"><b>Gambar 6</b> : Event Log Android Studio</span><br /></td></tr></tbody></table><br /><p><span style="font-family: georgia;">Nama aplikasi default setelah building selesai adalah <i>app-release</i> dengan format <i>.aab</i>. Anda bisa mengganti namanya namun tidak untuk formatnya. Misalnya digantikan dengan : <i>NamaAplikasi_V3.05.aab</i>.</span></p><p><span style="font-family: georgia;">Aplikasi pun siap untuk dipublikasikan ke Playstore. Bagi Anda yang belum mendaftar Google Play Console Developer, untuk diketahui bahwa diperlukan biaya US$25 untuk mendaftar dan hanya dapat dibayar dengan kartu kredit. Hanya itu untuk selamanya, tidak ada biaya lain lagi.</span></p><p><span style="font-family: georgia;">Sekian untuk tutorial sederhana ini. Terimakasih untuk kunjungan dan waktunya, jika ada pertanyaan silahkan di sampaikan melalui kolom komentar.</span></p></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-13427417405881822632020-11-23T00:36:00.005+07:002021-02-22T18:43:56.977+07:00Menampilkan Native Ads AdMob di Aplikasi Android<p><span style="font-family: georgia;">Penerapan iklan <b>Native AdMob</b> sedikit lebih rumit dibandingkan penerapan jenis iklan AdMob lainnya. Misalnya iklan <b>Banner </b>yang dalam menerapkannya hanya membutuhkan beberapa baris kode XML / Java maka iklan siap untuk ditampilkan. Berbeda dengan <b>Native Ads</b> yang harus di kustominasi dan memiliki aturan yang cukup banyak.<span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;">Untuk menampilkan iklan <b>Native</b>, kita harus menyediakan layout / container, misalnya : <i>CardView </i>atau layout seperti <i>LinearLayout</i>, <i>Relativelayout </i>dan sebagainya. Kemudian juga kita harus menyediakan object-object view di dalam sebuah Layout Resource File seperti <i>TextView </i>untuk judul dari iklan, nama pengiklan, deskripsi iklan, lalu <i>ImageView </i>untuk icon perusahaan pengiklan dan sebuah listener untuk mengunjungi halaman website pengiklan (opsional).</span></p><p><span style="font-family: georgia;"><br /></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/JC727NuEYhY" width="320" youtube-src-id="JC727NuEYhY"></iframe></div><span style="font-family: georgia;"><p><span style="font-family: georgia;"><br /></span></p>Tentunya Anda sudah tidak asing dengan AdMob, oleh karena itu Anda mencari artikel bagaimana cara menerapkan iklan </span><b style="font-family: georgia;">Native Ads</b><span style="font-family: georgia;">. Karena biasanya bagi kita yang baru atau akan mengenal AdMob, kebanyakan dari kita memulainnya dari iklan Banner. Silahkan untuk membaca postingan-postingan sebelumnya di blog ini mengenai iklan banner, interstitial dan rewarded video jika Anda belum familiar atau Anda sampai disini karena kesasar. Postingan dapat ditemukan melalui tombol navigasi All Post atau Sitemap blog ini.</span><p></p><p><span style="font-family: georgia;">Baik, mari kita mulai! Hal pertama adalah mengimport <i>SDK Google Ads</i> terlebih dahulu. Letakkan baris kode library berikut ini ke dalam dependencies pada file <b>build.gradle(Module: app)</b> kemudian sync gradle.</span></p><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">implementation <span style="color: green; font-weight: bold;">'com.google.android.gms:play-services-ads:19.5.0'</span></pre></div><div><br /></div><span style="font-family: georgia;"><b>build.gradle(Module: app) :</b></span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">apply <span style="color: green; font-weight: bold;">plugin</span>: <span style="color: green; font-weight: bold;">'com.android.application'<br /></span><span style="color: green; font-weight: bold;"><br /></span>android <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>compileSdkVersion <span style="color: blue;">29<br /></span><span style="color: blue;"> </span>buildToolsVersion <span style="color: green; font-weight: bold;">"29.0.3"<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span>defaultConfig <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>applicationId <span style="color: green; font-weight: bold;">"com.gwnbs.nativeads"<br /></span><span style="color: green; font-weight: bold;"> </span>minSdkVersion <span style="color: blue;">19<br /></span><span style="color: blue;"> </span>targetSdkVersion <span style="color: blue;">29<br /></span><span style="color: blue;"> </span>versionCode <span style="color: blue;">1<br /></span><span style="color: blue;"> </span>versionName <span style="color: green; font-weight: bold;">"1.0"<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span>testInstrumentationRunner <span style="color: green; font-weight: bold;">"androidx.test.runner.AndroidJUnitRunner"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;"> </span>buildTypes <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>release <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>minifyEnabled <span style="color: #000043; font-weight: bold;">false<br /></span><span style="color: #000043; font-weight: bold;"> </span>proguardFiles getDefaultProguardFile(<span style="color: green; font-weight: bold;">'proguard-android-optimize.txt'</span>), <span style="color: green; font-weight: bold;">'proguard-rules.pro'<br /></span><span style="color: green; font-weight: bold;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"> }<br /></span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span>dependencies <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>implementation fileTree(<span style="color: green; font-weight: bold;">dir</span>: <span style="color: green; font-weight: bold;">"libs"</span>, <span style="color: green; font-weight: bold;">include</span>: [<span style="color: green; font-weight: bold;">"*.jar"</span>])<br /> implementation <span style="color: green; font-weight: bold;">'androidx.appcompat:appcompat:1.2.0'<br /></span><span style="color: green; font-weight: bold;"> </span>implementation <span style="color: green; font-weight: bold;">'androidx.constraintlayout:constraintlayout:2.0.4'<br /></span><span style="color: green; font-weight: bold;"> </span>testImplementation <span style="color: green; font-weight: bold;">'junit:junit:4.13'<br /></span><span style="color: green; font-weight: bold;"> </span>androidTestImplementation <span style="color: green; font-weight: bold;">'androidx.test.ext:junit:1.1.2'<br /></span><span style="color: green; font-weight: bold;"> </span>androidTestImplementation <span style="color: green; font-weight: bold;">'androidx.test.espresso:espresso-core:3.3.0'<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: grey; font-style: italic;">//Library SDK Admob<br /></span><span style="color: grey; font-style: italic;"> </span>implementation <span style="color: green; font-weight: bold;">'com.google.android.gms:play-services-ads:19.5.0'<br /></span><span style="font-weight: bold;">}</span></pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;"><div>Dan jangan lupa untuk menginput meta data ID aplikasi AdMob ke dalam tag application di <b>AndroidManifest.xml</b>. Untuk App ID dan Unit Ad ID AdMob dalam contoh ini semuanya menggunakan ID test yang disediakan di <b><a href="https://developers.google.com/admob/android/test-ads" target="_blank">Google Developers</a></b>. Klik link tersebut untuk melihat seluruh jenis test unit ID iklan. Berikut penampakan AndroidManifest.xml pada contoh ini :</div><br /><b>AndroidManifest.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">manifest </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">package</span><span style="color: green; font-weight: bold;">="com.gwnbs.nativeads"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">application<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:allowBackup</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:icon</span><span style="color: green; font-weight: bold;">="@mipmap/ic_launcher"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:label</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:roundIcon</span><span style="color: green; font-weight: bold;">="@mipmap/ic_launcher_round"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:supportsRtl</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:theme</span><span style="color: green; font-weight: bold;">="@style/AppTheme"</span>><br /> <<span style="color: navy; font-weight: bold;">activity </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:name</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /> <<span style="color: navy; font-weight: bold;">intent-filter</span>><br /> <<span style="color: navy; font-weight: bold;">action </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:name</span><span style="color: green; font-weight: bold;">="android.intent.action.MAIN" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">category </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:name</span><span style="color: green; font-weight: bold;">="android.intent.category.LAUNCHER" </span>/><br /> </<span style="color: navy; font-weight: bold;">intent-filter</span>><br /> </<span style="color: navy; font-weight: bold;">activity</span>><br /><br /> <span style="color: grey; font-style: italic;"><!-- Meta data id aplikasi --><br /></span><span style="color: grey; font-style: italic;"> </span><<span style="color: navy; font-weight: bold;">meta-data<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:name</span><span style="color: green; font-weight: bold;">="com.google.android.gms.ads.APPLICATION_ID"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:value</span><span style="color: green; font-weight: bold;">="ca-app-pub-3940256099942544~3347511713"</span>/><br /> </<span style="color: navy; font-weight: bold;">application</span>><br /><br /></<span style="color: navy; font-weight: bold;">manifest</span>></pre></div><div><br /></div><div><br /></div><div>Misalnya aplikasi kita menyediakan konten artikel, dan kita akan menyisipkan iklan <b>Native Ads</b> di antara artikel tersebut. Berikut di <b>activity_main.xml</b> kita memiliki <i>ScrollView </i>sebagai root layout dengan sebuah <i>LinearLayout </i>sebagai layout child nya, 2 buah <i>TextView </i>sebagai konten artikel dan sebuah <i>LinearLayout </i>sebagai container untuk iklan <b>Native Ads</b> nya.</div><br /><b>activity_main.xml</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">ScrollView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textKontenApp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/adColorBodyText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="18sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Konten aplikasi"</span>/><br /><br /> <span style="color: grey; font-style: italic;"><!-- layout container untuk iklan native--><br /></span><span style="color: grey; font-style: italic;"> </span><<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/adContainer"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="10dp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textKontenApp2"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/adColorBodyText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="18sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Konten aplikasi"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">ScrollView</span>></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Kemudian kita harus membuat sebuah <b><i>Layout Resource File</i></b> dengan <i>UnifiedNativeAdView </i>sebagai root layoutnya, sebuah <i>LinearLayout </i>sebagai layout child dari root layout tersebut. Aturan untuk menampilkan iklan <b>Native Ads</b> salah satunya adalah atribusi, yaitu kita harus menyediakan sebuah view yang menginformasikan pengguna aplikasi bahwa konten yang tayang ini adalah sebuah iklan. Disini kita menggunakan <i>TextView </i>dengan nilai stringnya "Ad".</span><p></p><div><span style="font-family: georgia;">Disini kita juga menyediakan sebuah <i>TextView </i>dengan icon (opsional) sebagai listener untuk merefresh iklan. View berikutnya ialah <i>ImageView </i>untuk icon perusahaan pengiklan, <i>TextView </i>untuk headline dari iklan, <i>Textview </i>untuk nama perusahaan pengiklan, <i>RatingBar </i>(opsional) untuk rating iklan, <i>TextView </i>untuk deskripsi iklan, <i>MediaView </i>untuk menampilkan konten iklan, dan terakhir <i>Button </i>(opsional) untuk mengunjungi halaman website pengiklan.</span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><span style="font-family: georgia;">Layout ini kita namakan <b>native_ad_layout.xml</b>. Berikut isi keseluruhan dari file ini.<br /><br /><b>native_ad_layout.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">com.google.android.gms.ads.formats.UnifiedNativeAdView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/adColorBackground"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">RelativeLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/adAtribusi"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/adColorAtribusi"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_alignParentStart</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Ad"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="15sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/btnRefreshAd"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerHorizontal</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="?selectableItemBackground"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/adColorBodyText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Refresh"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="10sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_gravity</span><span style="color: green; font-weight: bold;">="end"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:drawableTopCompat</span><span style="color: green; font-weight: bold;">="@drawable/ic_refresh" </span>/><br /><br /> </<span style="color: navy; font-weight: bold;">RelativeLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/adIcon"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="60dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="60dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:adjustViewBounds</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:scaleType</span><span style="color: green; font-weight: bold;">="fitXY"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/adHeadlineText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="20sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/adColorHeading"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Ad Headline"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/adAdvertiserName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="16sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/adColorBodyText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Advertiser name"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">RatingBar<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/adRating"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:numStars</span><span style="color: green; font-weight: bold;">="5"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:stepSize</span><span style="color: green; font-weight: bold;">="0.5"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">style</span><span style="color: green; font-weight: bold;">="?android:attr/ratingBarStyleSmall"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:isIndicator</span><span style="color: green; font-weight: bold;">="true"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/adBodyText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/adColorBodyText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Ad body text"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="18sp"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">com.google.android.gms.ads.formats.MediaView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/mediaView"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="180dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_gravity</span><span style="color: green; font-weight: bold;">="center_horizontal"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/btnAction"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/adColorButtonText"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Install Now"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">com.google.android.gms.ads.formats.UnifiedNativeAdView</span>></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Sebelum kita melanjutkan ke MainActivity.java, berikut isi dari value <b>strings.xml</b> dan <b>colors.xml</b> yang digunakan dalam contoh ini. Untuk file-file yang tidak dituliskan dalam tutorial ini, artinya tidak ada yang dirubah / ditambah di dalam file tersebut (bawaan saat membuat project).<br /><br /><b>colors.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">resources</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimary"</span>>#F57C00</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimaryDark"</span>>#F57C00</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorAccent"</span>>#0288D1</<span style="color: navy; font-weight: bold;">color</span>><br /><br /> <span style="color: grey; font-style: italic;"><!-- Warna untuk object-object view iklan native --><br /></span><span style="color: grey; font-style: italic;"> </span><<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="adColorAtribusi"</span>>#FBC02D</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="adColorBackground"</span>>#F6EBEB</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="adColorHeading"</span>>#512DA8</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="adColorBodyText"</span>>#000000</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="adColorButton"</span>>#C2185B</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="adColorButtonText"</span>>#530B0B</<span style="color: navy; font-weight: bold;">color</span>><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div><br /><b>strings.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">resources</span>><br /> <<span style="color: navy; font-weight: bold;">string </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="app_name"</span>>Native Ads</<span style="color: navy; font-weight: bold;">string</span>><br /><br /> <<span style="color: navy; font-weight: bold;">string </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="konten_app"</span>><![CDATA[<h2>What is Lorem Ipsum?</h2><p><strong>Lorem Ipsum</strong>&nbsp;is simply dummy<br /> text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the<br /> 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not<br /> only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised<br /> in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing<br /> software like Aldus PageMaker including versions of Lorem Ipsum.</p><h2>Where does it come from?</h2><p>Contrary to<br /> popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC,<br /> making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one<br /> of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in<br /> classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33<br /> of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise<br /> on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum,<br /> \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.</p><p>The standard chunk of Lorem Ipsum used since<br /> the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from \"de Finibus Bonorum et Malorum\"<br /> by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by<br /> H. Rackham.</p>]]></<span style="color: navy; font-weight: bold;">string</span>><br /><br /> <<span style="color: navy; font-weight: bold;">string </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="konten_app2"</span>><![CDATA[<h2>Why do we use it?</h2><p>It is a long established fact that a reader will be<br /> distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a<br /> more-or-less normal distribution of letters, as opposed to using \'Content here, content here\', making it look like<br /> readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text,<br /> and a search for \'lorem ipsum\' will uncover many web sites still in their infancy. Various versions have evolved over<br /> the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p><h2>Where can I get some?<br /> </h2><p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in<br /> some form, by injected humour, or randomised words which don\'t look even slightly believable. If you are going to use a<br /> passage of Lorem Ipsum, you need to be sure there isn\'t anything embarrassing hidden in the middle of text. All the<br /> Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true<br /> generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence<br /> structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from<br /> repetition, injected humour, or non-characteristic words etc.</p>]]></<span style="color: navy; font-weight: bold;">string</span>><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div></span><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Selanjutnya di <b>MainActivity.java</b>. Pertama-tama membuat variabel global untuk objek <i>UnifiedNativeAd</i>, kemudian di dalam onCreate kita menginisialisasi dan menyetel nilai kedua TextView artikel nya dari HTML. Disini menggunakan HTML supaya teks dapat di kustominasi seperti terlihat pada file value <b>strings.xml</b> diatas.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kemudian untuk menampilkan iklan, kita membuatnya di dalam sebuah metode privat di luar onCreate, ini karena kita akan menggunakan kode untuk menampilkan iklan ini di 2 tempat yakni di dalam klik listener tombol refresh iklan dan di dalam onCreate. Selanjutnya metode privat untuk mengkoneksikan seluruh atribut iklan dengan objek-objek view pada layout <b>native_ad_layout.xml</b> yang akan dipanggil di dalam metode privat menampilkan iklan. Berikut isi selengkapnya :<br /><br /><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.nativeads;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.text.Html;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Button;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ImageView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.LinearLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.RatingBar;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.AdListener;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.AdLoader;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.AdRequest;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.LoadAdError;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.MobileAds;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.formats.MediaView;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.formats.UnifiedNativeAd;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.formats.UnifiedNativeAdView;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.initialization.InitializationStatus;<br /><span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.initialization.OnInitializationCompleteListener;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>UnifiedNativeAd <span style="color: #660e7a; font-weight: bold;">nativeAd</span>;<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: grey; font-style: italic;">//Inisialisasi dan menyetel nilai string TextView artikel<br /></span><span style="color: grey; font-style: italic;"> </span>TextView textKontenApp = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textKontenApp</span>);<br /> TextView textKontenApp2 = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textKontenApp2</span>);<br /> textKontenApp.setText(Html.<span style="font-style: italic;">fromHtml</span>(getString(R.string.<span style="color: #660e7a; font-style: italic; font-weight: bold;">konten_app</span>)));<br /> textKontenApp2.setText(Html.<span style="font-style: italic;">fromHtml</span>(getString(R.string.<span style="color: #660e7a; font-style: italic; font-weight: bold;">konten_app2</span>)));<br /><br /> <span style="color: grey; font-style: italic;">//Inisialisasi SDK AdMob<br /></span><span style="color: grey; font-style: italic;"> </span>MobileAds.<span style="font-style: italic;">initialize</span>(<span style="color: navy; font-weight: bold;">this</span>, <span style="color: navy; font-weight: bold;">new </span>OnInitializationCompleteListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onInitializationComplete(InitializationStatus initializationStatus) {<br /><br /> }<br /> });<br /><br /> showOrRefreshAd(<span style="color: green; font-weight: bold;">"Displaying ad..."</span>);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Metode untuk menampilkan iklan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">private void </span>showOrRefreshAd(<span style="color: navy; font-weight: bold;">final </span>String toastMessage) {<br /> AdLoader.Builder builder = <span style="color: navy; font-weight: bold;">new </span>AdLoader.Builder(<span style="color: navy; font-weight: bold;">this</span>, <span style="color: green; font-weight: bold;">"ca-app-pub-3940256099942544/2247696110"</span>);<br /> builder.forUnifiedNativeAd(<span style="color: navy; font-weight: bold;">new </span>UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: #660e7a;">toastMessage</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> <span style="color: #660e7a; font-weight: bold;">nativeAd </span>= unifiedNativeAd;<br /> LinearLayout adContainer = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">adContainer</span>);<br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"InflateParams"</span>)<br /> UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">native_ad_layout</span>, <span style="color: navy; font-weight: bold;">null</span>);<br /> TextView btnRefreshAd = adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">btnRefreshAd</span>);<br /> populateNativeAd(unifiedNativeAd, adView);<br /> adContainer.removeAllViews();<br /> adContainer.addView(adView);<br /><br /> <span style="color: grey; font-style: italic;">//metode klik untuk tombol refresh iklan.<br /></span><span style="color: grey; font-style: italic;"> </span>btnRefreshAd.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> showOrRefreshAd(<span style="color: green; font-weight: bold;">"Ad refreshed successfully."</span>);<br /> }<br /> });<br /> }<br /> });<br /><br /> AdLoader adLoader = builder.withAdListener(<span style="color: navy; font-weight: bold;">new </span>AdListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onAdFailedToLoad (LoadAdError adError) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Ad failed to load!"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> }<br /> }).build();<br /> adLoader.loadAd(<span style="color: navy; font-weight: bold;">new </span>AdRequest.Builder().build());<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Mengkoneksikan seluruh atribut iklan dengan object-object view yang ada di native_ad_layout.xml<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">private void </span>populateNativeAd (UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {<br /> adView.setIconView(adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">adIcon</span>));<br /> adView.setHeadlineView(adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">adHeadlineText</span>));<br /> adView.setAdvertiserView(adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">adAdvertiserName</span>));<br /> adView.setBodyView(adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">adBodyText</span>));<br /> adView.setStarRatingView(adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">adRating</span>));<br /> adView.setMediaView((MediaView) adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">mediaView</span>));<br /> adView.setCallToActionView(adView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">btnAction</span>));<br /><br /> adView.getMediaView().setMediaContent(nativeAd.getMediaContent());<br /> ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(nativeAd.getBody() == <span style="color: navy; font-weight: bold;">null</span>) {<br /> adView.getBodyView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">INVISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> ((TextView) adView.getBodyView()).setText(nativeAd.getBody());<br /> adView.getBodyView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(nativeAd.getAdvertiser() == <span style="color: navy; font-weight: bold;">null</span>) {<br /> adView.getAdvertiserView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">INVISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> ((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());<br /> adView.getAdvertiserView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(nativeAd.getStarRating() == <span style="color: navy; font-weight: bold;">null</span>) {<br /> adView.getStarRatingView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">INVISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> ((RatingBar) adView.getStarRatingView()).setRating(nativeAd.getStarRating().floatValue());<br /> adView.getStarRatingView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(nativeAd.getIcon() == <span style="color: navy; font-weight: bold;">null</span>) {<br /> adView.getIconView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> ((ImageView) adView.getIconView()).setImageDrawable(nativeAd.getIcon().getDrawable());<br /> adView.getIconView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(nativeAd.getCallToAction() == <span style="color: navy; font-weight: bold;">null</span>) {<br /> adView.getCallToActionView().setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">INVISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());<br /> }<br /><br /> adView.setNativeAd(nativeAd);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Menonaktifkan iklan jika iklan masih aktif namun activity sudah tidak berjalan.<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onDestroy() {<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">nativeAd </span>!=<span style="color: navy; font-weight: bold;">null</span>) {<br /> <span style="color: #660e7a; font-weight: bold;">nativeAd</span>.destroy();<br /> }<br /> <span style="color: navy; font-weight: bold;">super</span>.onDestroy();<br /> }<br />}</pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Untuk argumen string yang ada di dalam metode <i>showOrRefreshAd </i>ditunjukkan untuk menampilkan pesan toast berbeda. Itu karena kita memanggil metode privat tersebut di 2 tempat sehingga kita perlu membuat argumen string tersebut untuk menampilkan pesan toast yang berbeda.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Saya rasa sekian untuk tutorial ini. Terimakasih untuk kunjungan dan waktunya, jika ada yang ingin disampaikan silahkan posting di kolom komentar.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-11068226010462283152020-11-22T00:08:00.004+07:002021-02-22T18:44:22.049+07:00Android RecyclerView Dengan Multiple Item Selection (Konsep Layout TV Shows)<p><span style="font-family: georgia;">Pada kesempatan ini blog gwnbs akan kembali membahas <b>Android RecyclerView</b> dimana kali ini akan dicontohkan multiple selection (dapat memilih lebih dari satu item) untuk item-item <b>RecyclerView </b>pada sebuah aplikasi program TV series. Aplikasi ini belum disempurnakan, mungkin nanti akan disempurnakan dan diposting di blog ini jika ada kesempatan. <span></span></span></p><a name='more'></a><p></p><p><span style="font-family: georgia;"><br /></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/scz_Fbuj_Ms" width="320" youtube-src-id="scz_Fbuj_Ms"></iframe></div><br /><span style="font-family: georgia;"><br /></span><p></p><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUzgpDJ8cVrAFnldJGdJ_sdhyUgWXUjdOyjMm5VUtL-A_CPc9KVnla9KS0qSqgHfnlIi9mzAcMirVm4roPLikaTuLPPgH3wwQA3TP6kzoyLYuM2SV4jiC5aAsiDx0hFWZSHxByLhizjjc/s1366/Screenshot+%252833%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Android RecyclerView Multiple Item Selection" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUzgpDJ8cVrAFnldJGdJ_sdhyUgWXUjdOyjMm5VUtL-A_CPc9KVnla9KS0qSqgHfnlIi9mzAcMirVm4roPLikaTuLPPgH3wwQA3TP6kzoyLYuM2SV4jiC5aAsiDx0hFWZSHxByLhizjjc/w320-h180/Screenshot+%252833%2529.png" title="Android RecyclerView" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="font-family: times;">Gambar 1 : Screenshoot dari struktur project pada contoh ini.</span><br /></td></tr></tbody></table><span style="font-family: georgia;"><br /></span><p></p><p><span style="font-family: georgia;">Nama package pada contoh ini adalah <i>com.gwnbs.rvmultiple</i>. Pertama yang kita lakukan adalah memasukkan 2 library yakni RoundedImageview dan library <b>RecyclerView </b>itu sendiri ke file <b><i>build.gradle(Module: app)</i></b>. Berikut di bawah keseluruhan isi dari file ini.</span></p><span style="font-family: georgia;"><b>build.gradle(Module: app)</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">apply <span style="color: green; font-weight: bold;">plugin</span>: <span style="color: green; font-weight: bold;">'com.android.application'<br /></span><span style="color: green; font-weight: bold;"><br /></span>android <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>compileSdkVersion <span style="color: blue;">29<br /></span><span style="color: blue;"> </span>buildToolsVersion <span style="color: green; font-weight: bold;">"29.0.3"<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span>defaultConfig <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>applicationId <span style="color: green; font-weight: bold;">"com.gwnbs.rvmultiple"<br /></span><span style="color: green; font-weight: bold;"> </span>minSdkVersion <span style="color: blue;">19<br /></span><span style="color: blue;"> </span>targetSdkVersion <span style="color: blue;">29<br /></span><span style="color: blue;"> </span>versionCode <span style="color: blue;">1<br /></span><span style="color: blue;"> </span>versionName <span style="color: green; font-weight: bold;">"1.0"<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span>testInstrumentationRunner <span style="color: green; font-weight: bold;">"androidx.test.runner.AndroidJUnitRunner"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;"> </span>buildTypes <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>release <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span>minifyEnabled <span style="color: #000043; font-weight: bold;">false<br /></span><span style="color: #000043; font-weight: bold;"> </span>proguardFiles getDefaultProguardFile(<span style="color: green; font-weight: bold;">'proguard-android-optimize.txt'</span>), <span style="color: green; font-weight: bold;">'proguard-rules.pro'<br /></span><span style="color: green; font-weight: bold;"> </span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"> }<br /></span><span style="font-weight: bold;">}<br /></span><span style="font-weight: bold;"><br /></span>dependencies <span style="font-weight: bold;">{<br /></span><span style="font-weight: bold;"> </span><span style="color: grey; font-style: italic;">//Library bawaan<br /></span><span style="color: grey; font-style: italic;"> </span>implementation fileTree(<span style="color: green; font-weight: bold;">dir</span>: <span style="color: green; font-weight: bold;">"libs"</span>, <span style="color: green; font-weight: bold;">include</span>: [<span style="color: green; font-weight: bold;">"*.jar"</span>])<br /> implementation <span style="color: green; font-weight: bold;">'androidx.appcompat:appcompat:1.2.0'<br /></span><span style="color: green; font-weight: bold;"> </span>implementation <span style="color: green; font-weight: bold;">'androidx.constraintlayout:constraintlayout:2.0.4'<br /></span><span style="color: green; font-weight: bold;"> </span>testImplementation <span style="color: green; font-weight: bold;">'junit:junit:4.13'<br /></span><span style="color: green; font-weight: bold;"> </span>androidTestImplementation <span style="color: green; font-weight: bold;">'androidx.test.ext:junit:1.1.2'<br /></span><span style="color: green; font-weight: bold;"> </span>androidTestImplementation <span style="color: green; font-weight: bold;">'androidx.test.espresso:espresso-core:3.3.0'<br /></span><span style="color: green; font-weight: bold;"><br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: grey; font-style: italic;">//Tambahan Library yang dibutuhkan<br /></span><span style="color: grey; font-style: italic;"> </span>implementation <span style="color: green; font-weight: bold;">'androidx.recyclerview:recyclerview:1.1.0'<br /></span><span style="color: green; font-weight: bold;"> </span>implementation <span style="color: green; font-weight: bold;">'com.makeramen:roundedimageview:2.3.0'<br /></span><span style="font-weight: bold;">}</span></pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="8611345975" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><p><span style="font-family: georgia;">Dalam tutorial ini, akan terdapat 8 item <b>RecyclerView </b>dimana setiap item akan terdiri dari gambar poster, nama, pembuat, rating, dan cuplikan cerita dari program TV tersebut. Seluruh gambar saya unduh dari pencarian Google. Jika Anda mengikuti contoh ini, silahkan <b><a href="https://drive.google.com/file/d/1MIGHXevAT8X-YN-uKwi7dmQeU9sT0G1f/view?usp=sharing" target="_blank">download disini</a> </b>kemudian copy gambar-gambar tersebut dan tempatkan di folder <b>drawable </b>project Anda.</span></p><p><span style="font-family: georgia;">Selanjutnya 2 buah icon xml sebagai tombol menu dan tombol pencarian, letaknya juga di folder <b>drawable</b>, dapat dibuat melalui Vector Asset di Android Studio. Untuk saat ini, kedua icon tombol tersebut hanya sebagai penghias semata (belum difungsikan). Berikut isi dari kedua icon xml tersebut.</span></p><span style="font-family: georgia;"><b>ic_menu.xml</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="?attr/colorControlNormal"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M4,18h16c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1L4,16c-0.55,0 -1,0.45 -1,1s0.45,1 1,1zM4,13h16c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1L4,11c-0.55,0 -1,0.45 -1,1s0.45,1 1,1zM3,7c0,0.55 0.45,1 1,1h16c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1L4,6c-0.55,0 -1,0.45 -1,1z"</span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><b>ic_search.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="?attr/colorControlNormal"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M15.5,14h-0.79l-0.28,-0.27c1.2,-1.4 1.82,-3.31 1.48,-5.34 -0.47,-2.78 -2.79,-5 -5.59,-5.34 -4.23,-0.52 -7.79,3.04 -7.27,7.27 0.34,2.8 2.56,5.12 5.34,5.59 2.03,0.34 3.94,-0.28 5.34,-1.48l0.27,0.28v0.79l4.25,4.25c0.41,0.41 1.08,0.41 1.49,0 0.41,-0.41 0.41,-1.08 0,-1.49L15.5,14zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"</span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div></span><p><span style="font-family: georgia;"><br /></span></p><p><span style="font-family: georgia;">Kemudian kita juga akan membutuhkan 2 buah background drawable yang dapat dibuat melalui Drawable Resource File. Letaknya juga berada di dalam folder <b>drawable</b>. Background ini difungsikan untuk item saat dipilih. Berikut isi kedua file ini.</span></p><span style="font-family: georgia;"><b>tvshow_bg.xml :</b></span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">shape </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:shape</span><span style="color: green; font-weight: bold;">="rectangle"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">solid </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:color</span><span style="color: green; font-weight: bold;">="@color/colorTextIcons"</span>/><br /> <<span style="color: navy; font-weight: bold;">corners </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:radius</span><span style="color: green; font-weight: bold;">="6dp"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">shape</span>></pre></div><div><br /></div><span style="font-family: georgia;"><b>tvshow_selected_bg.xml :</b></span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">shape </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:shape</span><span style="color: green; font-weight: bold;">="rectangle"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">solid </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:color</span><span style="color: green; font-weight: bold;">="@color/colorTextIcons"</span>/><br /> <<span style="color: navy; font-weight: bold;">corners </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:radius</span><span style="color: green; font-weight: bold;">="6dp"</span>/><br /> <<span style="color: navy; font-weight: bold;">stroke<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="2dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:color</span><span style="color: green; font-weight: bold;">="@color/colorAccent"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">shape</span>></pre></div><p><span style="font-family: georgia;"><br /></span></p><p><span style="font-family: georgia;">Sebelum kita memulai layout dan koding java, berikut isi dari value colors.xml dan styles.xml yang digunakan untuk contoh ini.</span></p><span style="font-family: georgia;"><b>colors.xml</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">resources</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimary"</span>>#41D3BD</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimaryDark"</span>>#41D3BD</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorAccent"</span>>#FDA14F</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorTextIcons"</span>>#FFFFFF</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorTextPrimary"</span>>#212121</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorTextSecondary"</span>>#757575</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorRartingActive"</span>>#FDA14F</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorRatingInactive"</span>>#C9C9D1</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorBackground"</span>>#EEEEEE</<span style="color: navy; font-weight: bold;">color</span>><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div><span style="font-family: georgia;"><span style="font-family: georgia;"><div><span style="font-family: georgia;"><span style="font-family: georgia;"><br /></span></span></div><b>styles.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">resources</span>><br /> <span style="color: grey; font-style: italic;"><!-- Base application theme. --><br /></span><span style="color: grey; font-style: italic;"> </span><<span style="color: navy; font-weight: bold;">style </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="AppTheme" </span><span style="color: blue; font-weight: bold;">parent</span><span style="color: green; font-weight: bold;">="Theme.AppCompat.Light.NoActionBar"</span>><br /> <span style="color: grey; font-style: italic;"><!-- Customize your theme here. --><br /></span><span style="color: grey; font-style: italic;"> </span><<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimary"</span>>@color/colorPrimary</<span style="color: navy; font-weight: bold;">item</span>><br /> <<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimaryDark"</span>>@color/colorPrimaryDark</<span style="color: navy; font-weight: bold;">item</span>><br /> <<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorAccent"</span>>@color/colorAccent</<span style="color: navy; font-weight: bold;">item</span>><br /> </<span style="color: navy; font-weight: bold;">style</span>><br /><br /> <<span style="color: navy; font-weight: bold;">style </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="RatingBar" </span><span style="color: blue; font-weight: bold;">parent</span><span style="color: green; font-weight: bold;">="AppTheme"</span>><br /> <<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorControlActivated"</span>>@color/colorRartingActive</<span style="color: navy; font-weight: bold;">item</span>><br /> <<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorControlNormal"</span>>@color/colorRatingInactive</<span style="color: navy; font-weight: bold;">item</span>><br /> </<span style="color: navy; font-weight: bold;">style</span>><br /><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div><div><span style="font-family: georgia;"><span style="font-family: georgia;"><br /></span></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Ada 2 layout yang digunakan dalam contoh ini, yakni layout untuk activity dan layout untuk item-item pada RecyclerView. Untuk layout activity yaitu <b>activity_main.xml</b>, terdapat 5 komponen view didalamnya : sebuah TextView, 2 buah ImageView, sebuah Button, dan sebuah RecyclerView tentunya. Berikut isinya :<br /><br /><b>activity_main.xml</b> : <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:animateLayoutChanges</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorBackground"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutHeader"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="?actionBarSize"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorPrimary"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center_vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="parent"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="25dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="25dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/ic_menu"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="@color/colorTextIcons" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_weight</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="TV Shows"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorTextIcons"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="16sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="25dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="25dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/ic_search"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="@color/colorTextIcons" </span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/tvShowsRv"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:clipToPadding</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layoutManager</span><span style="color: green; font-weight: bold;">="androidx.recyclerview.widget.LinearLayoutManager"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintBottom_toTopOf</span><span style="color: green; font-weight: bold;">="@id/btnAddWatchlist"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toBottomOf</span><span style="color: green; font-weight: bold;">="@id/layoutHeader"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/btnAddWatchlist"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="40dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Add to Watchlist"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textAllCaps</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorTextPrimary"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="15sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:visibility</span><span style="color: green; font-weight: bold;">="gone"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintBottom_toBottomOf</span><span style="color: green; font-weight: bold;">="parent"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><br /><br /></span></span><div><span style="font-family: georgia;"><span style="font-family: georgia;">Selanjutnya layout untuk untuk item pada <b>RecyclerView </b>memiliki 8 komponen view yakni : 2 buah View, sebuah RoundedImageView (berasal dari library yang ditambahkan sebelumnya pada file buil.gradle), sebuah RatingBar, sebuah ImageView, dan 3 buah TextView. Layout ini kita beri nama <b>item_rv_tvshows.xml</b>. Berikut isinya :<br /><b><br /></b></span></span></div><div><span style="font-family: georgia;"><span style="font-family: georgia;"><b>item_rv_tvshows.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutTvshow"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="5dp"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">View<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/viewSupporter"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="15dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toBottomOf</span><span style="color: green; font-weight: bold;">="@id/imageTvshow"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">View<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/viewBackground"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@drawable/tvshow_bg"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingBottom</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintBottom_toBottomOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="@+id/textName"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">com.makeramen.roundedimageview.RoundedImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/imageTvshow"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="110dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="130dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="15dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:scaleType</span><span style="color: green; font-weight: bold;">="centerCrop"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintStart_toStartOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:riv_corner_radius</span><span style="color: green; font-weight: bold;">="6dp"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="35dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingTop</span><span style="color: green; font-weight: bold;">="15dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorTextPrimary"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="20sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintEnd_toEndOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintStart_toEndOf</span><span style="color: green; font-weight: bold;">="@id/imageTvshow"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="@id/imageTvshow"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">RatingBar<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/ratingBar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">style</span><span style="color: green; font-weight: bold;">="@style/Widget.AppCompat.RatingBar.Small"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:isIndicator</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:numStars</span><span style="color: green; font-weight: bold;">="5"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:theme</span><span style="color: green; font-weight: bold;">="@style/RatingBar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintStart_toStartOf</span><span style="color: green; font-weight: bold;">="@id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toBottomOf</span><span style="color: green; font-weight: bold;">="@id/textName"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textCreatedBy"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:ellipsize</span><span style="color: green; font-weight: bold;">="end"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:maxLines</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorTextSecondary"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="17sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintEnd_toEndOf</span><span style="color: green; font-weight: bold;">="@id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintStart_toStartOf</span><span style="color: green; font-weight: bold;">="@id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toBottomOf</span><span style="color: green; font-weight: bold;">="@id/ratingBar"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textStory"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="2dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:ellipsize</span><span style="color: green; font-weight: bold;">="end"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:maxLines</span><span style="color: green; font-weight: bold;">="2"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorTextSecondary"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="13sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintEnd_toEndOf</span><span style="color: green; font-weight: bold;">="@id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintStart_toStartOf</span><span style="color: green; font-weight: bold;">="@id/textName"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toBottomOf</span><span style="color: green; font-weight: bold;">="@id/textCreatedBy"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/imageSelected"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="50dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="50dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/checkmark"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:visibility</span><span style="color: green; font-weight: bold;">="gone"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintBottom_toTopOf</span><span style="color: green; font-weight: bold;">="@id/viewBackground"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintEnd_toEndOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="@id/viewBackground"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div></span></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Sekarang kita beralih pada koding java. Ada 4 java class yang kita gunakan dalam contoh ini yakni : tentunya class activity (<b>MainActivity.java</b>), class adapter (<b>TvShowsAdapter</b>), class untuk menyetel nilai-nilai object View yang ada pada item RecyclerView (<b>TvShow.java</b>), d</span><span style="font-family: georgia;">an interface sebagai listener item dipilih atau tidak pada RecyclerView (<b>TvShowsListener.java</b>).</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kita mulai dari <b>TvShowsListener.java</b>. Interface ini hanya berisi sebuah metode void dengan Boolean sebagai argumennya. Interface ini untuk menyetel visibility dari komponen Button yang ada di activity_main.xml. Saat Anda membuat file ini, pilih <i>interface </i>pada pilihan yang tersedia, atau bisa juga tidak, lalu salin kode dibawah ini maka class secara otomatis menjadi interface. Berikut isinya :<br /><br /><b>TvShowsListener.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.rvmultiple;<br /><br /><span style="color: navy; font-weight: bold;">interface </span>TvShowsListener {<br /> <span style="color: navy; font-weight: bold;">void </span>onTvShowAction (Boolean isSelected);<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Berikutnya file <b>TvShow.java</b> yang fungsinya sudah dijelaskan diatas. Dalam file ini terdapat 3 variabel dari object <b><i>String </i></b>untuk menyetel nilai string TextView nama, TextView pembuat, dan TextView cuplikan cerita. Kemudian sebuah <b><i>int </i></b>untuk menyetel Gambar poster. Sebuah <b><i>Boolean </i></b>untuk memberi 2 fungsi (dipilih dan tidak dipilih) saat item diklik, dengan nilai default <i>false</i>. Terakhir sebuah <b><i>float </i></b>untuk menyetel nilai rating. Berikut isinya :<br /><br /><b>TvShow.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.rvmultiple;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>TvShow {<br /> String <span style="color: #660e7a; font-weight: bold;">name</span>, <span style="color: #660e7a; font-weight: bold;">createdBy</span>, <span style="color: #660e7a; font-weight: bold;">story</span>;<br /> <span style="color: navy; font-weight: bold;">int </span><span style="color: #660e7a; font-weight: bold;">image</span>;<br /> Boolean <span style="color: #660e7a; font-weight: bold;">isSelected </span>= <span style="color: navy; font-weight: bold;">false</span>;<br /> <span style="color: navy; font-weight: bold;">float </span><span style="color: #660e7a; font-weight: bold;">rating</span>;<br />}<br /></pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Selanjutnya class <b>TvShowsAdapter.java</b>. Sebagaimana mestinya fungsi adapter, di class ini kita menghubungkan layout <b>item_rv_tvshows.xml</b> dengan <b>RecyclerView</b>. Untuk metode klik listener item dipilih dan tidak dipilih kita aplikasikan pada <b><i>ConstrainLayout </i></b>yang ada di <b>item_rv_tvshows.xml</b>, dan <b><i>Boolean </i></b>yang ada pada <b>TvShow.java</b> sebelumnya memberikan 2 fungsi kepada metode klik listener ini. Berikut isi selengkapnya :<br /><br /><b>TvShowsAdapter.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.rvmultiple;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.view.LayoutInflater;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.ViewGroup;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ImageView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.RatingBar;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.annotation.<span style="color: olive;">NonNull</span>;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.constraintlayout.widget.ConstraintLayout;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.recyclerview.widget.RecyclerView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>com.makeramen.roundedimageview.RoundedImageView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.ArrayList;<br /><span style="color: navy; font-weight: bold;">import </span>java.util.List;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>TvShowsAdapter <span style="color: navy; font-weight: bold;">extends </span>RecyclerView.Adapter<TvShowsAdapter.TvShowViewHolder> {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>List<TvShow> <span style="color: #660e7a; font-weight: bold;">tvShows</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>TvShowsListener <span style="color: #660e7a; font-weight: bold;">tvShowsListener</span>;<br /><br /> <span style="color: navy; font-weight: bold;">public </span>TvShowsAdapter(List<TvShow> tvShows, TvShowsListener tvShowsListener) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">tvShows </span>= tvShows;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">tvShowsListener </span>= tvShowsListener;<br /> }<br /><br /> <span style="color: olive;">@NonNull<br /></span><span style="color: olive;"> @Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public </span>TvShowViewHolder onCreateViewHolder(<span style="color: olive;">@NonNull </span>ViewGroup parent, <span style="color: navy; font-weight: bold;">int </span>viewType) {<br /> <span style="color: navy; font-weight: bold;">return new </span>TvShowViewHolder(LayoutInflater.<span style="font-style: italic;">from</span>(parent.getContext()).inflate(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">item_rv_tvshows</span>,<br /> parent, <span style="color: navy; font-weight: bold;">false</span>));<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onBindViewHolder(<span style="color: olive;">@NonNull </span>TvShowViewHolder holder, <span style="color: navy; font-weight: bold;">int </span>position) {<br /> holder.bindTvShow(<span style="color: #660e7a; font-weight: bold;">tvShows</span>.get(position));<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public int </span>getItemCount() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">tvShows</span>.size();<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public </span>List<TvShow> getSelectedTvShows () {<br /> List<TvShow> selectedTvShows = <span style="color: navy; font-weight: bold;">new </span>ArrayList<>();<br /> <span style="color: navy; font-weight: bold;">for </span>(TvShow tvShow : <span style="color: #660e7a; font-weight: bold;">tvShows</span>) {<br /> <span style="color: navy; font-weight: bold;">if </span>(tvShow.<span style="color: #660e7a; font-weight: bold;">isSelected</span>) {<br /> selectedTvShows.add(tvShow);<br /> }<br /> }<br /> <span style="color: navy; font-weight: bold;">return </span>selectedTvShows;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">class </span>TvShowViewHolder <span style="color: navy; font-weight: bold;">extends </span>RecyclerView.ViewHolder {<br /> ConstraintLayout <span style="color: #660e7a; font-weight: bold;">layoutTvshow</span>;<br /> View <span style="color: #660e7a; font-weight: bold;">viewBackground</span>;<br /> RoundedImageView <span style="color: #660e7a; font-weight: bold;">imageTvshow</span>;<br /> TextView <span style="color: #660e7a; font-weight: bold;">textName</span>, <span style="color: #660e7a; font-weight: bold;">textCreatedBy</span>, <span style="color: #660e7a; font-weight: bold;">textStory</span>;<br /> RatingBar <span style="color: #660e7a; font-weight: bold;">ratingBar</span>;<br /> ImageView <span style="color: #660e7a; font-weight: bold;">imageSelected</span>;<br /><br /> TvShowViewHolder(<span style="color: olive;">@NonNull </span>View itemView) {<br /> <span style="color: navy; font-weight: bold;">super</span>(itemView);<br /> <span style="color: #660e7a; font-weight: bold;">layoutTvshow </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layoutTvshow</span>);<br /> <span style="color: #660e7a; font-weight: bold;">viewBackground </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">viewBackground</span>);<br /> <span style="color: #660e7a; font-weight: bold;">imageTvshow </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">imageTvshow</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textName </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textName</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textCreatedBy </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textCreatedBy</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textStory </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textStory</span>);<br /> <span style="color: #660e7a; font-weight: bold;">ratingBar </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ratingBar</span>);<br /> <span style="color: #660e7a; font-weight: bold;">imageSelected </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">imageSelected</span>);<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">void </span>bindTvShow(<span style="color: navy; font-weight: bold;">final </span>TvShow tvShow) {<br /> <span style="color: #660e7a; font-weight: bold;">imageTvshow</span>.setImageResource(tvShow.<span style="color: #660e7a; font-weight: bold;">image</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textName</span>.setText(tvShow.<span style="color: #660e7a; font-weight: bold;">name</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textCreatedBy</span>.setText(tvShow.<span style="color: #660e7a; font-weight: bold;">createdBy</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textStory</span>.setText(tvShow.<span style="color: #660e7a; font-weight: bold;">story</span>);<br /> <span style="color: #660e7a; font-weight: bold;">ratingBar</span>.setRating(tvShow.<span style="color: #660e7a; font-weight: bold;">rating</span>);<br /> <span style="color: navy; font-weight: bold;">if </span>(tvShow.<span style="color: #660e7a; font-weight: bold;">isSelected</span>) {<br /> <span style="color: #660e7a; font-weight: bold;">viewBackground</span>.setBackgroundResource(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tvshow_selected_bg</span>);<br /> <span style="color: #660e7a; font-weight: bold;">imageSelected</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> <span style="color: #660e7a; font-weight: bold;">viewBackground</span>.setBackgroundResource(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tvshow_bg</span>);<br /> <span style="color: #660e7a; font-weight: bold;">imageSelected</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> }<br /><br /> <span style="color: #660e7a; font-weight: bold;">layoutTvshow</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a;">tvShow</span>.<span style="color: #660e7a; font-weight: bold;">isSelected</span>) {<br /> <span style="color: #660e7a; font-weight: bold;">viewBackground</span>.setBackgroundResource(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tvshow_bg</span>);<br /> <span style="color: #660e7a; font-weight: bold;">imageSelected</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a;">tvShow</span>.<span style="color: #660e7a; font-weight: bold;">isSelected </span>= <span style="color: navy; font-weight: bold;">false</span>;<br /> <span style="color: navy; font-weight: bold;">if </span>(getSelectedTvShows().size() == <span style="color: blue;">0</span>) {<br /> <span style="color: #660e7a; font-weight: bold;">tvShowsListener</span>.onTvShowAction(<span style="color: navy; font-weight: bold;">false</span>);<br /> }<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> <span style="color: #660e7a; font-weight: bold;">viewBackground</span>.setBackgroundResource(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tvshow_selected_bg</span>);<br /> <span style="color: #660e7a; font-weight: bold;">imageSelected</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> <span style="color: #660e7a;">tvShow</span>.<span style="color: #660e7a; font-weight: bold;">isSelected </span>= <span style="color: navy; font-weight: bold;">true</span>;<br /> <span style="color: #660e7a; font-weight: bold;">tvShowsListener</span>.onTvShowAction(<span style="color: navy; font-weight: bold;">true</span>);<br /> }<br /> }<br /> });<br /> }<br /> }<br />}<br /></pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Terakhir <b>MainActivity.java</b>, class ini kita buat mengimplementasi interface TvShowListener tadi supaya dapat mengatur visibility dari <b><i>Button </i></b>Add to Watchlist. Saat ada satu atau lebih item yang dipilih, visibility dari <b><i>Button </i></b>ini kita perlihatkan. Saat tidak ada item dipilih, visibility kita hilangkan.<div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Untuk metode klik listener <b>Button </b>Add to Watchlist, akan menampilkan pesan toast dengan isinya nama dari program TV shows. Nilai-nilai string untuk TextView nama, TextView pembuat, TextView cuplikan cerita, source gambar, dan float rating kita tentukan di dalam class ini.<br /><br /><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.rvmultiple;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.recyclerview.widget.RecyclerView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.Gravity;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Button;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.ArrayList;<br /><span style="color: navy; font-weight: bold;">import </span>java.util.List;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity <span style="color: navy; font-weight: bold;">implements </span>TvShowsListener {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>Button <span style="color: #660e7a; font-weight: bold;">btnAddWatchlist</span>;<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> RecyclerView tvShowsRv = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tvShowsRv</span>);<br /> <span style="color: #660e7a; font-weight: bold;">btnAddWatchlist </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">btnAddWatchlist</span>);<br /><br /> List<TvShow> tvShows = <span style="color: navy; font-weight: bold;">new </span>ArrayList<>();<br /><br /> TvShow gameOfThrones = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> gameOfThrones.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_game_of_thones</span>;<br /> gameOfThrones.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"Game of Thrones"</span>;<br /> gameOfThrones.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">4.7f</span>;<br /> gameOfThrones.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"David Benioff, D.B. Weiss"</span>;<br /> gameOfThrones.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"Nine noble families fight for control over the lands of Westeros, while an ancient enemy returns after being dormant for millennia."</span>;<br /> tvShows.add(gameOfThrones);<br /><br /> TvShow superGirl = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> superGirl.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_supergirl</span>;<br /> superGirl.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"Supergirl"</span>;<br /> superGirl.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">4.1f</span>;<br /> superGirl.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"Ali Adler, Greg Berlanti, Andrew Kreisberg"</span>;<br /> superGirl.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"The adventures of Superman's cousin in her own superhero career."</span>;<br /> tvShows.add(superGirl);<br /><br /> TvShow theFlash = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> theFlash.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_the_flash</span>;<br /> theFlash.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"The Flash"</span>;<br /> theFlash.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">4.0f</span>;<br /> theFlash.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"Greg Berlanti, Geoff Johns, Andrew Kreisberg"</span>;<br /> theFlash.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"After being struck by lightning, Barry Allen wakes up from his coma to discover he's been given the power of super speed, " </span>+<br /> <span style="color: green; font-weight: bold;">"becoming the next Flash, fighting crime in Central City."</span>;<br /> tvShows.add(theFlash);<br /><br /> TvShow spartacus = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> spartacus.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_spartacus</span>;<br /> spartacus.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"Spartacus"</span>;<br /> spartacus.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">4.4f</span>;<br /> spartacus.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"Steven S. DeKnight"</span>;<br /> spartacus.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"The life of Spartacus, the gladiator who lead a rebellion against the Romans. From his time as an ally of the Romans, " </span>+<br /> <span style="color: green; font-weight: bold;">"to his betrayal and becoming a gladiator, to the rebellion he leads and its ultimate outcome."</span>;<br /> tvShows.add(spartacus);<br /><br /> TvShow vikings = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> vikings.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_vikings</span>;<br /> vikings.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"Vikings"</span>;<br /> vikings.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">4.5f</span>;<br /> vikings.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"Michael Hirst"</span>;<br /> vikings.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"Vikings transports us to the brutal and mysterious world of Ragnar Lothbrok, " </span>+<br /> <span style="color: green; font-weight: bold;">"a Viking warrior and farmer who yearns to explore - and raid - the distant shores across the ocean."</span>;<br /> tvShows.add(vikings);<br /><br /> TvShow carnivalRow = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> carnivalRow.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_carnival_row</span>;<br /> carnivalRow.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"Carnival Row"</span>;<br /> carnivalRow.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">3.9f</span>;<br /> carnivalRow.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"Travis Beacham, René Echevarria"</span>;<br /> carnivalRow.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"A human detective and a fairy rekindle a dangerous affair in a Victorian fantasy world, " </span>+<br /> <span style="color: green; font-weight: bold;">"where the city's uneasy peace collapses when a string of murders reveals an unimaginable monster."</span>;<br /> tvShows.add(carnivalRow);<br /><br /> TvShow theWalkingDead = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> theWalkingDead.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_the_walking_dead</span>;<br /> theWalkingDead.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"The Walking Dead"</span>;<br /> theWalkingDead.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">4.2f</span>;<br /> theWalkingDead.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"Frank Darabont, Angela Kang"</span>;<br /> theWalkingDead.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"Sheriff Deputy Rick Grimes wakes up from a coma to learn the world is in ruins and must lead a group of survivors to stay alive."</span>;<br /> tvShows.add(theWalkingDead);<br /><br /> TvShow theLastKingdom = <span style="color: navy; font-weight: bold;">new </span>TvShow();<br /> theLastKingdom.<span style="color: #660e7a; font-weight: bold;">image </span>= R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tv_the_last_kingdom</span>;<br /> theLastKingdom.<span style="color: #660e7a; font-weight: bold;">name </span>= <span style="color: green; font-weight: bold;">"The Last Kingdom"</span>;<br /> theLastKingdom.<span style="color: #660e7a; font-weight: bold;">rating </span>= <span style="color: blue;">4.6f</span>;<br /> theLastKingdom.<span style="color: #660e7a; font-weight: bold;">createdBy </span>= <span style="color: green; font-weight: bold;">"Bernard Cornwell"</span>;<br /> theLastKingdom.<span style="color: #660e7a; font-weight: bold;">story </span>= <span style="color: green; font-weight: bold;">"As Alfred the Great defends his kingdom from Norse invaders, Uhtred - born a Saxon but raised by Vikings - seeks to claim his ancestral birthright."</span>;<br /> tvShows.add(theLastKingdom);<br /><br /> <span style="color: navy; font-weight: bold;">final </span>TvShowsAdapter tvShowsAdapter = <span style="color: navy; font-weight: bold;">new </span>TvShowsAdapter(tvShows, <span style="color: navy; font-weight: bold;">this</span>);<br /> tvShowsRv.setAdapter(tvShowsAdapter);<br /><br /> <span style="color: #660e7a; font-weight: bold;">btnAddWatchlist</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> List<TvShow> selectedTvShows = <span style="color: #660e7a;">tvShowsAdapter</span>.getSelectedTvShows();<br /> StringBuilder tvShowNames = <span style="color: navy; font-weight: bold;">new </span>StringBuilder();<br /> <span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>i = <span style="color: blue;">0</span>; i < selectedTvShows.size(); i++) {<br /> <span style="color: navy; font-weight: bold;">if </span>(i == <span style="color: blue;">0</span>) {<br /> tvShowNames.append(selectedTvShows.get(i).<span style="color: #660e7a; font-weight: bold;">name</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> tvShowNames.append(<span style="color: green; font-weight: bold;">"</span><span style="color: navy; font-weight: bold;">\n</span><span style="color: green; font-weight: bold;">"</span>).append(selectedTvShows.get(i).<span style="color: #660e7a; font-weight: bold;">name</span>);<br /> }<br /> }<br /> Toast toast = Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), tvShowNames.toString(), Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>);<br /> toast.setGravity(Gravity.<span style="color: #660e7a; font-style: italic; font-weight: bold;">CENTER</span>, <span style="color: blue;">0</span>, <span style="color: blue;">0</span>);<br /> toast.show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onTvShowAction(Boolean isSelected) {<br /> <span style="color: navy; font-weight: bold;">if </span>(isSelected) {<br /> <span style="color: #660e7a; font-weight: bold;">btnAddWatchlist</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> <span style="color: #660e7a; font-weight: bold;">btnAddWatchlist</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> }<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Demikian tutorial ini. Terima kasih untuk kunjungan dan waktunya. Jika ada pertanyaan silahkan di sampaikan di kolom komentar.</span></div></span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-20273027929999220832020-11-20T15:55:00.003+07:002021-02-22T18:45:04.530+07:00Membuat Sebuah Tombol (Listener) Memiliki Banyak Fungsi<span style="font-family: georgia;">Sebuah tombol pada aplikasi dapat diberikan beberapa atau banyak fungsi. Tombol disini maksudnya adalah objek yang diberikan "<i>listener</i>", tentu tidak harus objek / komponen Button, objek lain seperti Textview, Imageview ataupun Layout dapat diberikan listener.</span><div><span><a name='more'></a></span><span style="font-family: georgia;"><br /></span><div><div><span style="font-family: georgia;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/f29lBbgHHls" width="320" youtube-src-id="f29lBbgHHls"></iframe></div><br /><p><span style="font-family: georgia;">Dalam menerapkannya kita dapat menggunakan bantuan <i>java switch statement</i> ataupun <i>if-else statement</i> pada String, int, short, long, boolean dan lain sebagainya yang dapat bekerja pada switch atau if-else statement. Namun untuk boolean tentu kita akan mendapatkan hanya 2 fungsi saja karena memang nilai dari boolean hanya 2 (true dan false).</span></p><p><span style="font-family: georgia;">Baik, di artikel ini kita akan membuat 7 fungsi dari sebuah tombol / listener, menggunakan objek Button dengan <b>java switch statement</b>. Tentu Anda dapat membuat sebanyak mungkin yang diinginkan, karena ini hanya contoh.</span></p><p><span style="font-family: georgia;"><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="8611345975" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Misalnya kita ingin mengucapkan salam atau pesan kepada pengguna aplikasi yang baru saja menginstall aplikasi kita. Pada layout <b>activity_main.xml</b>, kita memiliki sebuah Textview dan sebuah Button. Nilai string dari Textview tersebut akan kita buat berubah dan berbeda sebanyak 7 kali setiap kali mengklik tombol. Lalu pada klik yang ke-7 pada tombol, visibility dari Textview dan Button kita hilangkan. Berikut isi layoutnya.</span></p><span style="font-family: georgia;"><b>activity_main.xml</b> : </span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintBottom_toBottomOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintLeft_toLeftOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintRight_toRightOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="parent"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textPesan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="40dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Selamat datang!"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/colorPrimaryDark"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/tombolNext"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Selanjutnya"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><span style="font-family: georgia;"><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Lalu pada <b>MainActivity.java</b>, pertama kita variabelkan dulu sebuah objek string dengan nilai "klik 1", kemudian memvariabelkan sebuah objek Textview dan Button / Tombol untuk kedua komponen view yang ada pada layout diatas. Lalu membuat klik listener untuk Button / Tombol tersebut.</span></div><div><span style="font-family: georgia;"><br /></span>
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="1348961299" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Di dalam klik listener dari tombol tersebut kita sematkan java switch statement untuk objek string yang divariabelkan tadi. Pada statement 1 sampai 6 (dari 7 statement), kita mengganti nilai dari objek string tadi, dan juga mengganti nilai string dari Textview. Pada statement 6, nilai string dari tombol juga diubah menjadi "selesai", dan khusus pada statement 7 tidak diubah nilainya namun dihilangkan visibility nya. Contoh berikut kita menggunakan <b><i>String java switch statement</i></b>.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.tombol;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Button;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: green; font-weight: bold;">"klik 1"</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>TextView <span style="color: #660e7a; font-weight: bold;">textPesan</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>Button <span style="color: #660e7a; font-weight: bold;">tombolNext</span>;<br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">textPesan </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textPesan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolNext</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: navy; font-weight: bold;">switch </span>(<span style="color: #660e7a; font-weight: bold;">setKlik</span>) {<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"klik 1"</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Terima kasih sudah mengunduh aplikasi ini."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: green; font-weight: bold;">"klik 2"</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"klik 2"</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Aplikasi ini dirancang khusus untuk kamu yang suka nge-klik tombol."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: green; font-weight: bold;">"klik 3"</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"klik 3"</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Di desain menarik dan akan mudah untuk dipahami."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: green; font-weight: bold;">"klik 4"</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"klik 4"</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Antarmuka pengguna yang simpel dan tentunya dukungan 24 jam jika kamu membutuhkan bantuan."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: green; font-weight: bold;">"klik 5"</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"klik 5"</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Semoga kamu menyukai aplikasi ini. Dan jangan lupa untuk memberikan ulasan kamu tentang aplikasi di Playstore ya.."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: green; font-weight: bold;">"klik 6"</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"klik 6" </span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Klik tombol dibawah untuk mengakhiri pesan sambutan ini."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: green; font-weight: bold;">"klik 7"</span>;<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setText(<span style="color: green; font-weight: bold;">"Selesai"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"klik 7" </span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /> });<br /> }<br />}</pre></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><br /></span></div>Bagaimana jika menggunakan object <b>int </b>pada <b><i>switch statement</i></b>? Kita hanya perlu merubah variabel object <b>String </b>menjadi <b>int </b>dan mengganti nilai string pada setiap statement menjadi integer (bilangan / angka bulat). Sehingga akan terlihat seperti berikut ini :</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> (int switch statement) :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.tombol;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Button;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: blue;">1</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>TextView <span style="color: #660e7a; font-weight: bold;">textPesan</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>Button <span style="color: #660e7a; font-weight: bold;">tombolNext</span>;<br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">textPesan </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textPesan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolNext</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: navy; font-weight: bold;">switch </span>(<span style="color: #660e7a; font-weight: bold;">setKlik</span>) {<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">1</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Terima kasih sudah mengunduh aplikasi ini."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: blue;">2</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">2</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Aplikasi ini dirancang khusus untuk kamu yang suka nge-klik tombol."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: blue;">3</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">3</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Di desain menarik dan akan mudah untuk dipahami."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: blue;">4</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">4</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Antarmuka pengguna yang simpel dan tentunya dukungan 24 jam jika kamu membutuhkan bantuan."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: blue;">5</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">5</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Semoga kamu menyukai aplikasi ini. Dan jangan lupa untuk memberikan ulasan kamu tentang aplikasi di Playstore ya.."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: blue;">6</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">6 </span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Klik tombol dibawah untuk mengakhiri pesan sambutan ini."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: blue;">7</span>;<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setText(<span style="color: green; font-weight: bold;">"Selesai"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">7 </span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /> });<br /> }<br />}</pre></div></span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="3360046960" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Int dan model data bilangan lainnya didalam sebuah metode klik listener atau di dalam onCreate activity, nilainya dapat terus berubah (bertambah atau berkurang tergantung dari operasi bilangan) secara otomatis setiap kali tombol tersebut di klik. Pada contoh diatas, di setiap statement kita menuliskan dan merubah nilai dari int itu secara manual.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Dengan memasukkan kode operasi bilangan matematika, maka setiap kali tombol di klik, nilai int akan berubah. Contoh kode misalnya : <span style="color: #38761d;">setKlik = setKlik + 2</span>. Maka setiap kali tombol di klik, nilai int akan bertambah 2, hingga nilai maksimum yang dapat ditampung int.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Seperti contoh diatas, pertama-tama kita hapuskan dulu nilai dari variabel int, kemudian di dalam metode klik tombol sebelum java switch statement kita tuliskan kode operasi bilangan matematika, lalu menghapus semua nilai int pada setiap statement. Maka akan terlihat seperti dibawah ini :</span></div><div><span style="font-family: georgia;"><br /><b>MainActivity.java</b> :</span><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.tombol;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Button;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">setKlik</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>TextView <span style="color: #660e7a; font-weight: bold;">textPesan</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>Button <span style="color: #660e7a; font-weight: bold;">tombolNext</span>;<br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">textPesan </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textPesan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolNext</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: #660e7a; font-weight: bold;">setKlik </span>= <span style="color: #660e7a; font-weight: bold;">setKlik </span>+ <span style="color: blue;">1</span>;<br /> <span style="color: navy; font-weight: bold;">switch </span>(<span style="color: #660e7a; font-weight: bold;">setKlik</span>) {<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">1</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Terima kasih sudah mengunduh aplikasi ini."</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">2</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Aplikasi ini dirancang khusus untuk kamu yang suka nge-klik tombol."</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">3</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Di desain menarik dan akan mudah untuk dipahami."</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">4</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Antarmuka pengguna yang simpel dan tentunya dukungan 24 jam jika kamu membutuhkan bantuan."</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">5</span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Semoga kamu menyukai aplikasi ini. Dan jangan lupa untuk memberikan ulasan kamu tentang aplikasi di Playstore ya.."</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">6 </span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setText(<span style="color: green; font-weight: bold;">"Klik tombol dibawah untuk mengakhiri pesan sambutan ini."</span>);<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setText(<span style="color: green; font-weight: bold;">"Selesai"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">7 </span>:<br /> <span style="color: #660e7a; font-weight: bold;">textPesan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">tombolNext</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /> });<br /> }<br />}</pre></div><div><br /></div><div><br /></div><span style="font-family: georgia;">Dari kode diatas, nilai awal int adalah 0. Klik pertama akan memberi nilainnya menjadi 1, klik kedua menjadi 2 dan seterusnya hingga 7. Nilai maksimumnya 7 karena saat nilainya berada di 7 kita menghilangkan visibility dari tombol yang tentunya membuat tombol tidak dapat diakses lagi. </span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Sekian tutorial sederhana ini, jika ada salah atau kurang harap dimaklumi. Terimakasih, bila ada pertanyaan bisa disampaikan di kolom komentar.</span></div></div></div></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-33188537092443786262020-11-18T20:18:00.005+07:002021-02-22T18:45:58.873+07:00Menghubungkan Aplikasi Ke Google Firebase Analytics<p><span style="font-family: georgia;"><b>Analytics </b>tentu sangat berguna bagi kita sebagai developer untuk mengetahui statistik aplikasi dan kegiatan yang dilakukan pengguna di aplikasi kita.</span> Kita dapat mengetahui jumlah download aplikasi, aktivitas-aktivitas pada aplikasi yang dikunjungi pengguna, asal negara pengguna, jumlah pengguna yang sedang online, jumlah pengguna baru, jumlah pengguna aktif, dan lain sebagainnya.<span></span></p><a name='more'></a><p></p><p>Baik, pertama yang perlu dilakukan tentunya mendaftar terlebih dahulu ke <b>Google Firebase</b>. Disini kita harus menyiapkan project aplikasi Android dan browser internet secara bersamaan. Untuk mendaftar ke Firebase, buka browser internet Anda dan silahkan terlebih dahulu login ke <b>Google Account</b>, setelah itu buka <a href="https://firebase.google.com/" target="_blank"><b>halaman website Google Firebase</b></a>, lalu klik <b>Get Started</b>.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTcEMmLHvG-ZJx8nUZnmtZj4R-yjwqQMfnbk0TaFaUxPGvpGbRi6FSsWuLG9dxkja4CzMT11MpH_8JeeKaLh5mIf65OBkCZcvmfMu6ekI8dU9K3bL9nv8KwxR-VOx85v64vvZbov7JZBw/s1366/Screenshot+%252817%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Mendaftar ke Google Firebase" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTcEMmLHvG-ZJx8nUZnmtZj4R-yjwqQMfnbk0TaFaUxPGvpGbRi6FSsWuLG9dxkja4CzMT11MpH_8JeeKaLh5mIf65OBkCZcvmfMu6ekI8dU9K3bL9nv8KwxR-VOx85v64vvZbov7JZBw/w320-h180/Screenshot+%252817%2529.png" title="Google firebase" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 1</b> : Halaman depan Google Firebase<br /></td></tr></tbody></table><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><p>Setelah itu akan muncul halaman selanjutnya yang berbunyi "Selamat datang di Firebase!" diikuti sebuah tombol link "Buat project" dibawahnya. Klik pada tombol tersebut untuk memulai membuat sebuah project terlebih dahulu.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfEBuquwJVGNWV-kq0HWFC78h7Y-92Qr8oD23R00bhu9glCZr3wQtl4Iogp8hGfzx03VcOTKsjVFYOCY2ZGHFFUyAtB2666X_FdTls5rTROLw03QWphIWzFxGfL73ITFga34HQN70ovoA/s1366/Screenshot+%252818%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Membuat project baru di Google Firebase" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfEBuquwJVGNWV-kq0HWFC78h7Y-92Qr8oD23R00bhu9glCZr3wQtl4Iogp8hGfzx03VcOTKsjVFYOCY2ZGHFFUyAtB2666X_FdTls5rTROLw03QWphIWzFxGfL73ITFga34HQN70ovoA/w320-h180/Screenshot+%252818%2529.png" title="Mendaftar Google Firebase" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 2</b> : Halaman membuat project di Firebase<br /></td></tr></tbody></table><br /><p>Berikutnya akan muncul halaman dengan field teks untuk memasukkan nama dari project. Isikan nama dari project (bebas, tidak harus sesuai dengan nama aplikasi Anda), klik centang pada checkbox persetujuan kemudian Lanjutkan.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2jRgkQk-L7RsUreXgWPs4NWcIOE-YltiRG32EKBZ73TTs3y1_DARJ3KhjGQIfnSvQ0TY2qTghgBMVi20U9oAHx-zMjdUM_QkRBOBUIaxKPbNWD0lbsq4ARozVhKYyQMNmY-qimvnqtjM/s1366/Screenshot+%252819%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Konfigurasi nama project untuk Firebase" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2jRgkQk-L7RsUreXgWPs4NWcIOE-YltiRG32EKBZ73TTs3y1_DARJ3KhjGQIfnSvQ0TY2qTghgBMVi20U9oAHx-zMjdUM_QkRBOBUIaxKPbNWD0lbsq4ARozVhKYyQMNmY-qimvnqtjM/w320-h180/Screenshot+%252819%2529.png" title="Nama project firebase" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 3</b> : Menentukan nama untuk sebuah project Firebase<br /></td></tr></tbody></table><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><p>Langkah berikutnya dari membuat project firebase, Anda akan dihadapkan dengan halaman seperti dibawah. Tidak perlu ada yang dirubah, langsung saja klik Lanjutkan.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZa_fN-7XVkGodM-ubclJ1oGRWPgHlZjNSG7G21O4Pj194l8rz22FPfhNeuCMQGMYvdMdpQH3QrcigmVitec-q0SRIIK5jhOoDiEQYiap6Iwh8Xyp_JsaKveC4eEsnvFdjAl6EavUnjJ8/s1366/Screenshot+%252820%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Menautkan project ke Google Analytics" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZa_fN-7XVkGodM-ubclJ1oGRWPgHlZjNSG7G21O4Pj194l8rz22FPfhNeuCMQGMYvdMdpQH3QrcigmVitec-q0SRIIK5jhOoDiEQYiap6Iwh8Xyp_JsaKveC4eEsnvFdjAl6EavUnjJ8/w320-h180/Screenshot+%252820%2529.png" title="Firebase Analytics" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 4</b> : Menautkan project ke Google Analytics<br /></td></tr></tbody></table><br /><p>Halaman selanjutnya yang akan muncul adalah halaman konfigurasi Google Analytics, yakni memilih negara lokasi analytics (di contoh ini kita pilih Indonesia), setelan berbagi dan persyaratan data Google Analytics; klik centang pada semua checkbox lalu klik tombol dibawahnya, tunggu sampai proses pembuatan project selesai.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdGJwClWwk1agXBkcL1A8Klng-TQzMVSTx9X1kA_nXJ4c5sJTN-5YaJoekDUHL52UvIw8PJF6-2sG6WcR1JdjV6pX7g67cFYhxRo7fMEfrwnTrg-eu992ieAgwsmI_QiyYhWvxL484AK8/s1366/Screenshot+%252821%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Konfigurasi setelan Google Analytics" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdGJwClWwk1agXBkcL1A8Klng-TQzMVSTx9X1kA_nXJ4c5sJTN-5YaJoekDUHL52UvIw8PJF6-2sG6WcR1JdjV6pX7g67cFYhxRo7fMEfrwnTrg-eu992ieAgwsmI_QiyYhWvxL484AK8/w320-h180/Screenshot+%252821%2529.png" title="Google Analytics" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 5</b> : Konfigurasi Google Analytics<br /></td></tr></tbody></table><p><br /></p><p>Setelah pada tahap diatas, pembuatan project telah selesai dan Anda akan dibawa ke halaman Ringkasan dari Project. Langkah berikutnya adalah menautkan aplikasi Android kita. Klik pada <b>icon Android</b> yang ada di halaman Ringkasan Project, setelah itu akan muncul halaman untuk mengisikan <b>Nama paket dari aplikasi</b>, <b>Nama aplikasi</b> dan <b>Sertifikat penandatanganan debug SHA-1</b>.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsk8cDXrUpv4Bwp4clxhtk333t0w8sNmSXX5Uyir47WjW8iyUuWPU9RXX4UcHePfwr7Q_5uw9aA82LDt6bGm0o719QGoGaSmRH_ml8FJNJloEwdOBUftlQMWQ3K_wqBaJpTTzm2H-NG8w/s1366/Screenshot+%252822%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Halaman ringkasan project Google Firebase" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsk8cDXrUpv4Bwp4clxhtk333t0w8sNmSXX5Uyir47WjW8iyUuWPU9RXX4UcHePfwr7Q_5uw9aA82LDt6bGm0o719QGoGaSmRH_ml8FJNJloEwdOBUftlQMWQ3K_wqBaJpTTzm2H-NG8w/w320-h180/Screenshot+%252822%2529.png" title="Ringkasan project Google Analytics" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 6</b> : Halaman Ringkasan Project<br /></td></tr></tbody></table><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><p>Untuk Nama paket wajib diisi, untuk Nama aplikasi dan Sertifikat SHA-1 sifatnya adalah opsional, namun saya selalu mengisi nya setiap kali menautkan aplikasi ke Firebase Analytics. Nama paket adalah seperti misalnya : <i>com.gwnbs.sharedpreferences</i>. Berikut dibawah ini gambar petunjuknya.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvAHGcKxWfQh7liRGRbdGOh24PRkjA0Z6LzFD1BR0XwcApUTbPa0KGfYtEpAi2-33WKXYw1Mg-a89a3iqnxkfOYd53kWdwJsV4gzM3C38B9HEYF72sL46khTt_P0DvLUCjgn3Ai0uwdfo/s1366/Screenshot+%252825%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Mengisikan nama paket di Firebase Analytics" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvAHGcKxWfQh7liRGRbdGOh24PRkjA0Z6LzFD1BR0XwcApUTbPa0KGfYtEpAi2-33WKXYw1Mg-a89a3iqnxkfOYd53kWdwJsV4gzM3C38B9HEYF72sL46khTt_P0DvLUCjgn3Ai0uwdfo/w320-h180/Screenshot+%252825%2529.png" title="Menautkan aplikasi ke Google Firebase" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 7</b> : Menuliskan rincian aplikasi<br /></td></tr></tbody></table><br /><p>Pada contoh gambar diatas, nama paketnya adalah : <i>com.gwnbs.sharedpreferences</i>, kemudian nama aplikasinya : <i>Shared Preferences</i>, dan sertifikat SHA-1 nya adalah <i>kombinasi angka-angka dengan huruf disertai simbol-simbol titik dua</i>.</p><p><b>Sertifikat penandatanganan debug SHA-1</b> tersebut bisa didapat melalu <i>signingReport</i> pada project di Android Studio Anda. Caranya adalah dengan membuka <b><i>icon gradle</i></b> yang ada disamping kanan sudut atas pada project aplikasi Android Studio, kemudian perluas folder <b>Tasks</b>, kemudian perluas lagi folder <b>android</b>, disitu Anda akan melihat <i>signingReport</i>. Seperti gambar dibawah ini :</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCT2lgJziGi0Wm9ESm-Y0Vz9ILMSGSRGJiM9B0dHewZVmWmFfhPdW-mJbUCOrcbTY3To6JGaotquaPXFXEfqYtRwTwhWFTSr1-YF62AWcWKNzae7c5vWwruXMHamIGaDdCDjR4usQ_Kis/s1366/Screenshot+%252823%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Mengakses SigningReport Android Studio" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCT2lgJziGi0Wm9ESm-Y0Vz9ILMSGSRGJiM9B0dHewZVmWmFfhPdW-mJbUCOrcbTY3To6JGaotquaPXFXEfqYtRwTwhWFTSr1-YF62AWcWKNzae7c5vWwruXMHamIGaDdCDjR4usQ_Kis/w320-h180/Screenshot+%252823%2529.png" title="SigningReport Android Studio" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 8</b> : Mengakses SigningReport project aplikasi Android Studio<br /></td></tr></tbody></table><br /><p>Kemudian double klik pada <i>signingReport </i>tersebut dan tunggu hingga proses signing selesai. Setelah proses selesai (hanya beberapa detik), maka akan ditampilkan rincian yang didalamnya terdapat beberapa jenis sertifikat dari aplikasi Anda, termasuk SHA1. Copy sertifikat tersebut lalu pastekan pada teks field pada tahapan sebelumnya.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiefJ-i6SLnJXhpny-lhyphenhypheneqLomu_6SaBHv7ZpFBwjm-mXxjpzXQ5iOT7GUa1lAgYIj-wCJ95H3OK6o7zXjN4zuWZ-ysRZ_tfVarXIDUj3yBAApYXpPOD8y-ih0NEcONYM-xdMChj7oWIzo/s1366/Screenshot+%252824%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Sertifikat penandatangan debug SHA-1 Android" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiefJ-i6SLnJXhpny-lhyphenhypheneqLomu_6SaBHv7ZpFBwjm-mXxjpzXQ5iOT7GUa1lAgYIj-wCJ95H3OK6o7zXjN4zuWZ-ysRZ_tfVarXIDUj3yBAApYXpPOD8y-ih0NEcONYM-xdMChj7oWIzo/w320-h180/Screenshot+%252824%2529.png" title="Sertifikat SHA-1" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 9</b> : Cara mendapatkan sertifikat SHA1<br /></td></tr></tbody></table><br /><p>Setelah penulisan rincian aplikasi selesai, klik tombol "Daftarkan Aplikasi". Tahap kedua dari menautkan aplikasi ke Firebase yaitu mengunduh file konfigurasi yaitu : <b>google-services.json</b>. Klik tombol untuk mengunduh file tersebut.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWYwzvN5I2g5MZmITDFd49kB2Kll8pkqYPej7Gsh_ndJfUYUIy36RLzYJMedv214lQY780YP31b0cSgx5tLLiL_7AcqhySOZHdB1qJJpb1uscWVMm3jGrokxTRPjuHMmTx5hNFcNueb3M/s1366/Screenshot+%252827%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Mengunduh file konfigurasi google-service.json" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWYwzvN5I2g5MZmITDFd49kB2Kll8pkqYPej7Gsh_ndJfUYUIy36RLzYJMedv214lQY780YP31b0cSgx5tLLiL_7AcqhySOZHdB1qJJpb1uscWVMm3jGrokxTRPjuHMmTx5hNFcNueb3M/w320-h180/Screenshot+%252827%2529.png" title="google-services.json" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 10</b> : Mengunduh file konfigurasi <b>google-services.json</b><br /></td></tr></tbody></table><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><p>Copy file <b>google-services.json</b> tersebut dari file explorer komputer Anda, kemudian berpindah ke project aplikasi Anda di Android Studio. Terlebih dahulu buat tampilan project Android Studio Anda terlihat sebagai tampilan "<b>Project</b>", kemudian perluas <i>folder <b>nama aplikasi</b></i>, lalu klik kanan pada folder <b><i>app </i></b>dan pilih <b><i>paste</i></b>. Maka file konfigurasi pun telah di tempel.</p><p><br /></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN81MwJaiU8JDbXS6CZdrnkZ9X8T6HF1OjXD4wtzme4njpeag-1UBWtZdM5HqjSHjmnJ56vedTmH_0JF4qfPRKr2rOw8QIB3rEuojVj9u8oeVaUpDkgpvfjEeJrlYZAo1LXf0bvNlnSbQ/s1366/Screenshot+%252828%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Peletakan file konfigurasi google-services.json" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN81MwJaiU8JDbXS6CZdrnkZ9X8T6HF1OjXD4wtzme4njpeag-1UBWtZdM5HqjSHjmnJ56vedTmH_0JF4qfPRKr2rOw8QIB3rEuojVj9u8oeVaUpDkgpvfjEeJrlYZAo1LXf0bvNlnSbQ/w320-h180/Screenshot+%252828%2529.png" title="google-services.json" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 11</b> : Peletakan file konfigurasi di project Aplikasi<br /></td></tr></tbody></table><br /><p>Kembali ke halaman menautkan aplikasi ke firebase pada browser internet Anda, kemudian klik tombol "berikutnya". Setelah itu akan muncul halaman petunjuk untuk memasukkan library (<b>SDK </b>= Software Development Kit) firebase yang diperlukan ke dalam project aplikasi. Dari beberapa SDK yang ditampilkan pada halaman tersebut, seharusnya hanya ada 4 SDK yang perlu kita masukkan ke dalam <i>build.gradle</i> project yaitu :</p><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: blue;">1</span>. classpath <span style="color: green; font-weight: bold;">'com.google.gms:google-services:4.3.4'<br /></span><span style="color: blue;">2</span>. apply <span style="color: green; font-weight: bold;">plugin</span>: <span style="color: green; font-weight: bold;">'com.google.gms.google-services'<br /></span><span style="color: blue;">3</span>. implementation platform(<span style="color: green; font-weight: bold;">'com.google.firebase:firebase-bom:26.1.0'</span>)<br /><span style="color: blue;">4</span>. implementation <span style="color: green; font-weight: bold;">'com.google.firebase:firebase-analytics'</span></pre></pre></div><div><br /></div>Karena SDK lainnya telah secara otomatis ada pada project Andorid saat project dibuat. Pun begitu, Anda tetap harus cross check saat menyunting file build.gradle aplikasi, bisa saja SDK yang seharusnya secara otomatis sudah termasuk namun tidak ada.<p></p><div>Beralih ke Android Studio, perluas folder <b>Gradle Scripts</b>, lalu buka <b><i>build.gradle(Project: nama_project)</i></b>, kemudian tempelkan SDK nomor 1 pada kotak diatas, seperti terlihat pada gambar dibawah ini.</div><div><br /></div><div><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6Tl8GERmvnawLvjZLKOeF7ZIFoEdSAruvVqBNnI-7yaz-a0yUSR9kR-Lw641jCKuTXjGYqMThBVJlOSDUrpj6eGgKFdoFnzkvSZeGP5DcBfekzIqwsuhTOlr9mYNR0L-v-PJT4QG91Ng/s1366/Screenshot+%252829%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Memasukkan library firebase ke build.gradle project" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6Tl8GERmvnawLvjZLKOeF7ZIFoEdSAruvVqBNnI-7yaz-a0yUSR9kR-Lw641jCKuTXjGYqMThBVJlOSDUrpj6eGgKFdoFnzkvSZeGP5DcBfekzIqwsuhTOlr9mYNR0L-v-PJT4QG91Ng/w320-h180/Screenshot+%252829%2529.png" title="Library firebase" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 12</b> : Memasukkan SDK Firebase ke build.gradle(Project: aplikasi)<br /></td></tr></tbody></table><br /><div><br /></div><div>Berikutnya buka <b><i>build.gradle(Module: app)</i></b>, lalu masukkan SDK nomor 2 pada kotak diatas tepat dibawah <span style="color: #800180;">apply plugin: 'com.android.application'</span>, seperti terlihat pada gambar dibawah ini.</div><div><br /></div><div><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5cWpGcMKIlvOrhQWqSwYrJULpyzGd1gsmhGWExeot4fPKcBWXFSBeiKE6BjtgxWqQW3WPqRyXY5LJYr5oCycpLV8eLydt0ufO-KGS3eM3jroHSheGMPKEK00oEsTtHu4dhI_d38ZsaDw/s1366/Screenshot+%252830%2529.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="1366" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5cWpGcMKIlvOrhQWqSwYrJULpyzGd1gsmhGWExeot4fPKcBWXFSBeiKE6BjtgxWqQW3WPqRyXY5LJYr5oCycpLV8eLydt0ufO-KGS3eM3jroHSheGMPKEK00oEsTtHu4dhI_d38ZsaDw/s320/Screenshot+%252830%2529.png" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 13</b> : Memasukkan SDK Firebase ke build.gradle(Module: app)<br /></td></tr></tbody></table><br /><div><br /></div><div>Masih di <b><i>build.gradle(Module: app)</i></b>, scroll ke paling bawah, dan masukkan library nomor 3 dan 4 di dalam dependencies.</div><div><br /></div><div><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQq7jkWWw6mhu5kNXbTk-xuJ6GvMGX0kvLdrZ2zJXWQRjbu-ElWS4c6kpH_hhi69CQrk_Ypk2a2dNtVBQHXr-wmuRO4ZPW43KWsSJx2WnZ1fRfOviSPjSM9PWv27fq2Er8JOoWXr4yjLo/s1366/Screenshot+%252831%2529.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="1366" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQq7jkWWw6mhu5kNXbTk-xuJ6GvMGX0kvLdrZ2zJXWQRjbu-ElWS4c6kpH_hhi69CQrk_Ypk2a2dNtVBQHXr-wmuRO4ZPW43KWsSJx2WnZ1fRfOviSPjSM9PWv27fq2Er8JOoWXr4yjLo/s320/Screenshot+%252831%2529.png" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><b>Gambar 14</b> : Memasukkan SDK Firebase ke build.gradle(Module: app)<br /></td></tr></tbody></table><br /><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><div>Setelah semua library di masukkan, lakukan sync gradle. Jangan lupa untuk mengkoneksikan komputer ke internet karena Android Studio perlu mengunduh library tersebut.</div><div><br /></div><div>Keperluan di Android Studio sudah beres, sekarang kembali ke halaman firebase dimana langkah terakhir di tinggalkan dan klik tombol "berikutnya", kemudian klik tombol "Lanjutkan ke console". </div><div><br /></div><div>Sampai disini semuanya sudah beres, namun jika Anda mendapati perintah untuk menjalankan aplikasi pada halaman firebase, silahkan untuk terlebih dahulu melakukan debug aplikasi Anda. Hal ini untuk memastikan sistem Firebase bahwa penautan aplikasi Anda ke Firebase berjalan dengan baik.</div><div><br /></div><div>Aplikasi Anda sudah siap untuk <b>Google Analytics</b>, tunggu beberapa saat pada halaman dashboard Firebase untuk data statistik aplikasi Anda ditampilkan (tentunya jika aplikasi sudah dirilis ke publik dan memiliki pengguna). Jangan lupa juga untuk mendownload aplikasi <b>Google Analytics</b> dari <b>Playstore</b>.</div><div><br /></div><div>Sekian tutorial ini. Terima kasih sudah meluangkan waktu berkunjung. Jika ada yang ingin disampaikan, bisa diposting di kolom komentar.</div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-15799746523263164202020-11-18T04:20:00.002+07:002021-02-22T18:46:20.102+07:00Menyimpan Data Menggunakan SharedPreferences Dengan String, Int dan Boolean<p>Dalam postingan kali ini akan dicontohkan menyimpan data menggunakan <b>SharedPreferences </b>dengan <b>Int</b>, <b>String</b>, dan <b>boolean</b>. Sebelumnya perlu diketahui terlebih dahulu perbedaan dari ketiga jenis data ini.<span></span></p><a name='more'></a><p></p><p></p><ul style="text-align: left;"><li><b>Int </b>adalah Integer (bilangan bulat), atau angka yang tidak dapat mengandung desimal. Nilai paling besar untuk int adalah 2147483647 dan nilai paling kecilnya -2147483648. Jika nilai lebih besar dari yang disebutkan, maka <b>int </b>tidak dapat menyimpannya, sebagai gantinnya Anda bisa menggunakan <b>float </b>ataupun <b>double </b>yang nilai maksimumnya lebih besar dari <b>int </b>dan dapat mengandung angka dengan desimal. Begitu juga jika nilai lebih rendah dari nilai minimum.</li><br /><li><b>String </b>adalah karakter atau koleksi karakter. Nilainya bisa huruf-huruf, angka, simbol dan lain sebagainya yang termasuk dalam kategori karakter, berbeda dengan <b>int </b>yang hanya dapat mengandung angka / digit saja.</li><br /><li>Sedangkan <b>boolean </b>hanya memiliki dua nilai yakni : <b><i>true </i></b>dan <b><i>false</i></b> saja.</li></ul><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><h3 style="text-align: left;"><b><br /></b></h3><h3 style="text-align: left;"><b>1. String</b></h3><div><b>SharedPreferences </b>adalah salah satu dari beberapa tipe penyimpanan di Android yang pengaplikasiannya paling sederhana. Di dalam sebuah aktivitas java class, untuk mulai menyimpan sebuah data menggunakan <b>SharedPreferences </b>cukup dengan beberapa baris kode. Pertama tentu dengan memvariabelkan object <b>SharedPreferences</b>, menentukan nama untuk penyimpanan SharedPreferencesnya, memilih tipe mode SharedPreferences (MODE_PRIVATE adalah yang terbaik), kemudian memanggil objek <b>SharedPreferences.Editor</b> dan memvariabelkannya, membuat kata kunci untuk data yang akan disimpan beserta datanya, lalu <i>apply </i>dan selesai.</div><div><br /></div><div>Sama halnya seperti pada SQLite, nama untuk SharedPreperences seperti halnya nama tabel pada SQLite, lalu kata kunci string pada SharedPreferences seperti halnya kolom pada SQLite.</div><div><br /></div><div>Fungsi dari <b>SharedPreferences.Editor</b> disini adalah untuk menyunting atau merubah data. Untuk kata kunci bentuknya adalah <b>String </b>/ karakter. Nilai dari sebuah <b>String </b>harus berada di dalam kutip (""). Contohnya :</div><div><br /></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">String ronaldo = <span style="color: green; font-weight: bold;">"Ronaldo : Siapa nama kamu?"</span>;</pre></div><div><br /></div><div><br /></div><div>Pada contoh <b>String </b>diatas, <b><i>ronaldo </i></b>adalah variabel dari sebuah objek <b>String</b>, dan <i>"Ronaldo : Siapa nama kamu"</i> adalah nilai dari <b>String </b>tersebut. Berikut contoh dari beberapa objek String lalu menggabungkannya dan menampilkannya dalam sebuah pesan Toast:</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">String ronaldo = <span style="color: green; font-weight: bold;">"Ronaldo : Siapa nama kamu?"</span>;<br />String messi = <span style="color: green; font-weight: bold;">"Messi : Nama saya Messi."</span>;<br />String ronaldoMessi = ronaldo + <span style="color: green; font-weight: bold;">"</span><span style="color: navy; font-weight: bold;">\n</span><span style="color: green; font-weight: bold;">" </span>+ messi;<br /><br />Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), ronaldoMessi, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();</pre></div><div><br /></div><div><br /></div>Maka sebuah pesan toast yang berbunyi seperti dibawah ini akan muncul dilayar:</div><div><br /></div><div><i>Ronaldo : Siapa nama kamu?</i></div><div><i>Messi : Nama saya Messi.</i></div><div><br /></div><div>Fungsi dari <span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">"</span><span style="color: navy; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">\n</span><span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">"</span> pada contoh kode diatas adalah untuk memberikan baris baru, seperti <br/> pada HTML atau Enter pada komputer saat Anda mengetik teks. Sedangan simbol (+) pada <b>String </b>berarti menyertakan. Berbeda pada <b>int </b>yang mana simbol (+) memiliki fungsi penjumlahan, begitu pula untuk simbol kalkulasi lainnya : simbol (-) untuk pengurangan, simbol (*) untuk perkalian, dan (/) untuk pembagian.</div><div><br /></div><div>Kemudian kita akan menyimpan kalimat percakapan antara Ronaldo dan Messi tersebut ke penyimpanan <b>SharedPreferences</b>, maka kodenya adalah seperti dibawah ini. Tentunya dalam hal ini kita menyimpan String.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> String ronaldo = <span style="color: green; font-weight: bold;">"Ronaldo : Siapa nama kamu?"</span>;<br /> String messi = <span style="color: green; font-weight: bold;">"Messi : Nama saya Messi."</span>;<br /> String ronaldoMessi = ronaldo + <span style="color: green; font-weight: bold;">"</span><span style="color: navy; font-weight: bold;">\n</span><span style="color: green; font-weight: bold;">" </span>+ messi;<br /><br /> SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> SharedPreferences.Editor editor = prefs.edit();<br /> editor.putString(<span style="color: green; font-weight: bold;">"kataKunci"</span>, ronaldoMessi);<br /> editor.apply();<br /> }<br />}</pre></div><br /><br /></div><div>Kalimat percakapan mereka akan otomatis disimpan ke penyimpanan <b>SharedPreferences </b>saat kita menjalankan aplikasi dan membuka Aktivitas MainActivity.java karena kita meletakkan seluruh kode didalam <i>onCreate</i>, berbeda jika diluar <b>onCreate </b>ataupun didalam sebuah metode klik tombol yang tentunya perlu mengklik tombol terlebih dahulu supaya data tersimpan.</div><div><br /></div><div>Jika kemudian Anda menyimpan string atau karakter lain dengan nama preferences dan kata kunci yang sama maka nilai nya akan berubah menjadi yang terbaru, dengan kata lain kalimat percakapan antara Ronaldo dan Messi akan tergantikan dengan nilai string terbaru yang Anda buat. Berlaku hal yang sama untuk <b>int</b>.</div><div><br /></div><div>Kita pun dapat menyimpan secara langsung sebuah string tanpa perlu memvariabelkan objek string. Seperti contoh diatas, maka akan menjadi :</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br />SharedPreferences.Editor editor = prefs.edit();<br />editor.putString(<span style="color: green; font-weight: bold;">"kataKunci"</span>, <span style="color: green; font-weight: bold;">"Ronaldo : Siapa nama kamu? </span><span style="color: navy; font-weight: bold;">\n</span><span style="color: green; font-weight: bold;"> Messi : Nama saya Messi."</span>);<br />editor.apply();</pre></div></div><div><br /></div><div><br /></div><div>Dari contoh diatas, kalimat percakapan Ronaldo dan Messi tersebut kita bisa menggabungkannya dalam satu tanda kutip, atau dengan kata lain kita sudah tidak membutuhkan simbol (+) karena sudah tidak menggunakan variabel. Pun begitu juga tidak salah jika kita melakukan penyertaan dengan simbol (+), misalnya : <span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">"Ronaldo : Siapa nama kamu?" </span><span style="background-color: white; font-family: Consolas; font-size: 9.8pt;">+ </span><span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">"</span><span style="color: navy; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">\n</span><span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">" </span><span style="background-color: white; font-family: Consolas; font-size: 9.8pt;">+ </span><span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">"Messi : Nama saya Messi."</span></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><div>Kemudian untuk memuat data kita menggunakan <i>getString</i>. Misalnya kita memiliki sebuah komponen tombol dan sebuah Textview di layout. Tombol tersebut akan kita gunakan untuk memuat data lalu menampilkan di Textview, sehingga akan menjadi seperti dibawah ini :</div><div><br /></div><div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> String ronaldo = <span style="color: green; font-weight: bold;">"Ronaldo : Siapa nama kamu?"</span>;<br /> String messi = <span style="color: green; font-weight: bold;">"Messi : Nama saya Messi."</span>;<br /> String ronaldoMessi = ronaldo + <span style="color: green; font-weight: bold;">"</span><span style="color: navy; font-weight: bold;">\n</span><span style="color: green; font-weight: bold;">" </span>+ messi;<br /><br /> <span style="color: navy; font-weight: bold;">final </span>SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> SharedPreferences.Editor editor = prefs.edit();<br /> editor.putString(<span style="color: green; font-weight: bold;">"kataKunci"</span>, ronaldoMessi);<br /> editor.apply();<br /><br /> Button loadData = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">loadData</span>);<br /> <span style="color: navy; font-weight: bold;">final </span>TextView showData = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">showData</span>);<br /> <br /> loadData.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> String loadDataRonaldoMessi = <span style="color: #660e7a;">prefs</span>.getString(<span style="color: green; font-weight: bold;">"kataKunci"</span>, <span style="color: green; font-weight: bold;">""</span>);<br /> <span style="color: #660e7a;">showData</span>.setText(loadDataRonaldoMessi);<br /> }<br /> });<br /><br /> }<br />}</pre></div><div><br /></div></div><div><h3 style="text-align: left;"><b>2. Int</b></h3><div>Seperti yang sudah dijelaskan diatas bahwa <b>int </b>adalah bilangan atau angka bulat dan nilai dari int tidak berada di dalam tanda kutip. Nilai dari <b>int </b>dapat diubah menjadi <b>String </b>begitupun sebaliknya. Yang perlu dipahami adalah nilai dari sebuah <b>string </b>haruslah karakter dengan angka bulat supaya dapat diubah menjadi int. Berikut contoh sederhana sebuah int.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">int </span>umurRonaldo = <span style="color: blue;">35</span>;</pre></div><br /><br /></div><div>Pada contoh diatas, umurRonaldo adalah variabel dari sebuah objek int, dan 35 adalah angka bulatnya. Berikut kita akan menjumlahkannya dengan nilai dari objek int lainnya.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">int </span>umurRonaldo = <span style="color: blue;">35</span>;<br /><span style="color: navy; font-weight: bold;">int </span>umurMessi = <span style="color: blue;">33</span>;<br /><span style="color: navy; font-weight: bold;">int </span>umurRonaldoMessi = umurRonaldo + umurMessi;<br /><br />Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), String.<span style="font-size: 9.8pt; font-style: italic;">valueOf</span><span style="font-size: 9.8pt;">(umurRonaldoMessi)</span>, Toast.<span style="color: #660e7a; font-size: 9.8pt; font-style: italic; font-weight: bold;">LENGTH_SHORT</span><span style="font-size: 9.8pt;">).show();</span></pre></div><div><br /></div><div><br /></div><div>Sebuah pesan toast haruslah dari nilai sebuah string, sehingga kita perlu mengkonversi nilai int tersebut menjadi string dengan menggunakan <span style="background-color: white; font-family: Consolas; font-size: 9.8pt;">String.</span><span style="font-family: Consolas; font-size: 9.8pt; font-style: italic;">valueOf</span><span style="background-color: white; font-family: Consolas; font-size: 9.8pt;">(nilai dari int)</span>. Atau yang lebih simpel cukup dengan <span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">"" </span><span style="background-color: white; font-family: Consolas; font-size: 9.8pt;">+ umurRonaldoMessi</span>.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"" </span>+ umurRonaldoMessi, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();</pre></div></div><div><br /></div><div><br /></div>Maka sebuah pesan toast angka 68 akan muncul dilayar ponsel saat aplikasi dijalankan. Jika menggunakan pengurangan (-) maka hasilnya adalah 2, jika perkalian (*) akan menjadi 1155, namun jika menggunakan pembagian (/) maka hasilnya adalah 1, karena nilainnya adalah angka desimal, yang mana angka desimal akan hilang pada int. Lalu kita akan menyimpan nilai tersebut ke penyimpanan <b>SharedPreferences </b>maka kodenya adalah seperti dibawah ini.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: navy; font-weight: bold;">int </span>umurRonaldo = <span style="color: blue;">35</span>;<br /> <span style="color: navy; font-weight: bold;">int </span>umurMessi = <span style="color: blue;">33</span>;<br /> <span style="color: navy; font-weight: bold;">int </span>umurRonaldoMessi = umurRonaldo + umurMessi;<br /><br /> SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> SharedPreferences.Editor editor = prefs.edit();<br /> editor.putInt(<span style="color: green; font-weight: bold;">"kataKunci"</span>, umurRonaldoMessi);<br /> editor.apply();<br /> }<br />}</pre></div><div><br /></div><div><br /></div>Sama seperti string diatas, kitapun juga bisa langsung menyimpan sebuah nilai dari int tanpa perlu memvariabelkan sebuah object int terlebih dahulu. Maka akan terlihat seperti dibawah ini.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br />SharedPreferences.Editor editor = prefs.edit();<br />editor.putInt(<span style="color: green; font-weight: bold;">"kataKunci"</span>, <span style="color: blue;">68</span>);<br />editor.apply();</pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Kemudian untuk memuat data, kita menggunakan <i>getInt</i>, sama seperti contoh string diatas, kita akan memuat data dengan sebuah tombol dan menampilkannya pada Textview. Sehingga akan seperti kode dibawah ini.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: navy; font-weight: bold;">int </span>umurRonaldo = <span style="color: blue;">35</span>;<br /> <span style="color: navy; font-weight: bold;">int </span>umurMessi = <span style="color: blue;">33</span>;<br /> <span style="color: navy; font-weight: bold;">int </span>umurRonaldoMessi = umurRonaldo + umurMessi;<br /><br /> <span style="color: navy; font-weight: bold;">final </span>SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> SharedPreferences.Editor editor = prefs.edit();<br /> editor.putInt(<span style="color: green; font-weight: bold;">"kataKunci"</span>, umurRonaldoMessi);<br /> editor.apply();<br /><br /> Button loadData = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">loadData</span>);<br /> <span style="color: navy; font-weight: bold;">final </span>TextView showData = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">showData</span>);<br /><br /> loadData.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: navy; font-weight: bold;">int </span>loadJumlahUmur = <span style="color: #660e7a;">prefs</span>.getInt(<span style="color: green; font-weight: bold;">"kataKunci"</span>, <span style="color: blue;">0</span>);<br /> <span style="color: #660e7a;">showData</span>.setText(String.<span style="font-style: italic;">valueOf</span>(loadJumlahUmur));<br /> }<br /> });<br /><br /> }<br />}</pre></div><br /><br /><h3 style="text-align: left;"><b>3. boolean</b></h3></div><div>Nilai dari <b>boolean </b>hanyalah <i>true </i>dan <i>false</i>, hal ini merujuk pada apa yang akan kita lakukan jika nilainya adalah <i>true </i>dan apa apa yang akan kita lakukan jika nilainya adalah <i>false</i>. Contohnya kita menggunakan boolean sebagai pendeteksi pengguna aplikasi kita, apakah pengguna menggunakan aplikasi untuk pertama kali atau tidak. Misal kita ingin memberikan sebuah pesan toast selamat datang kepada pengguna bila pertama kali membuka aplikasi.</div><div><br /></div><div>Pertama dengan memvariabelkan sebuah objek <b>boolean</b>, kemudian menyetel nilai default boolean tersebut dari SharedPreferences ke <i>true</i>, lalu di dalam nilai boolean tersebut kita sematkan kode untuk pesan toast dan merubah nilai boolean tersebut menjadi <i>false</i>.</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> SharedPreferences.Editor editor = prefs.edit();<br /><br /> <span style="color: navy; font-weight: bold;">boolean </span>pertamaKali = prefs.getBoolean(<span style="color: green; font-weight: bold;">"kataKunci"</span>, <span style="color: navy; font-weight: bold;">true</span>);<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(pertamaKali) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Selamat datang! Terima kasih sudah menggunakan aplikasi ini"</span>,<br /> Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> editor.putBoolean(<span style="color: green; font-weight: bold;">"kataKunci"</span>, <span style="color: navy; font-weight: bold;">false</span>);<br /> editor.apply();<br /> }<br /> }<br />}</pre></div><div><br /></div><div><br /></div><h3 style="text-align: left;"><b>Menghapus SharedPreferences</b></h3></div></div><div>Pada <b>SQLite </b>Database, kita dapat menghapus data secara keseluruhan dengan menghapus tabel dan satu-persatu dengan menghapus baris. Pun begitu pada <b>SharedPreferences</b>, kita dapat menghapus data secara keseluruhan dengan menghapus nama dari SharedPreferences dan satu-persatu dengan menghapus kata kunci string.</div><div><br /></div><div>Penghapusan seluruh data tentunya jika seluruh kata kunci string data Anda berada dalam satu nama SharedPreferences. Untuk menghapus data secara keseluruhan dapat dengan menggunakan seperti kode dibawah ini, misalnya kita memiliki sebuah tombol untuk menghapus :</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolHapus</span>).setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: grey; font-style: italic;">//Baris kode untuk menghapus data pada SharedPreferences secara keseluruhan<br /></span><span style="color: grey; font-style: italic;"> </span>getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>).edit().clear().apply();<br /> }<br />});</pre></div><div><br /></div><div><br /></div>Dan untuk menghapus data SharedPreferences berdasarkan kata kunci :</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;">findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolHapus</span>).setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: grey; font-style: italic;">//Kode untuk menghapus data pada SharedPreferences berdasarkan kata kunci<br /></span><span style="color: grey; font-style: italic;"> </span>SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"NAMA_DARI_PREFERENCES"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> SharedPreferences.Editor editor = prefs.edit();<br /> editor.remove(<span style="color: green; font-weight: bold;">"kataKunci"</span>);<br /> editor.apply();<br /> }<br />});</pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><h3 style="text-align: left;"><b>Menggunakan SharedPreferences Untuk Mengubah & menyimpan Nilai Dari Object</b></h3></div><div>Kita juga dapat mengubah dan menyimpan nilai dari objek menggunakan nilai dari string, int atau boolean yang tersimpan pada SharedPreferences. Namun untuk <b>boolean </b>tentunya hanya dapat memiliki 2 pilihan perubahan pada sebuah objek karena seperti yang sudah dijelaskan sebelumnya bahwa nilai dari boolean sendiri hanya dua, <i>true </i>dan <i>false</i>. Sedangkan untuk string dan int kita dapat memiliki banyak sekali pilihan.</div><div><br /></div><div>Berikut akan dicontohkan merubah latar belakang dari sebuah activity dan merubah nilai string dari sebuah Textview. Disini kita akan memiliki sebuah komponen TextView dan sebuah Spinner pada activity_main.xml. Pada Spinner akan memiliki 4 opsi perubahan warna latar belakang activity. Berikut isi dari <b>activity_main.xml</b> dan <b>MainActivity.java</b> :</div><div><br /></div><div><b>activity_main.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">RelativeLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/teksPilihLatar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerHorizontal</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Pilih Latar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">androidx.appcompat.widget.AppCompatSpinner<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/spinnerPilihLatar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerHorizontal</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@+id/teksPilihLatar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="30dp"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">RelativeLayout</span>></pre></div><br /><b><br /></b></div><div><b>MainActivity.java</b>: (menggunakan string)<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.sharedpreferences;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.content.SharedPreferences;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.AdapterView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ArrayAdapter;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.RelativeLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Spinner;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> RelativeLayout parent = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">parent</span>);<br /> TextView teksPilihLatar = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">teksPilihLatar</span>);<br /> Spinner spinnerPilihLatar = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">spinnerPilihLatar</span>);<br /><br /> SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"PENGATURAN"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> <span style="color: navy; font-weight: bold;">final </span>SharedPreferences.Editor editor = prefs.edit();<br /><br /> String [] pilihanLatar = {<span style="color: green; font-weight: bold;">"Pilih Warna"</span>, <span style="color: green; font-weight: bold;">"Merah"</span>, <span style="color: green; font-weight: bold;">"Kuning"</span>, <span style="color: green; font-weight: bold;">"Hijau"</span>, <span style="color: green; font-weight: bold;">"Biru"</span>};<br /> ArrayAdapter<String> adapter = <span style="color: navy; font-weight: bold;">new </span>ArrayAdapter<>(<span style="color: navy; font-weight: bold;">this</span>,<br /> android.R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">simple_list_item_1</span>, pilihanLatar);<br /> spinnerPilihLatar.setAdapter(adapter);<br /><br /> spinnerPilihLatar.setOnItemSelectedListener(<span style="color: navy; font-weight: bold;">new </span>AdapterView.OnItemSelectedListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onItemSelected(AdapterView<?> parent, View view, <span style="color: navy; font-weight: bold;">int </span>position, <span style="color: navy; font-weight: bold;">long </span>id) {<br /> <span style="color: navy; font-weight: bold;">switch </span>(position) {<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">1 </span>:<br /> <span style="color: #660e7a;">editor</span>.putString(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: green; font-weight: bold;">"Merah"</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">2 </span>:<br /> <span style="color: #660e7a;">editor</span>.putString(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: green; font-weight: bold;">"Kuning"</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">3 </span>:<br /> <span style="color: #660e7a;">editor</span>.putString(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: green; font-weight: bold;">"Hijau"</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">4 </span>:<br /> <span style="color: #660e7a;">editor</span>.putString(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: green; font-weight: bold;">"Biru"</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onNothingSelected(AdapterView<?> parent) {<br /><br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Memuat nilai string dari penyimpanan SharedPreferences<br /></span><span style="color: grey; font-style: italic;"> </span>String warnaLatar = prefs.getString(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: green; font-weight: bold;">"Merah"</span>);<br /> <span style="color: navy; font-weight: bold;">switch </span>(warnaLatar) {<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"Merah" </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">merah</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Merah"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"Kuning" </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">kuning</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Kuning"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"Hijau" </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">hijau</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Hijau"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: green; font-weight: bold;">"Biru" </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">biru</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Biru"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">private void </span>refreshActivity () {<br /> finish();<br /> startActivity(getIntent());<br /> }<br />}</pre></div><div><br /></div><div><br /></div><b>colors.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">resources</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimary"</span>>#275550</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimaryDark"</span>>#405754</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorAccent"</span>>#F57C00</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="hitam"</span>>#000000</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="merah"</span>>#CD1313</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="kuning"</span>>#FBC02D</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="hijau"</span>>#689F38</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="biru"</span>>#6DAEEF</<span style="color: navy; font-weight: bold;">color</span>><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div><div><br /></div><div><br /></div>Pada contoh diatas kita menggunakan <b>String</b>, jika menggunakan <b>int </b>kita hanya perlu merubah <i>putString </i>menjadi <i>putInt</i>, kemudian memberi nilai angka setelahnya. Lalu pada metode memuat data, objek <b>String </b>kita ubah menjadi <b>int</b>. Sehingga akan tampak seperti dibawah ini :</div><div><br /></div><div><b>MainAcrivity.java</b> : (menggunakan int)<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.sharedpreferences;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.content.SharedPreferences;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.AdapterView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ArrayAdapter;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.RelativeLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Spinner;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> RelativeLayout parent = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">parent</span>);<br /> TextView teksPilihLatar = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">teksPilihLatar</span>);<br /> Spinner spinnerPilihLatar = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">spinnerPilihLatar</span>);<br /><br /> SharedPreferences prefs = getSharedPreferences(<span style="color: green; font-weight: bold;">"PENGATURAN"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> <span style="color: navy; font-weight: bold;">final </span>SharedPreferences.Editor editor = prefs.edit();<br /><br /> String [] pilihanLatar = {<span style="color: green; font-weight: bold;">"Pilih Warna"</span>, <span style="color: green; font-weight: bold;">"Merah"</span>, <span style="color: green; font-weight: bold;">"Kuning"</span>, <span style="color: green; font-weight: bold;">"Hijau"</span>, <span style="color: green; font-weight: bold;">"Biru"</span>};<br /> ArrayAdapter<String> adapter = <span style="color: navy; font-weight: bold;">new </span>ArrayAdapter<>(<span style="color: navy; font-weight: bold;">this</span>,<br /> android.R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">simple_list_item_1</span>, pilihanLatar);<br /> spinnerPilihLatar.setAdapter(adapter);<br /><br /> spinnerPilihLatar.setOnItemSelectedListener(<span style="color: navy; font-weight: bold;">new </span>AdapterView.OnItemSelectedListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onItemSelected(AdapterView<?> parent, View view, <span style="color: navy; font-weight: bold;">int </span>position, <span style="color: navy; font-weight: bold;">long </span>id) {<br /> <span style="color: navy; font-weight: bold;">switch </span>(position) {<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">1 </span>:<br /> <span style="color: #660e7a;">editor</span>.putInt(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: blue;">1</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">2 </span>:<br /> <span style="color: #660e7a;">editor</span>.putInt(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: blue;">2</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">3 </span>:<br /> <span style="color: #660e7a;">editor</span>.putInt(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: blue;">3</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">4 </span>:<br /> <span style="color: #660e7a;">editor</span>.putInt(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: blue;">4</span>);<br /> <span style="color: #660e7a;">editor</span>.apply();<br /> refreshActivity();<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onNothingSelected(AdapterView<?> parent) {<br /><br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Memuat nilai string dari penyimpanan SharedPreferences<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">int </span>warnaLatar = prefs.getInt(<span style="color: green; font-weight: bold;">"warnaLatar"</span>, <span style="color: blue;">1</span>);<br /> <span style="color: navy; font-weight: bold;">switch </span>(warnaLatar) {<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">1 </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">merah</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Merah"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">2 </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">kuning</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Kuning"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">3 </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">hijau</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Hijau"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> <span style="color: navy; font-weight: bold;">case </span><span style="color: blue;">4 </span>:<br /> parent.setBackgroundColor(getResources().getColor(R.color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">biru</span>));<br /> teksPilihLatar.setText(<span style="color: green; font-weight: bold;">"Warna Latar Biru"</span>);<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">private void </span>refreshActivity () {<br /> finish();<br /> startActivity(getIntent());<br /> }<br />}</pre></div><div><br /></div><div><br /></div><b>Catatan :</b> Jika Anda mengikuti tutorial ini, saat beralih dari <b>String </b>ke <b>int</b>, sebaiknya gunakan kata kunci string berbeda, jika tidak maka akan terjadi crash saat Anda mencoba menjalankan aplikasi. Atau Anda bisa menghapus data aplikasi atau uninstall aplikasi terlebih dahulu sebelum menjalankan debug kembali. Karena string dan int tidak bisa dalam satu kata kunci.</div><div><br /></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/5YuDhfvfex4" width="320" youtube-src-id="5YuDhfvfex4"></iframe></div><br /><div><br /></div><div><br /></div><h3 style="text-align: left;"><b>Contoh Menyimpan Data Menggunakan SharedPreferences</b></h3><div>Contoh berikut kita akan memiliki 3 buah komponen Edittext untuk menuliskan data diri yaitu : nama, alamat dan umur. 3 komponen tombol untuk menyimpan data, melihat data, dan menghapus data, lalu sebuah Textview untuk menujukkan data. Kita akan memperlihatkan ketiga data dari Edittext pada sebuah Textview saja supaya tidak terlalu banyak komponen Textview. Berikut isi dari <b>activity_main.xml</b> dan <b>MainActivity.java</b> :</div><div><br /></div><div><b>activity_main.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">RelativeLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">EditText<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/inputNama"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:autofillHints</span><span style="color: green; font-weight: bold;">="no"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@null"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:hint</span><span style="color: green; font-weight: bold;">="Nama"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:inputType</span><span style="color: green; font-weight: bold;">="text"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColorHint</span><span style="color: green; font-weight: bold;">="@color/silver"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">EditText<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/inputAlamat"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@id/inputNama"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:autofillHints</span><span style="color: green; font-weight: bold;">="no"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@null"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:hint</span><span style="color: green; font-weight: bold;">="Alamat"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:inputType</span><span style="color: green; font-weight: bold;">="text"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColorHint</span><span style="color: green; font-weight: bold;">="@color/silver"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">EditText<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/inputUmur"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@id/inputAlamat"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:autofillHints</span><span style="color: green; font-weight: bold;">="no"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@null"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:hint</span><span style="color: green; font-weight: bold;">="Umur"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:inputType</span><span style="color: green; font-weight: bold;">="number"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColorHint</span><span style="color: green; font-weight: bold;">="@color/silver"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/simpanData"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@id/inputUmur"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerHorizontal</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Simpan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="22sp"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/lihatData"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@id/simpanData"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerHorizontal</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Lihat Data"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="22sp"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/hapusData"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@id/lihatData"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerHorizontal</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="40dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Hapus Data"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="22sp"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/dataDiri"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@id/hapusData"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="monospace"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"</span>/><br /> <br /></<span style="color: navy; font-weight: bold;">RelativeLayout</span>></pre></div><br /><div><b>MainActivity.java</b>:<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.sharedpreferences;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.content.SharedPreferences;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.EditText;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>EditText <span style="color: #660e7a; font-weight: bold;">inputNama</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>EditText <span style="color: #660e7a; font-weight: bold;">inputAlamat</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>EditText <span style="color: #660e7a; font-weight: bold;">inputUmur</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>TextView <span style="color: #660e7a; font-weight: bold;">dataDiri</span>;<br /> SharedPreferences <span style="color: #660e7a; font-weight: bold;">prefs</span>;<br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">inputNama </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">inputNama</span>);<br /> <span style="color: #660e7a; font-weight: bold;">inputAlamat </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">inputAlamat</span>);<br /> <span style="color: #660e7a; font-weight: bold;">inputUmur </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">inputUmur</span>);<br /> <span style="color: #660e7a; font-weight: bold;">dataDiri </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">dataDiri</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">prefs </span>= getSharedPreferences(<span style="color: green; font-weight: bold;">"PENGATURAN"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik Menyimpan data pada Edittext ke SharedPreferences<br /></span><span style="color: grey; font-style: italic;"> </span>findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">simpanData</span>).setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /><br /> <span style="color: grey; font-style: italic;">//Jika Edittext kosong maka data tidak akan disimpan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">inputNama</span>.getText().toString().isEmpty()) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Nama perlu diisi"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">inputAlamat</span>.getText().toString().isEmpty()) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Alamat perlu diisi"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">inputUmur</span>.getText().toString().isEmpty()) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Umur perlu diisi"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Menyimpan data pada Edittext ke SharedPreferences<br /></span><span style="color: grey; font-style: italic;"> </span>SharedPreferences.Editor editor = <span style="color: #660e7a; font-weight: bold;">prefs</span>.edit();<br /> editor.putString(<span style="color: green; font-weight: bold;">"nama"</span>, <span style="color: #660e7a; font-weight: bold;">inputNama</span>.getText().toString());<br /> editor.putString(<span style="color: green; font-weight: bold;">"alamat"</span>, <span style="color: #660e7a; font-weight: bold;">inputAlamat</span>.getText().toString());<br /> editor.putInt(<span style="color: green; font-weight: bold;">"umur"</span>, Integer.<span style="font-style: italic;">parseInt</span>(<span style="color: #660e7a; font-weight: bold;">inputUmur</span>.getText().toString()));<br /> editor.apply();<br /><br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Data berhasil disimpan"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /><br /> <span style="color: grey; font-style: italic;">//Menghapus semua teks pada Edittext setelah data tersimpan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">inputNama</span>.getText().clear();<br /> <span style="color: #660e7a; font-weight: bold;">inputAlamat</span>.getText().clear();<br /> <span style="color: #660e7a; font-weight: bold;">inputUmur</span>.getText().clear();<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik Memuat data dari SharedPreferences dan menampilkannya ke Textview<br /></span><span style="color: grey; font-style: italic;"> </span>findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">lihatData</span>).setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> String nama = <span style="color: #660e7a; font-weight: bold;">prefs</span>.getString(<span style="color: green; font-weight: bold;">"nama"</span>, <span style="color: green; font-weight: bold;">"Belum ada data nama"</span>);<br /> String alamat = <span style="color: #660e7a; font-weight: bold;">prefs</span>.getString(<span style="color: green; font-weight: bold;">"alamat"</span>, <span style="color: green; font-weight: bold;">"Belum ada data alamat"</span>);<br /> <span style="color: navy; font-weight: bold;">int </span>umur = <span style="color: #660e7a; font-weight: bold;">prefs</span>.getInt(<span style="color: green; font-weight: bold;">"umur"</span>, <span style="color: blue;">0</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">dataDiri</span>.setText(nama + <span style="color: green; font-weight: bold;">"</span><span style="color: navy; font-weight: bold;">\n</span><span style="color: green; font-weight: bold;">" </span>+ alamat + <span style="color: green; font-weight: bold;">"</span><span style="color: navy; font-weight: bold;">\n</span><span style="color: green; font-weight: bold;">" </span>+ umur);<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik untuk menghapus kata kunci string SharedPreferences<br /></span><span style="color: grey; font-style: italic;"> </span>findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">hapusData</span>).setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> SharedPreferences.Editor editor = <span style="color: #660e7a; font-weight: bold;">prefs</span>.edit();<br /> editor.remove(<span style="color: green; font-weight: bold;">"nama"</span>);<br /> editor.remove(<span style="color: green; font-weight: bold;">"alamat"</span>);<br /> editor.remove(<span style="color: green; font-weight: bold;">"umur"</span>);<br /> editor.apply();<br /><br /> <span style="color: #660e7a; font-weight: bold;">dataDiri</span>.setText(<span style="color: green; font-weight: bold;">""</span>);<br /><br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Data dihapus!"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> }<br />}</pre></div></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/R0TPsrF_TLM" width="320" youtube-src-id="R0TPsrF_TLM"></iframe></div><br /><div><br /></div><div>Saya rasa cukup sekian untuk tutorial ini, terima kasih sudah meluangkan waktu berkunjung. Jika ada yang ingin ditanyakan silahkan diposting di kolom komentar.</div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-26777495318246641592020-11-16T16:56:00.002+07:002021-02-22T18:46:38.652+07:00Contoh Sederhana Menggunakan SQLite Database Android (Aplikasi Catatan)<p>Pada artikel ini blog gwnbs akan berbagi tentang contoh penggunaan <b>SQLite Database</b> pada aplikasi Android. <b>SQLite Database </b>memiliki metode untuk membuat, menghapus, menjalankan perintah SQL, dan melakukan tugas manajemen database umum lainnya. Dalam tutorial ini kita akan mengaplikasikan <b>CRUD </b>(Create, Read, Update, Delete) data pada sebuah aplikasi catatan sederhana.<span></span></p><a name='more'></a><p></p><p><br /></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/IfF4o7DWxNQ" width="320" youtube-src-id="IfF4o7DWxNQ"></iframe></div><br /><p>Pada contoh aplikasi catatan sederhana ini, kita hanya akan memiliki satu aktivitas saja yakni <b>MainActivity.java</b> dengan <b>activity_main.xml</b> untuk layout, tentunya supaya tidak merepotkan, dan kita akan banyak menggunakan kodingan "<i>setVisibility</i>" pada objek-objeknya lalu dialog untuk memperlihatkan daftar catatan.</p><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Pertama mari kita mulai dengan terlebih dahulu membuat sebuah Java Class sebagai pembantu penghubung ke SQLite nya. Disini kita namakan dengan <b>DBHelper.java</b>, Class ini memperpanjang class SQLiteOpenHelper. Di dalam class ini hal-hal yang harus ditentukan terlebih dahulu adalah nama dari databasenya, versi databasenya, kemudian nama tabel dan kolom-kolom untuk menempatkan atribut-atribut dari catatan nantinya.<div><br /></div><div>Atribut catatan pada contoh ini hanya 3 saja yakni : id catatan, judul catatan dan konten catatannya. Id digunakan nantinya untuk menyunting dan menghapus catatan. Sebelum lebih lanjut terhadap class DBHelper.java, terlebih dahulu kita perlu membuat sebuah class lagi sebagai metode <b>Setter </b>dan <b>Getter </b>data catatan, yakni yang sudah disebutkan sebelumnya : id, judul, dan catatan. Disini kita berikan nama <b>SetterGetterData.java</b>. Berikut isi dari java class ini :</div><div><br /></div><div><b>SetterGetterData.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 300px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.sqlite;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>SetterGetterData {<br /><br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">id</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">judul</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">catatan</span>;<br /><br /> <span style="color: navy; font-weight: bold;">public int </span>getId() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">id</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>setId(<span style="color: navy; font-weight: bold;">int </span>id) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">id </span>= id;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public </span>String getJudul() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">judul</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>setJudul(String judul) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">judul </span>= judul;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public </span>String getCatatan() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">catatan</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>setCatatan(String catatan) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">catatan </span>= catatan;<br /> }<br />}<br /></pre></div><div><br /></div><div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /></div>Kita lanjut pada class <b>DBHelper.java</b>, dalam memasukkan dan memperbaharui data catatan kita akan menggunakan bantuan <i>boolean </i>untuk mengetahui apakah data catatan berhasil disimpan atau tidak. Lalu untuk mendapatkan data dari seluruh catatan pada database, kita menggunakan <i>Cursor </i>untuk mencegah dari memuat semua data kedalam memori, juga tentunya karena data pada catatan berubah-ubah. Terakhir metode menghapus data kita cukup menggunakan void saja.</div><div><br /></div><div>Untuk metode hapus disini kita buat 2 metode penghapusan yaitu : penghapusan secara satu-persatu dan penghapusan secara keseluruhan. Penghapusan satu persatu adalah melalui id catatan dan penghapusan seluruh data catatan adalah dengan menghapus tabel catatan pada database. Berikut isi selengkapnya dari class ini, disertai sedikit java comment didalamnya.</div><div><br /></div><div><b>DBHelper.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 350px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.sqlite;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.content.ContentValues;<br /><span style="color: navy; font-weight: bold;">import </span>android.content.Context;<br /><span style="color: navy; font-weight: bold;">import </span>android.database.Cursor;<br /><span style="color: navy; font-weight: bold;">import </span>android.database.sqlite.SQLiteDatabase;<br /><span style="color: navy; font-weight: bold;">import </span>android.database.sqlite.SQLiteOpenHelper;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>DBHelper <span style="color: navy; font-weight: bold;">extends </span>SQLiteOpenHelper {<br /><br /> <span style="color: grey; font-style: italic;">//Penentuan nama dan versi database<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public </span>DBHelper (Context context) {<br /> <span style="color: navy; font-weight: bold;">super</span>(context, <span style="color: green; font-weight: bold;">"catatan.db"</span>, <span style="color: navy; font-weight: bold;">null</span>, <span style="color: blue;">1</span>);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Perintah membuat tabel catatan dan kolom-kolom untuk atribut catatan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onCreate(SQLiteDatabase db) {<br /> db.execSQL(<span style="color: green; font-weight: bold;">"CREATE TABLE catatan_table(id INTEGER PRIMARY KEY, judul TEXT, catatan TEXT)"</span>);<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onUpgrade(SQLiteDatabase db, <span style="color: navy; font-weight: bold;">int </span>oldVersion, <span style="color: navy; font-weight: bold;">int </span>newVersion) {<br /> db.execSQL(<span style="color: green; font-weight: bold;">"DROP TABLE IF EXISTS catatan_table"</span>);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Memasukkan data catatan ke dalam SQLite database<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public boolean </span>masukkanCatatan (SetterGetterData sgd) {<br /> SQLiteDatabase db = getWritableDatabase();<br /> ContentValues cv = <span style="color: navy; font-weight: bold;">new </span>ContentValues();<br /> cv.put(<span style="color: green; font-weight: bold;">"judul"</span>, sgd.getJudul());<br /> cv.put(<span style="color: green; font-weight: bold;">"catatan"</span>, sgd.getCatatan());<br /> <span style="color: navy; font-weight: bold;">return </span>db.insert(<span style="color: green; font-weight: bold;">"catatan_table"</span>, <span style="color: navy; font-weight: bold;">null</span>, cv) > <span style="color: blue;">0</span>;<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Mendapatkan seluruh data catatan dari Database<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public </span>Cursor dapatkanSemuaCatatan () {<br /> SQLiteDatabase db = getReadableDatabase();<br /> <span style="color: navy; font-weight: bold;">return </span>db.rawQuery(<span style="color: green; font-weight: bold;">"Select * from " </span>+ <span style="color: green; font-weight: bold;">"catatan_table"</span>, <span style="color: navy; font-weight: bold;">null</span>);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Memperbaharui catatan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public boolean </span>perbaharuiCatatan(SetterGetterData sgd, <span style="color: navy; font-weight: bold;">int </span>id) {<br /> SQLiteDatabase db = getWritableDatabase();<br /> ContentValues cv = <span style="color: navy; font-weight: bold;">new </span>ContentValues();<br /> cv.put(<span style="color: green; font-weight: bold;">"judul"</span>, sgd.getJudul());<br /> cv.put(<span style="color: green; font-weight: bold;">"catatan"</span>, sgd.getCatatan());<br /> <span style="color: navy; font-weight: bold;">return </span>db.update(<span style="color: green; font-weight: bold;">"catatan_table"</span>, cv, <span style="color: green; font-weight: bold;">"id" </span>+ <span style="color: green; font-weight: bold;">"=" </span>+ id,<br /> <span style="color: navy; font-weight: bold;">null</span>) > <span style="color: blue;">0</span>;<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Metode menghapus sebuah catatan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public void </span>hapusCatatan (<span style="color: navy; font-weight: bold;">int </span>id) {<br /> SQLiteDatabase db = getReadableDatabase();<br /> db.delete(<span style="color: green; font-weight: bold;">"catatan_table"</span>, <span style="color: green; font-weight: bold;">"id" </span>+ <span style="color: green; font-weight: bold;">"=" </span>+ id, <span style="color: navy; font-weight: bold;">null</span>);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Menghapus seluruh data catatan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public void </span>hapusSemuaCatatan () {<br /> SQLiteDatabase db = getReadableDatabase();<br /> db.delete(<span style="color: green; font-weight: bold;">"catatan_table"</span>, <span style="color: navy; font-weight: bold;">null</span>, <span style="color: navy; font-weight: bold;">null</span>);<br /> }<br /><br />}<br /></pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Sekarang di <b>activity.main.xml</b>, di dalam layout ini memiliki 7 views dan 4 layouts. 2 komponen Edittext untuk mengisi judul dan konten catatan, Textview sebagai tombol untuk menyimpan catatan, kemudian sebuah tombol pembatalan, dan 2 buah komponen Textview lagi sebagai navigasi untuk membuat catatan baru dan melihat seluruh data catatan. Berikut isinya.</div><div><br /></div><div><b>activity_main.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 350px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">RelativeLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutUtama"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerInParent</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/buatCatatanBaru"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="?selectableItemBackground"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">=" Buat Catatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="30sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:drawableStartCompat</span><span style="color: green; font-weight: bold;">="@drawable/ic_tambah" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/daftarCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="?selectableItemBackground"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">=" Daftar Catatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="30sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:drawableStartCompat</span><span style="color: green; font-weight: bold;">="@drawable/ic_daftar" </span>/><br /><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">RelativeLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:visibility</span><span style="color: green; font-weight: bold;">="gone"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutJudul"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center_vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">View<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="4dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorAccent"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">EditText<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/judulCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_weight</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:autofillHints</span><span style="color: green; font-weight: bold;">="no"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@null"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center_vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:hint</span><span style="color: green; font-weight: bold;">="Judul catatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:inputType</span><span style="color: green; font-weight: bold;">="textCapWords"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/simpanCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Simpan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="12sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:drawableTopCompat</span><span style="color: green; font-weight: bold;">="@drawable/ic_simpan" </span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">EditText<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/isiCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_below</span><span style="color: green; font-weight: bold;">="@id/layoutJudul"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:autofillHints</span><span style="color: green; font-weight: bold;">="no"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@null"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="top"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:hint</span><span style="color: green; font-weight: bold;">="Konten catatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:inputType</span><span style="color: green; font-weight: bold;">="textMultiLine|textCapSentences"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:minHeight</span><span style="color: green; font-weight: bold;">="250dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="20sp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">Button<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/batal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_alignParentBottom</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_centerHorizontal</span><span style="color: green; font-weight: bold;">="true"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Batal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">RelativeLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">RelativeLayout</span>></pre></div><div><br /></div><div><br /></div>Daftar seluruh data catatan akan ditampilkan menggunakan <b>Listview </b>di dalam sebuah dialog dan hanya diperlihatkan judul dari catatan, tombol edit dan hapus catatan. Sehingga kita akan memerlukan layout custom dan sebuah java class sebagai adapter. Layout custom kita namakan <b>item_catatan.xml</b> dan adapter kita namakan <b>AdapterDaftarCatatan.java</b>. </div><div><br /></div><div>Di dalam custom layout tersebut kita memiliki sebuah Textview untuk judul dari catatan, dan 2 buah komponen Imageview sebagai tombol untuk menghapus catatan satu-persatu dan untuk menyunting catatan. Seluruh kodingan java yang diperlukan untuk semua komponen pada layout ini diaplikasikan di dalam file <b>AdapterDaftarCatatan.java</b>. Berikut isi dari kedua file ini.</div><div><br /></div><div><b>item_catatan.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">LinearLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center_vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:ignore</span><span style="color: green; font-weight: bold;">="UselessParent"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/itemJudul"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_weight</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="22sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="3dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Judul catatan nantinya disini"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/editCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="15dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/ic_edit" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/hapusCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/ic_hapus" </span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre></div><div><br /></div><div><br /></div><b>AdapterDaftarCatatan.java</b>:<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 300px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.sqlite;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.content.Context;<br /><span style="color: navy; font-weight: bold;">import </span>android.content.Intent;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.LayoutInflater;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.ViewGroup;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.BaseAdapter;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ImageView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AlertDialog;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.List;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>AdapterDaftarCatatan <span style="color: navy; font-weight: bold;">extends </span>BaseAdapter {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>List<SetterGetterData> <span style="color: #660e7a; font-weight: bold;">sgd</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>Context <span style="color: #660e7a; font-weight: bold;">context</span>;<br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">id</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>AlertDialog <span style="color: #660e7a; font-weight: bold;">daftarCatatan</span>;<br /><br /> <span style="color: navy; font-weight: bold;">public </span>AdapterDaftarCatatan(List<SetterGetterData> sgd, Context context, AlertDialog daftarCatatan) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">sgd </span>= sgd;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">context </span>= context;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">daftarCatatan </span>= daftarCatatan;<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public int </span>getCount() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">sgd</span>.size();<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public </span>Object getItem(<span style="color: navy; font-weight: bold;">int </span>position) {<br /> <span style="color: navy; font-weight: bold;">return </span>position;<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public long </span>getItemId(<span style="color: navy; font-weight: bold;">int </span>position) {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: blue;">0</span>;<br /> }<br /><br /> <span style="color: olive;">@SuppressLint</span>({<span style="color: green; font-weight: bold;">"ViewHolder"</span>, <span style="color: green; font-weight: bold;">"InflateParams"</span>})<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public </span>View getView(<span style="color: navy; font-weight: bold;">int </span>position, View view, ViewGroup parent) {<br /><br /> LayoutInflater layoutInflater = LayoutInflater.<span style="font-style: italic;">from</span>(<span style="color: #660e7a; font-weight: bold;">context</span>);<br /> view = layoutInflater.inflate(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">item_catatan</span>, <span style="color: navy; font-weight: bold;">null</span>);<br /><br /> TextView itemJudul = view.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">itemJudul</span>);<br /> ImageView editCatatan = view.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">editCatatan</span>);<br /> ImageView hapusCatatan = view.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">hapusCatatan</span>);<br /><br /> <span style="color: navy; font-weight: bold;">final </span>SetterGetterData setterGetterData = <span style="color: #660e7a; font-weight: bold;">sgd</span>.get(position);<br /> itemJudul.setText(setterGetterData.getJudul());<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik untuk melihat catatan<br /></span><span style="color: grey; font-style: italic;"> </span>itemJudul.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> Intent i = <span style="color: navy; font-weight: bold;">new </span>Intent(<span style="color: #660e7a; font-weight: bold;">context</span>, MainActivity.<span style="color: navy; font-weight: bold;">class</span>);<br /> i.putExtra(<span style="color: green; font-weight: bold;">"judul"</span>, <span style="color: #660e7a;">setterGetterData</span>.getJudul());<br /> i.putExtra(<span style="color: green; font-weight: bold;">"catatan"</span>, <span style="color: #660e7a;">setterGetterData</span>.getCatatan());<br /> i.putExtra(<span style="color: green; font-weight: bold;">"lihat"</span>, <span style="color: navy; font-weight: bold;">true</span>);<br /> i.setFlags(Intent.<span style="color: #660e7a; font-style: italic; font-weight: bold;">FLAG_ACTIVITY_NEW_TASK</span>);<br /> <span style="color: #660e7a; font-weight: bold;">context</span>.startActivity(i);<br /> <span style="color: #660e7a; font-weight: bold;">daftarCatatan</span>.dismiss();<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//metode klik untuk menyunting catatan, dengan mengirimkan intent ke aktivitas yang sama<br /></span><span style="color: grey; font-style: italic;"> //setelah aktivitas di refresh, mode edit catatan aktif. <br /></span><span style="color: grey; font-style: italic;"> </span>editCatatan.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: #660e7a; font-weight: bold;">id </span>= <span style="color: #660e7a;">setterGetterData</span>.getId();<br /> Intent i = <span style="color: navy; font-weight: bold;">new </span>Intent(<span style="color: #660e7a; font-weight: bold;">context</span>, MainActivity.<span style="color: navy; font-weight: bold;">class</span>);<br /> i.putExtra(<span style="color: green; font-weight: bold;">"id"</span>, <span style="color: #660e7a; font-weight: bold;">id</span>);<br /> i.putExtra(<span style="color: green; font-weight: bold;">"judul"</span>, <span style="color: #660e7a;">setterGetterData</span>.getJudul());<br /> i.putExtra(<span style="color: green; font-weight: bold;">"catatan"</span>, <span style="color: #660e7a;">setterGetterData</span>.getCatatan());<br /> i.putExtra(<span style="color: green; font-weight: bold;">"edit"</span>, <span style="color: navy; font-weight: bold;">true</span>);<br /> i.setFlags(Intent.<span style="color: #660e7a; font-style: italic; font-weight: bold;">FLAG_ACTIVITY_NEW_TASK</span>);<br /> <span style="color: #660e7a; font-weight: bold;">context</span>.startActivity(i);<br /> <span style="color: #660e7a; font-weight: bold;">daftarCatatan</span>.dismiss();<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//menghapus catatan satu-persatu<br /></span><span style="color: grey; font-style: italic;"> </span>hapusCatatan.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: #660e7a; font-weight: bold;">id </span>= <span style="color: #660e7a;">setterGetterData</span>.getId();<br /> DBHelper dbHelper = <span style="color: navy; font-weight: bold;">new </span>DBHelper(<span style="color: #660e7a; font-weight: bold;">context</span>);<br /> dbHelper.hapusCatatan(<span style="color: #660e7a; font-weight: bold;">id</span>);<br /> notifyDataSetChanged();<br /> dbHelper.close();<br /> Toast.<span style="font-style: italic;">makeText</span>(<span style="color: #660e7a; font-weight: bold;">context</span>, <span style="color: green; font-weight: bold;">"Catatan dihapus"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> <span style="color: #660e7a; font-weight: bold;">daftarCatatan</span>.dismiss();<br /> }<br /> });<br /><br /> <span style="color: navy; font-weight: bold;">return </span>view;<br /> }<br />}<br /></pre></pre></div><div><br /></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Selanjutnya yang kita butuhkan adalah custom layout untuk dialog yang akan menampilkan daftar seluruh catatan. Layout ini kita berikan nama <b>layout_daftar_catatan.xml</b>. Di dalam layout ini sebuah komponen Listview yang disebutkan diatas sebelumnya disematkan, kemudian sebuah Textview sebagai tombol untuk menghapus seluruh data catatan. Pengaplikasian seluruh koding java untuk layout ini dilakukan di <b>MainActivity.java</b>. Berikut isi dari layout ini :</div><div><br /></div><div><b>layout_daftar_catatan.xml</b> :<br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">LinearLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@color/colorPrimary"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:ignore</span><span style="color: green; font-weight: bold;">="UselessParent"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center_vertical"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/judulDaftar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_weight</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="27sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Daftar Catatan" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/hapusSemuaCatatan"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="?selectableItemBackground"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Hapus Semua"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="10sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:drawableTopCompat</span><span style="color: green; font-weight: bold;">="@drawable/ic_hapus_semua" </span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">ListView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/listDaftar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:clickable</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:focusable</span><span style="color: green; font-weight: bold;">="false"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="10dp"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre></div><div><br /></div><div><br /></div>Terakhir pada <b>MainActivity.java</b>. Cukup bayak kodingan yang diaplikasikan pada file ini, di dalamnya saya sematkan java comment pada tiap-tiap metode sebagai sedikit penjelasan. Berikut isi selengkapnya dari file aktivitas ini.</div><div><br /></div><div><b>MainActivity.java</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 350px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.sqlite;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.content.Intent;<br /><span style="color: navy; font-weight: bold;">import </span>android.database.Cursor;<br /><span style="color: navy; font-weight: bold;">import </span>android.graphics.drawable.ColorDrawable;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.LayoutInflater;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Button;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.EditText;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.LinearLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ListView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.RelativeLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AlertDialog;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.ArrayList;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: grey; font-style: italic;">//Variabel global<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">private </span>TextView <span style="color: #660e7a; font-weight: bold;">simpanCatatan</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>EditText <span style="color: #660e7a; font-weight: bold;">judulCatatan</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>EditText <span style="color: #660e7a; font-weight: bold;">isiCatatan</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>Button <span style="color: #660e7a; font-weight: bold;">batal</span>;<br /> <span style="color: navy; font-weight: bold;">private boolean </span><span style="color: #660e7a; font-weight: bold;">edit </span>= <span style="color: navy; font-weight: bold;">false</span>;<br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">id </span>= <span style="color: blue;">0</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>RelativeLayout <span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>LinearLayout <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>;<br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: grey; font-style: italic;">//View id dari seluruh komponen pada layout<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">judulCatatan </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">judulCatatan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">isiCatatan </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">isiCatatan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">simpanCatatan </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">simpanCatatan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">batal </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">batal</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutCatatan </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layoutCatatan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layoutUtama</span>);<br /><br /> <span style="color: grey; font-style: italic;">//Local variabel dan view id<br /></span><span style="color: grey; font-style: italic;"> </span>TextView buatCatatanBaru = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">buatCatatanBaru</span>);<br /> TextView daftarCatatan = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">daftarCatatan</span>);<br /><br /> <span style="color: grey; font-style: italic;">//Menerima intent yang dikirimkan oleh tombol edit catatan.<br /></span><span style="color: grey; font-style: italic;"> //jika nilai boolean edit adalah true maka mode sunting catatan aktif.<br /></span><span style="color: grey; font-style: italic;"> </span>Intent i = getIntent();<br /> <span style="color: #660e7a; font-weight: bold;">edit </span>= i.getBooleanExtra(<span style="color: green; font-weight: bold;">"edit"</span>, <span style="color: navy; font-weight: bold;">false</span>);<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">edit</span>) {<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Mode untuk melihat catatan<br /></span><span style="color: grey; font-style: italic;"> //Aktif saat judul catatan pada daftar catatan di klik.<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">boolean </span>lihat = i.getBooleanExtra(<span style="color: green; font-weight: bold;">"lihat"</span>, <span style="color: navy; font-weight: bold;">false</span>);<br /> <span style="color: navy; font-weight: bold;">if </span>(lihat) {<br /> <span style="color: #660e7a; font-weight: bold;">simpanCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">INVISIBLE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">batal</span>.setText(<span style="color: green; font-weight: bold;">"Kembali"</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /> <span style="color: #660e7a; font-weight: bold;">judulCatatan</span>.setText(i.getStringExtra(<span style="color: green; font-weight: bold;">"judul"</span>));<br /> <span style="color: #660e7a; font-weight: bold;">isiCatatan</span>.setText(i.getStringExtra(<span style="color: green; font-weight: bold;">"catatan"</span>));<br /> <span style="color: #660e7a; font-weight: bold;">id </span>= i.getIntExtra(<span style="color: green; font-weight: bold;">"id"</span>, <span style="color: blue;">0</span>);<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik untuk tombol membuat catatan baru<br /></span><span style="color: grey; font-style: italic;"> </span>buatCatatanBaru.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: #660e7a; font-weight: bold;">simpanCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">batal</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">batal</span>.setText(<span style="color: green; font-weight: bold;">"Batal"</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik untuk tombol menyimpan catatan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">simpanCatatan</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">judulCatatan</span>.getText().toString().isEmpty()) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Judul tidak boleh kosong"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">isiCatatan</span>.getText().toString().isEmpty()) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Konten catatan tidak boleh kosong"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /><br /> DBHelper dbHelper = <span style="color: navy; font-weight: bold;">new </span>DBHelper(getApplicationContext());<br /> SetterGetterData sgd = <span style="color: navy; font-weight: bold;">new </span>SetterGetterData();<br /> sgd.setJudul(<span style="color: #660e7a; font-weight: bold;">judulCatatan</span>.getText().toString());<br /> sgd.setCatatan(<span style="color: #660e7a; font-weight: bold;">isiCatatan</span>.getText().toString());<br /><br /> <span style="color: navy; font-weight: bold;">boolean </span>masukkanCatatan;<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">edit</span>) {<br /> masukkanCatatan = dbHelper.perbaharuiCatatan(sgd, <span style="color: #660e7a; font-weight: bold;">id</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> masukkanCatatan = dbHelper.masukkanCatatan(sgd);<br /> }<br /> <span style="color: navy; font-weight: bold;">if </span>(masukkanCatatan) {<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Catatan berhasil disimpan"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Kesalahan terjadi!"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> }<br /> dbHelper.close();<br /><br /> <span style="color: #660e7a; font-weight: bold;">judulCatatan</span>.getText().clear();<br /> <span style="color: #660e7a; font-weight: bold;">isiCatatan</span>.getText().clear();<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik untuk tombol melihat daftar catatan<br /></span><span style="color: grey; font-style: italic;"> </span>daftarCatatan.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> dialogDaftarCatatan();<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Metode klik untuk tombol membatalkan pembuatan atau penyuntingan catatan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">batal</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: #660e7a; font-weight: bold;">judulCatatan</span>.getText().clear();<br /> <span style="color: #660e7a; font-weight: bold;">isiCatatan</span>.getText().clear();<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">edit </span>= <span style="color: navy; font-weight: bold;">false</span>;<br /> }<br /> });<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Dialog untuk menampilkan daftar catatan<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: navy; font-weight: bold;">private void </span>dialogDaftarCatatan () {<br /> AlertDialog.Builder b = <span style="color: navy; font-weight: bold;">new </span>AlertDialog.Builder(<span style="color: navy; font-weight: bold;">this</span>);<br /> LayoutInflater inflater = LayoutInflater.<span style="font-style: italic;">from</span>(<span style="color: navy; font-weight: bold;">this</span>);<br /> View v = inflater.inflate(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layout_daftar_catatan</span>, <span style="color: navy; font-weight: bold;">null</span>);<br /> TextView hapusSemuaCatatan = v.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">hapusSemuaCatatan</span>);<br /> TextView judulDaftar = v.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">judulDaftar</span>);<br /> b.setView(v);<br /><br /> ArrayList<SetterGetterData> setterGetterData = <span style="color: navy; font-weight: bold;">new </span>ArrayList<>();<br /> ListView listDaftar = v.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">listDaftar</span>);<br /> <span style="color: navy; font-weight: bold;">final </span>DBHelper dh = <span style="color: navy; font-weight: bold;">new </span>DBHelper(getApplicationContext());<br /> Cursor cursor = dh.dapatkanSemuaCatatan();<br /> cursor.moveToFirst();<br /> <span style="color: navy; font-weight: bold;">if </span>(cursor.getCount() > <span style="color: blue;">0</span>) {<br /> <span style="color: navy; font-weight: bold;">while </span>(!cursor.isAfterLast()) {<br /> SetterGetterData sgd = <span style="color: navy; font-weight: bold;">new </span>SetterGetterData();<br /> sgd.setId((cursor.getInt(cursor.getColumnIndexOrThrow(<span style="color: green; font-weight: bold;">"id"</span>))));<br /> sgd.setJudul((cursor.getString(cursor.getColumnIndexOrThrow(<span style="color: green; font-weight: bold;">"judul"</span>))));<br /> sgd.setCatatan((cursor.getString(cursor.getColumnIndexOrThrow(<span style="color: green; font-weight: bold;">"catatan"</span>))));<br /> setterGetterData.add(sgd);<br /> cursor.moveToNext();<br /> }<br /> dh.close();<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">final </span>AlertDialog daftarCatatan = b.create();<span style="color: navy; font-weight: bold;">if </span>(daftarCatatan.getWindow() !=<span style="color: navy; font-weight: bold;">null</span>) {<br /> daftarCatatan.getWindow().setBackgroundDrawable(<span style="color: navy; font-weight: bold;">new </span>ColorDrawable(<span style="color: blue;">0</span>));<br /> }<br /> AdapterDaftarCatatan adapterDaftarCatatan = <span style="color: navy; font-weight: bold;">new </span>AdapterDaftarCatatan(setterGetterData, getApplicationContext(),<br /> daftarCatatan);<br /> listDaftar.setAdapter(adapterDaftarCatatan);<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(listDaftar.getAdapter().getCount() < <span style="color: blue;">2</span>) {<br /> hapusSemuaCatatan.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">INVISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> hapusSemuaCatatan.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">if </span>(listDaftar.getAdapter().getCount() < <span style="color: blue;">1</span>) {<br /> judulDaftar.setText(<span style="color: green; font-weight: bold;">"Kosong"</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> judulDaftar.setText(<span style="color: green; font-weight: bold;">"Daftar Catatan"</span>);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">//Menghapus seluruh catatan<br /></span><span style="color: grey; font-style: italic;"> </span>hapusSemuaCatatan.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: #660e7a;">dh</span>.hapusSemuaCatatan();<br /> <span style="color: #660e7a;">dh</span>.close();<br /> <span style="color: #660e7a;">daftarCatatan</span>.dismiss();<br /> Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Seluruh catatan dihapus"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();<br /> }<br /> });<br /><br /> daftarCatatan.show();<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>onBackPressed () {<br /> <span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>.getVisibility() == View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>) {<br /> <span style="color: #660e7a; font-weight: bold;">layoutCatatan</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">GONE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setVisibility(View.<span style="color: #660e7a; font-style: italic; font-weight: bold;">VISIBLE</span>);<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> finishAffinity();<br /> }<br /> }<br />}</pre></div><div><br /></div><div><br /></div><div><br /></div>Dan berikut screenshoot dari project ini, yang memperlihatkan folder-folder dan file-file yang digunakan pada tutorial ini.</div><div><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpDbam0iIoUXuKXr9PJ5KdwkzpzMZYXZ9yyOtRy7sEuo4eNBCa6AOKlicPu_8563kWCx8wPB8nOhuM1nHSnGFG9tA4KZzxgPYm_rMvFrvGe53EnCdVVdmcOO3ps_xd-7VW8Ujk9O8JzXY/s1366/Screenshot+%252816%2529.png" style="margin-left: auto; margin-right: auto;"><img alt="Conntoh menggunakan SQLite Database di Android" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpDbam0iIoUXuKXr9PJ5KdwkzpzMZYXZ9yyOtRy7sEuo4eNBCa6AOKlicPu_8563kWCx8wPB8nOhuM1nHSnGFG9tA4KZzxgPYm_rMvFrvGe53EnCdVVdmcOO3ps_xd-7VW8Ujk9O8JzXY/w320-h180/Screenshot+%252816%2529.png" title="SQLite Database" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Gambar 1 : Folder-folder dan file-file yang digunakan.<br /></td></tr></tbody></table><br /><div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>Untuk icon drawable yang digunakan dalam tutorial ini jumlah seluruhnya ada 6. Dapat dibuat melalui vector asset pada Android Studio. Pun begitu, berikut saya sertakan seluruhnya.</div><div><br /></div><div><b>ic_daftar.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 200px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="?attr/colorControlNormal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M4,14h4v-4L4,10v4zM4,19h4v-4L4,15v4zM4,9h4L8,5L4,5v4zM9,14h12v-4L9,10v4zM9,19h12v-4L9,15v4zM9,5v4h12L21,5L9,5z" </span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div><br /></div><div><br /></div><div><b>ic_edit.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 200px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z" </span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div><br /></div><div><br /></div><div><b>ic_hapus.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 200px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="24dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2L18,7L6,7v12zM8,9h8v10L8,19L8,9zM15.5,4l-1,-1h-5l-1,1L5,4v2h14L19,4z" </span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div><br /></div><div><br /></div><div><b>ic_hapus_semua.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 200px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="#CC0000"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z" </span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div><br /></div><div><br /></div><div><b>ic_simpan.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 200px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="?attr/colorControlNormal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M17,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,7l-4,-4zM12,19c-1.66,0 -3,-1.34 -3,-3s1.34,-3 3,-3 3,1.34 3,3 -1.34,3 -3,3zM15,9L5,9L5,5h10v4z" </span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div><br /></div><div><br /></div><div><b>ic_tambah.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 200px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">vector </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:height</span><span style="color: green; font-weight: bold;">="30dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:tint</span><span style="color: green; font-weight: bold;">="?attr/colorControlNormal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportWidth</span><span style="color: green; font-weight: bold;">="24"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:viewportHeight</span><span style="color: green; font-weight: bold;">="24"</span>><br /> <<span style="color: navy; font-weight: bold;">path<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fillColor</span><span style="color: green; font-weight: bold;">="@android:color/white"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:pathData</span><span style="color: green; font-weight: bold;">="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" </span>/><br /></<span style="color: navy; font-weight: bold;">vector</span>><br /></pre></div><div><br /></div><div><br /></div>Kemudian dibawah ini isi dari file-file value, yakni : colors.xml, strings.xml dan styles.xml.</div><div><br /></div><div><b>colors.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 150px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">resources</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimary"</span>>#F57C00</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimaryDark"</span>>#93452D</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorAccent"</span>>#AFB42B</<span style="color: navy; font-weight: bold;">color</span>><br /> <<span style="color: navy; font-weight: bold;">color </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="hitam"</span>>#272424</<span style="color: navy; font-weight: bold;">color</span>><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div><br /></div><div><br /></div><div><b>strings.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 100px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">resources</span>><br /> <<span style="color: navy; font-weight: bold;">string </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="app_name"</span>>SQ Lite</<span style="color: navy; font-weight: bold;">string</span>><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div><br /></div><div><br /></div><div><b>styles.xml</b> :<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 150px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">resources</span>><br /> <span style="color: grey; font-style: italic;"><!-- Base application theme. --><br /></span><span style="color: grey; font-style: italic;"> </span><<span style="color: navy; font-weight: bold;">style </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="AppTheme" </span><span style="color: blue; font-weight: bold;">parent</span><span style="color: green; font-weight: bold;">="Theme.AppCompat.Light.DarkActionBar"</span>><br /> <span style="color: grey; font-style: italic;"><!-- Customize your theme here. --><br /></span><span style="color: grey; font-style: italic;"> </span><<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimary"</span>>@color/colorPrimary</<span style="color: navy; font-weight: bold;">item</span>><br /> <<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorPrimaryDark"</span>>@color/colorPrimaryDark</<span style="color: navy; font-weight: bold;">item</span>><br /> <<span style="color: navy; font-weight: bold;">item </span><span style="color: blue; font-weight: bold;">name</span><span style="color: green; font-weight: bold;">="colorAccent"</span>>@color/colorAccent</<span style="color: navy; font-weight: bold;">item</span>><br /> </<span style="color: navy; font-weight: bold;">style</span>><br /><br /></<span style="color: navy; font-weight: bold;">resources</span>></pre></div><div><br /></div><div><br /></div>Sekian tutorial ini, semoga bermanfaat. Terima kasih sudah meluangkan waktu untuk berkunjung, jika ada yang ingin ditanyakan silahkan diposting di kolom komentar.</div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-65937123100250159112020-11-08T00:49:00.003+07:002021-02-22T18:46:56.404+07:00Contoh Penerapan SeekBar Android<p><b>SeekBar </b>adalah perpanjangan dari ProgressBar yang menambahkan thumb (jempol) yang dapat ditarik ke kanan atau kiri untuk menyetel tingkat kemajuan sesuatu. Dalam tutorial ini akan dicontohkan penerapan <b>SeekBar </b>untuk menyetel ukuran dari sebuah TextView.<span></span></p><a name='more'></a><p></p><p><br /></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/hPdwlstbZmM" width="296" youtube-src-id="hPdwlstbZmM"></iframe></div><div class="separator" style="clear: both; text-align: center;"><br /></div><p>Berikut contoh pemanggilan komponen <b>SeekBar </b>pada layout XML :</p><pre style="background-color: white; font-family: Consolas; font-size: 9.8pt;"><<span style="color: navy; font-weight: bold;">SeekBar<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/seekBar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:max</span><span style="color: green; font-weight: bold;">="100"</span>/></pre><p>Atribut <span style="color: #660e7a; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">android</span><span style="color: blue; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">:max</span><span style="color: green; font-family: Consolas; font-size: 9.8pt; font-weight: bold;">="100"</span> akan membatasi nilai maksimum dari progress, sehingga bila thumb sudah mencapai nilai 100, thumb tidak akan bergeser lagi ke kanan. Nilai dapat di atur sesuai dengan yang dibutuhkan.</p><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Dalam contoh ini kita akan memiliki 3 komponen TextView, 2 komponen ImageView sebagai tombol untuk mengurangi dan menambah progress disamping dari menggeser thumb, dan tentunya komponen Seekbar itu sendiri. Berikut isi lengkap XML <b>activity_main </b>:<div>
<div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:ignore</span><span style="color: green; font-weight: bold;">="UnusedAttribute"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutSeekBar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_gravity</span><span style="color: green; font-weight: bold;">="center_vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintBottom_toBottomOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintLeft_toLeftOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintRight_toRightOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="parent" </span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/teksKurang"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="0"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="20sp" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/tombolKurang"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/ic_remove"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@drawable/bg_tombol"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:foreground</span><span style="color: green; font-weight: bold;">="?selectableItemBackground" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">SeekBar<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/seekBar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_weight</span><span style="color: green; font-weight: bold;">="1"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingTop</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingBottom</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:max</span><span style="color: green; font-weight: bold;">="100"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/tombolTambah"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:src</span><span style="color: green; font-weight: bold;">="@drawable/ic_add"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@drawable/bg_tombol"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:foreground</span><span style="color: green; font-weight: bold;">="?selectableItemBackground" </span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/teksTambah"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginEnd</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="25"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="20sp" </span>/><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/hasil"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="Ukuran Teks"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="@color/hitam"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toBottomOf</span><span style="color: green; font-weight: bold;">="@id/layoutSeekBar"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintStart_toStartOf</span><span style="color: green; font-weight: bold;">="parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintEnd_toEndOf</span><span style="color: green; font-weight: bold;">="parent"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><br /></div><div><br /></div><div>Untuk icon source ImageView tombol kurang dan tambah, dapat dibuat melalui Vector Asset pada Android Studio, kemudian background drawable bulat <b>bg_tombol</b> untuk kedua tombol tersebut dapat dibuat melalui Drawable Resource File pada Android Studio, isi file nya seperti dibawah ini :</div><div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 150px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">shape </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:shape</span><span style="color: green; font-weight: bold;">="oval"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">stroke </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:width</span><span style="color: green; font-weight: bold;">="1dp"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">shape</span>></pre></div><br /></div><div><br /></div>Selanjutnya pada <b>MainActivity.java</b>, tentunya pertama-tama dengan menentukan variabel-variabel untuk komponen-komponen diatas, dan variabel untuk sebuah integer atau int. Integer adalah bilangan bulat digunakan tentunya sebagai ukuran TextView nantinya saat <b>Seekbar </b>digeser atau tombol di tekan.</div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br />Berikut isi selengkapnya dari activity <b>MainActivity.java</b> dengan sedikit penjelasan "java comment" di dalamnya :<div><br /><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.seekbar;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.annotation.<span style="color: olive;">SuppressLint</span>;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ImageView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.SeekBar;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: grey; font-style: italic;">//Variabel Global privat Komponen<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">private </span>TextView <span style="color: #660e7a; font-weight: bold;">teksTambah</span>, <span style="color: #660e7a; font-weight: bold;">hasil</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>SeekBar <span style="color: #660e7a; font-weight: bold;">seekBar</span>;<br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">kemajuan</span>;<br /><br /><br /> <span style="color: olive;">@SuppressLint</span>(<span style="color: green; font-weight: bold;">"SetTextI18n"</span>)<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: grey; font-style: italic;">//view ID<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">teksTambah </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">teksTambah</span>);<br /> <span style="color: #660e7a; font-weight: bold;">hasil </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">hasil</span>);<br /> <span style="color: #660e7a; font-weight: bold;">seekBar </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">seekBar</span>);<br /><br /> <span style="color: grey; font-style: italic;">//lokal variabel dan View ID<br /></span><span style="color: grey; font-style: italic;"> </span>ImageView tombolKurang = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolKurang</span>);<br /> ImageView tombolTambah = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolTambah</span>);<br /><br /> <span style="color: grey; font-style: italic;">//Metode untuk seekbar saat digeser<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">seekBar</span>.setOnSeekBarChangeListener(<span style="color: navy; font-weight: bold;">new </span>SeekBar.OnSeekBarChangeListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onProgressChanged(SeekBar seekBar, <span style="color: navy; font-weight: bold;">int </span>progress, <span style="color: navy; font-weight: bold;">boolean </span>fromUser) {<br /> <span style="color: #660e7a; font-weight: bold;">teksTambah</span>.setText(String.<span style="font-style: italic;">valueOf</span>(progress));<br /> <span style="color: #660e7a; font-weight: bold;">hasil</span>.setText(<span style="color: green; font-weight: bold;">"Ukuran Teks " </span>+ progress);<br /> <span style="color: #660e7a; font-weight: bold;">hasil</span>.setTextSize((<span style="color: navy; font-weight: bold;">float</span>) progress);<br /> <span style="color: #660e7a; font-weight: bold;">kemajuan </span>= progress;<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onStartTrackingTouch(SeekBar seekBar) {<br /><br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onStopTrackingTouch(SeekBar seekBar) {<br /><br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//Konversi dari nilai textview teksTambah ke int<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">kemajuan </span>= Integer.<span style="font-style: italic;">parseInt</span>(<span style="color: #660e7a; font-weight: bold;">teksTambah</span>.getText().toString());<br /><br /> <span style="color: grey; font-style: italic;">//metode klik untuk imageView tombol tambah<br /></span><span style="color: grey; font-style: italic;"> </span>tombolTambah.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: grey; font-style: italic;">//Setiap kali tombol tambah di klik, nilai bertambah 1<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">kemajuan </span>= <span style="color: #660e7a; font-weight: bold;">kemajuan </span>+ <span style="color: blue;">1</span>;<br /> <span style="color: #660e7a; font-weight: bold;">teksTambah</span>.setText(<span style="color: green; font-weight: bold;">"" </span>+ <span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">hasil</span>.setText(<span style="color: green; font-weight: bold;">"Ukuran Teks " </span>+ <span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">hasil</span>.setTextSize((<span style="color: navy; font-weight: bold;">float</span>) <span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">seekBar</span>.setProgress(<span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> }<br /> });<br /><br /> <span style="color: grey; font-style: italic;">//metode klik untuk imageView tombol kurang<br /></span><span style="color: grey; font-style: italic;"> </span>tombolKurang.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> <span style="color: grey; font-style: italic;">//Setiap kali tombol kurang di klik, nilai berkurang 1<br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">kemajuan </span>= <span style="color: #660e7a; font-weight: bold;">kemajuan </span>- <span style="color: blue;">1</span>;<br /> <span style="color: #660e7a; font-weight: bold;">teksTambah</span>.setText(<span style="color: green; font-weight: bold;">"" </span>+ <span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">hasil</span>.setText(<span style="color: green; font-weight: bold;">"Ukuran Teks " </span>+ <span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">hasil</span>.setTextSize((<span style="color: navy; font-weight: bold;">float</span>) <span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> <span style="color: #660e7a; font-weight: bold;">seekBar</span>.setProgress(<span style="color: #660e7a; font-weight: bold;">kemajuan</span>);<br /> }<br /> });<br /> }<br />}</pre></div><div><br /></div><div><br /></div>Sekian dan terima kasih atas waktu Anda mampir di artikel ini. Jika ada yang ingin ditanyakan silahkan diposting melalui komentar.</div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-63474830734603909682020-09-21T13:58:00.002+07:002020-09-21T14:14:26.185+07:00Contoh Penerapan RecyclerView Android<span style="font-family: georgia;"><b>RecyclerView </b>adalah sebuah ViewGroup, sama halnya dengan ListView dan GridView yang sudah terlebih dahulu sedikit dibahas sebelumnya di blog ini. Namun <b>RecyclerView </b>ini adalah peningkatan dari kedua ViewGroup</span> <span style="font-family: georgia;">tersebut sehingga tentu memiliki performa lebih baik daripada ListView dan GridView.</span><br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoUJQgMWbYsPG2zwzYNCMfarA6wJIXFm_S3oH8hH1QAPBmKtGwf2i0Gv98HWn4AjEbvGPJMzRBdscdD5d7cUQ_X4h6psAyknUlXVzDfkk3KV6cjen6zP3aFpBDGfuyNjYbpUDXiaJ8eSk/s1920/android_recyclerview.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="cara menggunakan recyclerview android" border="0" data-original-height="1920" data-original-width="1080" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoUJQgMWbYsPG2zwzYNCMfarA6wJIXFm_S3oH8hH1QAPBmKtGwf2i0Gv98HWn4AjEbvGPJMzRBdscdD5d7cUQ_X4h6psAyknUlXVzDfkk3KV6cjen6zP3aFpBDGfuyNjYbpUDXiaJ8eSk/w180-h320/android_recyclerview.jpg" title="android recyclerview" width="180" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Gambar 1 : RecyclerView Android<br /></td></tr></tbody></table><br /><span style="font-family: georgia;">Mari kita mulai penerapannya, hal pertama yang harus dilakukan adalah menambakan library RecyclerView ke dalam file <i>build.gradle (Module: app)</i> :</span></p><pre style="background-color: white; font-family: Consolas; font-size: 9.8pt;">implementation <span style="color: green; font-weight: bold;">'androidx.recyclerview:recyclerview:1.1.0'</span></pre><p><span style="font-family: georgia;">Cara yang lebih praktis dalam menambahkan library adalah melalui <i>Design Editor XML </i>layout, hanya perlu klik ikon download seperti contoh gambar dibawah ini yang ditandai dengan lingkaran warna merah, sekaligus memperlihatkan nama-nama seluruh folder dan file yang dipakai dalam tutorial ini :</span><br /><br /><p></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl37sYrzDMhenam27-l0ga4NstUXIod3WMJqMq19NmKuv0i8wZvWIqT7D-gy1XmxOQfWRmwy4UzPFrbU0_lHzkr7r1KZc7m1C-RxyNv4qiG6aF_I8qskAQBQx_XGW7iTNtyJwmX3jB1H0/s1366/recyclerView_android.png" style="margin-left: auto; margin-right: auto;"><img alt="Contoh penerapan recyclerview android" border="0" data-original-height="768" data-original-width="1366" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl37sYrzDMhenam27-l0ga4NstUXIod3WMJqMq19NmKuv0i8wZvWIqT7D-gy1XmxOQfWRmwy4UzPFrbU0_lHzkr7r1KZc7m1C-RxyNv4qiG6aF_I8qskAQBQx_XGW7iTNtyJwmX3jB1H0/w320-h180/recyclerView_android.png" title="recyclerview android" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Gambar 2 : Folder-folder dan file-file yang dipakai.</td></tr></tbody></table><br /><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><span style="font-family: georgia;">Disini kita akan menggunakan 2 buah layout yakni <b>activity_main.xml</b> sebagai layout aktivitasnya dan <b>item_recyclerview.xml</b> sebagai custom layout untuk RecyclerView. Lalu 3 java class yakni <b>MainActivity.java</b> sebagai konteks atau aktivitasnya, <b>DataItem.java</b> sebagai konstruktor dan setter dan getter dari item-item yang ada pada RecyclerView. Terakhir <b>RecyclerViewAdapter.java</b> sebagai adapter atau penghubung seluruh class java yang ada.</span><p></p><div><span style="font-family: georgia;">Berikut isi dari kedua layout xml yang disebutkan diatas.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_main.xml</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">LinearLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="?attr/colorAccent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginBottom</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingStart</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingEnd</span><span style="color: green; font-weight: bold;">="0dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingTop</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:paddingBottom</span><span style="color: green; font-weight: bold;">="10dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="RecyclerView"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="#FFFFFF"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="30sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">androidx.recyclerview.widget.RecyclerView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/recyclerView"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre></div><br /><div><br /></div><div><span style="font-family: georgia;"><b>item_recyclerview.xml</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">LinearLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/itemLayout"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="#F6DBDB"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="horizontal"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center_vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="5dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_margin</span><span style="color: green; font-weight: bold;">="5dp"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">ImageView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/imageItem"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="50dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="50dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:contentDescription</span><span style="color: green; font-weight: bold;">="@string/app_name"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">LinearLayout<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginStart</span><span style="color: green; font-weight: bold;">="15dp"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textItem"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="#000000"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"</span>/><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/textItemDeskripsi"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="#393434"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="18sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"</span>/><br /><br /> </<span style="color: navy; font-weight: bold;">LinearLayout</span>><br /><br /></<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre></div><br /><div></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><div><span style="font-family: georgia;">Untuk urusan koding, kurang lebih sama dengan cara penerapan pada Listview ataupun Gridview, sedikit perbedaan hanya pada adapter. Berikut isi dari ketiga file java yang disebutkan diatas.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>DataItem.java</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.recyclerview;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>DataItem {<br /><br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">gambar</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">teks</span>, <span style="color: #660e7a; font-weight: bold;">teksDeskripsi</span>;<br /><br /> <span style="color: navy; font-weight: bold;">public </span>DataItem(<span style="color: navy; font-weight: bold;">int </span>gambar, String teks, String teksDeskripsi) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">gambar </span>= gambar;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">teks </span>= teks;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">teksDeskripsi </span>= teksDeskripsi;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public int </span>getGambar() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">gambar</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public </span>String getTeks() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">teks</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public </span>String getTeksDeskripsi() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">teksDeskripsi</span>;<br /> }<br />}<br /></pre></div><br /><div><br /></div><div><span style="font-family: georgia;"><b>RecyclerViewAdapter.java</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.recyclerview;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.content.Context;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.Gravity;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.LayoutInflater;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.ViewGroup;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.ImageView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.LinearLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.annotation.<span style="color: olive;">NonNull</span>;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.recyclerview.widget.RecyclerView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.List;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>RecyclerViewAdapter <span style="color: navy; font-weight: bold;">extends </span>RecyclerView.Adapter<RecyclerViewAdapter.KategoriViewHolder> {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>List<DataItem> <span style="color: #660e7a; font-weight: bold;">dataItem</span>;<br /> Context <span style="color: #660e7a; font-weight: bold;">context</span>;<br /><br /> <span style="color: navy; font-weight: bold;">public </span>RecyclerViewAdapter(List<DataItem> dataItem, Context context) {<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">dataItem </span>= dataItem;<br /> <span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">context </span>= context;<br /> }<br /><br /> <span style="color: olive;">@NonNull<br /></span><span style="color: olive;"> @Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public </span>KategoriViewHolder onCreateViewHolder(ViewGroup parent, <span style="color: navy; font-weight: bold;">int </span>viewType) {<br /> <span style="color: navy; font-weight: bold;">return new </span>KategoriViewHolder(<br /> LayoutInflater.<span style="font-style: italic;">from</span>(parent.getContext()).inflate(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">item_recyclerview</span>, parent, <span style="color: navy; font-weight: bold;">false</span>)<br /> );<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onBindViewHolder(<span style="color: olive;">@NonNull </span><span style="color: navy; font-weight: bold;">final </span>KategoriViewHolder holder, <span style="color: navy; font-weight: bold;">final int </span>position) {<br /> DataItem dItem = <span style="color: #660e7a; font-weight: bold;">dataItem</span>.get(position);<br /> holder.<span style="color: #660e7a; font-weight: bold;">textItem</span>.setText(dItem.getTeks());<br /> holder.<span style="color: #660e7a; font-weight: bold;">textItemDeskripsi</span>.setText(dItem.getTeksDeskripsi());<br /> holder.<span style="color: #660e7a; font-weight: bold;">imageItem</span>.setImageResource(dItem.getGambar());<br /><br /> holder.<span style="color: #660e7a; font-weight: bold;">itemLayout</span>.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> Toast t = Toast.<span style="font-style: italic;">makeText</span>(<span style="color: #660e7a; font-weight: bold;">context</span>, <span style="color: green; font-weight: bold;">"Item no " </span>+ <span style="color: #660e7a;">position</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>);<br /> t.setGravity(Gravity.<span style="color: #660e7a; font-style: italic; font-weight: bold;">CENTER</span>,<span style="color: blue;">0</span>,<span style="color: blue;">0</span>);<br /> t.show();<br /> }<br /> });<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public int </span>getItemCount() {<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">dataItem</span>.size();<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public static class </span>KategoriViewHolder <span style="color: navy; font-weight: bold;">extends </span>RecyclerView.ViewHolder {<br /> TextView <span style="color: #660e7a; font-weight: bold;">textItem</span>, <span style="color: #660e7a; font-weight: bold;">textItemDeskripsi</span>;<br /> ImageView <span style="color: #660e7a; font-weight: bold;">imageItem</span>;<br /> LinearLayout <span style="color: #660e7a; font-weight: bold;">itemLayout</span>;<br /><br /> KategoriViewHolder(<span style="color: olive;">@NonNull </span>View itemView) {<br /> <span style="color: navy; font-weight: bold;">super</span>(itemView);<br /> <span style="color: #660e7a; font-weight: bold;">textItem </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textItem</span>);<br /> <span style="color: #660e7a; font-weight: bold;">textItemDeskripsi </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">textItemDeskripsi</span>);<br /> <span style="color: #660e7a; font-weight: bold;">imageItem </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">imageItem</span>);<br /> <span style="color: #660e7a; font-weight: bold;">itemLayout </span>= itemView.findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">itemLayout</span>);<br /> }<br /> }<br />}<br /></pre></div><br /><div><br /></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: Consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.recyclerview;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.recyclerview.widget.LinearLayoutManager;<br /><span style="color: navy; font-weight: bold;">import </span>androidx.recyclerview.widget.RecyclerView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><br /><span style="color: navy; font-weight: bold;">import </span>java.util.ArrayList;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> RecyclerView <span style="color: #660e7a; font-weight: bold;">recyclerView</span>;<br /> RecyclerViewAdapter <span style="color: #660e7a; font-weight: bold;">recyclerViewAdapter</span>;<br /> ArrayList<DataItem> <span style="color: #660e7a; font-weight: bold;">dataItems</span>;<br /><br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi1 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Flash Drive..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi2 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Handphone..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi3 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Kamera..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi4 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Keyboard..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi5 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Laptop..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi6 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Printer..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi7 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Radio..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi8 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Video Game..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi9 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Video Camera..."</span>;<br /> <span style="color: navy; font-weight: bold;">static </span>String <span style="color: #660e7a; font-style: italic;">deskripsi10 </span>= <span style="color: green; font-weight: bold;">"Keterangan mengenai Wifi..."</span>;<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">recyclerView </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">recyclerView</span>);<br /> <span style="color: #660e7a; font-weight: bold;">dataItems </span>= <span style="color: navy; font-weight: bold;">new </span>ArrayList<>();<br /> <span style="color: #660e7a; font-weight: bold;">recyclerViewAdapter </span>= <span style="color: navy; font-weight: bold;">new </span>RecyclerViewAdapter(<span style="color: #660e7a; font-weight: bold;">dataItems</span>, <span style="color: navy; font-weight: bold;">this</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_flashdisk</span>, <span style="color: green; font-weight: bold;">"Flash Drive"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi1</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_hp</span>, <span style="color: green; font-weight: bold;">"Handphone"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi2</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_kamera</span>, <span style="color: green; font-weight: bold;">"Kamera"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi3</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_keyboard</span>, <span style="color: green; font-weight: bold;">"Keyboard"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi4</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_laptop</span>, <span style="color: green; font-weight: bold;">"Laptop"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi5</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_printer</span>, <span style="color: green; font-weight: bold;">"Printer"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi6</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_radio</span>, <span style="color: green; font-weight: bold;">"Radio"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi7</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_game</span>, <span style="color: green; font-weight: bold;">"Video Game"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi8</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_video</span>, <span style="color: green; font-weight: bold;">"Video Camera"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi9</span>));<br /> <span style="color: #660e7a; font-weight: bold;">dataItems</span>.add(<span style="color: navy; font-weight: bold;">new </span>DataItem(R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ic_wifi</span>, <span style="color: green; font-weight: bold;">"Wifi"</span>, <span style="color: #660e7a; font-style: italic;">deskripsi10</span>));<br /><br /> <span style="color: #660e7a; font-weight: bold;">recyclerView</span>.setAdapter(<span style="color: #660e7a; font-weight: bold;">recyclerViewAdapter</span>);<br /> <span style="color: #660e7a; font-weight: bold;">recyclerView</span>.setLayoutManager(<span style="color: navy; font-weight: bold;">new </span>LinearLayoutManager(<span style="color: navy; font-weight: bold;">this</span>));<br /> }<br />}</pre></div><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-87725564281919244302020-09-05T06:06:00.003+07:002020-09-05T06:25:10.185+07:00Menggunakan Pick Color (Ambil Warna) <p><span style="font-family: georgia;">Postingan kali ini akan mendemokan cara menerapkan <i>pick color</i> atau ambil warna yang bisa diterapkan untuk mengganti warna latar belakang layout, warna teks dan sebagainya yang bisa diwarnai. Disini kita akan menggunakan library dari seorang developer asal Indonesia dengan nama samaran Yukuku.</span></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/JaOaQtz_HjM" width="320" youtube-src-id="JaOaQtz_HjM"></iframe></div><p><span style="font-family: georgia;">Hal pertama, silahkan untuk memasukkan library dibawah ini ke dalam file build.gradle (module: app), lalu jangan lupa untuk sync gradle. Jika mendapati kesulitan atau belum terbiasa dengan hal seperti ini, Anda dapat menemukan tutorial-tutorial nya di blog ini, silahkan menggunakan fitur search (pencarian).</span></p><pre style="background-color: white; font-family: consolas; font-size: 9.8pt;">implementation <span style="color: green; font-weight: bold;">'com.github.yukuku:ambilwarna:2.0.1'</span></pre>
<br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="9186637062" style="display: block; text-align: center;"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<br /><br />
<span style="font-family: georgia;">Kita akan coba menerapkannya untuk mengganti warna latar belakang layout utama. Berikut isi dari layout <b>activity_main</b>, sebuah Linearlayout dan sebuah TextView yang masing-masing sudah diberikan identitas (ID). TextView ini akan kita gunakan sebagai tombol untuk memunculkan dialog pick color nya.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>activity_main.xml</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: consolas; font-size: 9.8pt;"><pre style="font-family: consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?><br /></span><<span style="color: navy; font-weight: bold;">LinearLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/layoutUtama"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation</span><span style="color: green; font-weight: bold;">="vertical"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity</span><span style="color: green; font-weight: bold;">="center"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:padding</span><span style="color: green; font-weight: bold;">="20dp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>><br /><br /> <<span style="color: navy; font-weight: bold;">TextView<br /></span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/gantiWarna"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:background</span><span style="color: green; font-weight: bold;">="@android:drawable/list_selector_background"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="GANTI WARNA"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="#000000"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="30sp"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textStyle</span><span style="color: green; font-weight: bold;">="bold"<br /></span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_gravity</span><span style="color: green; font-weight: bold;">="center"</span>/><br /><br /></<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre></pre></div><div><br /></div><div><br /></div><span style="font-family: georgia;">Selanjutnya pada <b>MainActivity.java</b>, pertama-tama dengan mendeklarasikan terlebih dahulu variabel Linearlayout dan objek int. Kemudian mendeklarasikan id dari Linearlayout tersebut dan menerapkan klik listener untuk TextView di dalam <i>onCreate</i>.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Lalu untuk penerapan dialog pick colornya kita akan buat diluar <i>onCreate </i>supaya tidak terlalu ramai kodingan di dalam <i>onCreate</i>. Terdapat dua metode di dalam dialog pick color yaitu : <b><i>onOk </i></b>dan <b><i>onCancel</i></b>, yang mana tentunya kode untuk merubah warna akan diletakkan di dalam <b><i>onOk</i></b> sedangkan <b><i>onCancel </i></b>akan membatalkan aksi.</span></div><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b> :</span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.pickcolor;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.LinearLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>yuku.ambilwarna.AmbilWarnaDialog;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>LinearLayout <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>;<br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">warnaDipilih</span>;<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layoutUtama</span>);<br /> TextView gantiWarna = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">gantiWarna</span>);<br /><br /> gantiWarna.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> bukaDialogWarna();<br /> }<br /> });<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">private void </span>bukaDialogWarna() {<br /> AmbilWarnaDialog dialogWarna = <span style="color: navy; font-weight: bold;">new </span>AmbilWarnaDialog(<span style="color: navy; font-weight: bold;">this</span>,<br /> <span style="color: #660e7a; font-weight: bold;">warnaDipilih</span>, <span style="color: navy; font-weight: bold;">true</span>, <span style="color: navy; font-weight: bold;">new </span>AmbilWarnaDialog.OnAmbilWarnaListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onOk(AmbilWarnaDialog dialog, <span style="color: navy; font-weight: bold;">int </span>warna) {<br /> <span style="color: #660e7a; font-weight: bold;">warnaDipilih </span>= warna;<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setBackgroundColor(warna);<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onCancel(AmbilWarnaDialog dialog) {<br /> }<br /> });<br /> dialogWarna.show();<br /> }<br />}</pre></div><div><br /></div><div><br /></div><span style="font-family: georgia;">Simpel dan mudah bukan?.. Nah, untuk menyimpan hasil perubahan warna kita bisa menggunakan <b>Sharedpreferences </b>untuk memerintah system Android mengingatnya. Jika menggunakan Sharedpreferences tentunya kita harus punya nilai defaultnya. Pertama kita deklarasikan dulu variabel untuk Sharedprefences dan nilai int untuk warna default.</span><div><span style="font-family: georgia;"><br /></span></div><div><span style="font-family: georgia;">Kemudian kita buat metode penyimpanannya dan memasukkannya ke dalam metode <b><i>onOk </i></b>pada dialog pick color sebelumnya, lalu memuat penyimpanannya di dalam <i>onCreate</i>. Sehingga berikut kode selengkapnya pada <b>MainActivity.java</b> :</span></div><div><span style="font-family: georgia;"><br /></span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.pickcolor;<br /><br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;<br /><br /><span style="color: navy; font-weight: bold;">import </span>android.content.SharedPreferences;<br /><span style="color: navy; font-weight: bold;">import </span>android.graphics.Color;<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;<br /><span style="color: navy; font-weight: bold;">import </span>android.view.View;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.LinearLayout;<br /><span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;<br /><br /><span style="color: navy; font-weight: bold;">import </span>yuku.ambilwarna.AmbilWarnaDialog;<br /><br /><span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {<br /><br /> <span style="color: navy; font-weight: bold;">private </span>LinearLayout <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>;<br /> <span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">warnaDipilih</span>;<br /> <span style="color: navy; font-weight: bold;">private </span>SharedPreferences <span style="color: #660e7a; font-weight: bold;">pref</span>;<br /> <span style="color: navy; font-weight: bold;">static int </span><span style="color: #660e7a; font-style: italic;">warnaDefault </span>= Color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">RED</span>;<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);<br /><br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">layoutUtama</span>);<br /> <span style="color: #660e7a; font-weight: bold;">pref </span>= getSharedPreferences(<span style="color: green; font-weight: bold;">"SIMPAN"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> <span style="color: #660e7a; font-weight: bold;">warnaDipilih </span>= <span style="color: #660e7a; font-weight: bold;">pref</span>.getInt(<span style="color: green; font-weight: bold;">"simpan"</span>, <span style="color: #660e7a; font-style: italic;">warnaDefault</span>);<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setBackgroundColor(<span style="color: #660e7a; font-weight: bold;">warnaDipilih</span>);<br /><br /> TextView gantiWarna = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">gantiWarna</span>);<br /><br /> gantiWarna.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {<br /> bukaDialogWarna();<br /> }<br /> });<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">private void </span>bukaDialogWarna() {<br /> AmbilWarnaDialog dialogWarna = <span style="color: navy; font-weight: bold;">new </span>AmbilWarnaDialog(<span style="color: navy; font-weight: bold;">this</span>,<br /> <span style="color: #660e7a; font-weight: bold;">warnaDipilih</span>, <span style="color: navy; font-weight: bold;">true</span>, <span style="color: navy; font-weight: bold;">new </span>AmbilWarnaDialog.OnAmbilWarnaListener() {<br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onOk(AmbilWarnaDialog dialog, <span style="color: navy; font-weight: bold;">int </span>warna) {<br /> <span style="color: #660e7a; font-weight: bold;">warnaDipilih </span>= warna;<br /> <span style="color: #660e7a; font-weight: bold;">layoutUtama</span>.setBackgroundColor(warna);<br /> simpanHasil();<br /> }<br /><br /> <span style="color: olive;">@Override<br /></span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onCancel(AmbilWarnaDialog dialog) {<br /> }<br /> });<br /> dialogWarna.show();<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">private void </span>simpanHasil () {<br /> <span style="color: #660e7a; font-weight: bold;">pref </span>= getSharedPreferences(<span style="color: green; font-weight: bold;">"SIMPAN"</span>, <span style="color: #660e7a; font-style: italic; font-weight: bold;">MODE_PRIVATE</span>);<br /> SharedPreferences.Editor edit = <span style="color: #660e7a; font-weight: bold;">pref</span>.edit();<br /> edit.putInt(<span style="color: green; font-weight: bold;">"simpan"</span>, <span style="color: #660e7a; font-weight: bold;">warnaDipilih</span>);<br /> edit.apply();<br /> }<br />}</pre></div><div><br /></div><div><br /></div><span style="font-family: georgia;">Baik, sekian saja mudah-mudahan dapat dipahami, jika ada yang ingin disampaikan bisa diposting di kolom komentar.</span>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0tag:blogger.com,1999:blog-7303454535718500704.post-20305838673431417032020-09-03T22:35:00.004+07:002021-01-23T04:45:07.361+07:00Cara Memasang Iklan Video Reward AdMob<p><span style="font-family: georgia;"><b>Update </b>: Untuk cara terbaru silahkan bisa dilihat pada postingan <a href="https://www.gwnbs.com/2021/01/cara-pasang-iklan-banner-interstitial-rewarded-terbaru.html">Cara (terbaru) Pasang Iklan Banner, Interstitial, dan Rewarded AdMob</a>.<br /><br /><br />Kali ini blog gwnbs akan membahas <i>cara instalasi atau memunculkan iklan video reward AdMob</i> di aplikasi Android. Cukup lama tidak update blog dikarenakan tidak ada kesibukan sama sekali. Seperti biasanya kita tidak akan bertele-tele. Hal pertama yang harus dilakukan adalah sama seperti cara pemasangan jenis iklan lainnya yaitu memasukkan library Google Ads di bawah ini ke file build.gradle (Module: app) : </span></p><p><span style="color: #800180; font-family: georgia;">implementation 'com.google.android.gms:play-services-ads-lite:19.3.0' </span></p><p><span style="font-family: georgia;">Setelah itu lakukan sync gradle. Iklan video reward adalah sebuah jenis iklan berbentuk video, biasanya berdurasi tidak lebih dari 30 detik. Setelah pengguna selesai menonton video, maka akan mendapatkan sebuah reward yang nilainya ditentukan oleh developer atau penyedia aplikasi. Iklan ini sangat populer, sering diaplikasikan pada permainan.</span></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/ApwK6_WZFSc" width="320" youtube-src-id="ApwK6_WZFSc"></iframe></div><p><span style="font-family: georgia;">Contoh simpel berikut pada <b>activity_main</b> akan ada sebuah komponen Button dan sebuah komponen TextView. Disini kita akan membuat tombol tersebut memiliki 2 fungsi, pertama untuk menyetel teks pada TextView yaitu angka 1 sampai 5, fungsi kedua adalah memunculkan dialog setelah klik ke-5. Dialog akan memberi informasi bawha jumlah klik tombol sudah habis yaitu 5 kali klik dan jika ingin mengembalikan nilai klik dari awal lagi maka harus menonton iklan reward terlebih dahulu.</span></p><br />
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" data-ad-client="ca-pub-6475664550624774" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="5653805910" style="display: block; text-align: center;"></ins> <script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<br />
<span style="font-family: georgia;"><b>activity_main.xml </b>:</span> <div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: consolas; font-size: 9.8pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version</span><span style="color: green; font-weight: bold;">="1.0" </span><span style="color: blue; font-weight: bold;">encoding</span><span style="color: green; font-weight: bold;">="utf-8"</span><span style="font-style: italic;">?>
</span><<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout
</span><span style="color: navy; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res/android"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/apk/res-auto"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: green; font-weight: bold;">="http://schemas.android.com/tools"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="match_parent"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="match_parent"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">tools</span><span style="color: blue; font-weight: bold;">:context</span><span style="color: green; font-weight: bold;">=".MainActivity"</span>>
<<span style="color: navy; font-weight: bold;">Button
</span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/tombolPlay"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="PLAY"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="200dp"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintLeft_toLeftOf</span><span style="color: green; font-weight: bold;">="parent"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintRight_toRightOf</span><span style="color: green; font-weight: bold;">="parent"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toTopOf</span><span style="color: green; font-weight: bold;">="parent" </span>/>
<<span style="color: navy; font-weight: bold;">TextView
</span><span style="color: navy; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id</span><span style="color: green; font-weight: bold;">="@+id/teksAngka"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width</span><span style="color: green; font-weight: bold;">="wrap_content"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height</span><span style="color: green; font-weight: bold;">="wrap_content"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_marginTop</span><span style="color: green; font-weight: bold;">="30dp"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:text</span><span style="color: green; font-weight: bold;">="0"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textSize</span><span style="color: green; font-weight: bold;">="25sp"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:fontFamily</span><span style="color: green; font-weight: bold;">="serif"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:textColor</span><span style="color: green; font-weight: bold;">="#000000"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintLeft_toLeftOf</span><span style="color: green; font-weight: bold;">="parent"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintRight_toRightOf</span><span style="color: green; font-weight: bold;">="parent"
</span><span style="color: green; font-weight: bold;"> </span><span style="color: #660e7a; font-weight: bold;">app</span><span style="color: blue; font-weight: bold;">:layout_constraintTop_toBottomOf</span><span style="color: green; font-weight: bold;">="@id/tombolPlay" </span>/>
</<span style="color: navy; font-weight: bold;">androidx.constraintlayout.widget.ConstraintLayout</span>></pre></div><div><br />
</div><br />
<div><span style="font-family: georgia;">Kemudian kita akan mulai melakukan koding java pada konteks <b>MainActivity.java</b>. Hal pertama yang dilakukan adalah membuat global variable untuk objek <b style="font-style: italic;">RewardedVideoAd, int, boolean, </b>dan <b style="font-style: italic;">TextView</b>. Untuk objek <b><i>Button </i></b>sendiri dapat dilakukan langsung di dalam <i>onCreate</i>.</span></div><div><span style="font-family: georgia;"><br />
</span></div><div><span style="font-family: georgia;"><b>Boolean </b>disini kita gunakan untuk memberikan 2 fungsi klik kepada komponen Button. Nilai boolean hanya ada 2 yaitu <i>true </i>dan <i>false</i>, nilai default akan kita setel ke "<i>true</i>". Lalu untuk <b>int </b>sendiri kita gunakan untuk menghitung jumlah klik. <b>int </b>adalah bilangan, dan disini kita hanya akan menggunakan nya sampai bilangan ke-5 saja.</span></div><div><span style="font-family: georgia;"><br />
</span></div><div><span style="font-family: georgia;">Nilai int 1 sampai dengan 5 harus berada di dalam metode "<i>boolean true</i>", lalu setelah nilai <b>int </b>ke-5 atau klik kelima, didalamnya <b>boolean </b>kita setel menjadi "<i>false</i>" supaya dapat menjalankan fungsi kedua dari tombol yaitu memunculkan dialog seperti yang sudah dijelaskan di atas.</span></div><div><span style="font-family: georgia;"><br />
</span></div><div><span style="font-family: georgia;">Di dalam <i>PositveButton </i>pada Alert dialog lah kode untuk memunculkan iklan kita sematkan. Sedangkan <i>NegativeButton </i>tentunya akan membatalkan dialog dan tidak akan merubah apa-apa terhadap nilai klik. Kemudian kita perlu menerapkan listener kepada objek RewardedAdVideo. </span></div><div><span style="font-family: georgia;"><br />
</span></div><div><span style="font-family: georgia;">Terdapat 8 metode implementasi untuk listener objek ini yaitu <i>onRewardVideoAdLoaded, onRewardVideoAdOpened, onRewardVideoStarted, onRewardVideoAdClosed, onReward (RewardItem rewardItem), onRewardVideoAdLeftApplication, onRewardVideoAdFailedToLoad, </i>dan terakhir<i> onRewardVideoCompleted.</i></span></div><br />
<div><span style="font-family: georgia;">Dari semua metode implementasi listener diatas, yang harus diperhatikan adalah <b><i>onReward (RewardItem rewardItem)</i></b>, karena didalam metode inilah kita dapat memberikan hadiah / reward kepada pengguna, pada contoh ini rewardnya ialah memberi lagi 5 klik baru dengan cara mereset nilai dari int kembali menjadi 0, kemudian menyetel boolean kembali ke "true".</span></div><div><span style="font-family: georgia;"><br />
</span></div><div><span style="font-family: georgia;"><b>MainActivity.java</b></span></div><div style="background-color: white; border-radius: 10px; border: 1px dashed rgb(0, 102, 0); height: 250px; overflow: auto; padding: 10px; text-align: left; width: auto;"><pre style="font-family: consolas; font-size: 9.8pt;"><span style="color: navy; font-weight: bold;">package </span>com.gwnbs.iklanreward;
<span style="color: navy; font-weight: bold;">import </span>android.content.DialogInterface;
<span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;
<span style="color: navy; font-weight: bold;">import </span>android.view.View;
<span style="color: navy; font-weight: bold;">import </span>android.widget.Button;
<span style="color: navy; font-weight: bold;">import </span>android.widget.TextView;
<span style="color: navy; font-weight: bold;">import </span>android.widget.Toast;
<span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AlertDialog;
<span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity;
<span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.AdRequest;
<span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.MobileAds;
<span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.reward.RewardedVideoAd;
<span style="color: navy; font-weight: bold;">import </span>com.google.android.gms.ads.reward.RewardedVideoAdListener;
<span style="color: navy; font-weight: bold;">public class </span>MainActivity <span style="color: navy; font-weight: bold;">extends </span>AppCompatActivity {
<span style="color: navy; font-weight: bold;">private </span>RewardedVideoAd <span style="color: #660e7a; font-weight: bold;">reward</span>;
<span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">klik</span>;
<span style="color: navy; font-weight: bold;">private boolean </span><span style="color: #660e7a; font-weight: bold;">boleh </span>= <span style="color: navy; font-weight: bold;">true</span>;
<span style="color: navy; font-weight: bold;">private </span>TextView <span style="color: #660e7a; font-weight: bold;">teksAngka</span>;
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {
<span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);
setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">activity_main</span>);
Button tombolPlay = findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">tombolPlay</span>);
<span style="color: #660e7a; font-weight: bold;">teksAngka </span>= findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">teksAngka</span>);
tombolPlay.setOnClickListener(<span style="color: navy; font-weight: bold;">new </span>View.OnClickListener() {
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(View v) {
<span style="color: #660e7a; font-weight: bold;">klik </span>= <span style="color: #660e7a; font-weight: bold;">klik </span>+ <span style="color: blue;">1</span>;
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">boleh</span>) {
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">klik </span>== <span style="color: blue;">1</span>) {
<span style="color: #660e7a; font-weight: bold;">teksAngka</span>.setText(String.<span style="font-style: italic;">valueOf</span>(<span style="color: #660e7a; font-weight: bold;">klik</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">klik </span>== <span style="color: blue;">2</span>) {
<span style="color: #660e7a; font-weight: bold;">teksAngka</span>.setText(String.<span style="font-style: italic;">valueOf</span>(<span style="color: #660e7a; font-weight: bold;">klik</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">klik </span>== <span style="color: blue;">3</span>) {
<span style="color: #660e7a; font-weight: bold;">teksAngka</span>.setText(String.<span style="font-style: italic;">valueOf</span>(<span style="color: #660e7a; font-weight: bold;">klik</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">klik </span>== <span style="color: blue;">4</span>) {
<span style="color: #660e7a; font-weight: bold;">teksAngka</span>.setText(String.<span style="font-style: italic;">valueOf</span>(<span style="color: #660e7a; font-weight: bold;">klik</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">klik </span>== <span style="color: blue;">5</span>) {
<span style="color: #660e7a; font-weight: bold;">teksAngka</span>.setText(String.<span style="font-style: italic;">valueOf</span>(<span style="color: #660e7a; font-weight: bold;">klik</span>));
<span style="color: #660e7a; font-weight: bold;">boleh </span>= <span style="color: navy; font-weight: bold;">false</span>;
}
} <span style="color: navy; font-weight: bold;">else </span>{
dialogKlikHabis();
}
}
});
<span style="color: #660e7a; font-weight: bold;">reward </span>= MobileAds.<span style="font-style: italic;">getRewardedVideoAdInstance</span>(<span style="color: navy; font-weight: bold;">this</span>);
<span style="color: #660e7a; font-weight: bold;">reward</span>.setRewardedVideoAdListener(<span style="color: navy; font-weight: bold;">new </span>RewardedVideoAdListener() {
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewardedVideoAdLoaded() {
Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Iklan reward tersedia"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewardedVideoAdOpened() {
Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Iklan dibuka"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewardedVideoStarted() {
Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Iklan video dimulai"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewardedVideoAdClosed() {
Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Iklan ditutup"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewarded(com.google.android.gms.ads.reward.RewardItem rewardItem) {
<span style="color: #660e7a; font-weight: bold;">boleh </span>= <span style="color: navy; font-weight: bold;">true</span>;
<span style="color: #660e7a; font-weight: bold;">klik </span>= <span style="color: blue;">0</span>;
<span style="color: #660e7a; font-weight: bold;">teksAngka</span>.setText(<span style="color: green; font-weight: bold;">"0"</span>);
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewardedVideoAdLeftApplication() {
System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"Menuju sumber iklan"</span>);
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewardedVideoAdFailedToLoad(<span style="color: navy; font-weight: bold;">int </span>i) {
Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Iklan gagal dimuat"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onRewardedVideoCompleted() {
<span style="color: #660e7a; font-weight: bold;">reward</span>.loadAd(<span style="color: green; font-weight: bold;">"ca-app-pub-3940256099942544/5224354917"</span>, <span style="color: navy; font-weight: bold;">new </span>AdRequest.Builder().build());
}
});
<span style="color: #660e7a; font-weight: bold;">reward</span>.loadAd(<span style="color: green; font-weight: bold;">"ca-app-pub-3940256099942544/5224354917"</span>, <span style="color: navy; font-weight: bold;">new </span>AdRequest.Builder().build());
}
<span style="color: navy; font-weight: bold;">private void </span>dialogKlikHabis () {
AlertDialog.Builder alert = <span style="color: navy; font-weight: bold;">new </span>AlertDialog.Builder(MainActivity.<span style="color: navy; font-weight: bold;">this</span>);
alert.setTitle(<span style="color: green; font-weight: bold;">"Klik Habis"</span>);
alert.setMessage(<span style="color: green; font-weight: bold;">"Klik nya habis. Apakah kamu mau menonton video untuk mendapatkan 5 klik lagi?"</span>);
alert.setPositiveButton(<span style="color: green; font-weight: bold;">"Tonton"</span>, <span style="color: navy; font-weight: bold;">new </span>DialogInterface.OnClickListener() {
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(DialogInterface dialog, <span style="color: navy; font-weight: bold;">int </span>which) {
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">reward</span>.isLoaded()) {
<span style="color: #660e7a; font-weight: bold;">reward</span>.show();
} <span style="color: navy; font-weight: bold;">else </span>{
Toast.<span style="font-style: italic;">makeText</span>(getApplicationContext(), <span style="color: green; font-weight: bold;">"Opps! Iklan tidak ada"</span>, Toast.<span style="color: #660e7a; font-style: italic; font-weight: bold;">LENGTH_SHORT</span>).show();
}
}
});
alert.setNegativeButton(<span style="color: green; font-weight: bold;">"Tidak"</span>, <span style="color: navy; font-weight: bold;">new </span>DialogInterface.OnClickListener() {
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(DialogInterface dialog, <span style="color: navy; font-weight: bold;">int </span>which) {
dialog.dismiss();
}
});
alert.show();
}
<span style="color: olive;">@Override
</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onDestroy () {
<span style="color: #660e7a; font-weight: bold;">reward</span>.destroy(<span style="color: navy; font-weight: bold;">this</span>);
<span style="color: navy; font-weight: bold;">super</span>.onDestroy();
}
}</pre></div><div><br />
</div><br />
<div><span style="font-family: georgia;">Saya rasa cukup sekian, mudah-mudahan penjelasan diatas cukup jelas. Silahkan tinggalkan komentar jika ada yang kurang jelas atau ada hal lain yang ingin disampaikan. Terima kasih.</span></div>gwnbshttp://www.blogger.com/profile/12534611619519529670noreply@blogger.com0Lampung, Indonesia-4.5585849 105.4068079-67.914016009537391 -35.218192099999996 58.796846209537392 -113.96819210000001