Search This Blog

Showing posts with label dialogs. Show all posts
Showing posts with label dialogs. Show all posts

31 May 2010

Disclaimer


Use the Alert Dialogs example as a starting point.....

Add this icon to your src\main\android\res\drawable directory

I grabbed that icon from the Tango project, but had to change the filename to be compatable with Android.

Then from the Google Analytics example, create an analytics profile, update your pom.xml and AndroidManifest.xml


Instead of putting the tracker code directly into the onCreate method, let's move that into the "I Accept!" button.  Note the change to onDestroy as well!

public class DisclaimerTestActivity extends Activity {
    private static String TAG = "DisclaimerTest";
    private GoogleAnalyticsTracker tracker = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("Disclaimer");
        dialog.setMessage("In order to provide support for this application, we reserve the right to anonymously track and report usage information in this application.  We will track which parts of the application are popular but will not track who you are or what you type / look at.");
        dialog.setIcon(R.drawable.network);
        dialog.setButton("I Accept!", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                tracker = GoogleAnalyticsTracker.getInstance();
                tracker.start("UA-YOUR-ACCOUNT-HERE", DisclaimerTestActivity.this);
                setContentView(R.layout.main);
                track("/");
                dispatch();
                return;
            }
        });
        dialog.setButton2("No Way!", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
                return;
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if(tracker != null) tracker.stop();
    }

    // track("Clicks", "Button", "clicked", 77
    private void track(String category, String action, String label, int value)
    {
        tracker.trackEvent(category, action, label, value);
    }

    // track("/testApplicationHomeScreen");
    // track("/download");
    private void track(String page)
    {
        tracker.trackPageView(page);
    }

    // Allows sending in bulk by queuing up multiple tracks before calling dispatch
    // timestamps based on when dispatch is called
    private void dispatch()
    {
        tracker.dispatch();
    }
}


Redeploy (mvn clean install).  Now, if you accept the disclaimer it will do analytic notifications (which can take up to 24 hours to show up).  If you cancel, it will just close the app.

23 April 2010

Progress Bar

Again building off any of our previous examples; this time we are going to do a progress dialog...

Open MyActivity.java and replace the onCreate method:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ProgressDialog dialog = new ProgressDialog(MyActivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("Downloading 200gb...");
        dialog.setCancelable(true);
        dialog.setMax(200);
        dialog.setProgress(0);
        dialog.show();

        Thread t = new Thread(new Runnable(){
            public void run() {
                while(dialog.getProgress() < dialog.getMax())
                {
                    dialog.incrementProgressBy(1);
                    try{Thread.sleep(50);}catch(Exception e){/* no-op */}
                }
                dialog.dismiss();
            }
        });
        t.start();
    }

Redeploy (mvn clean install) and launch.

Loading, please wait...

This time we are going to show the standard Please Wait dialog with the spinning circle.  This example is taken from the developer documentation.  Using any of our examples as a starting point, let's replace the onCreate method in MyActivity.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", "Loading. Please wait...", true);
        Thread t = new Thread(new Runnable(){
            public void run() {
                try{
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // no-op
                }
                dialog.dismiss();
            }
        });
        t.start();
    }

Redeploy (mvn clean install) and the dialog will show for 5 seconds before going away.

Checkbox dialog

Building on the previous example, this time we want to be able to select and unselect various items.  Because of that, this example will not actually close the dialog.

Open MyActivity.java and add a variable for storing the initial states:
final boolean[] states = {false, false, true};

Replace the setItems or setSingleChoiceItems with:
        builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener(){
            public void onClick(DialogInterface dialogInterface, int item, boolean state) {
                Toast.makeText(getApplicationContext(), items[item] + " set to " + state, Toast.LENGTH_SHORT).show();
            }
        });

Redeploy (mvn clean install) and you should be able to (un)select different ones.

Radio button selection dialog

Building on the previous list-selection example, we will add a radio-button icon to the list.

Open MyActivity from the previous example and change this line:
builder.setItems(items, new DialogInterface.OnClickListener(){
to:
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener(){

The '-1' specifies which item to select by default. 0 would have been Red, -1 is none.

By default, the window will not close when they make a selection.  Replace this line:
                return;
with this line:

                dialogInterface.dismiss();


Redeploy (mvn clean install) and test.

List picker and fading popup notice

This example can be created from any of the previous as we are just going to replace the content of MyActivity.  In this case, we are going to create a popup, allow the user to choose one, and then show a temporary message telling them what they chose.  This example is directly from the developer documentation.

Open MyActivity.java and replace the contents of the onCreate method with the following:


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final CharSequence[] items = {"Red", "Green", "Blue"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a color");
        builder.setItems(items, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                return;
            }
        });
        builder.create().show();
    }

Once you redeploy (mvn clean install) and launch your app, you will see the dialog.  Choose one and see the popup.

22 April 2010

Alert Dialogs

Using our last example as a starting point, we are going to add an alert dialog.  Make sure to do a 'mvn install' before we get started so your IDE doesn't try to guess where R.layout.main is.

Open MyActivity.java. Let's modify the onCreate method:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("It's an alert!");
        dialog.setMessage("This alert has a message.");
        dialog.setIcon(R.drawable.icon);
        dialog.setButton("OK", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                return;
            }
        });
        dialog.show();
    }

Redeploy (mvn clean install) and you'll see the alert when you launch your app.