কম্পিউটার টিউটোরিয়াল

Kotlin-এ Android-এ ImageView ক্লিয়ার করার পদ্ধতি


এই উদাহরণটি দেখায় কিভাবে Kotlin ব্যবহার করে Android-এ একটি ImageView ক্লিয়ার করা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: activity_main.xml ডিজাইন করুন

res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:onClick="clickHere"
        android:text="Click here" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button"
        android:layout_centerInParent="true"
        android:background="@drawable/image" />
</RelativeLayout>

ধাপ ৩: MainActivity.kt-এ ক্লিয়ার লজিক লিখুন

src/MainActivity.kt ফাইলে নিচের কোডটি যোগ করুন:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        imageView = findViewById(R.id.imageView)
    }

    fun clickHere(view: View) {
        imageView.setBackgroundDrawable(null)
        Toast.makeText(this, "Image cleared", Toast.LENGTH_SHORT).show()
    }
}

ধাপ ৪: AndroidManifest.xml কনফিগার করুন

androidManifest.xml ফাইলে নিচের কোডটি যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

অ্যাপ্লিকেশন রান করুন

ডিভাইস সংযুক্ত করে Android Studio থেকে Run করুন। "Click here" বাটনে ক্লিক করলে ইমেজ ক্লিয়ার হবে।

  1. Android-এ ArrayBlockingQueue খালি করার (clear) পদ্ধতি

  2. Picasso লাইব্রেরি ব্যবহার করে অ্যান্ড্রয়েডে URL থেকে ImageView-এ ছবি লোড করার পদ্ধতি

  3. অ্যান্ড্রয়েডে কোড ব্যবহার করে ImageView-এর মার্জিন সেট করার পদ্ধতি

  4. অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে গ্যালারি থেকে ছবি বেছে নেওয়ার উপায়