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

অ্যান্ড্রয়েডে ডাইনামিকভাবে ভিউ যোগ ও মুছে ফেলার পদ্ধতি


ভূমিকা

এই উদাহরণটি দেখাবে কিভাবে LayoutInflater ব্যবহার করে রানটাইমে ডাইনামিকভাবে ভিউ (EditText, Spinner, Button) যোগ ও মুছে ফেলতে হয়।

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

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: মূল লেআউট (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" 
    xmlns:app="https://schemas.android.com/apk/res-auto" 
    android:id="@+id/parent_linear_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    android:layout_margin="5dp" 
    android:orientation="vertical"> 
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="50dp" 
        android:orientation="horizontal" > 
        <EditText 
            android:id="@+id/number_edit_text" 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="5" 
            android:inputType="phone"/> 
        <Spinner 
            android:id="@+id/type_spinner" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_weight="3" 
            android:entries="@array/types" 
            android:gravity="right" /> 
        <Button 
            android:id="@+id/delete_button" 
            android:layout_width="0dp" 
            android:layout_height="40dp" 
            android:layout_weight="1" 
            android:background="@android:drawable/ic_delete" 
            android:onClick="onDelete"/> 
    </LinearLayout> 
    <Button 
        android:id="@+id/add_field_button" 
        android:layout_width="100dp" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="5dp" 
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp" 
        android:background="#555" 
        android:layout_gravity="center" 
        android:onClick="onAddField" 
        android:textColor="#FFF" 
        android:text="Add Field" 
        android:paddingLeft="5dp"/> 
</LinearLayout>

ধাপ ৩: ফিল্ড লেআউট (res/layout/field.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="https://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal"> 
    <EditText 
        android:id="@+id/number_edit_text" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="5" 
        android:inputType="phone"/> 
    <Spinner 
        android:id="@+id/type_spinner" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="3" 
        android:entries="@array/types" 
        android:gravity="right" /> 
    <Button 
        android:id="@+id/delete_button" 
        android:layout_width="0dp" 
        android:layout_height="40dp" 
        android:layout_weight="1" 
        android:background="@android:drawable/ic_delete" 
        android:onClick="onDelete"/> 
</LinearLayout>

ধাপ ৪: স্ট্রিং অ্যারে (res/values/strings.xml)

<resources> 
    <string name="app_name">Sample</string> 
    <string-array name="types"> 
        <item>Mobile</item> 
        <item>Office</item> 
        <item>Home</item> 
    </string-array> 
</resources>

ধাপ ৫: স্টাইল (res/values/styles.xml)

<resources> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
        <item name="colorPrimary">@color/colorPrimary</item> 
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
        <item name="colorAccent">@color/colorAccent</item> 
        <item name="actionBarSize">36dip</item> 
    </style> 
</resources>

ধাপ ৬: মেইন এক্টিভিটি কোড (src/MainActivity.java)

package com.example.sample; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.LinearLayout; 

public class MainActivity extends AppCompatActivity { 
    private LinearLayout parentLinearLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout); 
    } 

    public void onAddField(View v) { 
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        final View rowView = inflater.inflate(R.layout.field, null); 
        // Add Field বাটনের আগে নতুন সারি যোগ করুন
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1); 
    } 

    public void onDelete(View v) { 
        parentLinearLayout.removeView((View) v.getParent()); 
    } 
}

ধাপ ৭: মেনিফেস্ট ফাইল (AndroidManifest.xml)

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

অ্যাপ্লিকেশন চালান

অ্যাপটি চালান। "Add Field" বাটনে ক্লিক করলে নতুন সারি যোগ হবে। প্রতিটি সারির ডিলিট বাটনে ক্লিক করলে সেটি মুছে যাবে।


  1. macOS-এ Kexts অ্যাড ও রিমুভ করার উপায়

  2. Mac-এ প্রিন্টার যোগ ও সরানোর পদ্ধতি

  3. অ্যান্ড্রয়েড ফোনে গুগল একাউন্ট যোগ, সরানো ও স্যুইচ করার পদ্ধতি

  4. আইফোন গ্রুপ মেসেজিং মেনে নিন: iMessage গ্রুপে পার্টিশিপ্যান্ট যোগ ও অপসারণ