I have TextView in my code. I want to test if an EditText is empty then fill the TextView with "some thing" or take the text from the EditText; but it doesn't change the text. Here is the code(in onCreate() method):
if ((textCity.length())==0){
cityText.setText("something");
}
else
cityText.setText(textCity);
where textCity
textCity=editText1.getText();
and cityText is the TextView
You could do something like this:
if (textCity.getText().toString().trim().length() == 0) {
cityText.setText("something");
}
else
cityText.setText(textCity);
Try this
if ((textCity.length() < 0)){
cityText.setText("something");
}
textCity=editText1.getText().toString(); //add this when you're grabbing the text from the
//textview
I assume that you haven't invoked it in the right place. You need to place a TextWatcher on your EditText. Try this out:
editText1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
cityText.setText(String.valueOf(s));
}
});
Related
I tried everything.
if (editText == null)...
if (etNumero1.getText().toString().isEmpty())...
if (etNumero1.getText().toString().equals("")...
if (etNumero1.getText().toString().trim().length() >= 0))
Nothing works. Even my teatcher cant tell whats wrong.
Sorry for bad english im Brazilian.
Check, If the value is not an Empty
if (!etNumero1.getText().toString().isEmpty())
{
if(etNumero1.getText().toString().contains("."))
{
//Do your functionality here
}
}
else
{
//
}
Try
if (Integer.parseInt(etNumero1.getText().toString()) > 0)
If you specify your question, I could answer in better condition.
Use TextUtils.isEmpty("") check for null if not convert to formart to ###.##
Make sure that you are not making mistake on editText etNumero1??
I know editText is Edit Text but what is etNumero1?? please replace etNumero1 to editText, it should work
if (editText.getText().toString().isEmpty())...
Example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.editText);
Button button = findViewById( R.id.button );
button.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick (View v) {
if(editText.getText().toString().isEmpty()){
System.out.println("empty string");
}else{
System.out.println("" + editText.getText().toString());
}
}
});
}
if(String.valueOf(editText.getText() != null){
Double double = Double.parseDouble(editText.getText().toString);
}
I am working with validations in edit text. I have 5 edit text in my form and what I want is if the any of the edit text is null then it should not break its focus. I have tried verified answer from this link.
but in my case of 5 edit text its not working properly.
Please Help, Thanks.
Try this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1=editText1.getText().toString().trim();
String val2=editText2.getText().toString().trim();
String val3=editText3.getText().toString().trim();
String val4=editText4.getText().toString().trim();
String val5=editText5.getText().toString().trim();
if(TextUtils.isEmpty(val5)){
editText5.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText4.setFocusable(true);
}
if(TextUtils.isEmpty(val4)){
editText4.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
}else {
editText3.setFocusable(true);
editText1.setFocusable(false);
editText2.setFocusable(false);
editText4.setFocusable(false);
}
if(TextUtils.isEmpty(val3)){
editText3.requestFocus();
editText1.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText2.setFocusable(true);
}
if(TextUtils.isEmpty(val2)){
editText2.requestFocus();
}else {
editText1.setFocusable(true);
}
if(TextUtils.isEmpty(val1)){
editText1.requestFocus();
}
if(!TextUtils.isEmpty(val1) &&
!TextUtils.isEmpty(val2) &&
!TextUtils.isEmpty(val3) &&
!TextUtils.isEmpty(val4) &&
!TextUtils.isEmpty(val5) ){
Toast.makeText(MainActivity.this, "PASS", Toast.LENGTH_SHORT).show();
}
}
});
First check EditText empty,null or matches to some specific text
based on your requirement.
Check 5 EditTexts using if & else if statements. if a condition is true, change things to EditText which
you want to do.
In else part, set things you want to set if, if or else if conditions false.
According to my understanding in your question, I wrote a code. try it.
final EditText et1 = (EditText) findViewById(R.id.et1); //get referance to all 5 EditTexts. I showed for 2 EditTexts
final EditText et2 = (EditText) findViewById(R.id.et2);
if (et1.getText().toString().length() < 1) { //check EditText empty or not.
//do what you want to edit/change in EditText. like below.
et1.setFocusableInTouchMode(true);
et1.requestFocus();
} else if (et2.getText().toString().length() < 1) {
//Check for second EditText empty or not. Do this for all 5 edit texts.
et2.setFocusableInTouchMode(true);
et2.requestFocus();
}
else{
//if All EditTexts not empty. do what you want to edit/change in edittext.
et1.setFocusableInTouchMode(false);
et2.setFocusableInTouchMode(false);
}
I want to add a prefilled text like country code in an edittext for mobile number. Prefilled text should be uneditable. I have tried it by overriding onSelectionChanged(). Is there any better solution?
final EditText editText = new EditText(this){
int prevSelection;
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
if(selStart < 4){
setSelection(prevSelection);
return;
}
prevSelection = selStart;
}
};
Is this what you want?
editText.setText("This is a prefilled text"); // This to set prefilled text
editText.setFocusable(false); // This disable editing
OR
editText.setHint("This is sample country code"); // this is the hint
Hint is something like this:
try this
final EditText editText = new EditText(this){
int prevSelection;
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
if (!editText.getText().toString().startsWith("pre filled text")){
editText.setText("pre filled text" + editText.getText().toString());
}
}
};
The function will check if the EditText start with the "preFilled" text, if not it will add it to the beginning of the EditText
Implement android.text.TextWatcher in the Activity or Fragment containing the EditText.
In the afterTextChanged method add the country code to the beginning of the text.
It would be good to check if the country code is already filled in before adding it. Be careful not to end in an infinite loop here.
Try to use this:
editText.setText("country code" + editText.getText().toString());
http://pastebin.com/YmeB2D1N
Essentially, my program has two EditTexts, a Button, and a TextView. You input a number/string into the EditText, and an output is supposed to be shown in the TextView. However, all i get is a 'There is no Department'. How do i fix this?
I don't see anywhere that you are setting the values of department or name variables.
you can do:
EditText departmentEt = (EditText)findViewById(r.id.department_edit_text));
EditText nameEt = (EditText)findViewById(r.id.name_edit_text));
search.setOnClickListener(new OnClickListener) {
public void onClick(View v) {
if(deparmentEt.length() > 0) {
department = Integer.parseInt(departmentEt.getText().toString());
}
if(nameEt.length() > 0) {
name = nameEt.getText().toString();
}
//The rest of your code
}
}
use onTextChangedListener for EditText and
in that method write get value of EditText and set it to TextView.
I hope it works.
Code
textView= (TextView)findViewById(R.id.text);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
textView.setText(textMessage.getText().toString());
}
});
I have an EditText and a TextWatcher.
Skeleton of my code:
EditText x;
x.addTextChangedListener(new XyzTextWatcher());
XyzTextWatcher implements TextWatcher() {
public synchronized void afterTextChanged(Editable text) {
formatText(text);
}
}
My formatText() method inserts some hyphens at some positions of the text.
private void formatText(Editable text) {
removeSeparators(text);
if (text.length() >= 3) {
text.insert(3, "-");
}
if (text.length() >= 7) {
text.insert(7, "-");
}
}
private void removeSeparators(Editable text) {
int p = 0;
while (p < text.length()) {
if (text.charAt(p) == '-') {
text.delete(p, p + 1);
} else {
p++;
}
}
}
The problem I have is - what is displayed on my EditText isn't in sync with the Editable. When I debugged the code, I saw that the variable text (Editable) has the expected value, but what's shown on the EditText doesn't always match the Editable.
For example, when I have a text
x = "123-456-789"
I cut the text "456" from x manually.
After formatting, my Editable has the value "123-789-"
However, the value shown on my EditText is "123--789"
They have the same value in most cases though.
I assumed that the EditText IS the Editable and they should always match. Am I missing something?
Ok, you never actually change the EditText just the Editable. Android EditTexts are not children of the Editable class. Strings are subclasses of the Editable class. The onTextChangedListener doesn't receive the EditText as an argument but the Editable/String displayed in the EditText. After you format the Editable with the hyphens you then need to update the EditText. Something like this should work fine:
class MyClass extends Activity{
//I've ommited the onStart(), onPause(), onStop() etc.. methods
EditText x;
x.addTextChangedListener(new XyzTextWatcher());
XyzTextWatcher implements TextWatcher() {
public synchronized void afterTextChanged(Editable text) {
String s = formatText(text);
MyClass.this.x.setText(s);
}
}
}
To prevent the slowdown why not change the formatText method something like this?
private Editable formatText(Editable text) {
int sep1Loc = 3;
int sep2Loc = 7;
if(text.length==sep1Loc)
text.append('-');
if(text.length==sep2Loc)
text.append('-');
return text;
}
Note: I haven't tested this