Quantcast
Viewing all articles
Browse latest Browse all 21

Android Styling ActionBar Sliding Menu

Google has introduced the new Sliding Menu which we can use for Navigation and quick Accessibility. Prior to this release we relied on third party libraries, one of the famous sliding menu library is:

https://github.com/jfeinstein10/SlidingMenu

Best examples for the Actionbar sliding menu are googles: Gmail, Youtube, Playstore, Drive etc.

So today will learn how to implement Android’s builtin Actionbar sliding menu.

Here is the official documentation on implementing the Menu:

http://developer.android.com/design/patterns/navigation-drawer.html

In this tutorial we will Implement a custom menu not just a simple list, with social sharing options provided at the bottom of sliding menu (refer screenshot).
Also for this tutorial i used the famous thirdparty Actionbar support library: ‘Actionbar Sherlock‘ to support lower versions to API 14
(Library included with the download).

A video demo of the final output of this tutorial:

Here are the final screenshots:

Image may be NSFW.
Clik here to view.
td_sliding_menu_img1
          Image may be NSFW.
Clik here to view.
td_sliding_menu_img3
          Image may be NSFW.
Clik here to view.
td_sliding_menu_img2

Step 1: Create the main layout where your menu and corresponding content resides,
here we use DrawerLayout which holds the content frame and menu:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- mian content frame -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- include sliding menu here -->
    <include layout="@layout/sliding_menu" />

</android.support.v4.widget.DrawerLayout>

Note: We just moved the sliding menu to a new file and included in the main layout.

Step 2: Lets jump to sliding menu layout, here we created a custom layout to accomodate the profile and other options. Here’s the final code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/sliding_menu_bg"
    android:fillViewport="true" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/footer"
        android:fillViewport="true"
        android:scrollbars="none" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <RelativeLayout
                    android:id="@+id/rlProfile"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/list_selector"
                    android:orientation="vertical"
                    android:padding="8dp" >

                    <ImageView
                        android:id="@+id/avatar"
                        android:layout_width="42dp"
                        android:layout_height="43dp"
                        android:src="@drawable/def_profile_pic" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="42dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:layout_toRightOf="@+id/avatar"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:ellipsize="end"
                            android:shadowColor="@color/grey"
                            android:shadowDx="0"
                            android:shadowDy="-1"
                            android:shadowRadius="0.5"
                            android:singleLine="true"
                            android:text="USER NAME"
                            android:textColor="@color/white"
                            android:textSize="16sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/desc"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="bottom"
                            android:layout_marginTop="4dp"
                            android:ellipsize="end"
                            android:shadowColor="@color/grey"
                            android:shadowDx="0"
                            android:shadowDy="-1"
                            android:shadowRadius="0.5"
                            android:singleLine="true"
                            android:text="user@td.com"
                            android:textColor="@color/white"
                            android:textSize="12sp" />
                    </LinearLayout>
                </RelativeLayout>

                <View
                    android:layout_width="wrap_content"
                    android:layout_height="2dp"
                    android:background="#848484" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <RelativeLayout
                        android:id="@+id/rlHome"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/list_selector"
                        android:clickable="true"
                        android:padding="4dp" >

                        <ImageView
                            android:id="@+id/imgHome"
                            android:layout_width="32dp"
                            android:layout_height="32dp"
                            android:layout_centerVertical="true"
                            android:padding="8dp"
                            android:src="@drawable/home" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="4dp"
                            android:layout_marginRight="10dp"
                            android:layout_toRightOf="@+id/imgHome"
                            android:ellipsize="end"
                            android:shadowColor="@color/grey"
                            android:shadowDx="0"
                            android:shadowDy="-1"
                            android:shadowRadius="0.5"
                            android:singleLine="true"
                            android:text="Home"
                            android:textColor="@color/white"
                            android:textSize="16sp" />
                    </RelativeLayout>

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="4dp"
                        android:layout_marginTop="4dp"
                        android:background="#848484" />

                    <RelativeLayout
                        android:id="@+id/rlUploads"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/list_selector"
                        android:clickable="true"
                        android:padding="4dp" >

                        <ImageView
                            android:id="@+id/imgUploads"
                            android:layout_width="32dp"
                            android:layout_height="32dp"
                            android:layout_centerVertical="true"
                            android:padding="8dp"
                            android:src="@drawable/uploads" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="4dp"
                            android:layout_marginRight="10dp"
                            android:layout_toRightOf="@+id/imgUploads"
                            android:ellipsize="end"
                            android:shadowColor="@color/grey"
                            android:shadowDx="0"
                            android:shadowDy="-1"
                            android:shadowRadius="0.5"
                            android:singleLine="true"
                            android:text="Uploads"
                            android:textColor="@color/white"
                            android:textSize="16sp" />
                    </RelativeLayout>
                </LinearLayout>

                <View
                    android:layout_width="wrap_content"
                    android:layout_height="1dp"
                    android:background="#848484" />

                <RelativeLayout
                    android:id="@+id/rlSubscriptions"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/list_selector"
                    android:clickable="true"
                    android:paddingBottom="6dp"
                    android:paddingLeft="12dp"
                    android:paddingRight="6dp"
                    android:paddingTop="6dp" >

                    <ImageView
                        android:id="@+id/imgSubscriptions"
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        android:layout_centerVertical="true"
                        android:padding="8dp"
                        android:src="@drawable/subscriptions" />

                    <TextView
                        android:id="@+id/labelTaf"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="4dp"
                        android:layout_toRightOf="@+id/imgSubscriptions"
                        android:ellipsize="end"
                        android:shadowDx="0"
                        android:shadowDy="-1"
                        android:shadowRadius="0.5"
                        android:singleLine="true"
                        android:text="My Subscriptions"
                        android:textColor="@color/white"
                        android:textSize="16sp" />
                </RelativeLayout>

                <View
                    android:layout_width="wrap_content"
                    android:layout_height="1dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="#848484" />

                <!-- All your menu options goes here -->

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/ivMenuFacebook"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/fb_selector"
            android:padding="7dp"
            android:src="@drawable/menu_fb_icon" />

        <ImageView
            android:id="@+id/ivMenuTwitter"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/twt_selector"
            android:padding="7dp"
            android:src="@drawable/menu_twt_icon" />

        <ImageView
            android:id="@+id/ivMenuShare"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/share_selector"
            android:padding="7dp"
            android:src="@drawable/menu_share_icon" />
    </LinearLayout>

</RelativeLayout>

Step 3: Now we have our skeleton ready, lets jump to the implementation. Here is the MainActivity (Code is self explanatory with comments where required):

public class MainActivity extends SherlockFragmentActivity implements OnClickListener {

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

	// SLIDING MENU OPTIONS
	RelativeLayout rlProfile;
	RelativeLayout rlHome;
	RelativeLayout rlUploads;
	RelativeLayout rlSubscriptions;
	RelativeLayout rlPlaylists;
	RelativeLayout rlHistory;
	RelativeLayout rlFav;
	ImageView ivMenuFacebook;
	ImageView ivMenuTwitter;
	ImageView ivMenuShare;

	public static String CUR_PAGE_TITLE = "Title";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		initMenu();

		mTitle = mDrawerTitle = getTitle();
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

		mDrawerList = (RelativeLayout) findViewById(R.id.left_drawer);

		// set a custom shadow that overlays the main content when the drawer opens
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

		// enable ActionBar app icon to behave as action to toggle nav drawer
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);

		// ActionBarDrawerToggle ties together the the proper interactions
		// between the sliding drawer and the action bar app icon
		mDrawerToggle = new ActionBarDrawerToggle(this, // host Activity
				mDrawerLayout, // DrawerLayout object
				R.drawable.ic_drawer, // nav drawer image to replace 'Up' image
				R.string.drawer_open, // "open drawer" description for accessibility
				R.string.drawer_close // "close drawer" description for accessibility
		) {
			public void onDrawerClosed(View view) {
				getSupportActionBar().setTitle(mTitle);
				invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
			}

			public void onDrawerOpened(View drawerView) {
				getSupportActionBar().setTitle(mDrawerTitle);
				invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			switchFragment(new Category());
			setSelected(rlHome);
			mDrawerLayout.openDrawer(mDrawerList); // Keep drawer open by default
		}

	}

	private void initMenu() {

		rlProfile = (RelativeLayout) findViewById(R.id.rlProfile);
		rlHome = (RelativeLayout) findViewById(R.id.rlHome);
		rlUploads = (RelativeLayout) findViewById(R.id.rlUploads);
		rlSubscriptions = (RelativeLayout) findViewById(R.id.rlSubscriptions);
		rlFav = (RelativeLayout) findViewById(R.id.rlFav);
		rlPlaylists = (RelativeLayout) findViewById(R.id.rlPlaylists);
		rlHistory = (RelativeLayout) findViewById(R.id.rlHistory);

		ivMenuFacebook = (ImageView) findViewById(R.id.ivMenuFacebook);
		ivMenuTwitter = (ImageView) findViewById(R.id.ivMenuTwitter);
		ivMenuShare = (ImageView) findViewById(R.id.ivMenuShare);

		rlProfile.setOnClickListener(this);
		rlHome.setOnClickListener(this);
		rlUploads.setOnClickListener(this);
		rlSubscriptions.setOnClickListener(this);
		rlFav.setOnClickListener(this);
		rlPlaylists.setOnClickListener(this);
		rlHistory.setOnClickListener(this);

		ivMenuFacebook.setOnClickListener(this);
		ivMenuTwitter.setOnClickListener(this);
		ivMenuShare.setOnClickListener(this);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
				mDrawerLayout.closeDrawer(mDrawerList);
			} else {
				mDrawerLayout.openDrawer(mDrawerList);
			}
		}
		return true;

	}

	@Override
	public void onClick(View v) {

		// update the main content by replacing fragments
		Fragment newContent = new Category();
		Bundle bundle = new Bundle();

		if (v.getId() == R.id.rlProfile) {
			// PROFILE
			newContent = new UserProfile();
			bundle.putString(CUR_PAGE_TITLE, "Profile");
			setTitle("Profile");
			setSelected(rlProfile);
		} else if (v.getId() == R.id.rlHome) {
			// HOME
			setTitle("Home");
			bundle.putString(CUR_PAGE_TITLE, "Home");
			setSelected(rlHome);
		} else if (v.getId() == R.id.rlUploads) {
			// UPLOADS
			setTitle("Uploads");
			bundle.putString(CUR_PAGE_TITLE, "Uploads");
			setSelected(rlUploads);
		} else if (v.getId() == R.id.rlSubscriptions) {
			// Subscriptions
			setTitle("My Subscriptions");
			bundle.putString(CUR_PAGE_TITLE, "My Subscriptions");
			setSelected(rlSubscriptions);
		} else if (v.getId() == R.id.rlPlaylists) {
			// PLAYLISTS
			setTitle("Playlists");
			bundle.putString(CUR_PAGE_TITLE, "Playlists");
			setSelected(rlPlaylists);
		} else if (v.getId() == R.id.rlHistory) {
			// HISTORY
			setTitle("History");
			bundle.putString(CUR_PAGE_TITLE, "History");
			setSelected(rlHistory);
		} else if (v.getId() == R.id.rlFav) {
			// FAVOURITES
			setTitle("Favourites");
			bundle.putString(CUR_PAGE_TITLE, "Favourites");
			setSelected(rlFav);
		}

		// SHARE
		else if (v.getId() == R.id.ivMenuFacebook) {
			// FACEBOOK
			Toast.makeText(this, "Facebook pressed!", Toast.LENGTH_SHORT).show();
		} else if (v.getId() == R.id.ivMenuTwitter) {
			// TWITTER
			Toast.makeText(this, "Twitter pressed!", Toast.LENGTH_SHORT).show();
		} else if (v.getId() == R.id.ivMenuShare) {
			// SHARE
			Toast.makeText(this, "Share pressed!", Toast.LENGTH_SHORT).show();
		}

		if (newContent != null) {
			newContent.setArguments(bundle);
			switchFragment(newContent);
		}

	}

	// switching fragment
	private void switchFragment(Fragment fragment) {

		mDrawerLayout.closeDrawer(mDrawerList);

		FragmentManager fragmentManager = getSupportFragmentManager();
		fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

	}

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

	// set the selected option as enabled
	private void setSelected(RelativeLayout rl) {

		// reset all selections
		rlProfile.setSelected(false);
		rlHome.setSelected(false);
		rlUploads.setSelected(false);
		rlSubscriptions.setSelected(false);
		rlPlaylists.setSelected(false);
		rlHistory.setSelected(false);
		rlFav.setSelected(false);

		rl.setSelected(true); // set current selection

	}

	// When using the ActionBarDrawerToggle, you must call it during onPostCreate() and onConfigurationChanged()
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggles
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

}

As an addon for this tutorial, included the profile page where i used a custom Drawable to generate a rounded bitmap.

As a styling factor make the sliding menu a bit transparent to give the translucent effect.

Please share any tips related styling Actionbar sliding menu in the comments.

Image may be NSFW.
Clik here to view.
 Download Sources:

TDABSlidingMenu

Subscribe like and stay tuned!

The post Android Styling ActionBar Sliding Menu appeared first on Techie Dreams.


Viewing all articles
Browse latest Browse all 21

Trending Articles