Kotlin ile Android Studio Galeri Projesi

SplashActivity.kt kodları;

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        supportActionBar?.hide()

        Handler().postDelayed({
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        },3000)

    }
}

activity_splash.xml kodları;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/gradient_1"
    tools:context=".SplashActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="Resim Galerisi"
        android:textColor="#ffffff"
        android:textSize="30sp" />
</RelativeLayout>

drawable klasörüne gradient_1.xml dosyası oluşturup açınız;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="225"
        android:endColor="#044fab"
        android:startColor="#21d6d3"
        />
</shape>

yine aynı konuma buton_stil1.xml dosyası oluşturup açınız;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="7dp"
        />
    <solid
        android:color="#49A5CC"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <size
        android:width="100dp"
        android:height="30dp"
        />
    <stroke
        android:width="3dp"
        android:color="#878787"
        />
</shape>

 

activity_main.xml kodları;

<?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"
    tools:context=".MainActivity">
    <ListView
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:id="@+id/listViewKategoriler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity.kt kodları;

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    var adapterKategori: AdapterKategori?=null
    var liste=ArrayList<Kategori>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        liste.add(Kategori(1,"Çiçekler","https://images.unsplash.com/photo-1471899236350-e3016bf1e69e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
        liste.add(Kategori(2,"Arabalar","https://images.unsplash.com/photo-1489824904134-891ab64532f1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
        liste.add(Kategori(3,"Doğa","https://images.unsplash.com/photo-1441974231531-c6227db76b6e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
        liste.add(Kategori(4,"Tarihi Yerler","https://images.unsplash.com/photo-1509024644558-2f56ce76c490?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
        adapterKategori = AdapterKategori(applicationContext,liste)
        listViewKategoriler.setAdapter(adapterKategori)
    }
}

kategori_satir_goruntusu.xml dosyası;

res altında bulunan layout klasörünün altına ilgili isimde dosya açıp içine ekleyiniz.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content">

    <ImageView
        android:id="@+id/ivKategoriResim"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="0dp"
        app:srcCompat="@drawable/ic_launcher_background"
        tools:ignore="VectorDrawableCompat" />

    <TextView
        android:id="@+id/tvKategoriBaslik"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="104dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="TextView"
        android:textSize="24sp" />
</RelativeLayout>

Kategori.kt kodları;

class Kategori {
    var id=0
    var baslik:String? = null
    var resimUrl : String? = null

    constructor(){}
    constructor(id:Int,baslik:String?,resimUrl:String?){
        this.id=id
        this.baslik=baslik
        this.resimUrl=resimUrl
    }
}

Resim.kt kodları;

class Resim {
    var id=0
    var baslik:String? = null
    var resimUrl : String? = null

    constructor(){}
    constructor(id:Int,baslik:String?,resimUrl:String?){
        this.id=id
        this.baslik=baslik
        this.resimUrl=resimUrl
    }
}

AdapterKategori.kt kodları;

import android.content.Context
import android.content.Intent
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.bumptech.glide.Glide

class AdapterKategori : BaseAdapter {
    private var layoutInflater: LayoutInflater?=null
    private var context: Context?=null
    private var kategoriler: ArrayList<Kategori>?=null

    constructor(){}
    constructor(context: Context,kategoriler:ArrayList<Kategori>?){
        this.context=context
        this.kategoriler=kategoriler
        this.layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }


    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
       val v=layoutInflater!!.inflate(R.layout.kategori_satir_goruntusu,null)
        val tvBaslik = v.findViewById<TextView>(R.id.tvKategoriBaslik)
        val ivResim = v.findViewById<ImageView>(R.id.ivKategoriResim)
        Glide.with(v.context).load(kategoriler!!.get(position).resimUrl).into(ivResim);

        v.setOnClickListener {
            val intent = Intent(v.context,ResimlerActivity::class.java)
            intent.putExtra("baslik",kategoriler!!.get(position).baslik)
            intent.putExtra("kategori_id",kategoriler!!.get(position).id)
            v.context.startActivity(intent)
        }

        tvBaslik.text = kategoriler!![position].baslik
        return v
    }

    override fun getItem(position: Int): Any {
        return kategoriler!![position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return kategoriler!!.size
    }
}

build.gradle (project) repositories scope'u içerisine

mavenCentral()

build.gradle (module app) depentencies bloğu içerisine

annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

ekleyiniz, ardından Sync now sağ üst köşede belirecek tıklayıp, projenize kütüphaneyi dahil ediniz.

ResimlerActivity.kt isminde kotlin sınıfı açınız;

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_resimler.*

class ResimlerActivity : AppCompatActivity() {

    var resimListesi = ArrayList<Resim>()
    var sayi: Int=0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_resimler)

        val baslik = intent.getStringExtra("baslik")
        val kategori_id = intent.getIntExtra("kategori_id",0)

        this.setTitle(baslik)
        this.supportActionBar?.setDisplayHomeAsUpEnabled(true)

        if(kategori_id==1){ //Çiçekler
            resimListesi.add(Resim(0,"Bahar","https://images.unsplash.com/photo-1516205651411-aef33a44f7c2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
            resimListesi.add(Resim(1,"Gül","https://images.unsplash.com/photo-1495231916356-a86217efff12?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
            resimListesi.add(Resim(2,"Ayçiçeği","https://images.unsplash.com/photo-1470509037663-253afd7f0f51?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
        }else if(kategori_id==2){ //Arabalar
            resimListesi.add(Resim(0,"Araba 1","https://images.unsplash.com/photo-1542282088-fe8426682b8f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
            resimListesi.add(Resim(1,"Araba 2","https://images.unsplash.com/photo-1550355291-bbee04a92027?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
            resimListesi.add(Resim(2,"Araba 3","https://images.unsplash.com/photo-1518306727298-4c17e1bf6942?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
        }else if(kategori_id==3){ //Doğa
            resimListesi.add(Resim(0,"Doğa 1","https://images.unsplash.com/photo-1439853949127-fa647821eba0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
            resimListesi.add(Resim(1,"Doğa 2","https://images.unsplash.com/photo-1550236520-7050f3582da0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
            resimListesi.add(Resim(2,"Doğa 3","https://images.unsplash.com/photo-1455218873509-8097305ee378?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"))
        }else if(kategori_id==4){ //Tarihi Yerler
            resimListesi.add(Resim(0,"Eifel Kulesi","https://images.unsplash.com/photo-1525218291292-e46d2a90f77c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80"))
            resimListesi.add(Resim(1,"Coliseum","https://images.unsplash.com/photo-1562571963-ebb6e01d03ec?ixlib=rb-1.2.1&auto=format&fit=crop&w=287&q=80"))
            resimListesi.add(Resim(2,"Galata Kulesi","https://images.unsplash.com/photo-1575974613450-86b183b69f1e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=333&q=80"))
        }

        Glide.with(applicationContext).load(resimListesi.get(sayi).resimUrl).into(ivResim)
        tvBaslik.text = resimListesi.get(sayi).baslik

        btnIleri.setOnClickListener (View.OnClickListener{

            if(sayi<resimListesi.size){
                sayi++
            }
            Glide.with(applicationContext).load(resimListesi.get(sayi).resimUrl).into(ivResim)
            tvBaslik.text = resimListesi.get(sayi).baslik
        })

        btnRastgele.setOnClickListener (View.OnClickListener{

            Glide.with(applicationContext).load(resimListesi.random().resimUrl).into(ivResim)
            tvBaslik.text = resimListesi.random().baslik

        })

        btnGeri.setOnClickListener (View.OnClickListener{

            if(sayi>0){
                sayi--
            }
            Glide.with(applicationContext).load(resimListesi.get(sayi).resimUrl).into(ivResim)
            tvBaslik.text = resimListesi.get(sayi).baslik
        })



    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(item.itemId==android.R.id.home){
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

}

activity_resimler.xml kodları;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ResimlerActivity">

    <ImageView
        android:id="@+id/ivResim"
        android:layout_width="match_parent"
        android:layout_height="450dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        app:srcCompat="@drawable/ic_launcher_foreground"
        tools:ignore="VectorDrawableCompat" />

    <Button
        android:background="@drawable/buton_stil1"
        android:id="@+id/btnRastgele"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="169dp"
        android:layout_marginLeft="169dp"
        android:layout_marginBottom="52dp"
        android:text="Rastgele" />

    <Button
        android:background="@drawable/buton_stil1"
        android:id="@+id/btnGeri"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="17dp"
        android:layout_marginLeft="17dp"
        android:layout_marginBottom="55dp"
        android:text="Geri" />

    <Button
        android:background="@drawable/buton_stil1"
        android:id="@+id/btnIleri"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="49dp"
        android:text="İleri" />

    <TextView
        android:id="@+id/tvBaslik"
        android:layout_width="375dp"
        android:layout_height="44dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="23dp"
        android:layout_marginLeft="23dp"
        android:layout_marginBottom="114dp"
        android:gravity="center"
        android:text="TextView"
        android:textSize="24sp" />
</RelativeLayout>

AndroidManifest.xml dosyası;

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
    <activity android:name=".ResimlerActivity"></activity>
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"></activity>
</application>
Bu makaleyi paylaşın:

Toplam yorum sayısı: 0

Bir yorum bırakın