ObjectAnimator not reversing - java

I have a collection of Views able to move down the screen and subsequently reverse but now as I try to move a CardView it will go down but not back up.
When the User clicks a button, it prompts the collection of Views to drop down/fade away and when the User clicks the button again they return. In the space created by the collection of Views vanishing, a CardView drops down. I have the CardView to drop down but when the User clicks the button, the collection of views return but not the CardView - instead it just vanishes. Cheers!
MainActivity (NB: in OnCreate())
...
final View blankCard = findViewById(R.id.child_card_template);
blankCard.setVisibility(blankCard.INVISIBLE);
...
ImageButton btn = (ImageButton) findViewById(R.id.imageLeft);
btn.setOnClickListener(new View.OnClickListener() {
int click_Animation_Type = 0;
View viewTest = findViewById(R.id.child_card_template);
View viewCard = viewTest;
#Override
public void onClick(View view) {
switch(click_Animation_Type) {
case 0:
moveSecondRow();
moveCardViewLayout();
click_Animation_Type = 1;
break;
case 1:
rewindSecondRow();
rewindCardViewLayout();
click_Animation_Type = 0;
break;
}
}
public void moveCardViewLayout() {
View viewTest = findViewById(R.id.child_card_template);
ObjectAnimator viewTestMovi = ObjectAnimator.ofFloat(viewTest, "translationY", 0f, 258f);
if(viewTest.getVisibility()==View.INVISIBLE) {
viewTestMovi.setDuration(250);
viewTestMovi.start();
viewTest.setVisibility(viewTest.VISIBLE);
}
}
public void rewindCardViewLayout() {
View viewTest = findViewById(R.id.child_card_template);
ObjectAnimator viewTestMoviReverse = ObjectAnimator.ofFloat(viewTest, "translationY", 258f, 0f);
if (viewTest.getVisibility() == View.VISIBLE) {
viewTestMoviReverse.setDuration(250);
viewTestMoviReverse.start();
viewTest.setVisibility(viewCard.INVISIBLE);
//THERE'S NO ANIMATION, JUST TURNING INVISIBLE.
//Registers View is visible.
}
}
public void moveSecondRow() {
for (int i = 0; i < list.size(); i++) {
ObjectAnimator bottomRow = ObjectAnimator.ofFloat(list.get(i), "translationY", 0f, 300f);
bottomRow.setDuration(250);//set duration
Animation fadeOutAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out_anim);
bottomRow.start();//start animation
list.get(i).startAnimation(fadeOutAnimation);
list.get(i).setVisibility(View.GONE);
}
}
public void rewindSecondRow() {
for (int i = 0; i < list.size(); i++) {
ObjectAnimator bottomRow = ObjectAnimator.ofFloat(list.get(i), "translationY", 300f, 0f);
bottomRow.setDuration(300);//set duration
Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in_anim);
bottomRow.start();//start animation
list.get(i).setVisibility(View.VISIBLE);
list.get(i).startAnimation(fadeInAnimation);
}
}
});

every time click_Animation_Type starts with 0. Put click_Animation_Type = 0; layer below the setContentView.

Related

savedInstanceState changes colors based on previous user actions

