Android Styling ActionBar Sliding Menu
21:16
Read
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:
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:
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).
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:
Step 1: Create the main layout where your menu and corresponding content resides,
here we use DrawerLayout which holds the content frame and menu:
here we use DrawerLayout which holds the content frame and menu:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
<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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
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.
Download Sources:
Android Styling ActionBar Sliding Menu
Reviewed by Thanh Nguyen
on
21:16
Rating: 5
No comments: