summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/templates/activity_samples/quickaction/quick_action.java
blob: 921c805e14eb747dcd0e8c8ce0050677267bcf57 (plain)
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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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 #package_name#;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import #ManifestPackageName#.R;

public class #class_name# extends Activity {
	
    /*
     * Pixels added to QuickAction window growing window area to avoid fading edges
     */
    private static final int MARGIN_ADJUSTMENT = 8;
    
    /*
     * Pixels added to QuickAction window growing window area to avoid fading edges
     */
    private static final int ACTION_ITEM_SIZE = 64;
    
    /*
     * The current quick action window (to dismiss on application pause/rotation)
     */
    private PopupWindow currentQuickActionWindow = null;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.#layout_name#quickaction/quick_action_activity.xml#);
	}
	
	/**
	 * Create an action item
	 * @param name the action name
	 * @param imageResId the image ID to be set in the action item
	 * @return
	 */
	private View createAction (final String name, int imageResId) {
		View action = getLayoutInflater().inflate(R.layout.#layout_name#quickaction/quick_action_item.xml#, null);
		ImageView image = (ImageView) action.findViewById(R.id.action_image);
		TextView text = (TextView) action.findViewById(R.id.action_name);
		text.setText(name);
		
		/*
		 * Set image dimensions (Density Independent Pixels)
		 */
		action.setMinimumHeight((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ACTION_ITEM_SIZE, getResources().getDisplayMetrics()));
		action.setMinimumWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ACTION_ITEM_SIZE, getResources().getDisplayMetrics()));
		image.setImageResource(imageResId);
		
		/*
		 *	Set action being performed when item action is clicked 
		 */
		action.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
			    /*
			     * TODO: Place your action code here
			     */
				Toast.makeText(#class_name#.this, "You have selected " + name , Toast.LENGTH_SHORT).show();
			}
		});
		return action;
	}
	
	public void openQuickActionWindow(View target) {
		/*
		 * Get the layout of quick action window
		 */
		ViewGroup quickActionLayout = (ViewGroup) getLayoutInflater()
				.inflate(R.layout.#layout_name#quickaction/quick_action_grid.xml#, null);
		
		/*
		 * Get the quick action bar
		 */
		ViewGroup quickActionBar = (ViewGroup)quickActionLayout.findViewById(R.id.quick_action_bar);
		
		/*
		 * Add the actions (change to your own needs)
		 */
		quickActionBar.addView(createAction("Action 1", R.drawable.#drawable_name#quickaction/icon.png#));
		quickActionBar.addView(createAction("Action 2", R.drawable.#drawable_name#quickaction/icon.png#));
		quickActionBar.addView(createAction("Action 3", R.drawable.#drawable_name#quickaction/icon.png#));
		quickActionBar.addView(createAction("Action n", R.drawable.#drawable_name#quickaction/icon.png#));
		
		/*
		 * Create the window and set the content
		 * It is important to set the right context due to touch events
		 */
		currentQuickActionWindow = new PopupWindow(getApplicationContext());
		currentQuickActionWindow.setContentView(quickActionLayout);

		/*
		 * The popup must be touchable (to touch actions),
		 * focusable (if you have more actions than space) and,
		 * outside touchable (to be able to close the window)
		 */
		currentQuickActionWindow.setTouchable(true);
		currentQuickActionWindow.setFocusable(true);
		currentQuickActionWindow.setOutsideTouchable(true);
		
		/*
		 * Make background transparent
		 */
		currentQuickActionWindow.setBackgroundDrawable(new BitmapDrawable());
		
		/*
		 * Close Quick Action window when clicked outside
		 */
		currentQuickActionWindow.setTouchInterceptor(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					currentQuickActionWindow.dismiss();
					return true;
				}
				return false;
			}
		});
		
		/*
		 * Measure the Quick Action window.
		 * Add MARGIN_ADJUSTMENT density independent pixels to the size in order to not show scrolls and left some edge spaces
		 * Check layout files to check
		 */
		quickActionLayout.measure(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		currentQuickActionWindow.setWidth(quickActionLayout.getMeasuredWidth() + (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGIN_ADJUSTMENT, getResources().getDisplayMetrics()));
		currentQuickActionWindow.setHeight(quickActionLayout.getMeasuredHeight() + (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGIN_ADJUSTMENT, getResources().getDisplayMetrics()));
		
		/*
		 * Open the Quick Action window
		 */
		currentQuickActionWindow.showAsDropDown(target);
		
		/*
		 * Animate action list
		 * This must be manually called
		 */
		Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.#anim_name#quickaction/quickaction_list_anim.xml#);
		quickActionBar.startAnimation(animation);
	}
	
	@Override
    protected void onPause() {
	    /*
	     * Dismiss quick action if still opened
	     */
        if (currentQuickActionWindow != null) {
            currentQuickActionWindow.dismiss();
        }
        super.onPause();
    }
}