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

Android में डिफ़ॉल्ट टेक्स्ट वाला स्पिनर (Spinner) कैसे बनाएं?

Android में "चुनें..." जैसे डिफ़ॉल्ट टेक्स्ट वाला स्पिनर बनाने का गाइड

यह ट्यूटोरियल दिखाता है कि कैसे ArrayAdapter और स्पिनर में पहला आइटम डिफ़ॉल्ट प्लेसहोल्डर के रूप में उपयोग करके "Select a player..." जैसा प्रारंभिक टेक्स्ट दिखाया जाए।

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

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

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

res/layout/activity_main.xml में:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linearLayout"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Spinner Sample Program"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textSize="16sp"/>
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center">
    </Spinner>
</LinearLayout>

चरण 3: MainActivity.java में कोड जोड़ें

src/MainActivity.java में:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Spinner spinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = findViewById(R.id.spinner);
        List<String> footballPlayers = new ArrayList<>();
        footballPlayers.add(0, "Select a player from the list");
        footballPlayers.add("Cristian Ronaldo");
        footballPlayers.add("Lionel Messi");
        footballPlayers.add("Neymar Jr");
        footballPlayers.add("Isco");
        footballPlayers.add("Gareth Bale");
        footballPlayers.add("Luis Suarez");
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, footballPlayers);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    // डिफ़ॉल्ट आइटम चुना गया - कुछ न करें
                    return;
                }
                String item = parent.getItemAtPosition(position).toString();
                Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) { }
        });
    }
}

मुख्य बिंदु

  • footballPlayers.add(0, "Select...") - इंडेक्स 0 पर डिफ़ॉल्ट टेक्स्ट डालें
  • position == 0 चेक करके डिफ़ॉल्ट चयन को अनदेखा करें
  • setDropDownViewResource ड्रॉपडाउन दृश्य कस्टमाइज़ करता है

चरण 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>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

ऐप चलाएं

डिवाइस कनेक्ट करें, Run आइकन पर क्लिक करें, स्पिनर पर टैप करें और खिलाड़ी चुनकर टोस्ट देखें।

बेहतर UX के लिए टिप

डिफ़ॉल्ट आइटम को ग्रे कलर में दिखाने के लिए कस्टम एडाप्टर बनाएं जो getView() (चयनित दृश्य) और getDropDownView() (ड्रॉपडाउन आइटम) को अलग-अलग हैंडल करे।

  1. Android TextView में टेक्स्ट को जस्टिफाई कैसे करें? Android TextView में टेक्स्ट को जस्टिफाई कैसे करें?

    TextView में टेक्स्ट जस्टिफिकेशन Android 8.0 (API 26) से android:justificationMode=inter_word एट्रिब्यूट का उपयोग करके TextView में टेक्स्ट को जस्टिफाई (दोनों किनारों पर संरेखित) किया जा सकता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें (न्यूनतम API 26 या अधिक)। चरण 2

  1. एंड्रॉइड पर SD कार्ड को डिफ़ॉल्ट स्टोरेज कैसे बनाएं? एंड्रॉइड पर SD कार्ड को डिफ़ॉल्ट स्टोरेज कैसे बनाएं?

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

  1. Android पर Alexa को अपना डिफ़ॉल्ट असिस्टेंट कैसे बनाएं Android पर Alexa को अपना डिफ़ॉल्ट असिस्टेंट कैसे बनाएं

    यदि आप अपने फोन पर और अपने घर में वॉयस असिस्टेंट का उपयोग करते हैं, तो आप अपने फोन पर सेटिंग्स बदलकर लाभ उठा सकते हैं ताकि आप एक सुसंगत सेवा का उपयोग कर सकें। उदाहरण के लिए, यदि आप अपने फोन पर Android का उपयोग कर रहे हैं लेकिन आपको उपहार के रूप में Amazon Echo मिलता है और उसका भी उपयोग करते हैं, तो