Tutorial: Modal Dialogs

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Tutorial: Modal Dialogs

Postby darolla » Sat Jan 10, 2009 2:32 pm

Hello everyone,

this is my first tutorial and I hope it helps you out there.

Well I had some troubles (who hasn't ?) using dialogs, especially modal dialogs.

I'd like to show three steps how to deal with this topic.

Lets start...

Greetings,
DaRolla
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Top

First

Postby darolla » Sat Jan 10, 2009 2:35 pm

The first way is to use an AlertDialog.Builder.

It's pretty easy...

Here comes the empty Activity just starting the dialog.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package tut.orial;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.app.AlertDialog;
  9.  
  10. import android.app.AlertDialog.Builder;
  11.  
  12. import android.os.Bundle;
  13.  
  14.  
  15.  
  16. public class First extends Activity {
  17.  
  18.  
  19.  
  20.         @Override
  21.  
  22.         public void onCreate(Bundle savedInstanceState) {
  23.  
  24.                 super.onCreate(savedInstanceState);
  25.  
  26.  
  27.  
  28.                 Builder builder = new AlertDialog.Builder(this);
  29.  
  30.                 builder.setTitle("beer or wine");
  31.  
  32.                 builder.setIcon(R.drawable.beer);
  33.  
  34.                 builder.setMessage("which one do you prefer?");
  35.  
  36.                 builder.setPositiveButton("ok", null);
  37.  
  38.                 builder.setNegativeButton("cancel", null);
  39.  
  40.                 builder.show();
  41.  
  42.         }
  43.  
  44. }
  45.  
  46.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


You just need to instantiate an AlertDialog.Builder and use some setters. The listeners for the two buttons are already implemented.

Btw: the used R.drawable.beer is attached...

Greetings,
DaRolla
Attachments
beer.png
beer.png (6.9 KiB) Viewed 77320 times
first.png
first.png (10.16 KiB) Viewed 77320 times
Last edited by darolla on Sat Jan 10, 2009 4:00 pm, edited 1 time in total.
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Second

Postby darolla » Sat Jan 10, 2009 2:44 pm

Lets make things more difficult.

We'd like to have a modal dialog with some checkboxes.

It's not possible (please correct me if I'm wrong) to add some XML to the AlertDialog.Builder.

So we need to deal with a Dialog.

Unfortunately it's not possible to add our nice icon to the titlebar (I haven't seen a setter for this).

This is the beerwine.xml (the used bg.png is attached):
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.         android:orientation="vertical" android:stretchColumns="0"
  9.  
  10.         android:background="@drawable/bg" android:layout_gravity="center_vertical|center_horizontal"
  11.  
  12.         android:layout_height="wrap_content" android:layout_width="wrap_content">
  13.  
  14.  
  15.  
  16.         <TableRow>
  17.  
  18.                 <TextView android:textColor="#ffffff" android:text="beer:"
  19.  
  20.                         android:textSize="16px" android:layout_marginLeft="6dip"
  21.  
  22.                         android:layout_gravity="center_vertical" />
  23.  
  24.                 <CheckBox android:id="@+id/checkboxBeer"
  25.  
  26.                         android:layout_gravity="right|top" />
  27.  
  28.         </TableRow>
  29.  
  30.  
  31.  
  32.         <TableRow>
  33.  
  34.                 <TextView android:textColor="#ffffff" android:text="wine:"
  35.  
  36.                         android:textSize="16px" android:layout_marginLeft="6dip"
  37.  
  38.                         android:layout_gravity="center_vertical" />
  39.  
  40.                 <CheckBox android:id="@+id/checkboxWine"
  41.  
  42.                         android:layout_gravity="right|top" />
  43.  
  44.         </TableRow>
  45.  
  46.  
  47.  
  48.         <TableRow android:layout_marginTop="230px">
  49.  
  50.                 <Button android:id="@+id/buttonOK" android:text="ok"
  51.  
  52.                         android:layout_height="wrap_content" android:layout_width="wrap_content" />
  53.  
  54.         </TableRow>
  55.  
  56. </TableLayout>
  57.  
  58.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


The Java is pretty simple, just some more setters...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package tut.orial;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.app.Dialog;
  9.  
  10. import android.os.Bundle;
  11.  
  12.  
  13.  
  14. public class Second extends Activity {
  15.  
  16.  
  17.  
  18.         @Override
  19.  
  20.         public void onCreate(Bundle savedInstanceState) {
  21.  
  22.                 super.onCreate(savedInstanceState);
  23.  
  24.  
  25.  
  26.                 Dialog dialog = new Dialog(this);
  27.  
  28.                 dialog.setContentView(R.layout.beerwine);
  29.  
  30.                 dialog.setTitle("beer or wine");
  31.  
  32.                 dialog.show();
  33.  
  34.         }
  35.  
  36. }
  37.  
  38.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Greetings,
DaRolla
Attachments
second.png
second.png (42.22 KiB) Viewed 77312 times
bg.png
bg.png (72.04 KiB) Viewed 77312 times
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

RE: Second

Postby darolla » Sat Jan 10, 2009 2:48 pm

The problem is that pressing the ok buttons doesn't do anything. So we need to add a listener. Well, that shouldn't be that hard :roll:

The Listener needs a reference to the dialog in order to close it using dialog.dismiss(). Therefor we need to pass the variable to the nested class using a constructor with a parameter.

Here comes the Java with the Listener:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package tut.orial;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.app.Dialog;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.View;
  13.  
  14. import android.view.View.OnClickListener;
  15.  
  16. import android.widget.Button;
  17.  
  18.  
  19.  
  20. public class Second extends Activity {
  21.  
  22.  
  23.  
  24.         @Override
  25.  
  26.         public void onCreate(Bundle savedInstanceState) {
  27.  
  28.                 super.onCreate(savedInstanceState);
  29.  
  30.  
  31.  
  32.                 Dialog dialog = new Dialog(this);
  33.  
  34.                 dialog.setContentView(R.layout.beerwine);
  35.  
  36.                 dialog.setTitle("beer or wine");
  37.  
  38.                 dialog.show();
  39.  
  40.  
  41.  
  42.                 Button buttonOK = (Button) dialog.findViewById(R.id.buttonOK);
  43.  
  44.                 buttonOK.setOnClickListener(new OKListener(dialog));
  45.  
  46.         }
  47.  
  48.  
  49.  
  50.         protected class OKListener implements OnClickListener {
  51.  
  52.  
  53.  
  54.                 private Dialog dialog;
  55.  
  56.  
  57.  
  58.                 public OKListener(Dialog dialog) {
  59.  
  60.                         this.dialog = dialog;
  61.  
  62.                 }
  63.  
  64.  
  65.  
  66.                 public void onClick(View v) {
  67.  
  68.                         dialog.dismiss();
  69.  
  70.                 }
  71.  
  72.         }
  73.  
  74. }
  75.  
  76.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Greetings,
DaRolla
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

RE: RE: Second

Postby darolla » Sat Jan 10, 2009 2:53 pm

But we still do not have the feature setting or getting the checkbox states.

Ok, we can do this somehow, maybe the same way we are working with the ok button.

Imagine a dialog with more than 2 checkboxes and 1 button!

The Activity calling the dialog is full of code for the dialog (esp. Listeners).

And this way it's awful to reuse the dialog.

Lets take a look how to put the dialog and its code into a class similar to an Activity...

Greetings,
DaRolla
Last edited by darolla on Sat Jan 10, 2009 4:02 pm, edited 2 times in total.
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Third

Postby darolla » Sat Jan 10, 2009 3:25 pm

Instead of extending an Activity we now extend Dialog.

The onCreate method is the same as in an Activity.

But we need a constructor to let the user check or uncheck the 2 checkboxes starting the dialog.

The Listener for the ok button is the same, but we need to qualify the interface OnClickListener by android.view.View.OnClickListener or otherwise the android.content.Dialog.Interface.OnClickListener is taken.

The beerwine.xml is the same as in Second.

Here comes the empty Activity opening our own BeerWineDialog with both checkboxes checked:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package tut.orial;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10.  
  11.  
  12. public class Third extends Activity {
  13.  
  14.  
  15.  
  16.         @Override
  17.  
  18.         public void onCreate(Bundle savedInstanceState) {
  19.  
  20.                 super.onCreate(savedInstanceState);
  21.  
  22.  
  23.  
  24.                 BeerWineDialog dialog = new BeerWineDialog(this, true, true);
  25.  
  26.                 dialog.show();
  27.  
  28.         }
  29.  
  30. }
  31.  
  32.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


And this is the BeerWineDialog:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package tut.orial;
  3.  
  4.  
  5.  
  6. import android.app.Dialog;
  7.  
  8. import android.content.Context;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.View;
  13.  
  14. import android.widget.Button;
  15.  
  16. import android.widget.CheckBox;
  17.  
  18.  
  19.  
  20. public class BeerWineDialog extends Dialog {
  21.  
  22.  
  23.  
  24.         private boolean beer;
  25.  
  26.         private boolean wine;
  27.  
  28.  
  29.  
  30.         public BeerWineDialog(Context context, boolean beer, boolean wine) {
  31.  
  32.                 super(context);
  33.  
  34.                 this.beer = beer;
  35.  
  36.                 this.wine = wine;
  37.  
  38.         }
  39.  
  40.  
  41.  
  42.         @Override
  43.  
  44.         public void onCreate(Bundle savedInstanceState) {
  45.  
  46.                 super.onCreate(savedInstanceState);
  47.  
  48.  
  49.  
  50.                 setContentView(R.layout.beerwine);
  51.  
  52.                 setTitle("beer or wine");
  53.  
  54.  
  55.  
  56.                 CheckBox cbBeer = (CheckBox) findViewById(R.id.checkboxBeer);
  57.  
  58.                 cbBeer.setChecked(beer);
  59.  
  60.  
  61.  
  62.                 CheckBox cbWine = (CheckBox) findViewById(R.id.checkboxWine);
  63.  
  64.                 cbWine.setChecked(wine);
  65.  
  66.                
  67.  
  68.                 Button buttonOK = (Button) findViewById(R.id.buttonOK);
  69.  
  70.                 buttonOK.setOnClickListener(new OKListener());
  71.  
  72.         }
  73.  
  74.  
  75.  
  76.         private class OKListener implements android.view.View.OnClickListener {
  77.  
  78.  
  79.  
  80.                 @Override
  81.  
  82.                 public void onClick(View v) {
  83.  
  84.                         BeerWineDialog.this.dismiss();
  85.  
  86.                 }
  87.  
  88.         }
  89.  
  90.  
  91.  
  92. }
  93.  
  94.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Greetings,
DaRolla
Attachments
third.png
third.png (41.51 KiB) Viewed 77300 times
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Top

RE: Third

Postby darolla » Sat Jan 10, 2009 3:31 pm

The final problem to solve is how the calling class Third gets the states of the checkboxes from the BeerWineDialog.

Using just simple getters won't work.

So it's time for our own ReadyListener. It's fired when the ok button is pressed. It's defined as nested interface. It's implemented in Third and it's instance is passed through the BeerWineDialog constructor.

Greetings,
DaRolla
Last edited by darolla on Sat Jan 10, 2009 3:47 pm, edited 2 times in total.
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

RE: RE: Third

Postby darolla » Sat Jan 10, 2009 3:46 pm

