Introduction
In this article, we can learn how to click the items of Recyclerview to navigate to different activities in Quiz App. Here, I have given the items in grid view to click each item. So, I will provide a series of articles on this Quiz App, in upcoming articles.
If you are new to this application, follow my previous articles.
https://forums.developer.huawei.com/forumPortal/en/topic/0202877278014350004
https://forums.developer.huawei.com/forumPortal/en/topic/0201884103719030016?fid=0101187876626530001
https://forums.developer.huawei.com/forumPortal/en/topic/0202890333711770040
Requirements
- Any operating system (MacOS, Linux, and Windows).
- Must have a Huawei phone with HMS 4.0.0.300 or later.
- Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 and above installed.
- Minimum API Level 24 is required.
- Required EMUI 9.0.0 and later version devices.
How to integrate HMS Dependencies
First register as Huawei developer and complete identity verification on the Huawei developers website, refer to register a Huawei ID.
Create a project in android studio, refer Creating an Android Studio Project.
Generate a SHA-256 certificate fingerprint.
To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.
Note: Project Name depends on the user created name.
Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.
- Enter SHA-256 certificate fingerprint and click Save button, as follows.
- Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
- Add the below plugin and dependencies in build.gradle(Module) file.
apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Recyclerview
implementation 'androidx.recyclerview:recyclerview:1.2.1'
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
- Now Sync the gradle.
Let us move to development
I have created a project on Android studio with empty activity let us start coding.
In the Home.kt we can find the business logic for button click listeners.
class Home : AppCompatActivity(), HomeAdapter.ItemListener {
private lateinit var recyclerView: RecyclerView
private lateinit var arrayList: ArrayList<QuesIcons>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
recyclerView = findViewById(R.id.recyclerview_list)
arrayList = ArrayList()
arrayList.add(QuesIcons("Android", R.drawable.android_icon, "#09A9FF"))
arrayList.add(QuesIcons("HMS", R.drawable.huawei_icon, "#3E51B1"))
arrayList.add(QuesIcons("Sports", R.drawable.sports_icon, "#673BB7"))
arrayList.add(QuesIcons("Country Flags", R.drawable.flags_icon, "#4BAA50"))
val adapter = HomeAdapter(applicationContext, arrayList, this)
recyclerView.adapter = adapter
recyclerView.layoutManager = GridLayoutManager(this, 2)
recyclerView.setHasFixedSize(true)
}
override fun onItemClick(item: Int) {
when(item ) {
0 -> {val intent = Intent(this@Home, AndroidActivity::class.java)
startActivity(intent)
}
1 -> {val intent = Intent(this@Home, HMSActivity::class.java)
startActivity(intent)
}
2 -> {val intent = Intent(this@Home, SportsActivity::class.java)
startActivity(intent)
}
3 -> {val intent = Intent(this@Home, QuizActivity::class.java)
startActivity(intent)
}
}
}
}
In the HomeAdapter.kt we can find the business logic to holder the adapter items.
class HomeAdapter(private val mContext: Context, private val mValues: ArrayList<QuesIcons>, private var mListener: ItemListener?) :
RecyclerView.Adapter<HomeAdapter.ViewHolder>() {
inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
private val textView: TextView
private val imageView: ImageView
private val relativeLayout: RelativeLayout
private var item: QuesIcons? = null
fun setData(item: QuesIcons) {
this.item = item
textView.text = item.heading
imageView.setImageResource(item.titleImage)
relativeLayout.setBackgroundColor(Color.parseColor(item.colour))
}
override fun onClick(view: View) {
if (mListener != null) {
item?.let { mListener!!.onItemClick(adapterPosition) }
}
}
init {
v.setOnClickListener(this)
textView = v.findViewById<View>(R.id.text_item) as TextView
imageView = v.findViewById<View>(R.id.img_icon) as ImageView
relativeLayout = v.findViewById<View>(R.id.relativeLayout) as RelativeLayout
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view: View = LayoutInflater.from(mContext).inflate(R.layout.home_list, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.setData(mValues[position])
}
override fun getItemCount(): Int {
return mValues.size
}
interface ItemListener {
fun onItemClick(position: Int)
}
}
Create QuesIcons.kt data class to find the declared the data.
data class QuesIcons(var heading: String, var titleImage: Int, var colour: String)
In the activity_home.xml we can create the recycler view list.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingLeft="8dp"
android:paddingRight="8dp"
tools:context=".Home">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
In the home_list.xml we can create customize view for items.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_margin="4dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_gravity="center">
<ImageView
android:id="@+id/img_icon"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerInParent="true"
android:contentDescription="@null"
card_view:tint="@color/white" />
<TextView
android:id="@+id/text_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="@android:color/white"
android:textSize="16sp"
android:layout_below="@+id/img_icon" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Demo
Tips and Tricks
- Make sure you are already registered as Huawei developer.
- Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
- Make sure you have added the agconnect-services.json file to app folder.
- Make sure you have added SHA-256 fingerprint without fail.
- Make sure all the dependencies are added properly.
Conclusion
In this article, we have learned how to click the items of Recyclerview to navigate to different activities in Quiz App. Here, we can find the items in grid view to click each item. So, I will provide a series of articles on this Quiz App, in upcoming articles. So, I will provide a series of articles on this Quiz App, in upcoming articles.
I hope you have read this article. If you found it helpful, please provide likes and comments.
Reference
Clik here - https://www.geeksforgeeks.org/android-recyclerview/
Top comments (0)