Android Open Source - MaterialDemo Main Activity






From Project

Back to project page MaterialDemo.

License

The source code is released under:

Apache License

If you think the Android project MaterialDemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.epam.dziashko.aliaksei.materialdemo;
//  ww w.ja v a2s.c o  m
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.epam.dziashko.aliaksei.materialdemo.fragment.PagerFragment;
import com.epam.dziashko.aliaksei.materialdemo.fragment.RecyclerViewFragment;
import com.epam.dziashko.aliaksei.materialdemo.fragment.ToolbarFragment;


public class MainActivity extends ActionBarActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        setSupportActionBar(toolbar);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PagerFragment())
                    .commit();
        }

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
//                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
//                getSupportActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

    }

    public void selectRecycler(View view) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, new RecyclerViewFragment())
                .commit();
        mDrawerLayout.closeDrawers();
    }

    public void selectGrid(View view) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, new PagerFragment())
                .commit();
        mDrawerLayout.closeDrawers();
    }

    public void selectToolbar(View view) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, new ToolbarFragment())
                .commit();
        mDrawerLayout.closeDrawers();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}




Java Source Code List

com.epam.dziashko.aliaksei.materialdemo.ApplicationTest.java
com.epam.dziashko.aliaksei.materialdemo.DetailActivity.java
com.epam.dziashko.aliaksei.materialdemo.MainActivity.java
com.epam.dziashko.aliaksei.materialdemo.adapter.BaseArrayAdapter.java
com.epam.dziashko.aliaksei.materialdemo.adapter.Function.java
com.epam.dziashko.aliaksei.materialdemo.adapter.IBaseAdapter.java
com.epam.dziashko.aliaksei.materialdemo.data.Data.java
com.epam.dziashko.aliaksei.materialdemo.fragment.GridFragment.java
com.epam.dziashko.aliaksei.materialdemo.fragment.PagerFragment.java
com.epam.dziashko.aliaksei.materialdemo.fragment.RecyclerViewFragment.java
com.epam.dziashko.aliaksei.materialdemo.fragment.ToolbarFragment.java
com.epam.dziashko.aliaksei.materialdemo.view.SlidingTabLayout.java
com.epam.dziashko.aliaksei.materialdemo.view.SlidingTabStrip.java