Get entire text from EditText - java

When I enter "sa" into EditText it takes only s. How can I pass my entire text?
Suppose I want to compare the text with the string "sa", how to get entire text from edittext?
e = (EditText) findViewById(R.id.selection);
//edittext watcher
e.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//getting edittext
String s1 =s.toString();
System.out.println("edittext :" + s1);
// compare edittext string woth arraylist
if (s1.length() >=1) {
//taking stringbuilder to add list items StringBuilder sb = new StringBuilder();
for(inti=0;i<orginalarrayelements.length;i++)
{
//compare edittext with arraylist with region matches if(orginalarrayelements[i].regionMatches(true, 0, s1, 0, s1.length())){
sb.append(orginalarrayelements[i] +",");
System.out.println("modified array: " + sb);
}
}

I think you are trying to get the full text from your EditText, right?
OnTextChanged gets called every time your text changes, so it will always only contain either one letter or null if you deleted a letter.
If you want to get the full text, call e.getText().toString(); in your OnTextChanged, OnClick on a Button or anywhere else.
By the way: rethink you names: "e" is not a name you should use. Take something like "editText", "myEditText", "usernameEditText",...

Related

Checking for space in an edittext

I want to check if there's any space in an edittext. I use this method to get the selected text of edittext:
private String getSelection(EditText editText) {
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
Editable edit = editText.getText();
return edit.toString().substring(start, end);
}
But when i do this:
if (getSelection(et).getText.toString.contains(" ")) Log.d("TAG","There's a space")
contains() will be always false.
I think this:
if (getSelection(et).getText.toString.contains(" ")).....
Must be like this:
if (getSelection(et).contains(" ")) .....

EditText to enter 3 alphas and 4 digits - change input type dynamically

I need to set one of my EditText to enter three alpha value and four digits Ex:- ABC1234. So I need to change automatically input type from Text to phone when user typed three alphas. I came up with following code and but its not change to phone input type and text type is remains for any text length.
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String text = editText.getText().toString();
if(text.length()==1 || text.length()==2 || text.isEmpty()){
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
System.out.println("Length Text type : " + text.length());
}else{
editText.setRawInputType(InputType.TYPE_CLASS_PHONE);
System.out.println("Length phone type : " + text.length());
}
}
What I have done wrong.
setRawInputType() is usually used when you initialize the view, in a constructor of a custom view or in onCreate() method of an activity.
In your case you have to call setInputType() change the mode of the soft keyboard.

Need Help to optimize the following java code. It is the code for setting color of specific keywords in a textview.(Android Application)

I'm developing an app to view C-programs.I wanted to give a simple color scheme to the text which is stored in database,retrieved as string and then passed on to the textview.
The code I have written assigns green color to header file declarations and brackets,blue color is assigned to numbers,printf,scanf...red is assigned to datatypes such as int,char,float.
It is however very inefficient.Before applying this color scheme,my app was displaying the textview activity instantly.Now,depending on the length of the programs it takes up to 4 to 5 seconds which is really poor performance.
what it does is,it takes one keyword at a time,then iterates the complete text of textview looking for that particular keyword only and changes its color,sets the text again.
Thus,it traverses text of the entire textview 29 times as I have defined 29 keywords in String arrays( namely keywordsgreen,keywordsblue,keywordsred).
The activity's onCreate function contains the following code :
textView = (TextView) findViewById(R.id.textView1);
textView.setText(programtext);
textView.setBackgroundColor(0xFFE6E6E6);
//The problem stars here
String [] keywordsgreen={"#define","#include","stdio.h","conio.h","stdlib.h","math.h","graphics.h","string.h","malloc.h","time.h","{","}","(",")","<",">","&","while ","for "};
for(String y:keywordsgreen)
{
fontcolor(y,0xff21610B);
}
String [] keywordsred={"%d","%f","%c","%s","int ","char ","float","typedef","struct ","void "};
for(String y:keywordsred)
{
fontcolor(y,0xFFB40404);
}
String [] keywordsblue={"printf","scanf","\n","getch","0","1","2","3","4","5","6","7","8","9"};
for(String y:keywordsblue)
{
fontcolor(y,0xFF00056f);
}
The fontcolor function is as follows :
private void fontcolor(String text,int color)
{
Spannable raw=new SpannableString(textView.getText());
int index=TextUtils.indexOf(raw, text);
while (index >= 0)
{
raw.setSpan(new ForegroundColorSpan(color), index, index + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}
textView.setText(raw);
}

Add a character in an EditText

I need to add a character "S" right after the last written number in an EditText (Oe) so:
if i write i number : "123", it must send "123S" instead. If i write "1234", it must send "1234S" instead.
How to do that ?
my code:
((Button) findViewById(R.id.btn_write_HRM_Noty)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
str = Oe.getText().toString();
str = str.substring(0, 0) + "G" + str.substring(0, str.length());
mService.enableHRNotification(mDevice, str);
}
});
I'm a little confused. Are you simply asking how to add a character to a string? If so: str+='S'; will work. Then you simply add the string back to the EditText with .setText(str) or simply put it back into your notification with 'S' added (as Blundell suggested).
mService.enableHRNotification(mDevice, str + "S");
1) Make a new string including by putting together the edittext and the character you want to add finalString = (string + "s"). Then use the setText()method to change the contents shown in the edittext
2) You could do this directly after the user enters something in the text box (after it loses focus or they finish typing and go to the next field)
Check this thread out: How can i know when a edittext lost focus

how do you call a value entered in a UI to your java class?

Making a simple app for my android.
in my xml interface there is a text box in which the user will input a number (for example: 10). the id of this text box is "input1"
how do I call the value of input1 in java and then perform a calculation on it?
For example...
suppose input1 = x
x + 5 or x*2
and for that matter, how do I have the resulting value appear as constantly updated text output in a specified place on the UI?
In the Activity where you are using this layout XML, you would do this:
private EditText input;
private EditText result;
public void onCreate(Bundle b) {
setContentView(R.layout.my_activity);
// Extract the text fields from the XML layout
input = (EditText) findById(R.id.input1);
result = (EditText) findById(R.id.result);
// Perform calculation when input text changes
input.addKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keycode, KeyEvent keyevent) {
if (keyevent.getAction() == KeyEvent.ACTION_UP) {
doCalculation();
}
return false;
}
});
}
private void doCalculation() {
// Get entered input value
String strValue = input.getText().toString;
// Perform a hard-coded calculation
int result = Integer.parseInt(strValue) * 2;
// Update the UI with the result
result.setText("Result: "+ result);
}
Note that the above includes no error handling: it assumes that you have restricted the input1 text field to allow the input of integer numbers only.
In your XML you can also set android:inputType="number" to only allow numbers as entries.

Categories

Resources