Inputbox, InputField, Popup, AlertDialog, EditText

Quickly share your Android Code Snippets here...

Inputbox, InputField, Popup, AlertDialog, EditText

Postby plusminus » Thu Oct 30, 2008 5:41 am

'Generic' example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final FrameLayout fl = new FrameLayout(this);
  2.  
  3. final EditText input = new EditText(this);
  4.  
  5. input.setGravity(Gravity.CENTER);
  6.  
  7.  
  8.  
  9. fl.addView(input, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
  10.  
  11.  
  12.  
  13. input.setText("Preset Text");
  14.  
  15. new AlertDialog.Builder(this)
  16.  
  17.         .setView(fl)
  18.  
  19.         .setTitle("Please enter something...")
  20.  
  21.         .setPositiveButton("OK", new DialogInterface.OnClickListener(){
  22.  
  23.                 @Override
  24.  
  25.                 public void onClick(DialogInterface d, int which) {
  26.  
  27.                         d.dismiss();
  28.  
  29.                         Toast.makeText(OuterActivity.this, "Value: " + input.getText().toString(), Toast.LENGTH_LONG).show();
  30.  
  31.                 }
  32.  
  33.         })
  34.  
  35.         .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
  36.  
  37.                 @Override
  38.  
  39.                 public void onClick(DialogInterface d, int which) {
  40.  
  41.                         d.dismiss();
  42.  
  43.                 }
  44.  
  45.         }).create().show();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Specific example from AndTip (available in the AndroidMarket):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final FrameLayout fl = new FrameLayout(this);
  2.  
  3. final EditText input = new EditText(this);
  4.  
  5. input.setGravity(Gravity.CENTER);
  6.  
  7.  
  8.  
  9. fl.addView(input, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
  10.  
  11.  
  12.  
  13. input.setText(taxDf.format(this.mHsTaxRate.getProgress() / 4.0f));
  14.  
  15. new AlertDialog.Builder(this)
  16.  
  17.         .setView(fl)
  18.  
  19.         .setTitle("Enter tax rate...")
  20.  
  21.         .setPositiveButton(R.string.set, new DialogInterface.OnClickListener(){
  22.  
  23.                 @Override
  24.  
  25.                 public void onClick(DialogInterface d, int which) {
  26.  
  27.                         d.dismiss();
  28.  
  29.                         try{
  30.  
  31.                                 final float newTax;
  32.  
  33.                                 final String taxString = input.getText().toString();
  34.  
  35.                                 if(taxString.contains(","))
  36.  
  37.                                         newTax = Float.parseFloat(taxString.replace(",", ".")) + 0.125f;
  38.  
  39.                                 else
  40.  
  41.                                         newTax = Float.parseFloat(taxString) + 0.125f;
  42.  
  43.                                
  44.  
  45.                                 AndTip.this.mHsTaxRate.setProgress((int)(newTax * 4));
  46.  
  47.                         }catch(Exception e){
  48.  
  49.  
  50.  
  51.                         }
  52.  
  53.                 }
  54.  
  55.         })
  56.  
  57.         .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
  58.  
  59.                 @Override
  60.  
  61.                 public void onClick(DialogInterface d, int which) {
  62.  
  63.                         d.dismiss();
  64.  
  65.                 }
  66.  
  67.         }).create().show();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


SDK Version compatible: 1.0 (maybe prior versions too)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

reading user input

Postby pentace » Tue Nov 04, 2008 6:49 am

can you please help better explain how to read what the user has typed? for instance after somone changes the text in .setTitle("Please enter something...") i would like to pass the inputed text but no matter what i do i either receive errors in the code or no input passed
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

Postby plusminus » Thu Nov 06, 2008 4:40 am

You read out the written text in the onClick-Method.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Figured

Postby pentace » Thu Nov 06, 2008 5:06 am

I had figured it out it was an id10t error thanks for responding though
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

THANK YOU, THANK YOU, THANK YOU

Postby matany » Sun Dec 13, 2009 6:02 pm

Thank you so VERY much. This helped me a LOT, more than you can imagine!!!!

I struggled this dialog issue for few days. I almost redesigned my app because of this issue.


YOU ARE SIMPLY THE BEST!!!!!!!

HAPPY DEVELOPER :-)))))))))))))))))))))))))))))))))))))

Y
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Postby Droid123 » Mon Dec 14, 2009 1:20 am

Is it possible to create an AlertDialog that has a handle to the component that called it?

For instance, if I click an item in a listview I want the dialog to display the contents of the listitem

