본문 바로가기

공부/Android

안드로이드 액션바 어디서든 사용하기

Official Twitter application for Android  has introduced new Android UI features and behavior patternssuch as Dashboard, Search Bar, QuickAction and Action Bar. One of the interesting pattern isQuickActions that displays contextual actions in a list view. This pattern actually already exists inQuickContact dialog/bar in default Contact application (since Android 2.0).

QuickContact QuickContact

The QuickActions dialog is not included in standard Android SDK, so we have to create it manually. At first, i had no idea on how to create it so i decided to download and read the Contact app source code from  Android git. I found that the QuickContact dialog  uses private API call (com.android.internal.policy.PolicyManager) that does not exists in standard SDK. After posting question about it on google groups and stack overflow, i got the solution for it from Qberticus (thanx Qberticus!).

Qberticus’s QuickActions uses simple/plain layout so i have to create a custom layout so it will look like QuickContact in Contact app or QuickActions in Twitter app. Based on QuickContact source code, i made a slight modification on Qberticus’s BetterPopupWindow class and extended it to implement custom layout. I also made it customizeable, so the icon and text in action list can be customized.

Here are the screenshoots of QuickActions demo:

QuickContact / Twitter-like QuickActions

 

Code snippet
Create action list

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
final ActionItem chart = new ActionItem();
 
chart.setTitle("Chart");
chart.setIcon(getResources().getDrawable(R.drawable.chart));
chart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TestQuickAction.this, "Chart selected" , Toast.LENGTH_SHORT).show();
}
});
 
final ActionItem production = new ActionItem();
 
production.setTitle("Products");
production.setIcon(getResources().getDrawable(R.drawable.production));
production.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TestQuickAction.this, "Products selected", Toast.LENGTH_SHORT).show();
}
});

Line 01: Create new action
Line 03: Set action title
Line 03: Set action icon
Line 04: Set on click listener

Show QuickAction dialog

?
1
2
3
4
5
6
7
8
9
10
11
12
13
Button btn1 = (Button) this.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
QuickAction qa = new QuickAction(v);
 
qa.addActionItem(chart);
qa.addActionItem(production);
qa.setAnimStyle(QuickAction.ANIM_AUTO);
 
qa.show();
}
});

Line 05: Create new QuickAction dialog
Line 07-08: Add action item
Line 09: Set animation style
Line 011: Show QuickAction

Gallery3D-like QuickActions

 

QuickActions With Dismiss on Action Selected

?
1
2
3
4
5
6
7
8
addAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(NewQA.this, "Add " + text, Toast.LENGTH_SHORT).show();
 
mQuickAction.dismiss();
}
});

Implementation on My Application

BlitzDroid

 

Minapolitan (Prototype)

 

'공부 > Android' 카테고리의 다른 글

안드로이드 C2DM  (0) 2011.06.30
안드로이드에서 휠 모양 입력 구현  (0) 2011.06.07
안드로이드 HTML 파싱법  (0) 2011.06.04
안드로이드 Frame 애니메이션 구현  (0) 2011.04.04
안드로이드 커스텀 버튼  (0) 2011.01.04