getting data from EditText - java

I am trying to make a simple android application which contains a EditText and a Button . User will enter his data and this application will show him the data as a Toast.
Now The problem I am facing is , whenever button is pressed by user it's just showing a blank toast.Here is my code
EditText et;
String data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bsave=(Button) findViewById(R.id.bsave);
et=(EditText) findViewById(R.id.editText1);
data=et.getText().toString();
bsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("EditText",et.getText().toString());
Intent in=new Intent(MainActivity.this,SecondAct.class);
in.putExtra("DATA", data);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
}
please help me . Thanks in Advance!!!!

You are calling gettext() in oncreate(),when there is no text at all in editbox.. So your log inside onClick() will show data but toast will never show any thing with this code as variable data is ""..

You should do like
bsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
data=et.getText().toString();
Toast.makeText(MainActivity.this,data, Toast.LENGTH_SHORT).show();
}
});
in button onClick(...)

As shown above you need to save the data in the EditText inside of the Button's on click listener. As your code is now as soon as the edit text is created the text is being saved into the data variable, but you want it to set as soon as the button is clicked.

Related

Put text under cursor in edittext from java

I have got a two EditText in activity. I need after some event put text in current position of cursor. How can I do that?
et1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et1.setText("myString");
}
});
et1 is your edit text, do it for the second EditText either.

Convert Text from EditText to Integer Value Error

So I'm developing currently my own private App which I want to use only for me and maybe some friends.
Well I'm german so my english is maybe not the best I hope you can forgive me.
Now my Problem is that I want to set in my Optionsmenu a Budget for the current month to keep track of. I'm doing that by using an EditText with a Button.
Now I want to save this String which is getting entered in my EditText to a String value and a Integer value because I want to show the Value in a TextView on my MainPage and use the Integer value to calculate my current budget I got and so on.
Now I'm showing you guys my code and I hope u can tell me whats wrong with it.
I'm trying to get my Value in the Options class which is related to my OptionsMenu and later Trying to get the Value out of my Options class into my Main class.
public class Options extends Activity {
Button GoBack, SetBudget;
private int Budget;
String BudgetString = "";
EditText BudgetEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
GoBack = (Button) findViewById(R.id.button2);
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent GoBackToMain = new Intent("com.lunarrepublic.STARTINGPOINT");
startActivity(GoBackToMain);
}
});
SetBudget = (Button) findViewById(R.id.button8);
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
}
But if I try to set my "BudgetString" to Editable It won't work either.
The GoBack Button is unnecessary for my problem here so you don't have to read over it.
So I hope you guys understood what my problem is and can maybe help me getting it fixed
Thanks in advance
This is wrong
BudgetString = EditText.getText();
Use below one
BudgetString = BudgetEdit.getText().toString();
change this
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
to
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) Options.this.findViewById(R.id.editText1);
BudgetString = BudgetEdit.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});

Android - Saving login details

I am struggling in developing a way to have an option for the user to save their details when they login, I have searched on the internet but I have not found a solution... any help will be appreciated...
here is my code
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create a instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get The Refference Of Buttons
btnSignIn=(Button)findViewById(R.id.buttonSignIN);
btnSignUp=(Button)findViewById(R.id.buttonSignUP);
// Set OnClick Listener on SignUp button
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Create Intent for SignUpActivity abd Start The Activity
Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
startActivity(intentSignUP);
}
});
}
// Methos to handleClick Event of Sign In Button
public void signIn(View V)
{
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
// get the Refferences of views
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
startActivity(new Intent("com.example.system.HOMEACTIVITY"));
}
else
{
Toast.makeText(LoginActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
#Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}
Depending on your need, you could use sharedpreference to save all the user details. And wipe it when the user log out. Or you could use a more complicated solution like the Android accountmanager. Here the link to show how it all works together.
https://udinic.wordpress.com/tag/account-manager/

How to obtain Edittext values from dynamically created editttexts using a single button

I am splitting a string and putting the result in edittexts using a loop so that the user can edit the data.thereafter he can save all the data in
the edittexts by just pressing the final button.Problem is i don't know how to get each value from the edittext when he pressses the save button.this is my code:
EditText etstringone,etstringtwo;
Button btn_save;
btnsave=new Button(this);
String mystring="somevalue";
String del="\\|";
String[] splitResult = mystring.split(del);
for (String e : splitResult)
{
etstringone=new EditText(this);
etstringtwo=new EditText(this);
etstringone.setText(splitResult[0]);
etstringtwo.setText(splitResult[1]);
mylayout.addView(etstringone);
mylayout.addView(etstringtwo);
}
mylayout.addView(btnsave);
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
how do i go about this?thanks.
NB:emphasis on using a single button to get all the data,i managed to do a scenario for apppending a new save button each time the
loop runs but its not neat.
The edit texts are dynamically created, but you can still access the object. Either cache a reference to the edit texts or walk the child-views of your "mylayout".
EDIT
From your code, you declare
EditText etstringone,etstringtwo;
You instantiate them
etstringone=new EditText(this);
etstringtwo=new EditText(this);
Now I don't know the scope of this (are they local vars, class/member vars?) if class vars, you can reference them in the
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
body. eg
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String e1 = etstringone..getText().toString();
}
});

How to Change the Text of a button using EditText on Android

I am trying to use an EditText one one Activity to change the text of a button on another. I know I have to go through the SharedPreferences, and although this is where I am stuck.
Activity with the Button:
protected void onResume() {
super.onResume();
class1.setText(this.getButtonText());
}
public String getButtonText()
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String buttonText = prefs.getString("ButtonText", "Default button text"); // I am not sure how to get the button text here. This is what someone was trying to have me do?
return buttonText;
}
This is my Activity that has the EditText and the button to go back to the activity with the button:
public class EditClass1 extends Activity implements OnClickListener{
Button class1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editclass1);
SettingButtons();
class1.setOnClickListener(this);
}
private void SettingButtons() {
// TODO Auto-generated method stub
class1 = (Button) findViewById(R.id.edittoclass1);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.edittoclass1:
startActivity(new Intent("com.clayton.calendar.TOCLASS"));
break;
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("ButtonText", // This is not working
((TextView)findViewById(R.id.edittoclass1)).getText().toString());
editor.commit();
}
}
Try this:
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
EditText text = (EditText)findViewById(R.id.Setclass);
String text2 = text;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("ButtonText", // This is not working
((TextView)findViewById(R.id.edittoclass1)).getText().toString());
editor.commit();
}
Ignoring the shares preferences for a moment, why not just have a public static String variable in the class containing the button.
public static String buttonText = "somthing";
When in the class containing the edit text you can call in an event handler which listens for changes to the edit text or an event handler that is fired when a button is pressed.
ButtonActivity.buttonText = text.getText();
Then in the onResume() method of the activity containing the button
button.setText(buttonText);
Try this it might be a simpler way of doing what you want. Remember when declaring the buttonText variable make sure you remember to use the static keyword. Without it you will need a direct reference to the object, with the static keyword, you can just refer to the required class. However, being static button Text will be the same for all instances of the button containing activity. If you only ever intend on having one instance of the activity this is the solution for you. If not then you have to get a little more creative.

Categories

Resources