Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Expanding/Collapsing Toolbar

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 206
    Comment on it

    In the below example I have created Expanding toolbar in android. Here first I have created CoordinatorLayout in activity_main.xml layout then I have added RecyclerView, CollapsingToolbarLayout and Toolbar with in CoordinatorLayout, In MainActivity I have intialize Toolbar and CollapsingToolbarLayout, And In this Expanding Toolbar example i have also used RecyclerView function and RecyclerView support library. You can see below program it clearly describe you How to make Expanding Toolbar.

    Step(1)Added first RecyclerView  support  library -
     

       compile 'com.android.support:recyclerview-v7:22.2.1'

    Srep(2)activity_main.xml layout-
     

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapse_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
    

    Step(3)MainActivity-
     

     public class MainActivity extends AppCompatActivity {
        
            private ArrayList<String> stringArrayList;
            private RecyclerView recyclerView;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
                CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);
                collapsingToolbar.setTitle(getString(R.string.expand));
    
                recyclerView = (RecyclerView) findViewById(R.id.recycler);
                recyclerView.setHasFixedSize(true);
                LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                recyclerView.setLayoutManager(layoutManager);
    
                setData(); //adding data to array list
    
            }
    
            private void setData() {
                stringArrayList = new ArrayList<>();
    
                for (int i = 0; i < 100; i++) {
                    stringArrayList.add("Item " + (i + 1));
                }
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: