How can I expand/collapse an EditText when I click on it? - java

I want to create an animation to expand an EditText when I click on it(to a height set by me), and when it loses focus(I click on something else) it should collapse back to one line. This is what I tried, but it kinda does nothing..
public class CollapseDownAnimation extends Animation {
private EditText mDescription;
private boolean mDown;
public CollapseDownAnimation(EditText description, boolean down) {
this.mDescription=description;
this.mDown=down;
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation transformation) {
int newHeight;
if(mDown) {
newHeight=(int) (300 * interpolatedTime);
} else {
newHeight=(int) (300 * (1-interpolatedTime));
}
mDescription.getLayoutParams().height=newHeight;
mDescription.requestLayout();
}
#Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
#Override
public boolean willChangeBounds() {
return true;
}
}
EditText mDescription;
mDescription = view.findViewById(R.id.description);
CollapseDownAnimation anim = new CollapseDownAnimation(mDescription, mDescription.hasFocus());
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Log.d(TAG, "the focus is "+mDescription.hasFocus());
mDescription.setAnimation(anim);

Related

On scroll back radio getting uncheck

Here I have made a quiz app in which I use radio to select the option of question but when user scroll back most of the radio get uncheck
my adapter declear
setOption(optionA,1,pos);
setOption(optionB,2,pos);
setOption(optionC,3,pos);
setOption(optionD,4,pos);
optionA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectOption(optionA, 1, pos);
}
});
optionB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectOption(optionB, 2, pos);
}
});
optionC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectOption(optionC, 3, pos);
}
});
optionD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectOption(optionD, 4, pos);
}
});
}
private void selectOption(Button btn, int option_num, int quesId) {
if (privSelecetdB == null) {
DbQuery.g_quesList.get(quesId).setSelectedAns(option_num);
changeStatus(quesId,ANSWERED);
privSelecetdB= btn;
} else {
if(privSelecetdB.getId()==btn.getId()) {
btn.setBackground(null);
radioGroup.clearCheck();
DbQuery.g_quesList.get(quesId).setSelectedAns(-1);
changeStatus(quesId,UNANSWERED);
privSelecetdB= null;
}
else {
privSelecetdB.setBackground(null);
changeStatus(quesId,ANSWERED) ;
DbQuery.g_quesList.get(quesId).setSelectedAns(option_num);
privSelecetdB= btn;
}
}
}
private void changeStatus(int id, int status){
if(g_quesList.get(id).getStatus() != REVIEW){
g_quesList.get(id).setStatus(status);
}
}
private void setOption(RadioButton btn,int option_num,int quesId) {
if (DbQuery.g_quesList.get(quesId).getSelectedAns() == option_num) {
btn.setBackground(null);
} else {
btn.setBackground(null);
DbQuery.g_quesList.get(quesId).setSelectedAns(-1);
changeStatus(quesId,UNANSWERED);
radioGroup.clearCheck();
}
}
in the activity i use
private void setSnapHelper(){
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(QuestionView);
QuestionView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
View view = snapHelper.findSnapView(recyclerView.getLayoutManager());
quesid= recyclerView.getLayoutManager().getPosition(view);
if(g_quesList.get(quesid).getStatus() == NOT_VISITED)
g_quesList.get(quesid).setStatus(UNANSWERED);
if(g_quesList.get(quesid).getStatus() == REVIEW){
markImage.setVisibility(View.VISIBLE);
}
else{
markImage.setVisibility(View.GONE);
}
tvQuestionid.setText(String.valueOf(quesid+1)+"/" + String.valueOf(g_quesList.size()));
}
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
and declare of the database is
public static final int NOT_VISITED=0;
public static final int UNANSWERED=1;
public static final int ANSWERED=2;
public static final int REVIEW=3;
selected Ans-1,
NOT_VISITED
when the user selects the option and moves forward then it is working but when someone tries to come and change it or some of the left. The position till he move most of the option get unselected

How to repeat Android animation in java?

i have this code here. I want that the Imageview do the animation 5 times. How i need to set the setRepeatCount? How to inilize it?
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = ((ImageView) findViewById(R.id.imageView));
iv.setRotationY(0f);
//iv.animate().setDuration(10);
iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
iv.animate().rotationY(360f).setListener(null);
}
#Override
public void onAnimationCancel(Animator animation) {
}
});
The method setRepeatCount belongs to Animation class, you are using Animator.
Try this:
int times = 5;
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = findViewById(R.id.imageView);
iv.setRotationY(0f);
//iv.animate().setDuration(10);
final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
viewPropertyAnimator.rotationY(90f);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
times--;
if (times > 0) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
viewPropertyAnimator.rotationY(360f);
viewPropertyAnimator.start(); //Restart
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
});
viewPropertyAnimator.start(); //Init
}

Android animation problem - visibility change faster than animation

I have a problem with adjusting the animation to change the visibility of the view.
I've read about various tips, but the recommended solutions do not help me. Animation doesn't work smoothly - what am I doing wrong?
My code looks like this:
childRelativeLayout.setVisibility(View.GONE);
parentRelativeLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (childRelativeLayout.getVisibility() == View.VISIBLE) {
Animation slide_up = AnimationUtils.loadAnimation(context, R.anim.slide_up);
childRelativeLayout.startAnimation(slide_up);
//-------
childRelativeLayout.getLayoutTransition()
.enableTransitionType(LayoutTransition.CHANGING);
//-------
//OR
//-------
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//-------
childRelativeLayout.setVisibility(View.GONE);
} else {
Animation slide_down = AnimationUtils.loadAnimation(context, R.anim.slide_down);
childRelativeLayout.startAnimation(slide_down);
childRelativeLayout.setVisibility(View.VISIBLE);
}
}
});
slide_down.xml
<translate
android:duration="200"
android:fromYDelta="-100%"
android:toYDelta="0" />
slide_up.xml
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="-100%" />
you need to remove thread.Sleep() and add animation listner and set visibility GONE in animation end listner.
slide_up.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
childRelativeLayout.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
and for slide_down animation
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
childRelativeLayout.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I use Kotlin but in Java it will look the same
val animatorSet = AnimatorSet()
val positionAnimator = ValueAnimator.ofFloat(tv.x, 0F)
positionAnimator.duration = animationDuration
positionAnimator.addUpdateListener {
tv.x = positionAnimator.animatedValue as Float
}
val alphaAnimation = ValueAnimator.ofFloat(1F, 0F, 1F)
alphaAnimation.duration = animationDuration
alphaAnimation.addUpdateListener {
tv.alpha = alphaAnimation.animatedValue as Float
}
animatorSet.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation)
//If you need some events
}
})
positionAnimator.interpolator = AccelerateDecelerateInterpolator()
animatorSet.playTogether(positionAnimator, alphaAnimation)
animatorSet.start()
For JAVA. It's only example.
private void animate() {
animatorSet = new AnimatorSet();
ValueAnimator positionAnimator = ValueAnimator.ofFloat(0, 100F);
positionAnimator.setDuration(200);
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setX((Float) valueAnimator.getAnimatedValue());
}
});
ValueAnimator alphaAnimation = ValueAnimator.ofFloat(0f, 1F);
positionAnimator.setDuration(200);
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setAlpha((Float) valueAnimator.getAnimatedValue());
}
});
animatorSet.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation, boolean isReverse) {
}
#Override
public void onAnimationEnd(Animator animation, boolean isReverse) {
}
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
positionAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
animatorSet.playTogether(positionAnimator, alphaAnimation);
animatorSet.start();
}

Setting up an AnimationListener for an ObjectAnimator

In my program, I have an ObjectAnimator which moves an ImageView from left to right. I am trying to set up a listener which will execute a task when the ObjectAnimator is finished running. Here is the relevant section of code which I am currently using to try to accomplish this:
if (num == 350) {
nAnim = ObjectAnimator.ofFloat(gamePiece, "translationX", 0, num);
nAnim.setDuration(2125);
nAnim.start();
nAnim.addListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animator a) {
startGame(level);
}
#Override
public void onAnimationStart(Animator a) {
}
#Override
public void onAnimationCancel(Animator a) {
}
#Override
public void onAnimationRepeat(Animator a) {
}
});
When I try to run this in Android Studio, I am getting the error: MainActivity is not abstract and does not override abstract method onAnimationStart() in MainActivity. What do I have to do to fix this error?
Since you implemented AnimatorListener in your MainActivity, you must include all its abstract methods, and change nAnim.addListener(new Animat.... to nAnim.addListener(this)
#Override
public void onAnimationStart(Animator animation){
}
#Override
public void onAnimationEnd(Animator animation){
startGame(level)
}
#Override
public void onAnimationRepeat(Animator animation){
}
#Override
public void onAnimationCancel(Animator animation){
}
You should use AnimatorListener class instead of AnimationListener like below
nAnim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
Or switch to Kotlin and put animator.start() as the last line. Offcourse you don't have to implement all animations methods
val gamePiece = Button(this)
var num = 0
val animator = ObjectAnimator.ofFloat(gamePiece, View.TRANSLATION_X, 0f, num.toFloat())
animator.duration = 2125
if (num ==350){
animator.addListener(object : AnimatorListenerAdapter(){
override fun onAnimationStart(animation: Animator?) {
super.onAnimationStart(animation)
}
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
startGame(level)
}
})
}
//animator.start() should be the last line
animator.start()

How to animate TextureView?

I am trying to fade in a TextureView but for some reason its not animating. It just simply pops in the video, no fade at all and i dont really know why that is because after some research i have found that TextureView can be animated normally.
Here is my code, i hope you guys can give me a pointer in the right direction.
PS, i have left out all irrelevant code that does not concern itself with the textureview and the animation.
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener
{
private static final String TAG = MainActivity.class.getSimpleName();
private MediaPlayer mediaPlayer1;
private ArrayList<Uri> videoUris = new ArrayList<>();
private int current_video_index = 0;
private TextureView textureView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = (TextureView) findViewById(R.id.textureView);
mediaPlayer1 = new MediaPlayer();
mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer1.setOnPreparedListener(this);
mediaPlayer1.setOnCompletionListener(this);
initVideoUris();
initNewVideo();
}
private void startVideo(final Uri uri)
{
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener()
{
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
try {
mediaPlayer1.setDataSource(MainActivity.this, uri);
mediaPlayer1.setSurface(new Surface(surface));
mediaPlayer1.setOnCompletionListener(MainActivity.this);
mediaPlayer1.setOnPreparedListener(MainActivity.this);
mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer1.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
{
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
{
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface)
{
}
});
}
#Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
fadeInView(textureView);
}
#Override
public void onCompletion(MediaPlayer mp)
{
if (!moreVideosAvailable())
{
finish();
}
}
private boolean moreVideosAvailable()
{
return current_video_index < videoUris.size();
}
private void fadeInView(View view)
{
view.setAlpha(0f);
view.setVisibility(View.VISIBLE);
view.animate().alpha(1f).setDuration(2000).setListener(null).start();
}
}
I solved it by making a videoPlayerFragment that uses TextureView as the surface for displaying the video. Then simply animate the whole fragment instead of the textureView.

Categories