How can I display values from EditText user input? - android

I have a SharedPreferences that saves EditText input from one activity and displays the String value in another activity.
When I enter an input into the EditText fields and press (a button I created) "Save" (which commits the EditText to editor), the file is stored successfully. However, the display activity (which displays the stored String values in SharedPreferences) doesn't display the values. I am guessing it is because it is the onCreate, so the screen is "created" when it runs. How can I get it to update the EditText values when the user commits (presses save) immediately?
CustomStoreEditActivity - Just storing the user inputs (EditText entries):
final Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (saveButton.isClickable()) {
SharedPreferences prefs = getSharedPreferences(
"customtime", 0);
// prefs.registerOnSharedPreferenceChangeListener(this);
final SharedPreferences.Editor edit = prefs.edit();
EditText shopName = (EditText) findViewById(R.id.shopName);
EditText open1 = (EditText) findViewById(R.id.open1);
EditText close1 = (EditText) findViewById(R.id.close1);
EditText open2 = (EditText) findViewById(R.id.open2);
EditText close2 = (EditText) findViewById(R.id.close2);
EditText open3 = (EditText) findViewById(R.id.open3);
EditText close3 = (EditText) findViewById(R.id.close3);
EditText open4 = (EditText) findViewById(R.id.open4);
EditText close4 = (EditText) findViewById(R.id.close4);
EditText open5 = (EditText) findViewById(R.id.open5);
EditText close5 = (EditText) findViewById(R.id.close5);
EditText open6 = (EditText) findViewById(R.id.open6);
EditText close6 = (EditText) findViewById(R.id.close6);
EditText open7 = (EditText) findViewById(R.id.open7);
EditText close7 = (EditText) findViewById(R.id.close7);
EditText comments = (EditText) findViewById(R.id.comments);
edit.putString("shopName", shopName.getText().toString());
edit.putString("Monday1", open1.getText().toString());
edit.putString("Monday2", close1.getText().toString());
edit.putString("Tuesday1", open2.getText().toString());
edit.putString("Tuesday2", close2.getText().toString());
edit.putString("Wednesday1", open3.getText().toString());
edit.putString("Wednesday2", close3.getText().toString());
edit.putString("Thursday1", open4.getText().toString());
edit.putString("Thursday2", close4.getText().toString());
edit.putString("Friday1", open5.getText().toString());
edit.putString("Friday2", close5.getText().toString());
edit.putString("Saturday1", open6.getText().toString());
edit.putString("Saturday2", close6.getText().toString());
edit.putString("Sunday1", open7.getText().toString());
edit.putString("Sunday2", close7.getText().toString());
edit.putString("comments", comments.getText().toString());
edit.commit();
Intent myIntent = new Intent(getBaseContext(),
CustomStoreActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
Toast.makeText(getBaseContext(), "Opening Hours Saved!",
Toast.LENGTH_SHORT).show();
}
}
});
}
CustomStoreActivity - where I believe the problem lies:
private void displayPreferences(){
SharedPreferences prefs = getSharedPreferences("customtime", 0);
String shopName = prefs.getString("shopName", "Empty");
String shopTime1 = prefs.getString("Monday1", " ");
String shopTime2 = prefs.getString("Monday2", " ");
String shopComments = prefs.getString("comments", "");
TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs);
displayPrefs.setText(shopName + shopTime1 + shopTime2 + shopComments);
}
Thank you for your ample time.

I think part of what you're looking for is to put your CustomStoreActivity code in onResume instead of onCreate. Whatever code you move into onResume will occur whenever the CustomStoreActivity is brought to the front (including when it is first created).
Another alternative, assuming that CustomStoreActivity is used to launch the Activity that contains the EditTexts, is to use startActivityForResult and pass the data back in the result Intent instead of using the SharedPreferences. Here is a simple example (though note that the setResult call in this example is passed null where you would want to pass in an Intent, as documented here in the Android docs).
It also seems like you're trying to use your second Activity like a dialog box with editable fields. If that's the case, yet another alternative is to actually use a variant of the Dialog class within your CustomStoreActivity class instead of creating another Activity to act like one. See the Android doc for dialogs.

I'm not sure I really understand your question but are you trying to update the TextView of a different Activity from the one you are in? In which case I don't think this can be done and you should use Intents to pass the data to the activity

Related

attempt at shared preferences but getting errors

Am trying to get shared preferences working from here
I added the code from that thread, adapting it to my needs, it is showing no errors in the android studio until I compile it.
public class ChrStats extends Activity
{
private SharedPreferences savedFields
private Button saveButton;
private EditText editText;
private EditText editText2;
private EditText editText3;
private EditText editText4;
private EditText editText5;
private EditText editText6;
private EditText editText7;
// Add all your EditTexts...
// Upon creating your Activity, reload all the saved values.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
saveButton = (Button) findViewById (R.id.your_save_button_id);
editText = (EditText) findViewById (R.id.your_edit_text_1_id);
editText2 = (EditText) findViewById(R.id.your_edit_text_2_id);
editText3 = (EditText) findViewById(R.id.your_edit_text_3_id);
editText4 = (EditText) findViewById(R.id.your_edit_text_4_id);
editText5 = (EditText) findViewById(R.id.your_edit_text_5_id);
editText6 = (EditText) findViewById(R.id.your_edit_text_6_id);
editText7 = (EditText) findViewById(R.id.your_edit_text_7_id);
// Keep adding all your EditTexts the same way...
// "info" is just a tag name, use anything you like
savedFields = getSharedPreferences("info", MODE_PRIVATE);
// In case no value is already saved, use a Default Value
editText.setText(savednotes.getString("editText1", "Default Value 1"));
editText2.setText(savednotes.getString("editText2", "Default Value 2"));
editText3.setText(savednotes.getString("editText3", "Default Value 3"));
editText4.setText(savednotes.getString("editText4", "Default Value 4"));
editText5.setText(savednotes.getString("editText5", "Default Value 5"));
editText6.setText(savednotes.getString("editText6", "Default Value 6"));
editText7.setText(savednotes.getString("editText7", "Default Value 7"));
// Save the changes upon button click
saveButton.setOnClickListener(saveButtonListener);
}
public OnClickListener saveButtonListener = new OnClickListener () {
#Override
public void onClick(View v) {
SharedPreferences.Editor preferencesEditor = savedFields . edit ();
if (editText.getText().length() > 0) // Not empty
preferencesEditor.putString("editText", editText.getText());
if (editText2.getText().length() > 0) // Not empty
preferencesEditor.putString("editText2", editText2.getText());
if (editText3.getText().length() > 0) // Not empty
preferencesEditor.putString("editText3", editText3.getText());
if (editText4.getText().length() > 0) // Not empty
preferencesEditor.putString("editText4", editText4.getText());
if (editText5.getText().length() > 0) // Not empty
preferencesEditor.putString("editText5", editText5.getText());
if (editText6.getText().length() > 0) // Not empty
preferencesEditor.putString("editText6", editText6.getText());
if (editText7.getText().length() > 0) // Not empty
preferencesEditor.putString("editText7", editText7.getText());
// You can make a function so you woudn't have to repeat the same code for each EditText
// At the end, save (commit) all the changes
preferencesEditor.commit();
}
}
}
I expected it to throw up no errors, but I looked up the relevant references, and can't see many faults in it. Here is the screenshot of errors from the android studio.
So, i see 3 critical problems here:
no semicolon after private SharedPreferences savedFields
you have no declaration of savednotes. Maybe it should be savedFields instead?
Your java code placed in .kt file. idk how could this happend

Edittext get null values in oncreate in android

EditText edituname = (EditText) findViewById(R.id.username);
EditText editpassword = (EditText) findViewById(R.id.password);
String s1 = edituname.getText().toString();
String s2 = editpassword.getText().toString();
Toast.makeText(Main2Activity.this, s1, Toast.LENGTH_LONG).show();
The string s1 shows null value since the code is inside onCreate method. I want it in onCreate and not on buttonclick. So please tell me a solution to get the value of EditText in onCreate.
The value of edittext will be null at the time of initialization thats why it is null. If you want the value without any button click you can set TextWatcher on edittext you want

how can I parse a given edittext value from one activity to another? the value should overwrite a given textview in the new activity

