Handle textview that takes text from asynctask - android

I have a text view called tv that takes content from a web page. I want to store the content of that text view on button click but I don't want that the content of one day overwrite the content of the previous day ( for example today tv=ok and tomorrow tv=ok1 , I want to store ok and ok1 not only ok1)
Of course with shared preferences, I can store that text but I don't want to overwrite the text of the previous day
TextView tv = (TextView) findViewById(R.id.oks);
tv.setText(result[1].toString());
//in pass there is the text of tv(in main activity)
SharedPreferences ok = getSharedPreferences("pass", 0);
String str = ""+ok.getString("textvalue","");
SharedPreferences.Editor editor = ok.edit();
editor.putString("textvalue",str);
editor.apply();
TextView textView=(TextView) findViewById(R.id.preferiti);
textView.setText(ok.getString("textvalue",""));

This is your textview
//this are two fields which you want to append and getting somehow from API
String todayTv="ok" , tomorrowTv="ok1";
TextView tv = (TextView) findViewById(R.id.oks);
Let's set todayTv value on textView
tv.setText(todayTv);
now you want to store this value
SharedPreference sharedPref= getSharedPreferences("pass", 0);
Editor edit=sharedPref.edit();
edit.putString("textvalue",tv.getText().toString()); //getting value from textview
edit.apply();
Saved ! lets say you have now tomorrowTv which you want to show with your previous result i.e like this ok ok1
tv.setText(sharedPref.getString("textvalue","")+" "+tomorrowTv); //Concat strings and set text
Old Value - sharedPref.getString("textvalue","")
New Value - tomorrowTv
Now save it again with the above code for future
SharedPreference sharedPref= getSharedPreferences("pass", 0);
Editor edit=sharedPref.edit();
edit.putString("textvalue",tv.getText().toString()); //It will give you final string containg old and new value into one string
edit.apply();
Hope it will give you insight about It.
Read More : https://developer.android.com/training/basics/data-storage/shared-preferences.html
https://developer.android.com/reference/android/widget/TextView.html

Related

EditText hint as shared pref default?

Is it possible to set the EditText "hint" or default text to the value of a sharedpref string? I have 3 EditText, one button, when the button is clicked, the 3 values are saved in SharedPreferences for a later time in the app. As of right now, once the button is clicked the 3 edit texts stay to the values entered, but if you leave the activity and return, they all go back to blank. I'm wondering if there is a way to set them so they are the sharedpref value of the string, and if nothing is saved set them to the default given. Also, how do I set the default value of a sharedpref String? Thanks!
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
if (mEditTextBench.getText().length() > 0 && mEditTextDead.getText().length() > 0 && mEditTextSquat.getText().length() > 0) {
editor.putString("maxDead", mEditTextDead.getText().toString());
editor.putString("maxSquat", mEditTextSquat.getText().toString());
editor.putString("maxBench", mEditTextBench.getText().toString());
editor.apply();
This is my code in another layout to test to make sure it is properly saving.(which it is) I'm not entirely sure what the "null" is doing though. So if anyone can help with that too thanks!!
String maxDead = pref.getString("maxDead", null);
TextView textViewTest = (TextView) findViewById(R.id.textViewTest);
textViewTest.setText(maxDead);
Doh!! Figured it out! Added this to onCreate:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
String maxDeadHint = pref.getString("maxDead", "100");
mEditTextDead.setHint(maxDeadHint);
My bad! Thanks!
Once you make the appropriate replacements, this should work:
TextView deadText = (TextView) findViewById(R.id.//insert id//);
TextView squatText = (TextView) findViewById(R.id.//insert id//);
TextView benchText = (TextView) findViewById(R.id.//insert id//);
deadText.setHint(pref.getString("maxDead", "default_value"));
squatText.setHint(pref.getString("maxSquat", "default_value"));
benchText.setHint(pref.getString("maxBench", "default_value"));

Textview value not incrementing on button click

Iam developing a game in which i have to increment the textview value every time a "Rematch" button is tapped
Iam saving the textview value using shared preference, it works fine.
The Problem is that "if i tapped my 5 times on button then the value is shown 5, then if i closed the app and comes again it shows 5 (so shared pref works fine) but when i tapped on rematch again it again starts the textview value from 1, although my expected result was 6 because 5 was already saved
Here is my code for oncreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
SharedPreferences sp = getSharedPreferences(SP_NAME, MODE_PRIVATE);
tValue = sp.getString("textvalue","");
coins=(TextView)findViewById(R.id.textView1);
coins.setText(tValue);
and on button click iam doing this
count++;
coins.setText(String.valueOf(count));
SharedPreferences sp = getSharedPreferences(SP_NAME, MODE_PRIVATE);
SharedPreferences.Editor sedt = sp.edit();
sedt.putString("textvalue", coins.getText().toString());
sedt.commit();
The problem I think is that count when you start the activity again is 0. You have to make count to start from the value that is saved, this could be a way to do it
count = Integer.valueOf(sp.getString("textValue",""))
The problem is that you use the value of count instead of tValue. So don't do count++ but save tValue as int and increment that one. Also do setText(tValue). Then it will work. Good luck.
That's because count starts again as 0,
and when u press the button it wil increment to 1.
so in your onCreate after you get your saved value back.
assign it to count, like:
count = Integer.parseInt(tValue);
Hope this will help u out.

How to retain changed button text in android?

In my project I have one edit text and one button.
When the project loads and I press the button,
the text of the button changes to the value of edit text.
But when I press the back button the change of button text does not persist.
I have included the snapshot which can describe my problem better.
I basically want the button text change to persist.
Please help.
The text doesn't show up on reopening the app because it was stored temporarily and when you closed your app it got destroyed. To retain the text you can store it in shared preference or file and on app startup load the text of the button from that source and if source is not present(When opening app for the first time) then put the default text on the button.
In your activity onCreate () method, you can add this code:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String btnText = preferences.getString("btnText", "");
if(!btnText.equalsIgnoreCase(""))
{
yourButton.setText(btnText);
}
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("btnText",yourTextEdit.getText().toString());
editor.apply();
....your code
}
});
In your layout xml file, set your button default text. android:text="#string/yourtext"
You need to save the text somewhere and load it when the activity starts. There are various ways to do this but I think Shared Preferences will work for you.
In the on click function for the button set the Shared Preference and in the Activity's onCreate check if the same Shared Preference is set. If the value is present load it else load the default value.
When you press the button for first time after getting text from edit text use the following code to save the edit text text that to be set on button in shared prefrences:
In on Button Click method:
EditText et = (EditText) findViewById(....);
String text = et.getTex().toString();
then you set it on button and so..
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("buttonText", text);
editor.commit();
then when you relauch the app in onCreate method use the following code:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String value= prefs.getString("buttonText", "");
button.setText(value); //whatever you button is

highlight the linearlayout background in android

I have to develop one android application.
Here is the scenario:There are many images in a linearlayout. When selected, the layout should be displayed with another background. This works well now.
Now, I would like that when I open a new activity and come back tho this one, the last selected layout should still be the highlighted one (with the second background).
How can this be done ???
Now i have used below code for highlighting the image when pressed:
LinearLayout ar = new LinearLayout(this);
ar.setOrientation(LinearLayout.VERTICAL);
ar.setPadding(3, 3, 3, 3);
ar.setLayoutParams(artiLayoutParams);
ar.setGravity(Gravity.CENTER);
ar.setBackgroundColor(Color.parseColor("#666666"));
ar.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_selector));
ar.setId(position);
position++;
ar.setOnClickListener(mArticleClick);
EDIT:
private OnClickListener mArticleClick = new OnClickListener()
{
#Override
public void onClick ( View v )
{
int object = v.getId();
v.setSelected(true);
String articletitle = Appscontent.Sub_arraylist.get(object).toString();
Intent in = new Intent(MainActivity.this, SubCate.class);
in.putExtra("Title", articletitle);
startActivity(in);
}
};
Here i have to clicked one item means it is go to next activity.afterthat i have clicked back button means the selected item is stay on highlighted with another background.afterthat i have selected another item means its go to next activity.now i have to click back button means these item only highlight with background....but the pervious item also highlighted....
I wish to need the o/p like :
The last selected item only highlighted after click the back button...please help me...how can i do ..
EDIT:
I have declared int prevPosition = -1; globally...
Afterthat i have save the value here:
ar = new LinearLayout(this);
ar.setOrientation(LinearLayout.VERTICAL);
ar.setPadding(3, 3, 3, 3);
ar.setLayoutParams(artiLayoutParams);
ar.setGravity(Gravity.CENTER);
ar.setBackgroundColor(Color.parseColor("#666666"));
ar.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_selector));
ar.setId(position);
position++;
ar.setOnClickListener(mArticleClick);
SharedPreferences preferences = getSharedPreferences(SPF_NAME, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("POSITION", prevPosition);
editor.commit();
In Onclick method:
private OnClickListener mArticleClick = new OnClickListener()
{
#Override
public void onClick ( View v )
{
int object = v.getId();
SharedPreferences preferences = getSharedPreferences(SPF_NAME, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("POSITION", -1);
editor.commit();
But my background color is not staying here after press the back button...
This is "easy". In order to be able to know which image should be highlighted, you need to store this information somewere. A good practice is a database, but if you have only this info to store, you can create something like a shared preferance or some other types of global variables. Then, when you will re-open the activity with your listview (or come back to it), you should tell to your listview which item should be highlighted and highlight this item.

How can I display values from EditText user input?

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

Categories

Resources