Skip to content

Instantly share code, notes, and snippets.

@ikhlaqmalik13
Last active February 2, 2023 06:10
Show Gist options
  • Save ikhlaqmalik13/e03142aa61ef1701e78ffbfc8a071b5f to your computer and use it in GitHub Desktop.
Save ikhlaqmalik13/e03142aa61ef1701e78ffbfc8a071b5f to your computer and use it in GitHub Desktop.
API Integration With Kotlin for Getting Quranic Surah's
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".learning.ChapterInfoActivity">
<LinearLayout
android:paddingHorizontal="@dimen/_12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_chapter_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="id" />
<TextView
android:id="@+id/tv_short_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="short text" />
<TextView
android:id="@+id/tv_source"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="source" />
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".learning.RetrofitLearningActivity">
<LinearLayout
android:id="@+id/ll_chapters"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</androidx.core.widget.NestedScrollView>
package com.maple.kashin.learning
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.util.Log
import android.widget.Toast
import com.maple.kashin.databinding.ActivityChapterInfoBinding
import com.maple.kashin.learning.models.ChapterInfo
import com.maple.kashin.learning.models.QuranicChapterInfoResponseModel
import com.maple.kashin.learning.utils.QuranicChaptersInterface
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class ChapterInfoActivity : AppCompatActivity() {
private lateinit var binding: ActivityChapterInfoBinding
private var chapter : Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityChapterInfoBinding.inflate(layoutInflater)
setContentView(binding.root)
if(intent.hasExtra("ID")){
chapter = intent.getIntExtra("ID", 1)
}
val chaptersInfoApi = LearningRetrofitClient().getClient()
.create(QuranicChaptersInterface::class.java)
chaptersInfoApi.getChapterInfo(chapter)
.enqueue(object : Callback<QuranicChapterInfoResponseModel> {
override fun onResponse(
call: Call<QuranicChapterInfoResponseModel>,
response: Response<QuranicChapterInfoResponseModel>
) {
if (response.isSuccessful) {
setDataToViews(response.body()?.chapterInfo)
} else {
Toast.makeText(
this@ChapterInfoActivity,
"Something went wrong!!!",
Toast.LENGTH_SHORT
).show()
}
}
override fun onFailure(call: Call<QuranicChapterInfoResponseModel>, t: Throwable) {
Log.d("Failure", t.message.toString())
}
})
}
private fun setDataToViews(chapterInfo: ChapterInfo?) {
binding.apply {
tvChapterId.text = chapterInfo?.chapterId.toString()
tvShortText.text = chapterInfo?.shortText
tvSource.text = chapterInfo?.source
tvText.text = Html.fromHtml(chapterInfo?.text);
}
}
}
package com.maple.kashin.learning
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class LearningRetrofitClient {
private lateinit var retrofit: Retrofit
fun getClient(): Retrofit {
retrofit = Retrofit
.Builder()
.baseUrl("https://api.quran.com/api/v4/")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit
}
}
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
package com.maple.kashin.learning.models
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize
@Parcelize
data class QuranicChapterInfoResponseModel(
@field:SerializedName("chapter_info")
val chapterInfo: ChapterInfo? = null
) : Parcelable
@Parcelize
data class ChapterInfo(
@field:SerializedName("short_text")
val shortText: String? = null,
@field:SerializedName("id")
val id: Int? = null,
@field:SerializedName("chapter_id")
val chapterId: Int? = null,
@field:SerializedName("language_name")
val languageName: String? = null,
@field:SerializedName("source")
val source: String? = null,
@field:SerializedName("text")
val text: String? = null
) : Parcelable
package com.maple.kashin.learning.utils
import com.maple.kashin.learning.models.QuranicChapterInfoResponseModel
import com.maple.kashin.learning.models.QuranicChaptersResponseModel
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Path
interface QuranicChaptersInterface {
@GET("chapters?language=en")
fun getChapters(): Call<QuranicChaptersResponseModel>
@GET("chapters/{chapterNo}/info?language=en")
fun getChapterInfo(@Path("chapterNo") chapterNo: Int): Call<QuranicChapterInfoResponseModel>
}
package com.maple.kashin.learning.models
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize
@Parcelize
data class QuranicChaptersResponseModel(
@field:SerializedName("chapters")
val chapters: List<ChaptersItem?>? = null
) : Parcelable
@Parcelize
data class ChaptersItem(
@field:SerializedName("pages")
val pages: List<Int?>? = null,
@field:SerializedName("name_complex")
val nameComplex: String? = null,
@field:SerializedName("translated_name")
val translatedName: TranslatedName? = null,
@field:SerializedName("bismillah_pre")
val bismillahPre: Boolean? = null,
@field:SerializedName("revelation_order")
val revelationOrder: Int? = null,
@field:SerializedName("verses_count")
val versesCount: Int? = null,
@field:SerializedName("id")
val id: Int? = null,
@field:SerializedName("name_simple")
val nameSimple: String? = null,
@field:SerializedName("name_arabic")
val nameArabic: String? = null,
@field:SerializedName("revelation_place")
val revelationPlace: String? = null
) : Parcelable
@Parcelize
data class TranslatedName(
@field:SerializedName("name")
val name: String? = null,
@field:SerializedName("language_name")
val languageName: String? = null
) : Parcelable
package com.maple.kashin.learning
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.maple.kashin.R
import com.maple.kashin.databinding.ActivityRetrofitLearningBinding
import com.maple.kashin.databinding.RowSingleSurahItemBinding
import com.maple.kashin.learning.models.ChaptersItem
import com.maple.kashin.learning.models.QuranicChaptersResponseModel
import com.maple.kashin.learning.utils.QuranicChaptersInterface
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class RetrofitLearningActivity : AppCompatActivity() {
private lateinit var binding: ActivityRetrofitLearningBinding
private var chapters: ArrayList<ChaptersItem> = arrayListOf<ChaptersItem>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRetrofitLearningBinding.inflate(layoutInflater)
setContentView(binding.root)
val chaptersApi = LearningRetrofitClient().getClient()
.create(QuranicChaptersInterface::class.java)
chaptersApi.getChapters().enqueue(object : Callback<QuranicChaptersResponseModel> {
override fun onResponse(
call: Call<QuranicChaptersResponseModel>,
response: Response<QuranicChaptersResponseModel>
) {
if (response.isSuccessful) {
chapters.addAll(response.body()?.chapters as ArrayList<ChaptersItem>)
setDataToViews(chapters)
} else {
Toast.makeText(
this@RetrofitLearningActivity,
"Something went wrong!!!",
Toast.LENGTH_SHORT
).show()
}
}
override fun onFailure(call: Call<QuranicChaptersResponseModel>, t: Throwable) {
Log.d("Failure", t.message.toString())
}
})
}
private fun setDataToViews(chapters: ArrayList<ChaptersItem>) {
for (chapter in chapters) {
val chapterBinding: RowSingleSurahItemBinding =
RowSingleSurahItemBinding.inflate(layoutInflater)
chapterBinding.apply {
tvSno.text = "${chapter.id}"
tvSurahNameEng.text = chapter.nameSimple
tvSurahNameMeaningEng.text = chapter.translatedName?.name
tvArabicName.text = chapter.nameArabic
if (chapter.revelationPlace.equals("madinah")) {
ivMakiOrMadniSurah.setImageResource(R.drawable.madni)
}
root.setOnClickListener {
val intent = Intent(this@RetrofitLearningActivity, ChapterInfoActivity::class.java)
intent.putExtra("ID", chapter.id)
startActivity(intent)
}
}
binding.llChapters.addView(chapterBinding.root)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#f7fdfd"
android:layout_height="wrap_content"
android:paddingHorizontal="@dimen/_12dp"
android:paddingVertical="@dimen/_12dp">
<TextView
android:id="@+id/tv_sno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="0" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/_12dp"
android:layout_toEndOf="@+id/tv_sno"
android:layout_toStartOf="@+id/tv_arabic_name"
android:orientation="vertical">
<TextView
android:textColor="@color/black"
android:id="@+id/tv_surah_name_eng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Al-Surah"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="#7e9bac"
android:id="@+id/tv_surah_name_meaning_eng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surah meaning "
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_maki_or_madni_surah"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/makki" />
</LinearLayout>
</LinearLayout>
<TextView
android:textColor="#483d57"
android:id="@+id/tv_arabic_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="Surah Arabic Name"
android:textSize="16sp" />
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment