Text input UI control

  ... the case you feel the need for a new feature or want to submit one.

Text input UI control

Postby bigpete591 » Mon Mar 26, 2012 3:07 pm

Class for allowing users to enter text into an input box. Uses an android dialog for the input. Class is based off SpriteButton so it expects a tiled bitmap for the input box background.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import org.andengine.entity.sprite.ButtonSprite;
  2. import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
  3. import org.andengine.entity.text.Text;
  4. import org.andengine.opengl.font.Font;
  5. import org.andengine.opengl.texture.region.TiledTextureRegion;
  6. import org.andengine.opengl.vbo.VertexBufferObjectManager;
  7. import org.andengine.ui.activity.BaseGameActivity;
  8.  
  9. import android.app.AlertDialog;
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.content.DialogInterface.OnShowListener;
  13. import android.text.InputType;
  14. import android.view.Gravity;
  15. import android.view.inputmethod.InputMethodManager;
  16. import android.widget.EditText;
  17.  
  18. public class InputText extends ButtonSprite implements OnClickListener {
  19.  
  20.         private final String    mTitle;
  21.         private final String    mMessage;
  22.         private final Text              mText;
  23.         private boolean                 mIsPassword;
  24.         private String                  mValue;
  25.         private BaseGameActivity mContext;
  26.  
  27.         public InputText(float pX, float pY, final String title, final String message, TiledTextureRegion texture,
  28.                         Font font, int textOffsetX, int textOffsetY, VertexBufferObjectManager vbo, BaseGameActivity context) {
  29.                 super(pX, pY, texture, vbo, null);
  30.  
  31.                 this.mMessage = message;
  32.                 this.mTitle = title;
  33.                 this.mContext = context;
  34.                 this.mText = new Text(textOffsetX, textOffsetY, font, "", 256, vbo);
  35.                 attachChild(this.mText);
  36.                 setOnClickListener(this);
  37.         }
  38.  
  39.         public String getText() {
  40.                 return this.mValue;
  41.         }
  42.  
  43.         public boolean isPassword() {
  44.                 return this.mIsPassword;
  45.         }
  46.  
  47.         @Override
  48.         public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
  49.                 showTextInput();
  50.         }
  51.  
  52.         public void setPassword(final boolean isPassword) {
  53.                 this.mIsPassword = isPassword;
  54.         }
  55.  
  56.         public void setText(String text) {
  57.                 this.mValue = text;
  58.  
  59.                 if (isPassword() && text.length() > 0)
  60.                         text = String.format("%0" + text.length() + "d", 0).replace("0", "*");
  61.  
  62.                 this.mText.setText(text);
  63.         }
  64.  
  65.         public void showTextInput() {
  66.                 mContext.runOnUiThread(new Runnable() {
  67.                         @Override
  68.                         public void run() {
  69.                                 final AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
  70.  
  71.                                 alert.setTitle(InputText.this.mTitle);
  72.                                 alert.setMessage(InputText.this.mMessage);
  73.  
  74.                                 final EditText editText = new EditText(mContext);
  75.                                 editText.setTextSize(20f);
  76.                                 editText.setText(InputText.this.mValue);
  77.                                 editText.setGravity(Gravity.CENTER_HORIZONTAL);
  78.                                 if (isPassword())
  79.                                         editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  80.  
  81.                                 alert.setView(editText);
  82.  
  83.                                 alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  84.                                         @Override
  85.                                         public void onClick(DialogInterface dialog, int whichButton) {
  86.                                                 setText(editText.getText().toString());
  87.                                         }
  88.                                 });
  89.  
  90.                                 alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  91.                                         @Override
  92.                                         public void onClick(DialogInterface dialog, int whichButton) {
  93.  
  94.                                         }
  95.                                 });
  96.  
  97.                                 final AlertDialog dialog = alert.create();
  98.                                 dialog.setOnShowListener(new OnShowListener() {
  99.                                         @Override
  100.                                         public void onShow(DialogInterface dialog) {
  101.                                                 editText.requestFocus();
  102.                                                 final InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
  103.                                                 imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
  104.                                         }
  105.                                 });
  106.                                 dialog.show();
  107.                         }
  108.                 });
  109.         }
  110.  
  111. }
  112.  
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Example usage:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. InputText  password = new InputText(45, 465, "Password", "Enter password", mInputBoxTexture, mSmallFont, 17, 19, this.getVertexBufferObjectManager(), this);
  2. password.setPassword(true);
  3. mScene.attachChild(password);
  4. mScene.registerTouchArea(password);
  5.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
bigpete591
 
Posts: 9
Joined: Sun Dec 25, 2011 9:16 am

Re: Text input UI control

Postby Olieh » Mon Mar 26, 2012 4:12 pm

Do you mind if I add this to AndEngineHelperClasses? (https://github.com/aliHafizji/AndEngineHelperClasses) Its quite a useful control
Olieh
 
Posts: 7
Joined: Thu Jul 28, 2011 7:55 am

Re: Text input UI control

Postby bigpete591 » Mon Mar 26, 2012 4:19 pm

Sure, no problem.
bigpete591
 
Posts: 9
Joined: Sun Dec 25, 2011 9:16 am

Re: Text input UI control

Postby Sergio » Fri Aug 02, 2013 6:29 pm

It works wonders, thanks :)
Image
Sergio
 
Posts: 14
Joined: Wed Jul 10, 2013 6:12 pm

Re: Text input UI control

Postby kabuto178 » Tue Sep 24, 2013 8:42 pm

Thanks for the advice there, didn't know we had to use this kind of implementation with andengine I had thought it was implemented by default.
kabuto178
 
Posts: 105
Joined: Sun Sep 08, 2013 2:31 am

Re: Text input UI control

Postby kingswitch » Wed Jul 23, 2014 11:51 am

Does the code above works for GLES2???
and what does the mScene represent???
kingswitch
 
Posts: 8
Joined: Mon Jun 09, 2014 11:52 am

Re: Text input UI control

Postby Dantel » Thu Dec 04, 2014 7:23 pm

kingswitch wrote:Does the code above works for GLES2???
and what does the mScene represent???


Yes it should work. And mScene is your current scene, the one that you display at the moment you want to display this dialogue.
Come over to the new AndEngine forum! Don't stay here with all this spam.
andengine-community.com
(alternative domain still works: http://andengine.bbforum.co)
User avatar
Dantel
 
Posts: 246
Joined: Mon Jan 07, 2013 12:00 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 8 guests