I have three buttons and hichever button is selected first is marked with an X whilst the second button selected will be marked with an O and the last button is marked as X again.
If an X is marked then its colour is white, if it's a O then it is marked grey.
Now what I want to do is use the saved instance so that when I rotate the phone, the colours stay the same as they were. What is actually happening is that if I rotate the phone then which ever latest selection was made, all the text reverts to that colour.
So if I press for X and the O and rotate the phone, both X and O will be displayed as the grey colour which is O's colours.
If I then select the last X and rotate the phone, all the letters will be marked as a white colour which is X's colour.
I am unsure if it's the set colour that is causing it or that is remembers who's move it was previously and sets the colour according to that, my question is how to solve it so that all the letters keep their colour on rotation?
private boolean playerOneMove = true;
private Button[][] buttons = new Button[1][3];
private static final String TEXT_COLOR = "textColor";
private String textColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_player2);
btnObj1 = findViewById(R.id.button_00);
btnObj2 = findViewById(R.id.button_01);
btnObj3 = findViewById(R.id.button_02);
if (savedInstanceState != null) {
textColor = savedInstanceState.getString(TEXT_COLOR);
if(btnObj1 != null) {
btnObj1.setTextColor(Color.parseColor(textColor));
}
if (btnObj2 != null) {
btnObj2.setTextColor(Color.parseColor(textColor));
}
if (btnObj3 != null) {
btnObj3.setTextColor(Color.parseColor(textColor));
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = findViewById(resID);
buttons[i][j].setOnClickListener(this);
}
}
}
#Override
public void onClick(View v) {
if (!((Button) v).getText().toString().equals("")) {
return;
}
if (playerOneMove) {
((Button) v).setText("X");
textColor = "#e8e5e5";
((Button) v).setTextColor(Color.parseColor(textColor));
} else {
((Button) v).setText("O");
textColor = "#737374";
((Button) v).setTextColor(Color.parseColor(textColor));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("playerOneMove", playerOneMove);
outState.putString(TEXT_COLOR, textColor);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) { ;
super.onRestoreInstanceState(savedInstanceState);
playerOneMove = savedInstanceState.getBoolean("playerOneMove");
textColor = savedInstanceState.getString(TEXT_COLOR);
}
The behavior you see it's normal because you save a single textColor which will always be the one which was last set by the user. Instead you could simply iterate over the buttons array and save each button's text color:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
Button btn = buttons[i][j];
outState.putCharSequence(buttonID, btn.getText());
}
}
}
Then in onCreate()(remove the onRestoreInstanceState() method) restore the button's state:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_player2);
int playerX = Color.parseColor("#e8e5e5");
int playerO = Color.parseColor("#737374");
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = findViewById(resID);
buttons[i][j].setOnClickListener(this);
if (savedInstanceState != null) {
String btnState = savedInstanceState.getCharSequence(buttonID);
if (btnState.equals("X")) {
// this is player X
buttons[i][j].setTextColor(playerX);
} else if (btnState.equals("O")) {
// this is player O
buttons[i][j].setTextColor(playerO);
} else {
// unclicked btn, do you have another color?
}
}
}
}
If you have a greater number of buttons, it may make more sense to group the buttons statuses in a list and save that instead of the individual buttons states.

Pause AnimationDrawable and resume from the same frame