Please note that inside the OKListener.onClick the checkboxes states are read, because the local variables beer and whine are only the initial states. This is the BeerWineDialog:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package tut.orial;
  3.  
  4.  
  5.  
  6. import android.app.Dialog;
  7.  
  8. import android.content.Context;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.View;
  13.  
  14. import android.widget.Button;
  15.  
  16. import android.widget.CheckBox;
  17.  
  18.  
  19.  
  20. public class BeerWineDialog extends Dialog {
  21.  
  22.  
  23.  
  24.         public interface ReadyListener {
  25.  
  26.                 public void ready(boolean beer, boolean wine);
  27.  
  28.         }
  29.  
  30.  
  31.  
  32.         private boolean beer;
  33.  
  34.         private boolean wine;
  35.  
  36.         private ReadyListener readyListener;
  37.  
  38.  
  39.  
  40.         public BeerWineDialog(Context context, boolean beer, boolean wine, ReadyListener readyListener) {
  41.  
  42.                 super(context);
  43.  
  44.                 this.beer = beer;
  45.  
  46.                 this.wine = wine;
  47.  
  48.                 this.readyListener = readyListener;
  49.  
  50.         }
  51.  
  52.  
  53.  
  54.         @Override
  55.  
  56.         public void onCreate(Bundle savedInstanceState) {
  57.  
  58.                 super.onCreate(savedInstanceState);
  59.  
  60.  
  61.  
  62.                 setContentView(R.layout.beerwine);
  63.  
  64.                 setTitle("beer or wine");
  65.  
  66.  
  67.  
  68.                 CheckBox cbBeer = (CheckBox) findViewById(R.id.checkboxBeer);
  69.  
  70.                 cbBeer.setChecked(beer);
  71.  
  72.  
  73.  
  74.                 CheckBox cbWine = (CheckBox) findViewById(R.id.checkboxWine);
  75.  
  76.                 cbWine.setChecked(wine);
  77.  
  78.  
  79.  
  80.                 Button buttonOK = (Button) findViewById(R.id.buttonOK);
  81.  
  82.                 buttonOK.setOnClickListener(new OKListener());
  83.  
  84.         }
  85.  
  86.  
  87.  
  88.         private class OKListener implements android.view.View.OnClickListener {
  89.  
  90.  
  91.  
  92.                 @Override
  93.  
  94.                 public void onClick(View v) {
  95.  
  96.                         CheckBox cbBeer = (CheckBox) findViewById(R.id.checkboxBeer);
  97.  
  98.                         CheckBox cbWine = (CheckBox) findViewById(R.id.checkboxWine);
  99.  
  100.  
  101.  
  102.                         readyListener.ready(cbBeer.isChecked(), cbWine.isChecked());
  103.  
  104.  
  105.  
  106.                         BeerWineDialog.this.dismiss();
  107.  
  108.                 }
  109.  
  110.         }
  111.  
  112.  
  113.  
  114. }
  115.  
  116.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


This is Third logging the checkboxes states after closing the BeerWineDialog (using an instance of ReadyListener interface):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package tut.orial;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.util.Log;
  11.  
  12.  
  13.  
  14. public class Third extends Activity {
  15.  
  16.  
  17.  
  18.         @Override
  19.  
  20.         public void onCreate(Bundle savedInstanceState) {
  21.  
  22.                 super.onCreate(savedInstanceState);
  23.  
  24.  
  25.  
  26.                 BeerWineDialog dialog = new BeerWineDialog(this, true, true, new OnReadyListener());
  27.  
  28.                 dialog.show();
  29.  
  30.         }
  31.  
  32.  
  33.  
  34.         private class OnReadyListener implements BeerWineDialog.ReadyListener {
  35.  
  36.  
  37.  
  38.                 @Override
  39.  
  40.                 public void ready(boolean beer, boolean wine) {
  41.  
  42.                         Log.d("Third", "checkbox beer is " + beer);
  43.  
  44.                         Log.d("Third", "checkbox wine is " + wine);
  45.  
  46.                 }
  47.  
  48.  
  49.  
  50.         }
  51.  
  52. }
  53.  
  54.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Greetings,
DaRolla
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Done :)

Postby darolla » Sat Jan 10, 2009 3:58 pm

Well, thats it :D

I hope you understand my code and my explainations.

