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

Android EditText-এ প্রোগ্রাম্যাটিকভাবে Input Type সেট করা

EditText-এ InputType ডাইনামিকভাবে পরিবর্তন

রানটাইমে Java/Kotlin কোড দিয়ে EditText-এর ইনপুট টাইপ (নাম্বার, টেক্সট, পাসওয়ার্ড, ইমেইল ইত্যাদি) পরিবর্তন করার পদ্ধতি।

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

Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট (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:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:padding="4dp"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type something..." />
    <Button
        android:id="@+id/btnNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Set Input Type: NUMBER" />
    <Button
        android:id="@+id/btnText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Set Input Type: TEXT" />
    <Button
        android:id="@+id/btnPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Set Input Type: PASSWORD" />
    <Button
        android:id="@+id/btnEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Set Input Type: EMAIL" />
</LinearLayout>

ধাপ ৩: MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    Button btnNumber, btnText, btnPassword, btnEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        editText = findViewById(R.id.editText);
        btnNumber = findViewById(R.id.btnNumber);
        btnText = findViewById(R.id.btnText);
        btnPassword = findViewById(R.id.btnPassword);
        btnEmail = findViewById(R.id.btnEmail);
        
        btnNumber.setOnClickListener(v -> 
            editText.setInputType(InputType.TYPE_CLASS_NUMBER));
        
        btnText.setOnClickListener(v -> 
            editText.setInputType(InputType.TYPE_CLASS_TEXT));
        
        btnPassword.setOnClickListener(v -> 
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
        
        btnEmail.setOnClickListener(v -> 
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS));
        
        // কার্সর শেষে সরানোর জন্য
        editText.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) editText.setSelection(editText.getText().length());
        });
    }
}

ধাপ ৪: AndroidManifest.xml

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

InputType কনস্ট্যান্টস রেফারেন্স

কনস্ট্যান্টব্যাখ্যাকীবোর্ড টাইপ
TYPE_CLASS_TEXTসাধারণ টেক্সটQWERTY
TYPE_CLASS_NUMBERসুইচ নম্বরনাম্বার প্যাড
TYPE_CLASS_PHONEফোন নম্বরডায়ালপ্যাড
TYPE_CLASS_DATETIMEতারিখ/সময়ডেট/টাইম পিকার
TYPE_TEXT_VARIATION_PASSWORDপাসওয়ার্ড (হাইড)টেক্সট + হাইড
TYPE_TEXT_VARIATION_EMAIL_ADDRESSইমেইলQWERTY + @ .com
TYPE_TEXT_VARIATION_VISIBLE_PASSWORDদেখানো পাসওয়ার্ডটেক্সট + আনহাইড
TYPE_TEXT_VARIATION_URIURLQWERTY + / .com
TYPE_TEXT_FLAG_CAP_WORDSপ্রতিটি শব্দ ক্যাপিটালটেক্সট + অটো ক্যাপ
TYPE_TEXT_FLAG_MULTI_LINEমাল্টিলাইনএন্টার কী

কম্বিনেশন উদাহরণ

 সেট করুন ডটগুলো সুন্দর দেখানোর জন্য

            
  1. অ্যান্ড্রয়েডে বাটনের টেক্সট ফন্ট পরিবর্তন করা

  2. অ্যান্ড্রয়েডে TextView-এ স্প্যান ব্যবহার করে টেক্সটের রঙ পরিবর্তন করার পদ্ধতি

  3. Android-এ AsyncTask-এর জন্য টাইমআউট সেট করার পদ্ধতি

  4. Android-এ Outlook ইমেল সেটআপ করুন — দ্রুত ও সহজ গাইড