package io.ramesh.collapsingtoolbarwithwebview; import android.os.Bundle; import android.support.design.widget.AppBarLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.animation.AlphaAnimation; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener { private static final float PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR = 0.9f; private static final float PERCENTAGE_TO_HIDE_TITLE_DETAILS = 0.3f; private static final int ALPHA_ANIMATIONS_DURATION = 200; private boolean mIsTheTitleVisible = false; private boolean mIsTheTitleContainerVisible = true; private RelativeLayout mTitleContainer; private TextView textToolBarUserName, textUserName; private CircleImageView imageProfile, imageToolBar; private AppBarLayout mAppBarLayout; private Toolbar mToolbar; private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.mainToolbar); textToolBarUserName = (TextView) findViewById(R.id.textToolBarUserName); textUserName = (TextView) findViewById(R.id.textUserName); imageToolBar = (CircleImageView) findViewById(R.id.imageToolBar); imageProfile = (CircleImageView) findViewById(R.id.imageProfile); mTitleContainer = (RelativeLayout) findViewById(R.id.relativeLayoutTitle); mAppBarLayout = (AppBarLayout) findViewById(R.id.mainAppbar); webView = (WebView) findViewById(R.id.webView); mAppBarLayout.addOnOffsetChangedListener(this); startAlphaAnimation(textToolBarUserName, 0, View.INVISIBLE); startAlphaAnimation(imageToolBar, 0, View.INVISIBLE); webView.setWebViewClient(new MyWebViewClient()); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); webView.loadUrl("http://docs.google.com/gview?embedded=true&url=https://static.googleusercontent.com/media/www.google.com/en//pdf/google_ftc_dec2012.pdf"); webView.getSettings().setLoadsImagesAutomatically(true); } @Override public void onOffsetChanged(AppBarLayout appBarLayout, int offset) { int maxScroll = appBarLayout.getTotalScrollRange(); float percentage = (float) Math.abs(offset) / (float) maxScroll; handleAlphaOnTitle(percentage); handleToolbarTitleVisibility(percentage); } private void handleToolbarTitleVisibility(float percentage) { if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) { if (!mIsTheTitleVisible) { startAlphaAnimation(textToolBarUserName, ALPHA_ANIMATIONS_DURATION, View.VISIBLE); startAlphaAnimation(imageToolBar, ALPHA_ANIMATIONS_DURATION, View.VISIBLE); mIsTheTitleVisible = true; } } else { if (mIsTheTitleVisible) { startAlphaAnimation(textToolBarUserName, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE); startAlphaAnimation(imageToolBar, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE); mIsTheTitleVisible = false; } } } private void handleAlphaOnTitle(float percentage) { if (percentage >= PERCENTAGE_TO_HIDE_TITLE_DETAILS) { if (mIsTheTitleContainerVisible) { startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE); mIsTheTitleContainerVisible = false; } } else { if (!mIsTheTitleContainerVisible) { startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.VISIBLE); mIsTheTitleContainerVisible = true; } } } public static void startAlphaAnimation(View v, long duration, int visibility) { AlphaAnimation alphaAnimation = (visibility == View.VISIBLE) ? new AlphaAnimation(0f, 1f) : new AlphaAnimation(1f, 0f); alphaAnimation.setDuration(duration); alphaAnimation.setFillAfter(true); v.startAnimation(alphaAnimation); } private class MyWebViewClient extends WebViewClient { public MyWebViewClient() { } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); } } }