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

एंड्रॉइड में कोटलिन का उपयोग करके कस्टम रेटिंगबार कैसे बनाएं

एंड्रॉइड में कोटलिन का उपयोग करके कस्टम रेटिंगबार बनाना

यह उदाहरण दर्शाता है कि एंड्रॉइड में कोटलिन का उपयोग करके कस्टम रेटिंगबार कैसे बनाया जाए।

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

एंड्रॉइड स्टूडियो में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और सभी आवश्यक विवरण भरकर नया प्रोजेक्ट बनाएं।

चरण 2 - लेआउट फ़ाइल में कोड जोड़ें

res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:

<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"
    android:padding="16sp"
    tools:context=".MainActivity">

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:numStars="5"
        android:rating="2.0"
        android:stepSize="1.0" />

    <Button
        android:id="@+id/buttonCheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ratingBar"
        android:layout_centerHorizontal="true"
        android:text="CHECK" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="24sp" />

</RelativeLayout>

चरण 3 - MainActivity.kt में कोड जोड़ें

import android.os.Bundle
import android.widget.Button
import android.widget.RatingBar
import android.widget.RatingBar.OnRatingBarChangeListener
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    lateinit var ratingBar: RatingBar
    lateinit var button: Button
    lateinit var textView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        ratingBar = findViewById(R.id.ratingBar)
        ratingBar.numStars = 5
        button = findViewById(R.id.buttonCheck)
        textView = findViewById(R.id.textView)

        ratingBar.onRatingBarChangeListener = OnRatingBarChangeListener { _, rating, _ ->
            Toast.makeText(this@MainActivity, "Stars: " + rating.toInt(), Toast.LENGTH_SHORT).show()
        }

        button.setOnClickListener {
            textView.text = "आपको " + ratingBar.rating.toInt() + " स्टार मिले"
        }
    }
}

चरण 4 - AndroidManifest.xml में कोड जोड़ें

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.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>

ऐप चलाएं

अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। एंड्रॉइड स्टूडियो में प्रोजेक्ट की एक्टिविटी फ़ाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिवाइस पर डिफ़ॉल्ट स्क्रीन देखें।

  1. Android पर कस्टम डायलॉग बॉक्स कैसे बनाएं Android पर कस्टम डायलॉग बॉक्स कैसे बनाएं

    परिचयएक कस्टम डायलॉग उपयोगकर्ता इनपुट लेने या कस्टम लेआउट के साथ जानकारी प्रदर्शित करने की अनुमति देता है। यह लेख `AlertDialog.Builder` और एक कस्टम XML लेआउट का उपयोग करके एक कस्टम डायलॉग बनाने का तरीका दिखाता है।चरण-दर-चरण कार्यान्वयनचरण 1: मुख्य लेआउट (activity_main.xml)<Button android:id=

  1. Android में XML स्टाइल्स का उपयोग करके कस्टम बटन कैसे बनाएं Android में XML स्टाइल्स का उपयोग करके कस्टम बटन कैसे बनाएं

    परिचयXML ड्रॉएबल्स (शेप, सेलेक्टर) का उपयोग करके बटन के लिए कस्टम बैकग्राउंड (कोने, बॉर्डर, ग्रेडिएंट) बनाए जा सकते हैं बिना किसी इमेज एसेट के।कार्यान्वयनचरण 1: लेआउट (activity_main.xml) - तीन बटन<Button android:id=@+id/customButton android:background=@drawable/custom_button android:text=My Custo

  1. एंड्रॉइड में कस्टम रेटिंग बार कैसे बनाएं? एंड्रॉइड में कस्टम रेटिंग बार कैसे बनाएं?

    एंड्रॉइड में कस्टम RatingBar बनाना — ड्रॉएबल, स्टाइल, और प्रोग्रामेटिक कंट्रोल एंड्रॉइड का डिफ़ॉल्ट RatingBar सितारे दिखाता है, लेकिन इसका लुक कस्टमाइज़ करना (रंग, आइकन, आकार, एनिमेशन) आम जरूरत है। यह ट्यूटोरियल **XML स्टाइलिंग** और **प्रोग्रामेटिक कंट्रोल** दोनों दिखाता है। चरण 1: नया प्रोजेक्ट बन