I am trying to animate a set of images using AnimationDrawable. The user should have the ability to pause/resume the animation on a click of a button. I am doing so, using setVisible(boolean visible, boolean restart). Not completely sure if I understand the documentation correctly, but every time pause is pressed, the animation starts from the beginning.
Is there any way to resume the animation from the same frame?
From AnimationDrawable API:
...If restart is false, the drawable will resume from the most recent frame
Just to be clear:
activityAnimation.setVisible(false, false); Should stop the animation.
activityAnimation.setVisible(true, false); Should resume the animation from the same frame (it doesn't).
Here is my code:
The animation start() is called in onWindowFocusChanged.
The animation and the buttons are created in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
ImageView activityImage = (ImageView) findViewById(R.id.activity_image);
activityImage.setBackgroundResource(R.drawable.anim);
activityAnimation = (AnimationDrawable) activityImage.getBackground();
....
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageButton b = (ImageButton) v;
if ((Integer)b.getTag() == R.drawable.pause) {
b.setImageResource(R.drawable.play);
b.setTag(R.drawable.play);
activityAnimation.setVisible(false, false);
} else {
b.setImageResource(R.drawable.pause);
b.setTag(R.drawable.pause);
activityAnimation.setVisible(true, false);
}
}
});...
}
This is the content of the XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/one" android:duration="1000" />
<item android:drawable="#drawable/two" android:duration="1000" />
<item android:drawable="#drawable/three" android:duration="1000" />
<item android:drawable="#drawable/four" android:duration="1000" />
</animation-list>
I have noticed several of these old posts, but non had the answer.
How to reset AnimationDrawable (The other side of this issue)
How do I pause frame animation using AnimationDrawable? [Closed]
To pause and resume AnimationDrawable, simply customize it and handle with only one variable.
class CustomAnimationDrawable1() : AnimationDrawable() {
private var finished = false
private var animationFinishListener: AnimationFinishListener? = null
private var isPause = false
private var currentFrame = 0
fun setFinishListener(animationFinishListener: AnimationFinishListener?) {
this.animationFinishListener = animationFinishListener
}
interface AnimationFinishListener {
fun onAnimationFinished()
}
override fun selectDrawable(index: Int): Boolean {
val ret = if (isPause) {
super.selectDrawable(currentFrame)
} else {
super.selectDrawable(index)
}
if (!isPause) {
currentFrame = index
if (index == numberOfFrames - 1) {
if (!finished && ret && !isOneShot) {
finished = true
if (animationFinishListener != null) animationFinishListener!!.onAnimationFinished()
}
}
}
return ret
}
fun pause() {
isPause = true
}
fun resume() {
isPause = false
}
}
Since I needed a timer on the screen as well, the answer in my case was simply to create a runnable replace the ImageView background every second.
Here is the general idea:
Create the runnable, ImageView and few helpers:
ImageView exImage = null;
long startTime = 0;
long pauseTime = 0;
long pauseStartTime = 0;
List<Integer> animList = new ArrayList<>();
int animIt = 0;
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime - pauseTime;
double dSecs = (double) (millis / 100);
int pace = 10;
if (dSecs % pace == 0.0 && !animList.isEmpty()) {
animIt = (animIt == animList.size() - 1) ? 0 : animIt + 1;
exImage.setBackgroundResource(animList.get(animIt));
}
timerHandler.postDelayed(this, 100);
}
};
In OnCreate bind the pause and play buttons, fire the timer and set the first ImageView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
// Set the image view
exImage = (ImageView) findViewById(R.id.image_zero);
exImage.setBackgroundResource(R.drawable.fe_0);
// Start the timer
timerHandler.postDelayed(timerRunnable, 0);
// Bind the buttons
ImageButton pp = (ImageButton) findViewById(R.id.button_play_pause_toggle);
pp.setImageResource(R.drawable.pause);
pp.setTag(R.drawable.pause);
pp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageButton pp = (ImageButton) v;
//Pause
if ((Integer) pp.getTag() == R.drawable.pause) {
pauseStartTime = System.currentTimeMillis();
timerHandler.removeCallbacks(timerRunnable);
} else { // Resume
pauseTime += System.currentTimeMillis() - pauseStartTime;
timerHandler.postDelayed(timerRunnable, 0);
}
}
});
}

How to start activities by swiping different listview items in android?

