Building a back button into my application and storing edittext fields - android

I have 2 views in my app. A form input and a screen to review the form data and confirm. The user inputs the data on the form submission screen in edittext fields. When the user clicks submit, they are presented with the data they input and a 'back' and 'confirm' button. I want it so that if the user presses back, they are returned to the form input screen and the data they input should still be in the editText fields.
At the moment, I am using an onClickListener to point to the form submission screen but the forms are all blank. How do I keep the data in them?
This is the onClickListener:
bBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent j = new Intent(FormConfirm.this, FormCreate.class);
startActivity(j);
}
});

i don't know how to write code on android apps, but i think, why you not to store them into variable flag and call them on edit text field?
for example, if you already fill the form and click submit, you can make a function for store data first on variable flag and show the data in form submission, so when you click back button, you can call the value that you store on variable flag into edittext.
i hope my answer can give you some inspiration

As stated you should save the values somehow, for example by using SharedPreferences.
As an example, when you go from the Input-form Activity to the Submit-form Activity:
bSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent j = new Intent(FormCreate.this, FormSubmit.class);
saveInput();
startActivity(j);
}
});
Where the method saveInput() could look something like:
private void saveInput() {
EditText input = (EditText) findViewById(R.id.someId);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putString("input",input.getText().toString()).commit();
}
And then, when you press back, the back action could simply be something like:
bBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FormConfirm.this.finish();
}
});
That will cause the current activity to exit and your previous activity will be visible. If you want to display the last saved input when the Input-form Activity starts, you can simply do something like this:
private void loadInput(){
EditText input = (EditText) findViewById(R.id.someId);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String savedText = getString("input", "default input text");
input.setText(savedText);
}
and call that method in the onCreate-method in your Input-form Activity.

Had an inspiration and found a workaround that might not necessarily be correct but it works :) just changed the Java code for the activity to the following:
bBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});

Related

How to put EditText info into a Intent variable

I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.

Android Java - saving EditText's text when user presses "back"

I need to fill form and by pressing "Back" button save it , let's say, to preferences.
Or even show it to Log.
The problem is when I use onBackPressed() - Activity goes to onPause() and myEditText.getText().toString() returns result from XML and not the result that was actually in myEditText field. Same thing goes for onDestroy().
Of course, if I hang someButton.onClickListener() it saves my data well from myEditText because Activity doesn't go onPause() and doesn't take result from XML, but I need to save it exactly by pressing back button.
The code is basic:
EditText etCompetitionName;
Competition competition;
etCompetitionName=(EditText) findViewById(R.id.etCompetitionName);
Intent intent=getIntent();//I get number of object to work with
pos=intent.getIntExtra("pos", -1); // this is this number
competition=getObject(pos);// well, this is loading this object
olDetCompetitionName=competition.getName();//getting name from loaded object
etCompetitionName.setText(olDetCompetitionName);//set this text to EditText field
...
then :
protected void onDestroy() {
super.onDestroy();
//try to save
competition.setName(etCompetitionName.getText().toString());
saveObject(competition);
}
So, it saves old value, the one I set in the beggining. If I don't set it - it will be taken from layout xml. It is issue of onPause() I need to avoid it.
Your question is quite confusing but if you override onBackPressed() you can get the data.
#Override
public void onBackPressed()
{
String text = myEditText.getText().toString();
// save text
super.onBackPressed();
}
I know you said you did this but show how you are doing it if this doesn't work. This will put whatever the user has typed in the text variable then you can save it in SharedPreferences or wherever you want.
You can also do the same thing in onPause() if you want it to save if, say, something else comes in the foreground.
SecondActivity.Java -> I saved two edittext value in SharedPReference and called the MainActivity intent
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor = sharedPreferences.edit();
editor.putString("1", a.getText().toString());
editor.putString("2", b.getText().toString());
editor.commit();
Intent i = new Intent(SecondActivity.this,MainActivity.class);
SecondActivity.this.startActivity(i);
}
MainActivity.Java -> I have a button listener to show a Toast with values from the sharedpreference.
show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String a = pref.getString("1", "") ;
String b = pref.getString("2", "");
Toast.makeText(getApplicationContext(),a+" "+b, Toast.LENGTH_LONG).show();
}
});

sharedpreferences null method check

I have seven different activities and i call all data in final activity to calculate it, in all activities i placed a button with failsafe that if edit text is empty no next button is nonclickable.
Problem arises now if a user click directly the result page my application crashes, due to no data in sharedpreferences to calculate,
I want to have some method or code to check if DOUBLE of my sharedpreferences or entire file is empty user should get a toast message 'Fill the values first' and calculation page does not open. this way my app will remain alive.
Am I thinking in right direction?
Some example code:
please note seeresults is a button which is clicked to open page having calculation results (sharedpreferences values are accessed).
variable 'v' is a double
I have entered following code but its again having problems (if statement cannot be compared with double), can any one help me out
seeresults.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
abcPref = this.getSharedPreferences(FILE1, 0);
Boolean bCheck = abcPref.getBoolean("v", false);
if (v == -1) { Toast message = Toast.makeText(Timesaving.this, "No Data or Parameteres Was Entered in forms", 3000);
message.setGravity(Gravity.CENTER_VERTICAL , 0, 0);
message.show();
finish();}
else{
Intent openresult = new Intent("com.ideals.ideal.RESMAIN");
startActivity(openresult);
}
}
});
SharedPreferences sPrefs = getSharedPreferences("MyPrefs", 0);
Boolean bCheck = sPrefs.getBoolean("ButtonReceiverState", false);
When you are trying to retrieve the values from the sharedPreferences the call provides a fail-safe exactly for the possible nullpointer; you can set a default-value if there is no value found for a key.
In my case here, the boolean is set to false if there is no value to retrieve. This works for all different datatypes aswell.
A double will never be null. Java initializes it to 0 automatically. What you could do is set the default value for the preference where you use the double to Double.NaN and check against that.
Or you simple could define reasonable default values which will work for your calculations and which the user could change at will.

Save Setting onDestroy

How to save setting after we exit the app using onDestroy?
Example:
When the app start, it will start Main_Activity.class
Button button1;
public class Main_Activity extends Activity {
super.onCreate(savedInstanceState);
................
}
Added a button named "button1" and give an Action to open new activity when clicked
public void button1_newactivity (View v){
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener (new View.OnClickListener() {
public void onClick(View arg0) {
Intent secondactivity=new Intent (getApplicationContext(), Second_Activity.class);
startActivity(secondactivity);
}
});
}
Added 2 Checkbox on Second_Activity.class, as a default when the app start checkbox1 is selected and checkbox2 is not selected. But, when checkbox2 selected and checbox1 automatically not selected, after pressed another button it will start Third_Activity.class.
My Question is how can we save this setting, so when we exit the app, then start the app again, It will automatically start Third_Activity.class not Main_Activity.class like the first one?
What should we write in this part
protected void onDestroy(){
....................
}
use SharedPreferences to store which will be your first activity. launch your launcher activity like before. But in there check the value you saved in sharedpreference. So if you find that you have to start 3rd activity from the oncreate of launcher start the third and finish the first one. For example
public class Main_Activity extends Activity {
super.onCreate(savedInstanceState);
SharedPreferences pref = getSharedPreferences(name);
boolean b = pref.getBoolean("should_start_third", false);
if(b){
finish();
start third activity
}
................
}
Here in SharedPreferences i used a should_start_third boolean value to check if the third activity will start directly. this is by default false.
you have to save the value of shared preferences after the third checkbox is selected. to save use like following.
getSharedPreferences(name).edit().putBoolean("should_start_third", true).commit();

Creating Arrays which can save text in android and be able to open them

Now I know this may be a lot to ask for. But what I want to do is have the ability to save and open notes. Now currently I am able to save and load data on the phone to be opened the next time the app is open with this code here:
public class Notes extends Activity{
EditText savedText;
TextView dataResults;
public static String stringFolder = "SharedData";
SharedPreferences String;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
setupVariables();
String = getSharedPreferences(stringFolder, 0);
}
private void setupVariables(){
savedText = (EditText) findViewById(R.id.notes_text_view);
dataResults = (TextView) findViewById(R.id.notes_text_view);
}
public void saveNotes(View v)
{
String stringData = savedText.getText().toString();
SharedPreferences.Editor editor = String.edit();
editor.putString("SharedData", stringData);
editor.commit();
}
public void loadNotes(View v){
String = getSharedPreferences(stringFolder, 0);
String dataReturned = String.getString("SharedData", "Could not load");
dataResults.setText(dataReturned);
}
Now what I am hoping to do is every time the user clicks save it adds the text entered into an array under a name which the user can enter. When the user clicks load I want a list view to pop up showing all the names of the notes which have previously been saved and click on one to load it. i know this is a lot to ask but I'm wondering if anyone out there knows. Or there might be a very simple way to do this. I am very much a beginner to android.
Thanks, Reid
It seems like you want to display a dialog with EditText on saving and a dialog with ListView on loading. Basics on creating dialogs can be found here. See also this question.

Categories

Resources