Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके Android में ImageView को कैसे क्लियर करें?

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में ImageView को कैसे क्लियर (साफ़) किया जाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2: activity_main.xml में UI जोड़ें

<?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>

चरण 3: 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()
    }
}

चरण 4: 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 डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।

  1. Android में ArrayBlockingQueue को कैसे क्लियर करें? Android में ArrayBlockingQueue को कैसे क्लियर करें?

    ArrayBlockingQueue में clear() मेथड से सभी एलिमेंट्स हटाएं clear() क्यू के सभी एलिमेंट्स को एटॉमिकली हटा देता है, क्यू को खाली स्थिति में छोड़ता है। चरण 1: प्रोजेक्ट बनाएं Android Studio: File → New Project। चरण 2: लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-8?> <LinearLayout

  1. Android में Picasso का उपयोग करके URL से ImageView में इमेज लोड करना Android में Picasso का उपयोग करके URL से ImageView में इमेज लोड करना

    परिचयPicasso एक लोकप्रिय इमेज लोडिंग लाइब्रेरी है जो URL से इमेज डाउनलोड, कैशिंग और ImageView में प्रदर्शित करने को एक लाइन में सरल बनाती है।कार्यान्वयनचरण 1: build.gradle में निर्भरता जोड़ेंdependencies { implementation com.squareup.picasso:picasso:2.71828 }चरण 2: लेआउट (activity_main.xml)<

  1. Android में कोड का उपयोग करके ImageView का मार्जिन कैसे सेट करें Android में कोड का उपयोग करके ImageView का मार्जिन कैसे सेट करें

    परिचयरनटाइम पर व्यू के मार्जिन को बदलने के लिए, उसके `LayoutParams` को प्राप्त करें, मार्जिन सेट करें, और फिर `setLayoutParams()` कॉल करें।कार्यान्वयनचरण 1: लेआउट (activity_main.xml)<ImageView android:id=@+id/imageView android:layout_width=wrap_content android:layout_height=wrap_content android:b