কম্পিউটার

অ্যান্ড্রয়েডে বাম/ডান এবং উপরে/নীচের মধ্যে সোয়াইপ দিক কীভাবে সনাক্ত করবেন?


এই উদাহরণটি দেখায় কিভাবে আমি অ্যান্ড্রয়েডে বাম/ডান এবং উপরে/নীচের মধ্যে সোয়াইপ দিক সনাক্ত করতে পারি।

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

ধাপ 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:id="@+id/relativeLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="16dp"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="Swipe to Detect swipe Event"
      android:textSize="24sp"
      android:textStyle="bold" />
</RelativeLayout>

ধাপ 3 − src/MainActivity.java

-এ নিম্নলিখিত কোড যোগ করুন
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   OnSwipeTouchListener onSwipeTouchListener;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      onSwipeTouchListener = new OnSwipeTouchListener(this, findViewById(R.id.relativeLayout));
   }
   public static class OnSwipeTouchListener implements View.OnTouchListener {
      private final GestureDetector gestureDetector;
      Context context;
      OnSwipeTouchListener(Context ctx, View mainView) {
         gestureDetector = new GestureDetector(ctx, new GestureListener());
         mainView.setOnTouchListener(this);
         context = ctx;
      }
      @Override
      public boolean onTouch(View v, MotionEvent event) {
         return gestureDetector.onTouchEvent(event);
      }
      public class GestureListener extends
      GestureDetector.SimpleOnGestureListener {
         private static final int SWIPE_THRESHOLD = 100;
         private static final int SWIPE_VELOCITY_THRESHOLD = 100;
         @Override
         public boolean onDown(MotionEvent e) {
            return true;
         }
         @Override
         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
               float diffY = e2.getY() - e1.getY();
               float diffX = e2.getX() - e1.getX();
               if (Math.abs(diffX) > Math.abs(diffY)) {
                  if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                     if (diffX > 0) {
                        onSwipeRight();
                     } else {
                        onSwipeLeft();
                     }
                     result = true;
                  }
               }
               else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                  if (diffY > 0) {
                     onSwipeBottom();
                  } else {
                     onSwipeTop();
                  }
                  result = true;
               }
            }
            catch (Exception exception) {
               exception.printStackTrace();
            }
            return result;
         }
      }
      void onSwipeRight() {
         Toast.makeText(context, "Swiped Right", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeRight();
      }
      void onSwipeLeft() {
         Toast.makeText(context, "Swiped Left", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeLeft();
      }
      void onSwipeTop() {
         Toast.makeText(context, "Swiped Up", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeTop();
      }
      void onSwipeBottom() {
         Toast.makeText(context, "Swiped Down", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeBottom();
      }
      interface onSwipeListener {
         void swipeRight();
         void swipeTop();
         void swipeBottom();
         void swipeLeft();
      }
      onSwipeListener onSwipe;
   }
}

পদক্ষেপ 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>

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

অ্যান্ড্রয়েডে বাম/ডান এবং উপরে/নীচের মধ্যে সোয়াইপ দিক কীভাবে সনাক্ত করবেন?


  1. অ্যান্ড্রয়েডে গুগল ম্যাপে স্পর্শ এবং এর অবস্থান কীভাবে সনাক্ত করবেন?

  2. অ্যান্ড্রয়েডে ডান-থেকে-বামে এবং বাম-থেকে-ডানে সোয়াইপ অঙ্গভঙ্গি কীভাবে পরিচালনা করবেন?

  3. আইফোন এবং অ্যান্ড্রয়েডের মধ্যে ফাইলগুলি কীভাবে ভাগ করবেন

  4. কিভাবে কম্পিউটার এবং অ্যান্ড্রয়েড ফোনের মধ্যে ফাইল স্থানান্তর করা যায়