Make button slide out of screen - java

I am trying to create a welcome screen for a project. The activity is called Activity_WelcomeScreen. There is a button called playButton near the centre of the screen. I want the button to slide down to bottom of the screen when the user clicks on it while the intent changes the activity to Main Activity. The fade in and fade out are working but the playButton's animation doesn't work.
To confirm that I didn't make a mistake in the animation res file, I copied the fade out animation code from its file and put it in the slide_out_to_bottom file, and commented out the intent part. But still nothing happened.
I've tried using getBaseContext() instead of getApplicationContext() but that didn't work either.
This leads me to think I'm passing the context wrong, but if thats not the case, please correct me.
So if anybody knows how to make this work, please put it down below. Any help is appreciated.
This is the animation resource file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="1500"
/>
</set>
This is the onClickListener for the playButton:
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
slideOutToBottom = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_to_bottom);
playButton.setAnimation(slideOutToBottom);
intentWelcomeToMain = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intentWelcomeToMain);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});

Related

Why Alpha-animation is reset?

I have xml for alpha-hide animation:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.15"
android:duration="200" />
</set>
I have a code for launch this animation when click on the floating action button:
addDiaryMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listview.startAnimation(AnimationUtils.loadAnimation(Diarys.this, R.anim.alpha_hide));
}
});
And that's what happens. When you click on the button, the animation starts, but after a couple of seconds, the ListView loses its transparency. And this happens only if I click on the button immediately after the start of the activity. If after the start of the activity a couple of seconds pass, then the animation works as it should.
I don't know why it happens. Please help me to fix it.

Android Slide Animation within layout

How can I do this animation in my activity?
when you click a button in the layout, it will slide like this(can't post too many links and picture yet
I already tried doing tabbed but its not the one i'm looking for.
its my first time doing, I only know how to have slide animation between activity
Thanks in Advance
EDIT*
I want to know how to get this effect
http://i.stack.imgur.com/nJtFw.jpg
You can use a fragment for the central layout and then use Fragment animations like this
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
DetailsFragment newFragment = DetailsFragment.newInstance();
ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");
ft.commit();
Where the xml file for animation could look like.
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-1280"
android:valueTo="0"
android:duration="500"/>
</set>
There are many other animations you can apply, you can find more resources here

ImageButton background color change onClick

First-time Android dev, though have used C# and Java in the past.
Trying to make a simple, Windows 8-like GUI. At the moment I have one tile (ImageButton) with a background color set in activity_main.xml.
<ImageButton
android:id="#+id/btn1"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#FF0000"
android:onClick="changeColor"/>
And I have a function to change the color in MainActivity.java.
public void changeColor(){
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setBackgroundColor(Color.GREEN);
}
Compiles fine, but every time I click the red square, the app crashes.
I'm assuming there's something fundemental about how Android is developed that I'm missing which is leading to a very obvious mistake. Is there a better way to be doing this rather than ImageButtons?
Thanks!
Compiles fine, but every time I click the red square, the app crashes.
Because when adding android:onClick in xml then method must be public and accept a View as its only parameter which we want to call on View click:
public void changeColor(View view){
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setBackgroundColor(Color.GREEN);
}
It's better to create a selector and set it as backgraund to the button.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/button_pressed" />
<item android:color="#color/button_normal" />
</selector>
Hope it'll help you.
P.S. useful link
Is there a better way to be doing this rather than ImageButtons?
Yes, ImageButtons are mainly for creating a 'clickable' image. If you simply want a colored button, a regular Button will do fine.
You can do this by setting your Buttons background right from XML using a selector with a Drawable state list.
A good example can be found here: https://stackoverflow.com/a/3882151/1683141 (a color will also qualify as a Drawable)
Why isn't my current code working?
You should add the view as parameter to your method like this:
public void changeColor(View view){
view.setBackgroundColor(Color.GREEN);
}

Android: AlphaAnimation - no duration

I just want to create a fade out effect on a VideoView object.
The problem: Although I did set a certain duration for the animation, it looks like there's no duration. The VideoView object disappears immediately..
Things I've already tried:
AlphaAnimation anim = new AlphaAnimation(1, 0);
anim.setDuration(2000);
anim.setFillAfter(true);
mVideoView.setVisibility(View.VISIBLE);
mVideoView.startAnimation(anim);
the second try:
Animation anim = AnimationUtils.loadAnimation(this, R.anim.fadeout);
anim.setFillAfter(true);
mVideoView.startAnimation(anim);
in fadeout.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"
/>
</set>
I'd be glad if someone could help me here.
Ok, I'm not entirely sure VideoView supports this kind of animation.
I did a workaround by fading in a black ImageView object...yeah, it's not best solution.
But the effect is the same :D
Another way would be to create a screenshot, deactivate the VideoView and then to let the screenshot fade out.

Slide in another XML layout when button is clicked

I have a main.xml layout file and in my Java code, I am doing some calculation taken from the main layout and wanting to display in result.xml layout file. I know I can use the anim folder to hold the animation to slidei n the result layout like the following:
left to right:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
right to left:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700" />
</set>
I was wondering how to implement the above so when the button is clicked on the main layout file, the result layout file slides in from the right. And also if I wanted to go back to the main layout I can press a button (added on result layout) to slide right the main layout file
You can also use this function : overridePendingTransition which can define an animation for the upcoming activity and an other animation for the activity which exit.
Use it like this, in your second Activity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(android.R.anim.slide_out_right, 0);
setContentView(R.layout.myLayout);
first argument is for the animation enter, and 0 is for no animation.
And for an animation when you leave your second activity :
#Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.enterAnim, R.anim.leaveAnim);
}
You can always use default stock animations supplied by Android framework.
Heres an example code:
boolean isFirstXml=evaluatingConditionFunction();
LayoutInflater inflator=getLayoutInflater();
View view=inflator.inflate(isFirstXml?R.layout.myfirstxml:R.layout.myseconxml, null, false);
view.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
setContentView(view);
Call this from any of your activity which holds your Parent View.
For custom animations you can visit developer docs. Heres the documentation link.

Categories