Code Monkey home page Code Monkey logo

collapsing-toolbar-tutorial's Introduction

Collapsing-Toolbar-Tutorial

We'll design this type of sample Android App using Collapsing Toolbar Layout:

Prepare Gradle

First of all add these dependencies to your gradle file:

dependencies {
    ...
    compile 'com.android.support:design:26.0.0-alpha1'
    ...
   }

Here I used version 26.0.0-alpha1 and it's the latest version (till 26th July, 2017), you should use the latest version.

XML prepare

Your XML file should contain a CoordinatorLayout to you CollapsingToolbarLayout.

Inside CoordinatorLayout take two widgets AppBarLayout and NestedScrollView as siblings. AppBarLayout contains a widget CollapsingToolbarLayout. Inside it I took an ImageView and my custom Toolbar.

NestedScrollView holds our rest of the contents. I used only a TextView to show an article.

Java class modification

AppBarStateChangeListener is an abstract class. It helps to listen the state change of AppBarLayout.

Inside the onCreate() method of MainActivity we can listen the state change by this method and take action:

appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
    @Override
    public void onStateChanged(AppBarLayout appBarLayout, State state) {
        if(state.equals(State.COLLAPSED)) {
            toolbar.setBackgroundResource(R.drawable.custom_toolbar);
            Toast.makeText(getApplicationContext(), "COLLAPSED", Toast.LENGTH_SHORT).show();
        }
        else if (state.equals(State.EXPANDED)) {
            toolbar.setBackgroundResource(R.color.transparent);
            Toast.makeText(getApplicationContext(), "EXPANDED", Toast.LENGTH_SHORT).show();
        }
        else if ((state.equals(State.IDLE))){
            Toast.makeText(getApplicationContext(), "IDLE", Toast.LENGTH_SHORT).show();
        }
    }
});

To get the whole project please clone it and run at your machine. If you see any error please let me know or send pull request.

Thank you. :-)

collapsing-toolbar-tutorial's People

Contributors

hasancse91 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.