Save state of String within EditText and clear string within same button - android

I was wandering if there is anyway to save a String from an EditText and clear it within the same onClick button.
The reason is I am creating a jumble word game where 1 player can jumble a word and then clear the word so the 2nd player cannot see it. The reason why I want to save the String is so I can match it with what player 2 enters.
Any advise would be helpful. Thanks

Place the following fragments of code in your onClick:
// Save
String text = someEditText.getText();
// Clear:
someEditText.setText("");

Related

How to display database contents in a new activity at onclick of a button?

thing is, if i click a button GETDATA - then a new activity starts new.java and i want it to display the contents of my database.
in this new.java i have strings name and id. and i have lined database and this class, such that name and id have the corresponding details that i want to display.
now the ques is, how to display them ?
if i have a
EditText ta;
ta=findViewBy(R.id.edittext1);
ta.setText(name);
should this work ?
it isnt working. also, i dont want user to change the contents and want to make this filed un-editable.
should i use
TextView tf;
ta=findViewBy(R.id.textview1);
tf.setText(name);
?
even this isnt happening.
what is the procedure for this ?
it is done by using append.
TextView ta;
ta=(TextEdit)findViewById(Ri.id.textView1);
String a=getData(1); // goto database, id is 1
ta.append(a);

EditText User Input

Hello Android Developers. I did my research and i cant seem to know how to do the following: 1.-I want the user to enter some input (Numbers) in an EditText and i want whatever number he wrote on that edittext to stay there until he taps that edittext, and decides to input another number. What i mean by "staying there" is that when he leaves the app and he comes back in, for the input to be there. Please guide me step by step since im a begginer please.
Note:I would like for it to not have a "save" button, just for the input to stay put.
First, in your xml for the EditText, make sure to use android:inputType="number" so it is easier for the user to enter numbers.
Second, in onPause() of your Fragment or Activity (whichever holds the EditText, I will assume an Activity for the example code), save your EditText value :
String numbers = mET.getText().toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("numbers", numbers).apply();
Then, in onResume(), put it back:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String numbers = prefs.getString("numbers");
mET.setText(numbers);
I hope that helps!

ANDROID - textview find lineindex with string ; search in textview by linenumber

i know highlighting in text-view is possible
Highlight Textview Using EditText
and scrolling text-view is also possible
(got the scroll code from here and is successfully scrolling too)
textView scroll at first line
now the question is, i am searching and i want to highlight that text and navigate to it, when someone presses the search button, the highlighting part is perfect, now i can get the index of the word in the string, but not line number of the string in the text-view,
point is if i want to find a position of certain text in text-view, i.e. which line number is that on, how to do it ?
i found an answer for this, but later i realized its for iOS
Search occurrences of a string in a TextView
Correct if I am wrong, you want to know the position where a particular text is in a String? If so then you can do it by using the following
String text = "0123hello9012hello8901hello7890";
String word = "hello";
Log.d("startOfWordPosition",text.indexOf(word)); // prints "4"
Log.d("endOfWordPosition",text.lastIndexOf(word)); // prints "22"
As you see that it will tell you the position as to where the word is located but you have to think about case where a word may come more than once. If you are sure that word will occur only once then above code is perfect for you. If not, then you will have to somehow tackle the problem.
This is working for loading a file into a textview so that the user's last selection or cursor position is at the top of the screen.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
page.setSelection(pos, pos2);
Layout layout = page.getLayout();
scroll.scrollTo(0, layout.getLineTop(layout.getLineForOffset(pos2))); }
}, 500);
page is a TextView, pos and pos2 are ints and the two ends of the last selection by the user (they are the same int if it's just the cursor), scroll is a scrollview containing the textview. It is all in a Handler because of delay issues internal to Android's loading all these objects. Th filename, pos and pos2 are saved as settings on exit.

saving text written in edit text

we are doing android chat application and want to save the values in the edit text(name of the user) to our database.When he updates it,the update should reflect in the database also.Any help in this regard will be really appreciable.
Thanks in advance.
Your question isn't very clear, but hopefully this will help:
Getting text from an EditText:
Get a handle on your EditText, then call the EditTexts .getText() method when the user presses the send button, or however your app works. So at the start of your activity:
EditText message;
protected void onCreate(Bundle savedInstanceState) {
//set layout
setContentView(R.layout.your_layout);
//Get EditText from id
message = (EditText) findViewById(R.id.edit_text_id);
}
Then in your send button's onclick method:
String my_message = message.getText().toString()
Store data in a database:
Read http://developer.android.com/guide/topics/data/data-storage.html#db The notepad tutorial it mentions is very easy to follow and will show you how to set up a simple database. That example covers adding new records to the database and modifying existing ones, which should be sufficient for you.
If you clarify what exactly you were having difficulty with, I'll be able to make my answer more specific.

How can Dynamic Add and Remove View In android ? And this is also Store even if I closed the Application

In my Application I want to Add and Remove View (like Button, or Textview, Checkbox ) by Coding (Programming ).
In Details:
I have One EditText and One Add Button. User Enter Anything in EditText and Press the Add Button then this one is added in bellow LinearLayout, and whether User click on his/her added Button it will going to next LinearLayout.
I get sucess upto this.
but when user click the button in second LinearLayout then it will come back on first Linearlayout. I am getting error Here, i don't know where I made a Mistake.
And I also facing Problem about how can I Store this all. Like User Add 5 Button and closed the application and whenever he/she came back to application I need whatever he/she previously added.
Here is what i done.
http://dynamicandroidview.blogspot.com/2011/12/how-to-add-view-in-android-by-coding.html
Try to create a database table with minimum 2 columns in your case it will be id and buttonText.
Now when user clicks on the add button it will save text to the database and will create the button dynamically below any buttons which are already created before or as a new button.
Now in your onCreate method get the count of text thats stored in database.Some thing like the following code:
DB getData = DB.getInstance();
getData.open(this);
ArrayList<TextHolder> getList = new ArrayList<TextHolder>();
getList = getData.getAllTextFromGeT();
getData.close();
x = genList.size();
Here x will be the number/count of elements that are already stored in the database.Now you can another int say i and using this i and x in the for loop you can create buttons dynamically.
Inside the loop you can do something like the following to get text for all the buttons that are being created:
TextHolder firstOne = getList.get(i);
String text = firstOne.getText();
You will also need class with getters and setters method in order to convert DB elements into objects.Like in the above code getText() is our getter method which is getting elements from database and returning it here.
here text will be the text of the button.
So every-time users starts the application he will see all the buttons that he created when he ran the application before and newly added button will appear on the spot and also will be stored in the database for future retrieval.
Remember we are just storing text of the button and assigning it unique id which helps us to create the buttons.Hope this helps

Categories

Resources