Android Open Source - ElyTheme Silk Drawer Activity






From Project

Back to project page ElyTheme.

License

The source code is released under:

GNU General Public License

If you think the Android project ElyTheme 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.afollestad.silk.activities;
/* w ww.  ja v a2s  . c  o  m*/
import android.app.ActionBar;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.ViewDragHelper;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;

import java.lang.reflect.Field;

/**
 * An activity that makes interacting with a DrawerLayout quick and easy. All that you have to do is create a layout
 * modelled off the layouts used on the Android Developer website for drawer layouts.
 *
 * @author Aidan Follestad (afollestad)
 */
public abstract class SilkDrawerActivity extends FragmentActivity {

    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mTitle;

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        super.setTitle(title);
        getActionBar().setTitle(mTitle);
    }

    @Override
    public void setTitle(int titleId) {
        mTitle = getString(titleId);
        super.setTitle(titleId);
        getActionBar().setTitle(titleId);
    }

    /**
     * Checks whether or not any drawer is open in the DrawerActivity.
     */
    public final boolean isDrawerOpen() {
        DrawerLayout drawer = getDrawerLayout();
        return drawer != null && (drawer.isDrawerOpen(Gravity.START) || drawer.isDrawerOpen(Gravity.LEFT) || drawer.isDrawerOpen(Gravity.RIGHT));
    }

    /**
     * Gets the drawer indicator drawable resource that will be displayed next to the the home up button in the activity. This is usually
     * an icon consisting of 3 vertically orientated lines.
     */
    protected abstract int getDrawerIndicatorRes();

    /**
     * Gets the shadow drawable resource that will be used for the drawer.
     */
    protected abstract int getDrawerShadowRes();

    /**
     * Gets the layout used for the activity.
     */
    protected abstract int getLayout();

    /**
     * Gets the drawer layout, you should return the View from the layout.
     */
    public abstract DrawerLayout getDrawerLayout();

    /**
     * Gets the action bar title displayed when the drawer is open.
     */
    protected abstract int getOpenedTextRes();

    /**
     * Can be overridden.
     */
    protected void onDrawerOpened() {
    }

    /**
     * Can be overridden.
     */
    protected void onDrawerClosed() {
    }

    private void invalidateOpenClosed() {
        ActionBar ab = getActionBar();
        if (isDrawerOpen()) {
            if (getOpenedTextRes() != 0)
                ab.setTitle(getOpenedTextRes());
            onDrawerOpened();
        } else {
            if (mTitle != null)
                ab.setTitle(mTitle);
            onDrawerClosed();
        }
        invalidateOptionsMenu();
    }

    protected void setupDrawer() {
        mTitle = getTitle();
        DrawerLayout mDrawerLayout = getDrawerLayout();
        if (mDrawerLayout == null) {
            Log.w("SilkDrawerActivtiy", "You must return a drawer layout in getDrawerLayout() of your Activity in order for the drawer to be displayed.");
            return;
        }
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, getDrawerIndicatorRes(), getOpenedTextRes(), getOpenedTextRes()) {
            @Override
            public void onDrawerOpened(View drawerView) {
                invalidateOpenClosed();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                invalidateOpenClosed();

            }
        };
        mDrawerLayout.setDrawerShadow(getDrawerShadowRes(), Gravity.START);
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        try {
            Field mDragger = mDrawerLayout.getClass().getDeclaredField(
                    "mLeftDragger");//mRightDragger for right obviously
            mDragger.setAccessible(true);
            ViewDragHelper draggerObj = (ViewDragHelper) mDragger
                    .get(mDrawerLayout);

            Field mEdgeSize = draggerObj.getClass().getDeclaredField(
                    "mEdgeSize");
            mEdgeSize.setAccessible(true);
            int edge = mEdgeSize.getInt(draggerObj);

            mEdgeSize.setInt(draggerObj, edge * 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        if (getDrawerLayout() != null) {
            ActionBar ab = getActionBar();
            ab.setDisplayHomeAsUpEnabled(true);
        }
        setupDrawer();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (mDrawerToggle != null)
                    return mDrawerToggle.onOptionsItemSelected(item);
                else break;
        }
        return super.onOptionsItemSelected(item);
    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mDrawerToggle != null)
            mDrawerToggle.onConfigurationChanged(newConfig);
    }
}




