Android, using Edittext - java

I've got a few questions about edittext..
I'm trying to create a login screen on Android. I'm taking in a username and a password and then sending the results to another method. I've just got a few questions about it...
1) If I have two edittext's and the user enters values in both and then hits a "login" button, I want the button to take the values that were entered in the 2 edittext boxes, convert and save them to a string and then pass them to a function. I'm unsure how I'd construct this button. The problem I'm having is making it a string I'm currently doing this..
String user = edittext.getText();
But that doesn't seem to work. Also I'm unsure what way Android saves Edittext inputs.. Will it just automatically save it to the string once the user is no longer working out of the box? e.g will...
public boolean onKey(View v, int keyCode, KeyEvent event) {
String user = edittext.getText();
}
work? If not how would I define it?
Thanks.

You have to subscribe for the onClick event of the button,
then simple get the text from the EditText
You don't need to run code on the onKey event of EditText
(Button) findViewById(R.id.btnName)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//called when you press the button
String user =((EditText) findViewById(R.id.txtUser)).getText().toString();
String password =((EditText) findViewById(R.id.txtPassword)).getText().toString();
// do the rest of the job
}
});

Essentially this is what you want to do
String userName = "";
String password = "";
EditText userEdit = (EditText)findViewById(R.id.useredit);
EditText passEdit = (EditText)findViewById(R.id.passedit);
Button okButton = (Button)findViewById(R.id.okButton);
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
userName = userEdit.getText().toString();
password = passEdit.getText().toString();
}
});
Instead of using onKeyDown(...) you should register an on-click listener for your button and define its behavior there. Strangely enough you actually have to put .toString() after .getText() in order to store the text.
I would also suggest making all of your views (fields, buttons, images, ect) member variables so your references are persistent.

Related

i have made one app to get ascii value of my character that i am going to put in my edit text but when i press my button to conver my app gets closed

i have made one app to get ascii value of my character that i am going to put in my edit text. i have made the code as-> `
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
TextView textview=findViewById(R.id.textView2);
EditText edittext=findViewById(R.id.editTextTextPersonName);
String sipla=edittext.getText().toString();
Button button2 = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int t=sipla.charAt(0);
textview.setText("THE ASCII value of your character = "+t);
}
});`
but when i click my button my app gets closed. one more think
when i make certain changes in my code and put String sipla=edittext.getText().toString(); in my button setonclick function it works and everything is normal please tell me why my app was not working prevously .
The reason your app is crashing is because sipla is empty, and you try to get the first character from an empty string. The reason it is empty is that you get it in onCreate, before the user has had a chance to enter anything in the EditText. Even if they enter something later, you never update the value of sipla.
The fix for this is simple - don't get the value from the EditText until you are actually ready to use it and the user would have had time to enter something - so get it inside onClick.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This code runs as the activity is getting set up
Button button=findViewById(R.id.button);
TextView textview=findViewById(R.id.textView2);
EditText edittext=findViewById(R.id.editTextTextPersonName);
Button button2 = findViewById(R.id.button2);
// if you get the value of edittext here it will always be empty,
// at this point the user hasn't even seen the screen yet
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// This code runs much later, when the user
// clicks the button
// Don't get the string until you are ready to process it, this code
// doesn't run until the user clicks the button, at which
// time edittext may also have a user value in it
String sipla = edittext.getText().toString();
if( !sipla.isEmpty() ) {
int t=sipla.charAt(0);
textview.setText("THE ASCII value of your character = "+t);
}
else {
textview.setText("You didn't enter anything");
}
}
});
}
Also, if your app crashes, look in the Logcat tab to see the full error message and stack trace. Learn to read that - it is an invaluable debugging skill. Also, learn to use Log/print statements in your code to help you understand what values are being used and what order things are run in when you are confused.

How can I use a double between two classes?

I want to build a currency calculator.
There is a plain text and a button.
if somebody scribe a number in the plain text and press the button a dialog will be shown.
BUT the number of the plaintext is everytime 2.1311!
here is my code
//this is the Main Activity
public class MainActivity extends AppCompatActivity {
public void PesoInEuro (View view){
EditText Peso = findViewById(R.id.EuroBetrag);
String amountPeso = Peso.getText().toString();
double amountPesodouble = Double.parseDouble(amountPeso);
double amountEurodouble = amountPesodouble * 46.85;
String amountEuro = String.valueOf(amountEurodouble);
Button buttonOne = (Button) findViewById(R.id.PesoEuro);
buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
}
public void openDialog(){
DiaPesoEuro exampleDialog = new DiaPesoEuro();
exampleDialog.show(getSupportFragmentManager(), "example Dialog");
}
First of all, it's a good practice to find your views in the OnCreate method.
Secondly, you have read and calculate your values when the user hit the button.
In the code above, you got the data in the PesoInEuro, and when the user presses the button, it shows the retrieved data and your calculation is based on them.

Login database issues

I am trying to fill a database with records so that when you login it will either accept you or not. For now I just have one to test it out but it doesn't seem to work. When I put in the info for ID and password and click the Button, nothing happens. Can anyone tell me what I'm missing or doing wrong? Sorry if it's obvious, but I'm just stuck right now.
https://github.com/liliycode/Login/blob/master/login%20java
Your click listener is not correct. You should define click listener for button in onCreate method. When you click button you get data from edittext and invoke method which inserts data to DB.
First of all your are comparing EditTexts to strings which is wrong
if ( loginET.equals("Liliy") && login2ET.equals("1234"))
change this to
if ( loginET.getText().toString().equals("Liliy") && login2ET.getText().toString().equals("1234"))
And make your buttonListener in onCreate(), and at click of button call your method populateData()
pressBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//reading the inputs
String idET = loginET.getText().toString();
Double passET = Double.parseDouble(login2ET.getText().toString());
populateData();
}
});
Which checks for data and populates it.

EditText returning same value when activity is created again after the back button is pressed

That's my first post obviously here...okay so my problem is as follows:
#android: i have a TextEdit that a user enters in text, i get the text by getText() and it all works fine, but when i press the back button , and again come back to the same activity of the TextEdit and try to get the text again, it returns and empty string"" eventhough a different text is shown in the TextEdit view.
Could it be that the TextEdit is still "relating or corresponding" to the old closed activity?
Any ideas ?
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.messaging_screen2);
editText = (EditText) findViewById(R.id.msgText);
Button send = (Button)findViewById(R.id.Go);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textToSend=editText.getText().toString();
}
});
}
Then in an AsynckTask i send the textToSend,
#Override
protected String doInBackground(Void... arg0) {
while(!done)
{
if(isCancelled())
{
done=true;
break;
}
if(text.contains(Protocols.RESPONSE_OK))
{
System.out.println("getting message from edit text: "+textToSend);
publishProgress(textToSend);
}
}
return null;
}
Now the when i can send msgs normally but when i press the back button and then start the same activity again, the FIRST msg sent is exactly the old one( upon pressing the send button for the first time ). when i press the send button again, the shown text in the EditText is sent properly.
so any help ?
Thanks.
After some debugging i noticed that the value of textToSend in send.Onclick() method is not the same as the one in the doInBackGround of the AsynckTask after creating the activity again. This happens on the first click of send, after witch everything works fine.
Its weird, Paste your code,
Try set edittext.setText("test") in button's onClick() method. And check what return edittext.getText() when u back.
To change Activity use you this syntax?
Intent direction = new Intent(Class.this, DirectionClass.class);
startActivity(direction);

Formating the text within an EditText box after the user has finished

I would like to change the contents of an EditText box after the user has finished. So say if the user has entered a line of numbers such as 50126057, after he/she has finished I need some characters added to it before or after the user clicks the send button on my application. It needs to look like this: scn|50126057{) so that my application on the PC will understand it. Is there anyway this can be done?
Thanks and any help is much appreciated
P.S Sorry for spelling/grammar
While clicking on Button event,
btn.setOnClickListener(new onClickListener(){
public void onClick(View v){
String data="scn|"+edt.getText().toString()+"{)";
}
});
EditText word = (EditText) findViewById(R.id.username);
//inside button click event
//assign the word entered in the edit text to a string variable(in this case 'wordtoformat')
String wordtoformat=word.getText().toString();
//Now format the word as you want
wordtoformat="scn|"+wordtoformat+"{)";
//now assign the formated string back to the edit text
word.setText(text);
Hope it worked
You can use the OnFocus listener, something like this:
EditText et = (EditText)findViewById(*editText id*);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
String text = et.getText().ToString();
String newText = "scn|"+text+"{)";
et.setText(newText);
}
});
Sorry if I am not 100% accurate, but I've answered from work I've got no Android IDE here :)
Hope it helps you!
PS: The user is going to see the modified text only if he loses the focus of the EditText and then clicking the button or if by clicking the button the user won't leave the activity. But for the purpose of modifying the text, should work fine.

Categories

Resources