I'm using the following code for a slide in / slide out toggle for an AppBarLayout.
public void showToolbar(boolean show) {
if (appBar == null) {
Log.e(TAG, "showToolbar: Toolbar is null");
return;
}
boolean toolbarShown = Utils.isViewVisible(appBar);
Log.d(TAG, "showToolbar: shown:" +shown);
boolean changed = (show != toolbarShown);
if (changed) {
if (show) {
Log.d(TAG, "showToolbar: showing");
appBar.setVisibility(View.VISIBLE);
appBar.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
appBar.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animator) { }
#Override
public void onAnimationCancel(Animator animator) { }
#Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
} else {
Log.d(TAG, "showToolbar: hiding");
appBar.animate()
.translationY(-toolbar.getBottom())
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) { }
#Override
public void onAnimationEnd(Animator animator) {
appBar.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationCancel(Animator animator) { }
#Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
}
} else {
Log.d(TAG, "showToolbar: no change");
}
}
The animation works perfectly except for the first time showToolbar(true) is called to show the toolbar. The view just shows up without animation the first time. I have scoured the site and found similar questions but the solutions doesn't seem to be working for me.
It might be worth noting that this happens only when we want the appBar to be hidden first. My guess is that maybe for the animation to
Update 1:
public static boolean isViewVisible(View view) {
if (View.VISIBLE == view.getVisibility()) return true;
else return false;
}
Update 2
I have removed isViewWithinScreenBounds() method because that check isn't really needed.
Be sure to set the initial values for visibility and translationY.
If you want your toolbar to be initially hidden and show up with the first animation, be sure to set android:visibility="invisible" and NOT "gone", and negative android:translationY like -56dp.
Try with this
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:translationY="-120dp"
android:layout_height="120dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SOME TEXT HERE" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Activity code
#BindView(R.id.app_bar)
AppBarLayout appBar;
#BindView(R.id.toolbar)
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animateToolbar(false); //Just for testing purpose - delay execute of function animateToolbar() for 4 seconds
}
}, 4000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
animateToolbar(true);
return super.onCreateOptionsMenu(menu);
}
private void animateToolbar(boolean show) {
if (show) {
// As you can see in the xml layout initial position of the appBar is translated by its height to the top of the screen "-" sign
// Slide int from out of the screen to initial position -> from -120 dp (height of the app bar, see xml) to 0 dp
appBar.animate()
.translationY(0)
.setDuration(1000)
.start();
} else {
// Slide out from initial position to the top of the screen -> from 0 dp to -120 dp (height of the app bar, see xml)
appBar.animate()
.translationY(-appBar.getHeight())
.setDuration(1000)
.start();
}
}
Related
In my application I have Toolbar in the MainActivity and inside the MainActivity I have a ViewPager to show 4 fragment.
The toolbar contains some images (button).
I want in one of these fragments to hide the image from the toolbar.
I wrote the code below, but it hides the image in all the fragments.
My code:
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
troy.setVisibility(View.GONE);
}
}, 50);
}
}
I want to hide it just in my current fragment, not all of them.
How can I do it?
You can use addOnPageChangeListener() of your ViewPager
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 0) { // Condition may vary according to your needs...
troy.setVisibility(View.GONE);
} else {
troy.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I'm not using anchor, it should work.
Inicially, the visibility is gone (set by XML). When I press a button, it becomes visible (until here, works). Then, when I press another button, it should becomes gone, but nothing happens.
Reduced XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/tbTela3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ToolBar"
app:titleTextColor="#757575"
app:subtitleTextColor="#757575" />
<android.support.design.widget.CoordinatorLayout
android:id="#+id/cLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tbTela3">
<ScrollView>
[...]
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:elevation="8dp"
android:src="#drawable/replay"
android:visibility="gone"/>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
Reduced Main Activity:
public void ZoomIn() {
[...]
zoomIn.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation anim) {}
public void onAnimationRepeat(Animation anim) {}
public void onAnimationEnd(Animation anim) {
fab.setVisibility(View.VISIBLE); // WORKS FINE
}
});
fab.startAnimation(zoomIn);
}
[...]
public void Clear() {
[...]
anim.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
readX.requestFocus();
cardII.setVisibility(View.GONE); // WORKS FINE TOO
fab.setVisibility(View.GONE); // BUT THIS DON'T
fadeOut.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation anim) {}
public void onAnimationRepeat(Animation anim) {}
public void onAnimationEnd(Animation anim) {
clear.setVisibility(View.INVISIBLE);
}
});
clear.startAnimation(fadeOut);
}
});
anim.start();
}
fab.setVisibility(View.GONE) doesn't work independent of where it is on Clear... I reduced the code to be more readable, hope this isn't a problem.
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(View.NO_ID);
fab.setLayoutParams(p);
fab.setVisibility(View.GONE);
Or Try
fab.show(); //show
fab.hide(); //hide
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(View.NO_ID);
fab.setLayoutParams(p);
fab.setVisibility(View.invisible);
Dont use GONE
INVISIBLE:
This view is invisible, but it still takes up space for layout purposes.
GONE:
This view is invisible, and it doesn't take any space for layout purposes.
INVISIBLE:
Adapter's getView() function called
GONE:
Adapter's getView() function didn't call, thus preventing views to load, when it is unnecessary
Now everything works. Final code:
public void ZoomIn() {
[...]
fab.show();
}
I removed everything and put fab.show()
public void Clear() {
[...]
anim.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
readX.requestFocus();
cardII.setVisibility(View.GONE);
fab.setVisibility(View.GONE);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation anim) {}
public void onAnimationRepeat(Animation anim) {}
public void onAnimationEnd(Animation anim) {
clear.setVisibility(View.INVISIBLE);
}
});
clear.startAnimation(fadeOut);
}
});
anim.start();
}
I have been working on a little game and I need a progress bar animation, which begins when I touch a button for the first time and if I touch the button again before the animation ended the progressbar needs to reset.
In my code animation.start(); works well but the animation.cancel() does not seem to work.
French Version of this Question
My Activity Code containing the line that does not work:
public class MainActivity extends Activity {
Button b_bleu;
PrgressBar bar1;
#Override
public void onWindowFocusChanged(boolean hasFocus) {
b_bleu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ObjectAnimator animation = ObjectAnimator.ofInt(bar1, "progress", 100, 0);
animation.setDuration(5000);
animation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
//application se termine
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
animation.cancel();
animation.start();
}
});
}
I think the problem is that animation.cancel() has no effect since you call animation.start(); right after it.
Try something like this:
Declare a field:
private boolean mStarted = false;
Then set it in animation callbacks and check its value:
animation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
mHasStarted = true;
}
#Override
public void onAnimationEnd(Animator animation) {
mHasStarted = false; // reset the field value so that it can be started again onClick once it has ened
}
#Override
public void onAnimationCancel(Animator animation) {
mHasStarted = false;
}
});
if (mStarted) {
animation.cancel();
} else {
animation.start();
}
Hope this helps!
I want to change image after some animation to first image. I am trying with this code but only the second image is getting animated. First image is not displaying.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
anim();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void anim() {
Animation animfadein =AnimationUtils.loadAnimation(this,R.anim.fadein);
Animation animfadeout =AnimationUtils.loadAnimation(this,R.anim.fadeout);
findViewById(R.id.imageView1).startAnimation(animfadein);
findViewById(R.id.imageView1).startAnimation(animfadeout);
Context myActivity =getApplicationContext();
Toast.makeText(myActivity, "Please wait", Toast.LENGTH_SHORT).show();
button();
}
public void button() {
ImageView imgView1 = (ImageView)findViewById(R.id.imageView1);
Drawable myDrawable = getResources().getDrawable(R.drawable.start);
imgView1.setImageDrawable(myDrawable);
}
}
My xml file is like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_logo"/>
</LinearLayout>
I tried using setImageresource still same first image is not displaying.
Please let me know what mistake i have done.Thanks in advance :)
You are running everything in onCreate which is done before the user is even able to see any of the UI. It is until onStart that the user will see the UI. even so, the change will be almost immediate, so you might want to wait a few secs. Try this:
#Override
protected void onStart() {
super.onStart();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
#Override
public void run() {
anim();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
In general you are making a thread wait 5 secs then do the animation. I am not going to go over what's the best way to do this, but you certainly need to modify this on the UI thread, so I included it in the snippet.
For more information about threading go here:
http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html
You will need to use animation listener for achieving this:
animFadeIn.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
//start animFadeOut Here
}
});
Hope this helps.
This question already has answers here:
Display Animated GIF
(29 answers)
Closed 8 years ago.
Hey I'm new to android programming.
I want to know how I can easily implement a GIF in my Android app.
What I have to do?
I tried this, but I do something wrong. It's not working. >>THIS<<
Basically i am giving you following example which loads image online from some web-site and change it each after some time duration.
public class Gif_First extends Activity {
WebView mWebView;
int i = 0;
CountDownTimer mTimer;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.animation);
mTimer = new CountDownTimer(15000, 1000) {
private String[] myArray = {
"http://i.share.pho.to/09d99399_o.gif",
"http://i.share.pho.to/220dd194_o.gif",
"http://i.share.pho.to/6f3a5ae4_o.gif",
"http://i.share.pho.to/1b264cb7_o.gif",
"http://i.share.pho.to/824b1aab_o.gif",
"http://i.share.pho.to/c6728fb8_o.gif",
"http://i.share.pho.to/5508a2e1_o.gif",
"http://i.share.pho.to/aa64f9a9_o.gif",
"http://i.share.pho.to/d4619e93_o.gif",
"http://i.share.pho.to/2d50de0a_o.gif",
"http://i.share.pho.to/23c030eb_o.gif",
"http://i.share.pho.to/d6e3ccad_o.gif",
"http://i.share.pho.to/eaaeaf88_o.gif",
"http://i.share.pho.to/9708dd88_o.gif",
"http://i.share.pho.to/7f879974_o.gif",
"http://i.share.pho.to/efe8afa3_o.gif",
"http://i.share.pho.to/259954ac_o.gif",
"http://i.share.pho.to/bdbc28f2_o.gif" };
int currentIndex = 0;
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
if (currentIndex < myArray.length) {
mWebView.loadUrl(myArray[currentIndex]);
currentIndex++;
} else {
currentIndex = 0;
if (currentIndex < myArray.length)
mWebView.loadUrl(myArray[currentIndex]);
currentIndex++;
mTimer.start();
}
mTimer.start();
}
// code comment end
};
mTimer.start();
mWebView = (WebView) findViewById(R.id.webviewActionView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://i.share.pho.to/49cf97de_o.gif");
mWebView.setWebViewClient(new WebSliderWebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(mWebView, url);
//Toast.makeText(getApplicationContext(), "Done!",
// Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
//Toast.makeText(getApplicationContext(),
// "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class WebSliderWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
and here is XML File.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DogView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<WebView
android:id="#+id/webviewActionView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I think all link is dead but you can upload some image over there and change the link and then try.