Icon challenge solution

At first i Enabled view Binding for simplicity. For this go to build.gradle(:app) and add inside of android section what showed in below.

buildFeatures {
viewBinding = true
}

Then I created new layout resource for storing image and name of apps alongside (as shown below). You can use your custom layout.

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:padding=“16dp”
android:layout_margin=“4dp”>

<ImageView
    android:id="@+id/image_view"
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    />

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="44dp"
    android:layout_marginTop="16dp"
    android:textColor="@color/black"
    android:textSize="18sp"
    app:layout_constraintStart_toEndOf="@id/image_view"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

And then I used packageManager’s loadIcon() method for getting apps icons. You can paste this code instead of yours. It’s all in MainActivity.kt file.

private const val TAG = “NerdLauncherActivity”

class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.app_recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)

    setupAdapter()
}

private fun setupAdapter() {
    val startupIntent = Intent(Intent.ACTION_MAIN).apply {
        addCategory(Intent.CATEGORY_LAUNCHER)
    }
    val activities = packageManager.queryIntentActivities(startupIntent, 0)
    activities.sortWith(Comparator { a, b ->
        String.CASE_INSENSITIVE_ORDER.compare(
            a.loadLabel(packageManager).toString(),
            b.loadLabel(packageManager).toString()
        )
    })
    Log.i(TAG, "Found ${activities.size} activities")
    recyclerView.adapter = ActivityAdapter(activities)
}

private class ActivityHolder(val binding: SingleItemBinding) :
    RecyclerView.ViewHolder(binding.root),
    View.OnClickListener {
    private lateinit var resolveInfo: ResolveInfo

    init {
        itemView.setOnClickListener(this)
    }

    fun bindActivity(resolveInfo: ResolveInfo) {
        this.resolveInfo = resolveInfo
        val packageManager = itemView.context.packageManager
        val appName = resolveInfo.loadLabel(packageManager).toString()
        val appIcon = resolveInfo.loadIcon(packageManager)
      binding.textView.text = appName
        binding.imageView.setImageDrawable(appIcon)
    }

    override fun onClick(v: View) {
        val activityInfo = resolveInfo.activityInfo
        val intent = Intent(Intent.ACTION_MAIN).apply {
            setClassName(activityInfo.applicationInfo.packageName, activityInfo.name)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }

        val context = v.context
        context.startActivity(intent)
    }
}

private class ActivityAdapter(val activities: List<ResolveInfo>) :
    RecyclerView.Adapter<ActivityHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ActivityHolder {
        val binding =
            SingleItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ActivityHolder(binding)
    }

    override fun onBindViewHolder(holder: ActivityHolder, position: Int) {
        val resolveInfo = activities[position]
        holder.bindActivity(resolveInfo)
    }

    override fun getItemCount(): Int {
        return activities.size
    }

}

}

I hope that my solution can be helpful for somebody.