কম্পিউটার

অ্যান্ড্রয়েডে অ্যাক্টিভিটি থেকে ফ্র্যাগমেন্টে একটি ভেরিয়েবল কীভাবে পাস করবেন?


এই উদাহরণটি দেখায় যে আমি কীভাবে অ্যানড্রয়েডে অ্যাক্টিভিটি থেকে ফ্র্যাগমেন্টে একটি ভেরিয়েবল পাস করব৷

ধাপ 1 − অ্যান্ড্রয়েড স্টুডিওতে একটি নতুন প্রকল্প তৈরি করুন, ফাইল ⇒ নতুন প্রকল্পে যান এবং একটি নতুন প্রকল্প তৈরি করতে প্রয়োজনীয় সমস্ত বিবরণ পূরণ করুন৷

ধাপ 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:orientation="vertical"
   android:gravity="center"
   android:padding="2dp"
   tools:context=".MainActivity">
   <EditText
      android:id="@+id/editText"
      android:layout_marginTop="20sp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="ENTER YOUR MESSAGE" />
   <Button
      android:id="@+id/btnSendData"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Send data" />
   <FrameLayout
      android:id="@+id/frameLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
   </FrameLayout>
</LinearLayout>

ধাপ 3 − src/MainActivity.java

-এ নিম্নলিখিত কোড যোগ করুন
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
   EditText editText;
   Button button;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      FragmentManager fragmentManager = getSupportFragmentManager();
      final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      final MyFragment myFragment = new MyFragment();
      button = findViewById(R.id.btnSendData);
      editText = findViewById(R.id.editText);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Bundle b = new Bundle();
            b.putString("message", editText.getText().toString());
            myFragment.setArguments(b);
            fragmentTransaction.add(R.id.frameLayout, myFragment).commit();
         }
      });
   }
}

পদক্ষেপ 4৷ - একটি নতুন খণ্ড কার্যকলাপ তৈরি করুন এবং নিম্নলিখিত কোড যোগ করুন -

MyFragment.java −

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment {
   TextView textView;
   public MyFragment() {
   }
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
      View view = inflater.inflate(R.layout.fragment_my, container, false);
      textView = view.findViewById(R.id.textView);
      Bundle bundle = getArguments();
      String message = bundle.getString("message");
      textView.setText(message);
      return view;
   }
}

fragment_my.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="match_parent"
   android:gravity="center">
   <TextView
      android:id="@+id/textView"
      android:textSize="30dp"
      android:textStyle="bold"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
</LinearLayout>

ধাপ 5 - 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>

আসুন আপনার অ্যাপ্লিকেশন চালানোর চেষ্টা করি৷ আমি ধরে নিচ্ছি আপনি আপনার কম্পিউটারের সাথে আপনার আসল অ্যান্ড্রয়েড মোবাইল ডিভাইসটি সংযুক্ত করেছেন৷ অ্যান্ড্রয়েড স্টুডিও থেকে অ্যাপটি চালাতে, আপনার প্রোজেক্টের অ্যাক্টিভিটি ফাইলগুলির একটি খুলুন এবং টুলবার থেকে রান আইকনে ক্লিক করুন। একটি বিকল্প হিসাবে আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং তারপরে আপনার মোবাইল ডিভাইসটি পরীক্ষা করুন যা আপনার ডিফল্ট স্ক্রীন প্রদর্শন করবে –

অ্যান্ড্রয়েডে অ্যাক্টিভিটি থেকে ফ্র্যাগমেন্টে একটি ভেরিয়েবল কীভাবে পাস করবেন?


  1. কিভাবে Android কার্যকলাপ থেকে Android এ রিংটোন সেট করবেন?

  2. অ্যান্ড্রয়েডের ক্রিয়াকলাপগুলির মধ্যে কীভাবে অঙ্কনযোগ্য পাস করবেন?

  3. অ্যান্ড্রয়েড অ্যাপে একটি খণ্ড থেকে একটি কার্যকলাপ পদ্ধতি কল কিভাবে?

  4. অ্যান্ড্রয়েডে একটি কার্যকলাপ থেকে অন্য একটি কার্যকলাপ কিভাবে একটি ইমেজ পাস?