I am sort of new to android programming. I have a series of EditText fields and I want the program to allow a user to input a value into the EditText field and whatever value that is input should be transferred to another activity where the value overwrites the content of a textview. How can I do this?
final Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra(AnotherActivity.KEY_EXTRAS_MESSAGE_AUTHOR, this.myEditText.getText().toString()));
startActivity(intent);
in AnotherActivity:
public class AnotherActivity extends Activity {
public static final String KEY_EXTRAS_MESSAGE_AUTHOR= "author";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
String author = getIntent().getStringExtra(KEY_EXTRAS_MESSAGE_AUTHOR);
You can do this as shown below:
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
Read the value from edit text:
String readValue = editText.getText().toString();
Create an Intent and pass the value to another activity:
Intent intent = new Intent(this, anotherActivity.class);
//Passing the string here
intent.putExtra("value", readValue);
startActivity(intent);
In your anotherActivity catch the intent and set the value to edit text:
Intent intent = getIntent();
String result = intent.getStringExtra("value");
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);
If you do not want to fire the activity by using startActvity(intent), you can use the following procedure:
Save the edit text value in Shared preferences:
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
Read the value from edit text:
String readValue = editText.getText().toString();
Save the value:
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("value", readValue);
editor.commit();
In another activity use it as shown below
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
String result = sharedPreferences.getString("value");
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);

Android: Text not being applied to Card

So I'm trying to create an app where the user will go enter some text in two edittext fields and when a submit button is pressed, it will close the edit window and then add a new card to the list on the main activity. I've set up all my code and everything works without error. However, no cards are added and when the application starts, there's one card which has no text anywhere.
My Code:
MainActivity.java:
//The following lines of code are in onCreate() and setContentView() has been
//called
//Get the intent that launched the activity
Intent i = getIntent();
//Get the strings from the intent
String sig = i.getStringExtra("signature");
String lMessage = i.getStringExtra("long_message");
//Init mCardView
mCardView = (CardUI) findViewById(R.id.cardsview);
//Allow swipe to delete for cards
mCardView.setSwipeable(true);
//Add new card with the two extra strings from the edit text fields
mCardView.addCard(new MyCard(sig, lMessage));
//Draw the cards
mCardView.refresh();
AddText.java
//The following lines of code are in onCreate() and setContentView() has been
//called
//Find edit text's and button
EditText name = (EditText) findViewById(R.id.enter_name);
EditText message = (EditText) findViewById(R.id.enter_message);
button = (Button) findViewById(R.id.add_signature);
//Get the string from the edit text...whatever it may be.
final String signature = name.getText().toString();
final String longMessage = message.getText().toString();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make a new intent to start main activity
Intent intent = new Intent(getApplication(), YearbookMain.class);
//Put extras into intent
intent.putExtra("signature", signature);
intent.putExtra("long_message", longMessage);
//Start intent
startActivity(intent);
}
});
Again, I'm not getting any crashes it's just that the card is not being edited. I've tried everything I can think of so I'm hoping someone can think of something else!
Thanks

how to take input from user in android

i have a EditText in android in which i want the user to enter the text and checks for the condition "BYE"
Code sample:
EditText text = (EditText)findViewById(R.id.EditText01);
String abc= text .getText().toString();
while( !(abc).equals("bye")){
abc = text.getText().toString();//user should enter the text from keyboard and the while loop should go and chech the condition.but not able to enter the text
//do some operation with abc
}
How can i make user to enter the text??The UI should wait for the text to be entered(something like we have InputStreamReader in java applications).
Very Simple:-
EditText text = (EditText)findViewById(R.id.EditText01);
String str = text.getText().toString();
now in str u will get string which is entered in EditText
I don't think you need a loop to do this. From your comment it looks like you also have an "Enter" button or something that you click to do the checking. Just set an onclicklistener and onclick you can make the edittext invisible (or un-editable), check is the edittext is equal to "BYE" and then do your actions might look something like this:
final EditText ET = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.EnterButton);
B1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ET.setVisibility(View.INVISIBLE);
if(ET.getText().toString() == "BYE")
{
//do something if it is "BYE"
} else {
Context context = getApplicationContext();
CharSequence text = "Please enter BYE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
ET.setVisibility(View.VISIBLE);
} });
Instead of running this check in an infinite loop, only run it on every onKeyUp of the EditText. You know, anyway, that the condition will only ever be fulfilled if the user actually enters something.

Categories

Resources