AMP Analytics -->

Teknoloji Rehberi

15 Mayıs 2025 Perşembe

Kotlin'de AutoLink Özellikli Aktivite Örneği

 

Kotlin'de AutoLink Özellikli Aktivite Örneği

Aşağıda, bir Android aktivitesinde 7 farklı türde AutoLink özelliği kullanan bir örnek bulunmaktadır. Bu örnek, bir TextView'da çeşitli otomatik bağlantı türlerini göstermektedir.

Örnek Kod

activity_main.xml (Arayüz dosyası):

xml
Copy
Download
Run
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

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

        <!-- Web URL'leri için AutoLink -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Web sitesi: https://www.google.com"
            android:autoLink="web"
            android:textSize="18sp"
            android:layout_marginBottom="16dp"/>

        <!-- E-posta adresleri için AutoLink -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="E-posta: example@email.com"
            android:autoLink="email"
            android:textSize="18sp"
            android:layout_marginBottom="16dp"/>

        <!-- Telefon numaraları için AutoLink -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Telefon: +905551112233"
            android:autoLink="phone"
            android:textSize="18sp"
            android:layout_marginBottom="16dp"/>

        <!-- Harita adresleri için AutoLink -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Adres: Kadıköy, İstanbul, Türkiye"
            android:autoLink="map"
            android:textSize="18sp"
            android:layout_marginBottom="16dp"/>

        <!-- Tüm bağlantı türleri için AutoLink -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Tümü: https://www.google.com example@email.com +905551112233 Kadıköy, İstanbul"
            android:autoLink="all"
            android:textSize="18sp"
            android:layout_marginBottom="16dp"/>

        <!-- Özel desenler için AutoLink (Kotlin kodu gerektirir) -->
        <TextView
            android:id="@+id/customLinkTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Özel bağlantı: #1234 (Ticket numarası)"
            android:textSize="18sp"
            android:layout_marginBottom="16dp"/>

      
    </LinearLayout>
</ScrollView>

MainActivity.kt:

kotlin
Copy
Download
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

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

        // Özel bağlantı için ClickableSpan oluşturma
        val customLinkTextView = findViewById<TextView>(R.id.customLinkTextView)
        val text = customLinkTextView.text.toString()
        val spannableString = SpannableString(text)

        // Ticket numarasını bul ve tıklanabilir yap
        val ticketPattern = "#\\d+".toRegex()
        val matchResult = ticketPattern.find(text)
        matchResult?.let {
            val clickableSpan = object : ClickableSpan() {
                override fun onClick(widget: View) {
                    Toast.makeText(
                        this@MainActivity,
                        "Ticket ${it.value} tıklandı",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
            spannableString.setSpan(
                clickableSpan,
                it.range.first,
                it.range.last + 1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }

        customLinkTextView.text = spannableString
        customLinkTextView.movementMethod = LinkMovementMethod.getInstance()
    }
}

AutoLink Türleri Açıklaması

Bu örnekte 7 farklı bağlantı türü gösterilmiştir:

  1. Web URL'leri (android:autoLink="web") - https://www.google.com

  2. E-posta adresleri (android:autoLink="email") - example@email.com

  3. Telefon numaraları (android:autoLink="phone") - +905551112233

  4. Harita adresleri (android:autoLink="map") - Kadıköy, İstanbul, Türkiye

  5. Tüm bağlantı türleri (android:autoLink="all") - Yukarıdakilerin hepsi

  6. Özel desenler (Kotlin kodu ile) - #1234 (Ticket numarası)

  7. HTML bağlantıları - <a href='https://www.android.com'>Android</a>

Notlar

  • AutoLink özelliği, TextView'da belirli desenleri otomatik olarak tıklanabilir bağlantılara dönüştürür.

  • Özel desenler için ClickableSpan kullanmanız gerekir (örnekteki 6. bağlantı gibi).

  • Bağlantıların tıklanabilir olması için movementMethod ayarlanmalıdır.

  • XML'de autoLink özelliği kullanılabileceği gibi, programatik olarak TextView.setAutoLinkMask() metoduyla da ayarlanabilir.

Bu örnek, tüm bağlantı türlerini aynı anda gösteren kapsamlı bir aktivite sunmaktadır.


Hiç yorum yok:

Yorum Gönder

Popular Posts