Thứ Hai, 9 tháng 9, 2013

Quick Action Demo

Quick Action Demo

2
WP Greet Box icon
X
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic.
Description:
This example of will show how we can generate a Action list or a popup window on a button press.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it QuickActionDemo.
2.) You will see some default code into your main.xml, strings.xml and android manifest file.
3.) Now write following code into your main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"       
        android:gravity="center"
        android:textSize="17dp"
        android:textStyle="bold"
        android:text="Popup Window Action Demo"/>
         
    <Button
        android:id="@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="30dp"
        android:text="Button" />
          
</LinearLayout>
4.) Create ActionItem.java and write following 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
package com.org.QuickActionActivity;
 
import android.graphics.drawable.Drawable;
import android.graphics.Bitmap;
 
public class ActionItem {
    private Drawable icon;
    private Bitmap thumb;
    private String title;
    private boolean selected;
     
    public ActionItem() {}
    public ActionItem(Drawable icon) {
        this.icon = icon;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTitle() {
        return this.title;
    }
    public void setIcon(Drawable icon) {
        this.icon = icon;
    }
    public Drawable getIcon() {
        return this.icon;
    }
    public void setSelected(boolean selected) {
        this.selected = selected;
    }
    public boolean isSelected() {
        return this.selected;
    }
    public void setThumb(Bitmap thumb) {
        this.thumb = thumb;
    }
    public Bitmap getThumb() {
        return this.thumb;
    }
}
5.) Create NewQAAdapter.java and write following:
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
package com.org.QuickActionActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.content.Context;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
public class NewQAAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private String[] data;
     
    public NewQAAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }
 
    public void setData(String[] data) {
        this.data = data;
    }
     
    @Override
    public int getCount() {
        return data.length;
    }
 
    @Override
    public Object getItem(int item) {
        return data[item];
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
         
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list, null);
             
            holder      = new ViewHolder();
             
            holder.mTitleText   = (TextView) convertView.findViewById(R.id.t_name);
             
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
 
        holder.mTitleText.setText(data[position]);
         
        return convertView;
    }
 
    static class ViewHolder {
        TextView mTitleText;
    }
}
6.) Click here to download full code content and other src and res files.
Steps:
1.) Create a project named QuickActionDemo and set the information as stated in the image.
Build Target: Android 2.1
Application Name: QuickActionDemo
Package Name: com.org. QuickActionDemo
Activity Name: QuickActionDemo
Min SDK Version: 7
2.) Open QuickActionDemo.java file and write following code there:
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
package com.org.QuickActionActivity;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
 
public class QuickActionActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         
        setContentView(R.layout.main);
         
        //Add action item
        ActionItem addAction = new ActionItem();
         
        addAction.setTitle("Add");
        addAction.setIcon(getResources().getDrawable(R.drawable.ic_add));
 
        //Accept action item
        ActionItem accAction = new ActionItem();
         
        accAction.setTitle("Accept");
        accAction.setIcon(getResources().getDrawable(R.drawable.ic_accept));
         
        //Upload action item
        ActionItem upAction = new ActionItem();
         
        upAction.setTitle("Upload");
        upAction.setIcon(getResources().getDrawable(R.drawable.ic_up));
         
        final QuickAction mQuickAction  = new QuickAction(this);
         
        mQuickAction.addActionItem(addAction);
        mQuickAction.addActionItem(accAction);
        mQuickAction.addActionItem(upAction);
         
        //setup the action item click listener
        mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {        
            @Override
            public void onItemClick(int pos) {
                 
                if (pos == 0) { //Add item selected
                    Toast.makeText(QuickActionActivity.this, "Add item selected", Toast.LENGTH_SHORT).show();
                } else if (pos == 1) { //Accept item selected
                    Toast.makeText(QuickActionActivity.this, "Accept item selected", Toast.LENGTH_SHORT).show();
                } else if (pos == 2) { //Upload item selected
                    Toast.makeText(QuickActionActivity.this, "Upload items selected", Toast.LENGTH_SHORT).show();
                }  
            }
        });
        Button btn2 = (Button) this.findViewById(R.id.btn2);
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mQuickAction.show(v);
                mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
            }
        });
    }
}      
3.) Compile and build the project.
4.) Run on 2.1 simulator for the output.
Output

Không có nhận xét nào:

Đăng nhận xét