I am developing an application, and I need a ListView like contact ListView of Samsung Galaxy S
I could implement the swipe functionality with the help of Android - ListView slide left/right like Samsung contact ListView post.
I need to start different activities when different listview items are swiped. Can anyone assist me? Any help is appreciated.
This is my listview activity.
public class HomeActivity extends Activity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_listview);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// Create a ListView-specific touch listener. ListViews are given special treatment because by default they handle touches for their list items... i.e. they're in charge of drawing
// the pressed state (the list selector), handling list item clicks, etc.
SwipeListViewTouchListener touchListener =
new SwipeListViewTouchListener(
listView,
new SwipeListViewTouchListener.OnSwipeCallback() {
#Override
public void onSwipeLeft(ListView listView, int [] reverseSortedPositions) {
// Log.i(this.getClass().getName(), "swipe left : pos="+reverseSortedPositions[0]);
// TODO : YOUR CODE HERE FOR LEFT ACTION
}
#Override
public void onSwipeRight(ListView listView, int [] reverseSortedPositions) {
// Log.i(ProfileMenuActivity.class.getClass().getName(), "swipe right : pos="+reverseSortedPositions[0]);
// TODO : YOUR CODE HERE FOR RIGHT ACTION
}
},
true, // example : left action = dismiss
false); // example : right action without dismiss animation
listView.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
listView.setOnScrollListener(touchListener.makeScrollListener());
}}
This is my SwipeListViewTouchListener class.
public class SwipeListViewTouchListener implements View.OnTouchListener {
// Cached ViewConfiguration and system-wide constant values
private int mSlop;
private int mMinFlingVelocity;
private int mMaxFlingVelocity;
private long mAnimationTime;
// Fixed properties
private ListView mListView;
private OnSwipeCallback mCallback;
private int mViewWidth = 1; // 1 and not 0 to prevent dividing by zero
private boolean dismissLeft = true;
private boolean dismissRight = true;
// Transient properties
private List< PendingSwipeData > mPendingSwipes = new ArrayList< PendingSwipeData >();
private int mDismissAnimationRefCount = 0;
private float mDownX;
private boolean mSwiping;
private VelocityTracker mVelocityTracker;
private int mDownPosition;
private View mDownView;
private boolean mPaused;
/**
* The callback interface used by {#link SwipeListViewTouchListener} to inform its client
* about a successful swipe of one or more list item positions.
*/
public interface OnSwipeCallback {
/**
* Called when the user has swiped the list item to the left.
*
* #param listView The originating {#link ListView}.
* #param reverseSortedPositions An array of positions to dismiss, sorted in descending
* order for convenience.
*/
void onSwipeLeft(ListView listView, int[] reverseSortedPositions);
void onSwipeRight(ListView listView, int[] reverseSortedPositions);
}
/**
* Constructs a new swipe-to-action touch listener for the given list view.
*
* #param listView The list view whose items should be dismissable.
* #param callback The callback to trigger when the user has indicated that she would like to
* dismiss one or more list items.
*/
public SwipeListViewTouchListener(ListView listView, OnSwipeCallback callback) {
ViewConfiguration vc = ViewConfiguration.get(listView.getContext());
mSlop = vc.getScaledTouchSlop();
mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();
mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
mAnimationTime = listView.getContext().getResources().getInteger(
android.R.integer.config_shortAnimTime);
mListView = listView;
mCallback = callback;
}
/**
* Constructs a new swipe-to-action touch listener for the given list view.
*
* #param listView The list view whose items should be dismissable.
* #param callback The callback to trigger when the user has indicated that she would like to
* dismiss one or more list items.
* #param dismissLeft set if the dismiss animation is up when the user swipe to the left
* #param dismissRight set if the dismiss animation is up when the user swipe to the right
* #see #SwipeListViewTouchListener(ListView, OnSwipeCallback, boolean, boolean)
*/
public SwipeListViewTouchListener(ListView listView, OnSwipeCallback callback, boolean dismissLeft, boolean dismissRight) {
this(listView, callback);
this.dismissLeft = dismissLeft;
this.dismissRight = dismissRight;
}
/**
* Enables or disables (pauses or resumes) watching for swipe-to-dismiss gestures.
*
* #param enabled Whether or not to watch for gestures.
*/
public void setEnabled(boolean enabled) {
mPaused = !enabled;
}
/**
* Returns an {#link android.widget.AbsListView.OnScrollListener} to be added to the
* {#link ListView} using
* {#link ListView#setOnScrollListener(android.widget.AbsListView.OnScrollListener)}.
* If a scroll listener is already assigned, the caller should still pass scroll changes
* through to this listener. This will ensure that this
* {#link SwipeListViewTouchListener} is paused during list view scrolling.</p>
*
* #see {#link SwipeListViewTouchListener}
*/
public AbsListView.OnScrollListener makeScrollListener() {
return new AbsListView.OnScrollListener() {#
Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
setEnabled(scrollState != AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
}
#
Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {}
};
}
#
Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (mViewWidth < 2) {
mViewWidth = mListView.getWidth();
}
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
{
if (mPaused) {
return false;
}
// TODO: ensure this is a finger, and set a flag
// Find the child view that was touched (perform a hit test)
Rect rect = new Rect();
int childCount = mListView.getChildCount();
int[] listViewCoords = new int[2];
mListView.getLocationOnScreen(listViewCoords);
int x = (int) motionEvent.getRawX() - listViewCoords[0];
int y = (int) motionEvent.getRawY() - listViewCoords[1];
View child;
for (int i = 0; i < childCount; i++) {
child = mListView.getChildAt(i);
child.getHitRect(rect);
if (rect.contains(x, y)) {
mDownView = child;
break;
}
}
if (mDownView != null) {
mDownX = motionEvent.getRawX();
mDownPosition = mListView.getPositionForView(mDownView);
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(motionEvent);
}
view.onTouchEvent(motionEvent);
return true;
}
case MotionEvent.ACTION_UP:
{
if (mVelocityTracker == null) {
break;
}
float deltaX = motionEvent.getRawX() - mDownX;
mVelocityTracker.addMovement(motionEvent);
mVelocityTracker.computeCurrentVelocity(500); // 1000 by defaut but it was too much
float velocityX = Math.abs(mVelocityTracker.getXVelocity());
float velocityY = Math.abs(mVelocityTracker.getYVelocity());
boolean swipe = false;
boolean swipeRight = false;
if (Math.abs(deltaX) > mViewWidth / 2) {
swipe = true;
swipeRight = deltaX > 0;
} else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
swipe = true;
swipeRight = mVelocityTracker.getXVelocity() > 0;
}
if (swipe) {
// sufficent swipe value
final View downView = mDownView; // mDownView gets null'd before animation ends
final int downPosition = mDownPosition;
final boolean toTheRight = swipeRight;
++mDismissAnimationRefCount;
mDownView.animate()
.translationX(swipeRight ? mViewWidth : -mViewWidth)
.alpha(0)
.setDuration(mAnimationTime)
.setListener(new AnimatorListenerAdapter() {#
Override
public void onAnimationEnd(Animator animation) {
performSwipeAction(downView, downPosition, toTheRight, toTheRight ? dismissRight : dismissLeft);
}
});
} else {
// cancel
mDownView.animate()
.translationX(0)
.alpha(1)
.setDuration(mAnimationTime)
.setListener(null);
}
mVelocityTracker = null;
mDownX = 0;
mDownView = null;
mDownPosition = ListView.INVALID_POSITION;
mSwiping = false;
break;
}
case MotionEvent.ACTION_MOVE:
{
if (mVelocityTracker == null || mPaused) {
break;
}
mVelocityTracker.addMovement(motionEvent);
float deltaX = motionEvent.getRawX() - mDownX;
if (Math.abs(deltaX) > mSlop) {
mSwiping = true;
mListView.requestDisallowInterceptTouchEvent(true);
// Cancel ListView's touch (un-highlighting the item)
MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
(motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
mListView.onTouchEvent(cancelEvent);
}
if (mSwiping) {
mDownView.setTranslationX(deltaX);
mDownView.setAlpha(Math.max(0f, Math.min(1f,
1f - 2f * Math.abs(deltaX) / mViewWidth)));
return true;
}
break;
}
}
return false;
}
class PendingSwipeData implements Comparable < PendingSwipeData > {
public int position;
public View view;
public PendingSwipeData(int position, View view) {
this.position = position;
this.view = view;
}
#
Override
public int compareTo(PendingSwipeData other) {
// Sort by descending position
return other.position - position;
}
}
private void performSwipeAction(final View swipeView, final int swipePosition, boolean toTheRight, boolean dismiss) {
// Animate the dismissed list item to zero-height and fire the dismiss callback when
// all dismissed list item animations have completed. This triggers layout on each animation
// frame; in the future we may want to do something smarter and more performant.
final ViewGroup.LayoutParams lp = swipeView.getLayoutParams();
final int originalHeight = swipeView.getHeight();
final boolean swipeRight = toTheRight;
ValueAnimator animator;
if (dismiss)
animator = ValueAnimator.ofInt(originalHeight, 1).setDuration(mAnimationTime);
else
animator = ValueAnimator.ofInt(originalHeight, originalHeight - 1).setDuration(mAnimationTime);
animator.addListener(new AnimatorListenerAdapter() {#
Override
public void onAnimationEnd(Animator animation) {
--mDismissAnimationRefCount;
if (mDismissAnimationRefCount == 0) {
// No active animations, process all pending dismisses.
// Sort by descending position
Collections.sort(mPendingSwipes);
int[] swipePositions = new int[mPendingSwipes.size()];
for (int i = mPendingSwipes.size() - 1; i >= 0; i--) {
swipePositions[i] = mPendingSwipes.get(i).position;
}
if (swipeRight)
mCallback.onSwipeRight(mListView, swipePositions);
else
mCallback.onSwipeLeft(mListView, swipePositions);
ViewGroup.LayoutParams lp;
for (PendingSwipeData pendingDismiss: mPendingSwipes) {
// Reset view presentation
pendingDismiss.view.setAlpha(1f);
pendingDismiss.view.setTranslationX(0);
lp = pendingDismiss.view.getLayoutParams();
lp.height = originalHeight;
pendingDismiss.view.setLayoutParams(lp);
}
mPendingSwipes.clear();
}
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {#
Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
lp.height = (Integer) valueAnimator.getAnimatedValue();
swipeView.setLayoutParams(lp);
}
});
mPendingSwipes.add(new PendingSwipeData(swipePosition, swipeView));
animator.start();
}}
Okay so I tried the code out in Android Studio. Figured that in order for you to do a specific activity based on the swiped item, best way for you is be able to receive the position of the item that was swiped. So I checked the SwipeListViewTouchListener and found the specific part of the for performSwipeAction and noticed the parameter final int swipePosition and modified it...
private void performSwipeAction(final View swipeView, final int swipePosition, boolean toTheRight, boolean dismiss) {
// Animate the dismissed list item to zero-height and fire the dismiss callback when
// all dismissed list item animations have completed. This triggers layout on each animation
// frame; in the future we may want to do something smarter and more performant.
final ViewGroup.LayoutParams lp = swipeView.getLayoutParams();
final int originalHeight = swipeView.getHeight();
final boolean swipeRight = toTheRight;
ValueAnimator animator;
if (dismiss)
animator = ValueAnimator.ofInt(originalHeight, 1).setDuration(mAnimationTime);
else
animator = ValueAnimator.ofInt(originalHeight, originalHeight - 1).setDuration(mAnimationTime);
animator.addListener(new AnimatorListenerAdapter() {
#
Override
public void onAnimationEnd(Animator animation) {
--mDismissAnimationRefCount;
if (mDismissAnimationRefCount == 0) {
// No active animations, process all pending dismisses.
// Sort by descending position
Collections.sort(mPendingSwipes);
int[] swipePositions = new int[mPendingSwipes.size()];
for (int i = mPendingSwipes.size() - 1; i >= 0; i--) {
swipePositions[i] = mPendingSwipes.get(i).position;
}
if (swipeRight) {
mCallback.onSwipeRight(mListView, swipePositions);
Log.d("SAMPLE", "SAMPLE IN SWIPE -- " + swipePosition);
}else {
Log.d("SAMPLE", "SAMPLE IN SWIPE -- " + swipePosition);
mCallback.onSwipeLeft(mListView, swipePositions);
}
ViewGroup.LayoutParams lp;
for (PendingSwipeData pendingDismiss : mPendingSwipes) {
// Reset view presentation
pendingDismiss.view.setAlpha(1f);
pendingDismiss.view.setTranslationX(0);
lp = pendingDismiss.view.getLayoutParams();
lp.height = originalHeight;
pendingDismiss.view.setLayoutParams(lp);
}
mPendingSwipes.clear();
}
}
});
And was able to receive the proper position for the listview item (0 for the topmost listview item, and so on). Now all you have to do is change the parameters in the callback -- adding the swipePosition, then using it to refer to the listviewitem and starting the activity based on that. :D Hope this helps you in some way. :)