For questions just post here or write me a pm.

Thanks for reading.

Greetings,
DaRolla

PS: feel free to use the attached code.

PSS: I've found the beer picture here.
Attachments
BeerWine.zip
all sources and pictures
(82.32 KiB) Downloaded 1245 times
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Postby MrSnowflake » Sat Jan 10, 2009 5:29 pm

Looks awsome, you made a big effort to post this tutorial, kudos to you :).

I like the part where you advise to extends Dialog, as I don't think a lot of beginners would consider this!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby marcus_cps » Fri Mar 13, 2009 8:44 pm

Hi!

Very good tutorial!!
Thanks a lot... It was very useful.

I've been thinking... what about changing the entire dialog style (colors and everything).
Is it possible to change the dialog title background color? Or maybe use a background image for that?

Thanks very much.
marcus_cps
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 13, 2009 8:39 pm

Postby songotho » Wed Apr 08, 2009 10:39 am

Hi,

Thanks for your tutorial!

I have the same question with marcus_cps.
How can we change colors for title and messages ... in dialog.

Thanks,

--Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby songotho » Wed Apr 29, 2009 3:31 pm

I have work with AlertDialog and I have a question about the this: I have form in Dialog, for example:

alert_dialog.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6.    xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    android:orientation="vertical">
  13.  
  14.     <Button android:id="@+id/text_entry_button"
  15.  
  16.            android:layout_width="fill_parent"
  17.  
  18.            android:layout_height="wrap_content"
  19.  
  20.            android:text="DIALOG_TEXT_ENTRY"/>
  21.  
  22. </LinearLayout>
  23.  
  24.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


alert_dialog_text_entry.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:layout_width="fill_parent"
  7.  
  8.    android:layout_height="wrap_content"
  9.  
  10.    android:orientation="vertical">
  11.  
  12.  
  13.  
  14.     <TextView
  15.  
  16.        android:id="@+id/username_view"
  17.  
  18.        android:layout_height="wrap_content"
  19.  
  20.        android:layout_width="wrap_content"
  21.  
  22.        android:layout_marginLeft="20dip"
  23.  
  24.        android:layout_marginRight="20dip"
  25.  
  26.        android:text="@string/alert_dialog_username"
  27.  
  28.        android:gravity="left"
  29.  
  30.        android:textAppearance="?android:attr/textAppearanceMedium" />
  31.  
  32.            
  33.  
  34.     <EditText
  35.  
  36.        android:id="@+id/username_edit"
  37.  
  38.        android:layout_height="wrap_content"
  39.  
  40.        android:layout_width="fill_parent"
  41.  
  42.        android:layout_marginLeft="20dip"
  43.  
  44.        android:layout_marginRight="20dip"
  45.  
  46.        android:scrollHorizontally="true"
  47.  
  48.        android:autoText="false"
  49.  
  50.        android:capitalize="none"
  51.  
  52.        android:gravity="fill_horizontal"
  53.  
  54.        android:textAppearance="?android:attr/textAppearanceMedium" />
  55.  
  56.  
  57.  
  58.     <TextView
  59.  
  60.        android:id="@+id/password_view"
  61.  
  62.        android:layout_height="wrap_content"
  63.  
  64.        android:layout_width="wrap_content"
  65.  
  66.        android:layout_marginLeft="20dip"
  67.  
  68.        android:layout_marginRight="20dip"
  69.  
  70.        android:text="@string/alert_dialog_password"
  71.  
  72.        android:gravity="left"
  73.  
  74.        android:textAppearance="?android:attr/textAppearanceMedium" />
  75.  
  76.            
  77.  
  78.     <EditText
  79.  
  80.        android:id="@+id/password_edit"
  81.  
  82.        android:layout_height="wrap_content"
  83.  
  84.        android:layout_width="fill_parent"
  85.  
  86.        android:layout_marginLeft="20dip"
  87.  
  88.        android:layout_marginRight="20dip"
  89.  
  90.        android:scrollHorizontally="true"
  91.  
  92.        android:autoText="false"
  93.  
  94.        android:capitalize="none"
  95.  
  96.        android:gravity="fill_horizontal"
  97.  
  98.        android:password="true"
  99.  
  100.        android:textAppearance="?android:attr/textAppearanceMedium" />
  101.  
  102.        
  103.  
  104. </LinearLayout>
  105.  
  106.  
