Kotlinde Gelişmiş Özelliklere Sahip Günün Sözü Uygulaması

 

1. Güncellenmiş activity_main.xml

xml
Copy
Download
Run
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="24dp"
    android:background="@drawable/bg_gradient">

    <!-- Günün Sözü Kartı -->
    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="8dp"
        app:cardCornerRadius="16dp"
        app:cardBackgroundColor="@color/white">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="24dp">

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@drawable/ic_quote"
                android:layout_gravity="center"/>

            <TextView
                android:id="@+id/tvSoz"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textColor="@color/black"
                android:textStyle="italic"
                android:gravity="center"
                android:layout_marginTop="16dp"
                android:lineSpacingExtra="4dp"/>

            <TextView
                android:id="@+id/tvYazar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textColor="@color/gray"
                android:gravity="end"
                android:layout_marginTop="16dp"
                android:fontFamily="sans-serif-medium"/>

        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <!-- Butonlar -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="32dp"
        android:gravity="center"
        android:weightSum="3">

        <!-- Favori Butonu -->
        <ImageButton
            android:id="@+id/btnFavori"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:src="@drawable/ic_favorite_border"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:tint="@color/purple_500"/>

        <!-- Yenile Butonu -->
        <Button
            android:id="@+id/btnYenile"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="YENİLE"
            android:backgroundTint="@color/purple_500"
            android:textColor="@color/white"
            android:layout_marginHorizontal="8dp"/>

        <!-- Paylaş Butonu -->
        <ImageButton
            android:id="@+id/btnPaylas"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:src="@drawable/ic_share"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:tint="@color/purple_500"/>

    </LinearLayout>

    <!-- Widget Butonu -->
    <Button
        android:id="@+id/btnWidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WIDGET EKLE"
        android:layout_marginTop="16dp"
        android:backgroundTint="@color/teal_700"
        android:textColor="@color/white"/>

</LinearLayout>

2. Güncellenmiş MainActivity.kt

kotlin
Copy
Download
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    private lateinit var currentSoz: Pair<String, String>
    private val favoriSozler = mutableListOf<Pair<String, String>>()

    private val sozler = listOf(
        "Hayat bir bisiklet sürmek gibidir..." to "Albert Einstein",
        "Kod yazmak, şiir yazmaya benzer..." to "Linus Torvalds",
        // Diğer sözler...
    )

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

        val tvSoz = findViewById<TextView>(R.id.tvSoz)
        val tvYazar = findViewById<TextView>(R.id.tvYazar)
        val btnYenile = findViewById<Button>(R.id.btnYenile)
        val btnFavori = findViewById<ImageButton>(R.id.btnFavori)
        val btnPaylas = findViewById<ImageButton>(R.id.btnPaylas)
        val btnWidget = findViewById<Button>(R.id.btnWidget)
        val cardView = findViewById<CardView>(R.id.cardView)

        // İlk sözü göster
        rastgeleSozGoster(tvSoz, tvYazar)

        // Yenile Butonu
        btnYenile.setOnClickListener {
            rastgeleSozGoster(tvSoz, tvYazar)
            cardView.animate().rotationXBy(360f).setDuration(500).start()
            btnFavori.setImageResource(R.drawable.ic_favorite_border)
        }

        // Favori Butonu
        btnFavori.setOnClickListener {
            if (favoriSozler.contains(currentSoz)) {
                favoriSozler.remove(currentSoz)
                btnFavori.setImageResource(R.drawable.ic_favorite_border)
                Toast.makeText(this, "Favorilerden kaldırıldı", Toast.LENGTH_SHORT).show()
            } else {
                favoriSozler.add(currentSoz)
                btnFavori.setImageResource(R.drawable.ic_favorite_filled)
                Toast.makeText(this, "Favorilere eklendi", Toast.LENGTH_SHORT).show()
            }
        }

        // Paylaş Butonu
        btnPaylas.setOnClickListener {
            val paylasMetni = "${currentSoz.first} - ${currentSoz.second}"
            val intent = Intent().apply {
                action = Intent.ACTION_SEND
                putExtra(Intent.EXTRA_TEXT, paylasMetni)
                type = "text/plain"
            }
            startActivity(Intent.createChooser(intent, "Sözü Paylaş"))
        }

        // Widget Butonu
        btnWidget.setOnClickListener {
            val widgetIntent = Intent(this, SozWidgetProvider::class.java).apply {
                action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
                putExtra("SOZ", currentSoz.first)
                putExtra("YAZAR", currentSoz.second)
            }
            val ids = AppWidgetManager.getInstance(application)
                .getAppWidgetIds(ComponentName(application, SozWidgetProvider::class.java))
            widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
            sendBroadcast(widgetIntent)
            Toast.makeText(this, "Widget güncellendi!", Toast.LENGTH_SHORT).show()
        }
    }

    private fun rastgeleSozGoster(sozTextView: TextView, yazarTextView: TextView) {
        currentSoz = sozler.random()
        sozTextView.text = "\"${currentSoz.first}\""
        yazarTextView.text = "- ${currentSoz.second}"
    }
}