Java Source Code List

com.afollestad.cardsui.ApplicationTest.java
com.afollestad.cardsui.CardAdapter.java
com.afollestad.cardsui.CardBase.java
com.afollestad.cardsui.CardCenteredHeader.java
com.afollestad.cardsui.CardCompressed.java
com.afollestad.cardsui.CardCursorAdapter.java
com.afollestad.cardsui.CardHeader.java
com.afollestad.cardsui.CardListView.java
com.afollestad.cardsui.CardTheme.java
com.afollestad.cardsui.Card.java
com.afollestad.silk.ApplicationTest.java
com.afollestad.silk.SilkComparable.java
com.afollestad.silk.SilkCursorItem.java
com.afollestad.silk.Silk.java
com.afollestad.silk.activities.SilkDrawerActivity.java
com.afollestad.silk.adapters.ScrollStatePersister.java
com.afollestad.silk.adapters.SilkAdapter.java
com.afollestad.silk.adapters.SilkCursorAdapter.java
com.afollestad.silk.adapters.SilkSpinnerAdapter.java
com.afollestad.silk.dialogs.SilkDialog.java
com.afollestad.silk.fragments.feed.SilkCursorFeedFragment.java
com.afollestad.silk.fragments.feed.SilkFeedFragment.java
com.afollestad.silk.fragments.list.SilkCursorListFragment.java
com.afollestad.silk.fragments.list.SilkListFragment.java
com.afollestad.silk.http.SilkHttpBase.java
com.afollestad.silk.http.SilkHttpBody.java
com.afollestad.silk.http.SilkHttpCallback.java
com.afollestad.silk.http.SilkHttpClient.java
com.afollestad.silk.http.SilkHttpException.java
com.afollestad.silk.http.SilkHttpHeader.java
com.afollestad.silk.http.SilkHttpResponse.java
com.afollestad.silk.utilities.IOUtils.java
com.afollestad.silk.utilities.TimeUtils.java
com.afollestad.silk.views.ClickableToast.java
com.afollestad.silk.views.list.OnSilkScrollListener.java
com.afollestad.silk.views.list.SilkGridView.java
com.afollestad.silk.views.list.SilkListView.java
com.afollestad.silk.views.time.SilkDatePicker.java
it.gcaliendo.elytheme.Adw.java
it.gcaliendo.elytheme.ApplicationTest.java
it.gcaliendo.elytheme.DocksProvider.java
it.gcaliendo.elytheme.Docks.java
it.gcaliendo.elytheme.IconActivity.java
it.gcaliendo.elytheme.IconPack.java
it.gcaliendo.elytheme.IconsProvider.java
it.gcaliendo.elytheme.Icons.java
it.gcaliendo.elytheme.RequestActivity.java
it.gcaliendo.elytheme.ThemeActivity.java
it.gcaliendo.elytheme.Wallpaper.java
it.gcaliendo.elytheme.fragments.FragmentAbout.java
it.gcaliendo.elytheme.fragments.FragmentContact.java
it.gcaliendo.elytheme.fragments.FragmentExtras.java
it.gcaliendo.elytheme.fragments.FragmentTheme.java
it.gcaliendo.elytheme.helper.AppInfo.java
it.gcaliendo.elytheme.iconfragment.IconFragmentGames.java
it.gcaliendo.elytheme.iconfragment.IconFragmentLatest.java
it.gcaliendo.elytheme.iconfragment.IconFragmentMisc.java
it.gcaliendo.elytheme.iconfragment.IconFragmentPlay.java
it.gcaliendo.elytheme.iconfragment.IconFragmentSystem.java