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

Android में CalendarView विजेट का उपयोग कैसे करें?

Android CalendarView विजेट से तारीख चयन करना

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

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

Android Studio में File → New Project चुनें।

चरण 2: लेआउट फ़ाइल (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">

    <TextView
        android:id="@+id/dateView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="20dp"
        android:text="तारीख चुनें"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:textSize="18sp" />

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dateView"
        android:layout_marginTop="16dp"
        android:layout_marginHorizontal="16dp"
        android:dateTextAppearance="@style/CalendarDateText"
        android:weekDayTextAppearance="@style/CalendarWeekDayText" />

</RelativeLayout>

चरण 3: MainActivity (src/MainActivity.java)

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private CalendarView calendarView;
    private TextView dateView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendarView = findViewById(R.id.calendarView);
        dateView = findViewById(R.id.dateView);

        // तारीख परिवर्तन लिसनर सेट करें
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                // month 0-आधारित है (0=जनवरी), इसलिए +1 करें
                String formattedDate = String.format("%02d-%02d-%04d", dayOfMonth, month + 1, year);
                dateView.setText("चुनी गई तारीख: " + formattedDate);
            }
        });

        // वैकल्पिक: प्रोग्रामेटिकली तारीख सेट करें
        // calendarView.setDate(System.currentTimeMillis(), false, true);
        
        // वैकल्पिक: न्यूनतम/अधिकतम तारीख सेट करें
        // calendarView.setMinDate(System.currentTimeMillis() - 86400000); // कल से
        // calendarView.setMaxDate(System.currentTimeMillis() + 86400000 * 30); // 30 दिन बाद तक
    }
}

CalendarView कस्टमाइजेशन (styles.xml में)

<style name="CalendarDateText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textSize">14sp</item>
</style>

<style name="CalendarWeekDayText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@android:color/holo_blue_dark</item>
    <item name="android:textStyle">bold</item>
</style>

CalendarView के मुख्य XML एट्रिब्यूट्स

एट्रिब्यूट विवरण
android:firstDayOfWeek सप्ताह का पहला दिन (1=रविवार, 2=सोमवार...)
android:minDate न्यूनतम चुनने योग्य तारीख (मिलीसेकंड)
android:maxDate अधिकतम चुनने योग्य तारीख
android:shownWeekCount दिखाए जाने वाले सप्ताहों की संख्या
android:dateTextAppearance तारीख टेक्स्ट स्टाइल
android:weekDayTextAppearance सप्ताह दिन हेडर स्टाइल

चरण 4: AndroidManifest.xml

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

    <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>
                
                
            </intent-filter>
        </activity>

    </application>
</manifest>

ऐप चलाएं

ऐप लॉन्च करें, कैलेंडर पर किसी तारीख पर टैप करें, और ऊपर TextView में फॉर्मेट की गई तारीख देखें।

Android में CalendarView विजेट का उपयोग कैसे करें?

  1. एंड्रॉइड ऐप में कैलेंडर इवेंट कैसे जोड़ें एंड्रॉइड ऐप में कैलेंडर इवेंट कैसे जोड़ें

    इंटेंट के माध्यम से सिस्टम कैलेंडर ऐप में इवेंट जोड़ना एंड्रॉइड में कैलेंडर इवेंट जोड़ने का सबसे सरल तरीका Intent.ACTION_INSERT या Intent.ACTION_EDIT का उपयोग करके सिस्टम कैलेंडर ऐप को लॉन्च करना है। यह यूजर को इवेंट विवरण संपादित/पुष्टि करने देता है और किसी विशेष अनुमति की आवश्यकता नहीं होती। चरण

  1. Windows 11 PC में कैलेंडर ऐप का उपयोग कैसे करें Windows 11 PC में कैलेंडर ऐप का उपयोग कैसे करें

    Windows 11 का अपना फीचर-रिच कैलेंडर ऐप है जो आपके इवेंट्स, टास्क, मीटिंग्स आदि को शेड्यूल करके आपको संगठित रहने में मदद करता है। ऐप का शॉर्टकट आपके टास्कबार के दाएं कोने में बैठता है। बस अपने टास्कबार में समय और तारीख आइकन पर क्लिक करें और कैलेंडर फ्लाईआउट एक छोटी विंडो में पॉप अप होगा। हालांकि, नए

  1. विंडोज 10 कैलेंडर ऐप का उपयोग कैसे करें विंडोज 10 कैलेंडर ऐप का उपयोग कैसे करें

    माइक्रोसॉफ्ट ने विंडोज 10 में कैलेंडर ऐप का रूप और अनुभव बदल दिया है। सबसे ध्यान देने योग्य परिवर्तन जो आप विंडोज 10 कैलेंडर ऐप में पाएंगे, वह है उपयोगकर्ताओं के लिए अपने Google कैलेंडर को सिंक और देखने की क्षमता, कुछ ऐसा जो विंडोज 8.1 के लिए समान ऐप में समर्थित नहीं था। आइए नए विंडोज 10 कैलेंडर ऐप