Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SwipeMatch
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app/src/main/java/com/example/swipematch/
app/src/main/res/layout/
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package com.example.swipematch;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private int currentIndex = 0;
private String[] names = {"Alex", "Sam", "Jamie", "Taylor", "Jordan"};
private int[] images = {
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher
};

private float xDown;
private ImageView cardImage;
private TextView cardText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cardImage = findViewById(R.id.cardImage);
    cardText = findViewById(R.id.cardText);

    updateCard();

    cardImage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    xDown = event.getX();
                    return true;
                case MotionEvent.ACTION_UP:
                    float xUp = event.getX();
                    if (xUp - xDown > 150) {
                        nextCard("Liked");
                    } else if (xDown - xUp > 150) {
                        nextCard("Disliked");
                    }
                    return true;
            }
            return false;
        }
    });
}

private void updateCard() {
    if (currentIndex < names.length) {
        cardImage.setImageResource(images[currentIndex]);
        cardText.setText(names[currentIndex]);
    } else {
        cardText.setText("No more cards!");
        cardImage.setVisibility(View.INVISIBLE);
    }
}

private void nextCard(String action) {
    cardText.setText(action + ": " + names[currentIndex]);
    cardImage.postDelayed(new Runnable() {
        @Override
        public void run() {
            currentIndex++;
            cardImage.setVisibility(View.VISIBLE);
            updateCard();
        }
    }, 500);
}

}