3. Yeni Gerekli Dosyalar

res/drawable/ic_favorite_border.xml (Vector Asset)

xml
Copy
Download
Run
<vector android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M16.5,3c-1.74,0 -3.41,0.81 -4.5,2.09C10.91,3.81 9.24,3 7.5,3 4.42,3 2,5.42 2,8.5c0,3.78 3.4,6.86 8.55,11.54L12,21.35l1.45,-1.32C18.6,15.36 22,12.28 22,8.5 22,5.42 19.58,3 16.5,3zM12.1,18.55l-0.1,0.1 -0.1,-0.1C7.14,14.24 4,11.39 4,8.5 4,6.5 5.5,5 7.5,5c1.54,0 3.04,0.99 3.57,2.36h1.87C13.46,5.99 14.96,5 16.5,5c2,0 3.5,1.5 3.5,3.5 0,2.89 -3.14,5.74 -7.9,10.05z"/>
</vector>

res/drawable/ic_favorite_filled.xml (Vector Asset)

xml
Copy
Download
Run
<vector android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>

res/drawable/ic_share.xml (Vector Asset)

xml
Copy
Download
Run
<vector android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>
</vector>

4. Widget İçin Gerekli Sınıflar

SozWidgetProvider.kt

kotlin
Copy
Download
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.widget.RemoteViews

class SozWidgetProvider : AppWidgetProvider() {
    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        appWidgetIds.forEach { appWidgetId ->
            val views = RemoteViews(context.packageName, R.layout.widget_soz)
            
            // SharedPreferences'ten son sözü al
            val prefs = context.getSharedPreferences("SozPrefs", Context.MODE_PRIVATE)
            val soz = prefs.getString("son_soz", "Söz yükleniyor...")
            val yazar = prefs.getString("son_yazar", "")
            
            views.setTextViewText(R.id.widget_tvSoz, "\"$soz\"")
            views.setTextViewText(R.id.widget_tvYazar, "- $yazar")
            
            appWidgetManager.updateAppWidget(appWidgetId, views)
        }
    }
}

res/xml/widget_info.xml

xml
Copy
Download
Run
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="146dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/widget_soz"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen"/>

res/layout/widget_soz.xml

xml
Copy
Download
Run
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/widget_bg"
    android:padding="16dp">

    <TextView
        android:id="@+id/widget_tvSoz"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="@color/black"
        android:textStyle="italic"/>

    <TextView
        android:id="@+id/widget_tvYazar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:layout_marginTop="8dp"
        android:gravity="end"/>

</LinearLayout>

Özellikler:

  1. Favori Yönetimi: Kalp ikonuyla favorilere ekleme/çıkarma

  2. Sosyal Paylaşım: Tüm uygulamalara paylaşım desteği

  3. Widget Entegrasyonu: Ana ekranda günün sözünü gösterir

  4. Animasyonlar: Kart dönme efekti ve renk geçişleri

  5. Veri Kalıcılığı: SharedPreferences ile son sözü saklama

Uygulamayı çalıştırmak için AndroidManifest.xml'e widget provider'ı eklemeyi unutmayın!


Yorumlar

Tarih Bilgini Test Etmeye Hazır mısın?

Avrupa'dan Afrika'ya, Asya'dan Amerika ve İslam tarihine uzanan 40 soruluk dev genel kültür testimiz yayında!

🏆 Yarışmayı Tam Sayfa Aç
PDF Okuyucu İkonu

Hızlı PDF Okuyucu

Hızlı, hafif ve kullanıcı dostu PDF görüntüleme deneyimi.

Google Play'den İndir
Dersimiz Tarih

Dersimiz Tarih Uygulaması

Tarih Derslerinden Tarih Alanında Hemen Herşeye Kadar Dijital Tarih Bilgi Merkezi.

Google Play'den İndir

Bu blogdaki popüler yayınlar

YouTube ile Web Sitesi SEO'su: Kapsamlı Strateji Rehberi

Uygulama Reklam Butonu