Created
November 7, 2023 18:07
-
-
Save amoozeshbebin/1b9a7a00c5973af419c3e94d05047079 to your computer and use it in GitHub Desktop.
Grid View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MainActivity.kt: | |
| package com.iliyadev.gridview | |
| import androidx.appcompat.app.AppCompatActivity | |
| import android.os.Bundle | |
| import android.widget.AdapterView | |
| import android.widget.GridView | |
| import android.widget.Toast | |
| import com.iliyadev.gridview.adapters.PlayerAdapter | |
| class MainActivity : AppCompatActivity() { | |
| lateinit var gridView: GridView | |
| private var playerNames = arrayOf( | |
| "Cristiano Ronaldo", | |
| "Joao Felix", | |
| "Bernado Silva", | |
| "Andre Silve", | |
| "Bruno Fernandez", | |
| "William Carvalho", | |
| "Nelson Semedo", | |
| "Pepe", | |
| "Rui Patricio" | |
| ) | |
| private var playerImages = intArrayOf( | |
| R.drawable.ronaldo, | |
| R.drawable.felix, | |
| R.drawable.bernado, | |
| R.drawable.andre, | |
| R.drawable.bruno, | |
| R.drawable.carvalho, | |
| R.drawable.semedo, | |
| R.drawable.pepe, | |
| R.drawable.patricio | |
| ) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| init() | |
| } | |
| fun init(){ | |
| bindViews() | |
| title = "KotlinApp" | |
| val mainAdapter = PlayerAdapter(this, playerNames, playerImages) | |
| gridView.adapter = mainAdapter | |
| gridView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ -> | |
| Toast.makeText( | |
| applicationContext, "You CLicked " + playerNames[+position], | |
| Toast.LENGTH_SHORT).show() | |
| } | |
| } | |
| fun bindViews(){ | |
| gridView = findViewById(R.id.gridView) | |
| } | |
| } | |
| ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
| activity_main.xml: | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <GridView 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:id="@+id/gridView" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:numColumns="2" | |
| tools:context=".MainActivity"> | |
| </GridView> | |
| ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
| PlayerAdapter.kt: | |
| package com.iliyadev.gridview.adapters | |
| import android.content.Context | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.BaseAdapter | |
| import android.widget.ImageView | |
| import android.widget.TextView | |
| import com.iliyadev.gridview.R | |
| internal class PlayerAdapter( | |
| private val context: Context, | |
| private val numbersInWords: Array<String>, | |
| private val numberImage: IntArray | |
| ) : | |
| BaseAdapter() { | |
| private var layoutInflater: LayoutInflater? = null | |
| private lateinit var imageView: ImageView | |
| private lateinit var textView: TextView | |
| override fun getCount(): Int { | |
| return numbersInWords.size | |
| } | |
| override fun getItem(position: Int): Any? { | |
| return null | |
| } | |
| override fun getItemId(position: Int): Long { | |
| return 0 | |
| } | |
| override fun getView( | |
| position: Int, | |
| convertView: View?, | |
| parent: ViewGroup | |
| ): View? { | |
| var convertView = convertView | |
| if (layoutInflater == null) { | |
| layoutInflater = | |
| context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater | |
| } | |
| if (convertView == null) { | |
| convertView = layoutInflater!!.inflate(R.layout.row_item, null) | |
| } | |
| imageView = convertView!!.findViewById(R.id.imageView) | |
| textView = convertView.findViewById(R.id.textView) | |
| imageView.setImageResource(numberImage[position]) | |
| textView.text = numbersInWords[position] | |
| return convertView | |
| } | |
| } | |
| ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
| row_item.xml: | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:orientation="vertical" | |
| android:gravity="center" | |
| android:padding="8dp"> | |
| <ImageView | |
| android:id="@+id/imageView" | |
| android:layout_width="100dp" | |
| android:layout_height="100dp" /> | |
| <TextView | |
| android:textAlignment="center" | |
| android:gravity="center" | |
| android:id="@+id/textView" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginTop="16dp" | |
| android:text="Numbers" | |
| android:layout_marginBottom="10dp" | |
| android:textColor="@android:color/background_dark" | |
| android:textSize="24sp" | |
| android:textStyle="bold" /> | |
| </LinearLayout> | |
| ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
| Drawable Picture in poco file:::::: | |
| https://s31.picofile.com/file/8469349376/drawble.zip.html | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment