-
Notifications
You must be signed in to change notification settings - Fork 564
/
ActionBar.java
302 lines (259 loc) · 9.01 KB
/
ActionBar.java
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (C) 2010 Johan Nilsson <http://markupartist.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.markupartist.android.widget;
import java.util.LinkedList;
import com.markupartist.android.widget.actionbar.R;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ActionBar extends RelativeLayout implements OnClickListener {
private LayoutInflater mInflater;
private RelativeLayout mBarView;
private ImageView mLogoView;
private View mBackIndicator;
//private View mHomeView;
private TextView mTitleView;
private LinearLayout mActionsView;
private ImageButton mHomeBtn;
private RelativeLayout mHomeLayout;
private ProgressBar mProgress;
public ActionBar(Context context, AttributeSet attrs) {
super(context, attrs);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mBarView = (RelativeLayout) mInflater.inflate(R.layout.actionbar, null);
addView(mBarView);
mLogoView = (ImageView) mBarView.findViewById(R.id.actionbar_home_logo);
mHomeLayout = (RelativeLayout) mBarView.findViewById(R.id.actionbar_home_bg);
mHomeBtn = (ImageButton) mBarView.findViewById(R.id.actionbar_home_btn);
mBackIndicator = mBarView.findViewById(R.id.actionbar_home_is_back);
mTitleView = (TextView) mBarView.findViewById(R.id.actionbar_title);
mActionsView = (LinearLayout) mBarView.findViewById(R.id.actionbar_actions);
mProgress = (ProgressBar) mBarView.findViewById(R.id.actionbar_progress);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ActionBar);
CharSequence title = a.getString(R.styleable.ActionBar_title);
if (title != null) {
setTitle(title);
}
a.recycle();
}
public void setHomeAction(Action action) {
mHomeBtn.setOnClickListener(this);
mHomeBtn.setTag(action);
mHomeBtn.setImageResource(action.getDrawable());
mHomeLayout.setVisibility(View.VISIBLE);
}
public void clearHomeAction() {
mHomeLayout.setVisibility(View.GONE);
}
/**
* Shows the provided logo to the left in the action bar.
*
* This is meant to be used instead of the setHomeAction and does not draw
* a divider to the left of the provided logo.
*
* @param resId The drawable resource id
*/
public void setHomeLogo(int resId) {
// TODO: Add possibility to add an IntentAction as well.
mLogoView.setImageResource(resId);
mLogoView.setVisibility(View.VISIBLE);
mHomeLayout.setVisibility(View.GONE);
}
/* Emulating Honeycomb, setdisplayHomeAsUpEnabled takes a boolean
* and toggles whether the "home" view should have a little triangle
* indicating "up" */
public void setDisplayHomeAsUpEnabled(boolean show) {
mBackIndicator.setVisibility(show? View.VISIBLE : View.GONE);
}
public void setTitle(CharSequence title) {
mTitleView.setText(title);
}
public void setTitle(int resid) {
mTitleView.setText(resid);
}
/**
* Set the enabled state of the progress bar.
*
* @param One of {@link View#VISIBLE}, {@link View#INVISIBLE},
* or {@link View#GONE}.
*/
public void setProgressBarVisibility(int visibility) {
mProgress.setVisibility(visibility);
}
/**
* Returns the visibility status for the progress bar.
*
* @param One of {@link View#VISIBLE}, {@link View#INVISIBLE},
* or {@link View#GONE}.
*/
public int getProgressBarVisibility() {
return mProgress.getVisibility();
}
/**
* Function to set a click listener for Title TextView
*
* @param listener the onClickListener
*/
public void setOnTitleClickListener(OnClickListener listener) {
mTitleView.setOnClickListener(listener);
}
@Override
public void onClick(View view) {
final Object tag = view.getTag();
if (tag instanceof Action) {
final Action action = (Action) tag;
action.performAction(view);
}
}
/**
* Adds a list of {@link Action}s.
* @param actionList the actions to add
*/
public void addActions(ActionList actionList) {
int actions = actionList.size();
for (int i = 0; i < actions; i++) {
addAction(actionList.get(i));
}
}
/**
* Adds a new {@link Action}.
* @param action the action to add
*/
public void addAction(Action action) {
final int index = mActionsView.getChildCount();
addAction(action, index);
}
/**
* Adds a new {@link Action} at the specified index.
* @param action the action to add
* @param index the position at which to add the action
*/
public void addAction(Action action, int index) {
mActionsView.addView(inflateAction(action), index);
}
/**
* Removes all action views from this action bar
*/
public void removeAllActions() {
mActionsView.removeAllViews();
}
/**
* Remove a action from the action bar.
* @param index position of action to remove
*/
public void removeActionAt(int index) {
mActionsView.removeViewAt(index);
}
/**
* Remove a action from the action bar.
* @param action The action to remove
*/
public void removeAction(Action action) {
int childCount = mActionsView.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = mActionsView.getChildAt(i);
if (view != null) {
final Object tag = view.getTag();
if (tag instanceof Action && tag.equals(action)) {
mActionsView.removeView(view);
}
}
}
}
/**
* Returns the number of actions currently registered with the action bar.
* @return action count
*/
public int getActionCount() {
return mActionsView.getChildCount();
}
/**
* Inflates a {@link View} with the given {@link Action}.
* @param action the action to inflate
* @return a view
*/
private View inflateAction(Action action) {
View view = mInflater.inflate(R.layout.actionbar_item, mActionsView, false);
ImageButton labelView =
(ImageButton) view.findViewById(R.id.actionbar_item);
labelView.setImageResource(action.getDrawable());
view.setTag(action);
view.setOnClickListener(this);
return view;
}
/**
* A {@link LinkedList} that holds a list of {@link Action}s.
*/
public static class ActionList extends LinkedList<Action> {
}
/**
* Definition of an action that could be performed, along with a icon to
* show.
*/
public interface Action {
public int getDrawable();
public void performAction(View view);
}
public static abstract class AbstractAction implements Action {
final private int mDrawable;
public AbstractAction(int drawable) {
mDrawable = drawable;
}
@Override
public int getDrawable() {
return mDrawable;
}
}
public static class IntentAction extends AbstractAction {
private Context mContext;
private Intent mIntent;
public IntentAction(Context context, Intent intent, int drawable) {
super(drawable);
mContext = context;
mIntent = intent;
}
@Override
public void performAction(View view) {
try {
mContext.startActivity(mIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(mContext,
mContext.getText(R.string.actionbar_activity_not_found),
Toast.LENGTH_SHORT).show();
}
}
}
/*
public static abstract class SearchAction extends AbstractAction {
public SearchAction() {
super(R.drawable.actionbar_search);
}
}
*/
}