How to use same Edittext as two different in Android?

52 views Asked by At

Hi I am a Beginner in Android Development. I want to display data from xampp (phpmyadmin) in EditText if EditText value is not equal to ""(data from xampp!="") and if equal to ""(data from xampp==""), I want the user to type the value .{if the value is displayed it should not editable for user} I tried if..else

`

if(DepartStation_txt!=""){
   DS.setText(DepartStation_txt);
   DS.setEnabled(false);
}else {
    DS.setText("Enter the Value");
    DS.setEnabled(true);
}

` where DS is EditText DS=

(EditText) findViewById(R.id.DS);

in onCreate

Any functions that does this job and reason for this not working

1

There are 1 answers

0
Kozmotronik On

Simply use methods of String class to compare strings. Now first of all you need to read text from EditText control then you check if the string you want presents. For your case the sample code would be:

String DepartStation_txt;  // I don't know where do you get it from
String currentString = DS.getText().toString; // Read the current string first, if any 

if(currentString != null) {
    if(!currentString.isEqual(DepartStation_txt)) {
        DS.setText(DepartStation_txt);
        DS.setEnebled(false);
    }
    else {
        DS.setHint("Enter the value");
        DS.setEnabled(true);
    }
}
else {
// EditText has no string at all, user has to enter the value??
    DS.setHint("Enter the value");
    DS.setEnabled(true);
}

Note that I used the setHint method instead of the setText method since it is more suitable to instruct the users.