Saturday, June 4, 2016

Android AppBarLayout Example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
        <!--
            SCROLL_FLAG_ENTER_ALWAYS
                When entering (scrolling on screen) the view will scroll on any downwards scroll
                event, regardless of whether the scrolling view is also scrolling.

            SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED
                An additional flag for 'enterAlways' which modifies the returning view to
                only initially scroll back to it's collapsed height.

            SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
                When exiting (scrolling off screen) the view will be scrolled until it is 'collapsed'.

            SCROLL_FLAG_SCROLL
                The view will be scroll in direct relation to scroll events.

            SCROLL_FLAG_SNAP
                Upon a scroll ending, if the view is only partially visible then it will be
                snapped and scrolled to it's closest edge.
        -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways"
            />
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <android.support.design.widget.TabItem
                android:id="@+id/tab_item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 1"
                />
            <android.support.design.widget.TabItem
                android:id="@+id/tab_item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 2"
                />
            <android.support.design.widget.TabItem
                android:id="@+id/tab_item_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 3"
                />
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView 1"
                android:padding="75dp"
                android:gravity="center"
                android:background="#c2e9e8"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView 2"
                android:padding="75dp"
                android:gravity="center"
                android:background="#d1f7d9"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView 3"
                android:padding="75dp"
                android:gravity="center"
                android:background="#f2bcf5"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView 4"
                android:padding="75dp"
                android:gravity="center"
                android:background="#f1dcb8"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView 5"
                android:padding="75dp"
                android:gravity="center"
                android:background="#a4c9e2"
                />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private Toolbar mToolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        // Set the support action bar
        setSupportActionBar(mToolbar);

        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }
}
res/menu/toolbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/search"
        android:title="search"
        app:showAsAction="always"
        android:icon="@drawable/ic_search_white_48dp"
        />
    <item
        android:id="@+id/add"
        android:title="Add"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_add_circle_outline_white_48dp"
        />
    <item
        android:id="@+id/remove"
        android:title="Remove"
        app:showAsAction="never|withText"
        android:icon="@drawable/ic_remove_circle_outline_white_48dp"
        />
</menu>
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>

        <!-- Important for CoordinatorLayout -->
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>