2 players Tic Tac toe game

I am trying to make a two player tic-tac-toe game. When the first player clicks the OnClick method with mark X on the button, but I stuck - I don't know how to make the onClick() to detect when the 2nd player clicked and how to mark O on the button ..plz help.. My .java is below
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBoard();
}
int c[][];
int i, j, k = 0;
Button b[][];
TextView textView;
private void setBoard() {
b = new Button[4][4];
c = new int[4][4];
// Button = (TextView) findViewById(R.id.newgame);
b[1][3] = (Button) findViewById(R.id.one);
b[1][2] = (Button) findViewById(R.id.two);
b[1][1] = (Button) findViewById(R.id.three);
b[2][3] = (Button) findViewById(R.id.four);
b[2][2] = (Button) findViewById(R.id.five);
b[2][1] = (Button) findViewById(R.id.six);
b[3][3] = (Button) findViewById(R.id.seven);
b[3][2] = (Button) findViewById(R.id.eight);
b[3][1] = (Button) findViewById(R.id.nine);
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++)
c[i][j] = 2;
}
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
b[i][j].setOnClickListener(new MyClickListener(i, j));
if (!b[i][j].isEnabled()) {
b[i][j].setText("o");
b[i][j].setEnabled(true);
}
}
}
}
class MyClickListener implements View.OnClickListener {
int x;
int y;
public MyClickListener(int x, int y) {
this.x = x;
this.y = y;
}
public void onClick(View view) {
if (b[x][y].isEnabled()) {
b[x][y].setEnabled(false);
b[x][y].setText("X");
c[x][y] = 0;
textView.setText("");
//WHAT NEXT
}
}
}
}
}
Simply use a boolean to detetct the click. Say a boolean isFirstPlayerTurn is true when button is clicked first time. Make it false for second turn. Do this for all turns and you will know which player is clicking the button.
Example:
private boolean isFirstPlayerTurn = true;
...
void onclick() {
if (isFirstPlayerTurn) {
// clicked by player 1
isFirstPlayerTurn = false;
} else {
// clicked by player 2
isFirstPlayerTurn = true;
}
}
You should probably add a new variable
int playerTurn;
which should initially be set to 1. On the first onClick() execution, the value of playerTurn is 1, so place an X, and change the playerTurn to 2. On the second click, when you check the value of playerTurn, you will place an O, and change its value to 1. And this keep on going.
int player = 1;
public void onClick(View view){
if (b[x][y].isEnabled() {
b[x][y].setEnabled(false);
if (player == 1){
b[x][y].setText("X");
c[x][y] = 0;
player = 1;
} else {
b[x][y].setText("O");
c[x][y] = 1;
player = 2;
}
}
}

Programmatically scroll a ScrollView to focus on a Button or a view - Android

I have an Activity that has a ScrollView of 125 buttons. The 125 buttons are Levels in my game that slowly get unlocked (think of Candy Crush Saga). Right now if the user is on the level, let's say, 88 of 125 then the user has to manually scroll all the way down to button 88.
My question is, how would I make it so that when the Activity loads, it automatically scrolls down and centers the ScrollView on a certain button?
Below is my code of how the buttons are created and populated programmatically.
ScrollView scrollView;
#Override
public void onCreate(Bundle savedInstanceState) {
LinearLayout lay = (LinearLayout)findViewById(R.id.linearlayout);
for(int i = 0; i < 125; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
params.weight = 1;
params.gravity = Gravity.CENTER;
if(i == 0) {
TextView textview = new TextView(this);
textview.setLayoutParams(params);
textview.setTextSize(15);
textview.setText(c.getResources().getString(R.string.l1to5));
TextView textview2 = new TextView(this);
textview2.setLayoutParams(params);
textview.setTextSize(15);
textview2.setText(c.getResources().getString(R.string.goal100));
TextView textview3 = new TextView(this);
textview3.setLayoutParams(params);
textview.setTextSize(15);
textview3.setText(c.getResources().getString(R.string.catRandom));
if((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE) {
textview.setTextSize(22);
textview2.setTextSize(22);
textview3.setTextSize(22);
} else if((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE) {
textview.setTextSize(22);
textview2.setTextSize(22);
textview3.setTextSize(22);
}
lay.addView(textview);
lay.addView(textview2);
lay.addView(textview3);
View ruler = new View(this);
ruler.setBackgroundColor(0xFFC2BEBF);
lay.addView(ruler, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2));
}
if(i == 5) {
// ....
}
Button button = new Button(this);
int _id = getResources().getIdentifier("b" + (i + 1), "id", this.getPackageName());
button.setTag(_id);
button.setLayoutParams(params);
if((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE) {
button.setTextSize(22);
} else if((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE) {
button.setTextSize(22);
}
button.setText("Level " + (i + 1));
final int x = i + 1;
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
singleton.setLevelJustPlayed(x);
// ...
} else {
// ...
Intent intent = new Intent(Levels.this, Categories.class);
startActivity(intent);
}
}
});
lay.addView(button);
if(singleton.getLevel() >= (i + 1)) {
button.setEnabled(true);
} else {
button.setEnabled(false);
}
}
// end the onCreate() method
try
yourScrollView.scrollTo(0, (int) button.getY());
For Kotlin user
your_scrollview.post( { your_scrollview.scrollTo(0, your_view_inside_SV.getY().toInt()) })

Categories

Resources