Currently I'm using ListActivity+SimpleCursorAdapter+onContextItemSelected which works fun, but I like the flexibility of the AlertDialog but don't want to use global variables to copy the data, if you have any suggestions please let me know
Droid123
Experienced Developer
Experienced Developer
 
Posts: 77
Joined: Sat Nov 21, 2009 1:21 pm

Top

Re: Inputbox, InputField, Popup, AlertDialog, EditText

Postby sagaraborse » Mon May 30, 2011 6:18 pm

Hello, I tried above code but i got following result in logcat , Pls suggest something

LOGCAT:

05-30 22:31:33.232: INFO/ActivityManager(52): Displayed activity com.mymap/.HelloGoogleMaps: 2612 ms (total 2612 ms)
05-30 22:31:38.653: WARN/System.err(783): java.lang.NullPointerException
05-30 22:31:38.663: WARN/System.err(783): at android.os.Parcel.readException(Parcel.java:1224)
05-30 22:31:38.673: WARN/System.err(783): at android.os.Parcel.readException(Parcel.java:1206)
05-30 22:31:38.682: WARN/System.err(783): at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)
05-30 22:31:38.682: WARN/System.err(783): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
05-30 22:31:38.682: WARN/System.err(783): at com.mymap.InformOthersViaSMS.sendSmsMessage(InformOthersViaSMS.java:22)
05-30 22:31:38.692: WARN/System.err(783): at com.mymap.InformOthersViaSMS.sendSMS(InformOthersViaSMS.java:10)
05-30 22:31:38.702: WARN/System.err(783): at com.mymap.HelloGoogleMaps.onOptionsItemSelected(HelloGoogleMaps.java:365)
05-30 22:31:38.702: WARN/System.err(783): at android.app.Activity.onMenuItemSelected(Activity.java:2170)
05-30 22:31:38.712: WARN/System.err(783): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
05-30 22:31:38.712: WARN/System.err(783): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
05-30 22:31:38.712: WARN/System.err(783): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
05-30 22:31:38.722: WARN/System.err(783): at com.android.internal.view.menu.ExpandedMenuView.invokeItem(ExpandedMenuView.java:89)
05-30 22:31:38.733: WARN/System.err(783): at com.android.internal.view.menu.ExpandedMenuView.onItemClick(ExpandedMenuView.java:93)
05-30 22:31:38.733: WARN/System.err(783): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-30 22:31:38.733: WARN/System.err(783): at android.widget.ListView.performItemClick(ListView.java:3285)
05-30 22:31:38.743: WARN/System.err(783): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
05-30 22:31:38.753: WARN/System.err(783): at android.os.Handler.handleCallback(Handler.java:587)
05-30 22:31:38.753: WARN/System.err(783): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 22:31:38.762: WARN/System.err(783): at android.os.Looper.loop(Looper.java:123)
05-30 22:31:38.762: WARN/System.err(783): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-30 22:31:38.762: WARN/System.err(783): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 22:31:38.772: WARN/System.err(783): at java.lang.reflect.Method.invoke(Method.java:521)
05-30 22:31:38.772: WARN/System.err(783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-30 22:31:38.772: WARN/System.err(783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-30 22:31:38.772: WARN/System.err(783): at dalvik.system.NativeStart.main(Native Method)
05-30 22:31:44.352: DEBUG/dalvikvm(52): threadid=41: bogus mon 1+0>0; adjusting
05-30 22:31:44.473: DEBUG/dalvikvm(242): GC freed 43 objects / 2064 bytes in 142ms
05-30 22:31:49.233: INFO/NotificationService(52): enqueueToast pkg=com.mymap callback=android.app.ITransientNotification$Stub$Proxy@43ba0430 duration=1



It is null pointer Exception what can be solution for this???
User avatar
sagaraborse
Freshman
Freshman
 
Posts: 2
Joined: Mon May 30, 2011 6:03 pm

Re: Inputbox, InputField, Popup, AlertDialog, EditText

Postby sblantipodi » Fri Sep 02, 2011 6:50 pm

this snippet works and is similar to what I wrote for my app but in this way you need to tap
two times the screen to enter something.

The first tap shows the edittext, the second one edit it and shows the soft keyboard.

How can we show the soft keyb immediately?
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Re: Inputbox, InputField, Popup, AlertDialog, EditText

Postby irmoathlete » Thu Jan 26, 2012 9:13 pm

PlusMinus, this really helped I appreciate it. (I'm new to this forum so I just thought I'd give you credit for the snippet)
irmoathlete
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jan 26, 2012 9:11 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 3 guests