Get Length of String and set Max.Length of EditText

Put your problem here if it does not fit any of the other categories.

Get Length of String and set Max.Length of EditText

Postby travisjames » Thu Apr 09, 2009 6:53 am

Is there anyway to get the current length of the string within an EditText and is there a way to set Max Length of the EditText?

Any help is appreciated.
travisjames
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 02, 2009 10:00 pm
Location: Cordova, Tennessee

Top

Postby WarrenFaith » Thu Apr 09, 2009 7:44 am

hm... did you know, that you have R.attr where you can look for something like maxLength? Or use code completion in Eclipse...
check this out: android:maxLength

To get the string, simply use the OnKeyListener and set it to your EditText...
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby travisjames » Thu Apr 09, 2009 7:49 am

Thank you so much, I have one more question.

I have this code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.widget.Toast;
  2.  
  3. import android.content.SharedPreferences;
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. public class TwitterIt extends Activity {
  12.  
  13.     /** Called when the activity is first created. */
  14.  
  15.     @Override
  16.  
  17.     public void onCreate(Bundle savedInstanceState) {
  18.  
  19.         super.onCreate(savedInstanceState);
  20.  
  21.         setContentView(R.layout.main);
  22.  
  23.        
  24.  
  25.        
  26.  
  27.         Button update = (Button) findViewById(R.id.update);
  28.  
  29.         update.setOnClickListener(new Button.OnClickListener() {
  30.  
  31.                 public void onClick(View v) {
  32.  
  33.                         EditText status = (EditText) findViewById(R.id.status);
  34.  
  35.                         String updtext = status.getText().toString();
  36.  
  37.                        
  38.  
  39.                         SharedPreferences settings = TwitterIt.this.getSharedPreferences
  40.  
  41.                         ("com.travisjames.twitterit_preferences", 0);
  42.  
  43.                         String value = settings.getString("user", "defaultvalue");
  44.  
  45.                         String value2 = settings.getString("pass", "defaultvalue");
  46.  
  47.                        
  48.  
  49.                         if (value == "" || value2 == "") {
  50.  
  51.                                 Toast.makeText(TwitterIt.this, "Please enter your Twitter Username in Settings", Toast.LENGTH_LONG).show();
  52.  
  53.                         }
  54.  
  55.                         else {
  56.  
  57.                         Twitter twitter = new Twitter(value,value2);
  58.  
  59.                         twitter.setSource("twitterit");
  60.  
  61.                         twitter.updateStatus(updtext);
  62.  
  63.                         status.setText("");
  64.  
  65.                 }
  66.  
  67.                 }
  68.  
  69.         });
  70.  
  71.     }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


For some reason, when I empty the username preference and push my button, the application crashes and does not display the Toast I have created.
travisjames
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 02, 2009 10:00 pm
Location: Cordova, Tennessee

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests