I want to change a picture from another activity and display it as a picture or button, but it makes me a problem when I want to run the program, I'd love to help
imageView10 = (ImageView) findViewById(R.id.imageView10);
String s;
SharedPreferences mySharedPreferences2 = enter.this.getSharedPreferences("two", Context.MODE_PRIVATE);
s = mySharedPreferences2.getString("USERNAME2", "");
s = "R.drawable.add1";
imageView10.setBackgroundResource(s);
the s value is red...
Related
I'm working on an app that generates passwords randomly using the array. The password is in TextView. Everything is good unless I want to generate a new password second time. How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
Here are the variables that I'm using:
EditText dlugosc;
String haslo = "";
String pustak = "";
TextView haslo0;
And this is a code that I use to generate a password:
(znaki is the name of array)
dlugosc = findViewById(R.id.password_len);
haslo0 = findViewById(R.id.password);
String yui = dlugosc.getText().toString();
int x = Integer.parseInt(yui);
for(int i = 0; i < x; i++){
int Index = generator.nextInt(znaki.length);
haslo = znaki[Index] + haslo;
}
I have already tried doing an if structure:
if (haslo0 != null){
haslo0.setText(pustak);
haslo0.setText(haslo);
}
else
haslo0.setText(haslo);
But it doesn't help :(
When I want to have 7 chars in the password and click the button first time, the result is correct e.g. PKAjzQL. But when I click the button second time, the result is nBzcRjQPKAjzQL instead of nBzcRjQ.
Why are you appending the old string haslo behind the newly generated one in haslo = znaki[Index] + haslo;
Probably that's why you are getting output like that. Can you please try just setting newly generated password into the textview like
haslo = znaki[Index];
And then try to set text in the text view using haslo0.setText(haslo);
How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
The problem is not to "delete" the old text, the problem is that you have to clear the list for example, every time user clicks on the Button you clear the list doing : znaki.clear(), then it will only show the new password generated.
If you see your output :
First output :
PKAjzQL --> This is correct
Second output :
nBzcRjQPKAjzQL --> this is the new output + the old one
Can you give the code of the OnClickButton? And why are you setting the same TextView with diferents Strings when you click?
haslo0.setText(pustak);
haslo0.setText(haslo);
?
I am passing value from Activity A to Activity B. It works fine.
For Example, if I pass String "Harvey Brown", Then in next activity it also shows "Harvey Brown".
But I would also like to add "Author: " text before "Harvey Brown" and show the sum result in TextView such that now instead of only
"Harvey Brown".
It will show
Author:Harvey Brown
for every intent.
Here is the code, I'm using to pass String value:
intent.putExtra("Title",mData.get(position).getTitle());
and in next activity I received value by
Intent intent = getIntent();
String author_name = intent.getExtras().getString("author_name");
and I set the value accordingly as
author_name = (TextView) findViewById(R.id.author_nameid);
author_name.setText(author_name);
Solution:
Just change this line:
intent.putExtra("Title",mData.get(position).getTitle());
To
intent.putExtra("Title", "Author: " + mData.get(position).getTitle());
You will get your desired result.
You can add a 'Author:' Prefix like below -
author_name = (TextView) findViewById(R.id.author_nameid);
author_name.setText("Author: " + author_name);
I have a text in which there is indicator tag that indicate from where i will make text underline, I want to make text underline from that indicator also want to remove indicator so that it wont appear in string, here is what I'm trying:
String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
int i1 = consent.indexOf(">");
int i2 = consent.indexOf("</");
consentCheck.setMovementMethod(LinkMovementMethod.getInstance());
consentCheck.setText(consent, CheckBox.BufferType.SPANNABLE);
consent = consent.replace("</clickable>", "");
consent = consent.replace("<clickable>", "");
Spannable mySpannable = (Spannable)consentCheck.getText();
mySpannable.setSpan(clickableSpan, i1+1, i2 , Spannable.SPAN_INCLUSIVE_INCLUSIVE);
If you want to display the text in TextView you could replace "clickable" tags with "u" tags and then use Html.fromHtml() in setText:
String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
consent = consent.replace("<clickable>", "<u>").replace("</clickable>", "</u>");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
tvConsent.setText(Html.fromHtml(consent, Html.FROM_HTML_MODE_LEGACY));
} else {
tvConsent.setText(Html.fromHtml(consent));
}
EDIT:
If u want to edit "consent" part freely you could split the text into three parts and then edit each part independently
String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
String start = "<clickable>";
String end = "</clickable>";
String part1 = consent.substring(0, consent.indexOf(start));
String part2 = consent.substring(consent.indexOf(start)+start.length(),consent.indexOf(end));
String part3 = consent.substring(consent.indexOf(end), consent.length()).replace(end, "");
I have a main activity which has two fragments and I am trying to pass some data which I want to append on above whatever texts is already on a edittext on the next fragment.
Activity with two separate Tabs:
The following works fine:
Fragment #1:
String y = "TEST 1";
SharedPreferences prefs; // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("someId", y);
editor.commit();
Fragment #2:
SharedPreferences prefs; // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
String someId=prefs.getString("someId","");
showLog.setText(someId + "\n HERE"); //this overwrites the text and is multiline
What I am looking to do is I want the showLog to append above what's already there.
My showLog is the following:
<EditText
android:id="#+id/showLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Nothing to display"
android:inputType="textMultiLine"
android:lines="12"
android:paddingLeft="2dip"
android:singleLine="false"
android:textColor="#999999"
android:textSize="14dip"
android:textStyle="normal"
android:gravity="top" />
For example:
showLog already starts out with having "THIS IS A TEST" in the textbox
When the SharedPreference is called, the showLog should display the following:
TEST 1
HERE
THIS IS A TEST
But that's not happening. I tried using .append() which didn't have any affect.
I think I understand what you're trying to do now, you might want to try:
int start = showLog.getSelectionStart();
int end = showLog.getSelectionEnd();
String toIns = someId + "\n HERE";
showLog.getText().replace(Math.min(start, end), Math.max(start, end), toIns, 0, toIns.length());
Technically it's not appending, rather replacing the end of the string with new text.
EDIT: in light of the new issue arising, here are my edits to your project, let me know if there's anything wrong still.
My edited version is on the right, original on the left
CurrentTrip.java
DisplayTrip.java
What I'm doing with the text might not be exactly what you want, so make sure to let me know if it's not.
EDIT 2: And to delete the stored values:
final SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
EDIT 3: Having understood exactly what it is you want to do, here's one way to do it by storing the last trip added, and keeping track of the text you need in your TextView.
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
// Find the string we want
String someId = prefs.getString("someId","");
final Editor editor = prefs.edit();
// To stop a trip being added in onResume etc.
if(someId != prefs.getString("previous-trip", "")){
showLog.setText(someId + prefs.getString("previous", ""));
} else {
// Without this else, we'd have a blank box again
showLog.setText(prefs.getString("previous", ""));
}
// Store the latest trip that was added
editor.putString("previous-trip", someId);
// Store everything that's in the box so far for next time
editor.putString("previous", showLog.getText().toString());
// Commit to the prefs
editor.commit();
If I understand what you are trying to get then you should be able to do
String text = showLog.getText().toString();
showLog.setText(someId + "\n HERE" + text);
If you already have text in it then you just get that text and after opening up your SharedPreference put it all in with the original text last.
Intent extras
In first Activity
String someText = someTextString;
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("key", someText); // add some extra to the Intent to pass to next Activity
startActivity(i);
In SecondActivity
Intent intent = getIntent();
String myText = intent.getStringExtra("key"); // use same key value used in first Activity
About the part where you need to display the String:
// From what you have given, the TextView `showLog` is already displaying some text
// and you want to place another another String in front of it(prepend)
showLog.setText(stringToAdd + showLog.getText());
// "stringToAdd" is the string you trying to pass between activities(or fragments)
I am working on this app where I have one EditText field where you can write something and then it get saved and added to a list(TextView). I save the content of the EditText in this way:
saved += "*" + editTextFelt.getText().toString() + ". \n";
saved is a String.
Everything works fine, I can even reload the app and it's still displayed in the TextView, but if I try to write something and save it everything that was there, now disappears. Why?
CODE:
init Method()
sp = getSharedPreferences(fileName, 0);
betaView = (TextView)findViewById(R.id.betaTextView);
I've got a button to send the text, and this is like:
public void onClick(View v) {
switch(v.getId()){
case R.id.btnSend:
saved += "*" + editTextFelt.getText().toString() + ". \n";
SharedPreferences.Editor editor = sp.edit();
editor.putString("SAVED", saved);
editor.commit();
betaView.setText(sp.getString("SAVED", "Empty"));
How are you saving it? because when you save a text against a variable it replaces the previous one.
So you need to get the previous one and then append the new one and then again save it to SharedPreferences, something like this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String saved = sp.getString("YourVariable", "");
saved += "*" + editTextFelt.getText().toString() + ". \n"; //appending previous
//Editor to edit
SharedPreferences.Editor editor = preferences.edit();
editor.putString("YourVariable",saved);
editor.commit(); //don't forget to commit.
now set this appended text to your TextView like this:
betaView.setText(saved);