Parsed in 0.007 seconds, using GeSHi 1.0.8.4


AlertDialogSamples.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example.android.apis.app;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.app.AlertDialog;
  9.  
  10. import android.app.Dialog;
  11.  
  12. import android.app.ProgressDialog;
  13.  
  14. import android.content.DialogInterface;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.os.Handler;
  19.  
  20. import android.os.Message;
  21.  
  22. import android.view.LayoutInflater;
  23.  
  24. import android.view.View;
  25.  
  26. import android.view.View.OnClickListener;
  27.  
  28. import android.widget.Button;
  29.  
  30.  
  31.  
  32. import com.example.android.apis.R;
  33.  
  34.  
  35.  
  36. public class AlertDialogSamples extends Activity {
  37.  
  38.  
  39.  
  40. private static final int DIALOG_TEXT_ENTRY = 7;
  41.  
  42.  
  43.  
  44. @Override
  45.  
  46. protected Dialog onCreateDialog(int id) {
  47.  
  48.  switch (id) {
  49.  
  50.  case DIALOG_TEXT_ENTRY:
  51.  
  52.             LayoutInflater factory = LayoutInflater.from(this);
  53.  
  54.             final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
  55.  
  56.             return new AlertDialog.Builder(AlertDialogSamples.this)
  57.  
  58.                 .setIcon(R.drawable.alert_dialog_icon)
  59.  
  60.                 .setTitle(R.string.alert_dialog_text_entry)
  61.  
  62.                 .setView(textEntryView)
  63.  
  64.                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
  65.  
  66.                     public void onClick(DialogInterface dialog, int whichButton) {
  67.  
  68.    
  69.  
  70.                     }
  71.  
  72.                 })
  73.  
  74.                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
  75.  
  76.                     public void onClick(DialogInterface dialog, int whichButton) {
  77.  
  78.  
  79.  
  80.                     }
  81.  
  82.                 })
  83.  
  84.                 .create();
  85.  
  86.         }
  87.  
  88.         return null;
  89.  
  90.     }
  91.  
  92.  @Override
  93.  
  94.         protected void onCreate(Bundle savedInstanceState) {
  95.  
  96.         super.onCreate(savedInstanceState);
  97.  
  98.  
  99.  
  100.         setContentView(R.layout.alert_dialog);
  101.  
  102.  
  103.  
  104.         Button textEntry = (Button) findViewById(R.id.text_entry_button);
  105.  
  106.         textEntry.setOnClickListener(new OnClickListener() {
  107.  
  108.             public void onClick(View v) {
  109.  
  110.                 showDialog(DIALOG_TEXT_ENTRY);
  111.  
  112.             }
  113.  
  114.         });
  115.  
  116.    }
  117.  
  118. }
  119.  
  120.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


So how can I get data in DIALOG_TEXT_ENTRY when I type in form of dialog?
I have tried, so it cant define any variables or ....
Anyone help me,please?

Thanks,

--Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Re: Done :)

Postby ExYndiano » Fri Sep 18, 2009 9:22 pm

Hi Darolla,

I want to make you the best congratulations for this fantastic guide "Best practice".
This example is very simple to follow in all the section.
That's all need to me for complete my application.
Thank you so much.

ExYndiano.
ExYndiano
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Sep 18, 2009 4:59 pm

Great tutorial

Postby ynrajasekhar » Thu Oct 29, 2009 6:05 am

Hi,

Thanks for posting this tutorial.

Thanks.
ynrajasekhar
Freshman
Freshman
 
Posts: 2
Joined: Mon Aug 17, 2009 6:13 am

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests