Changing textview on edittext ( when possible without a button) - java

I'm kinda new to java programming for android, so if i make stupid mistakes, i'm sorry.
So basically, what i wanna make is an app where if you type in the answer correctly, the next textview is gonna be displayed. And when the next textView is displayed, you're needing to give a answer to that textView, when the answer is given correctly. The textview changes again. And so on.
Does anybody have an idea how to do this?
If you don't undarstand what im saying, here is a example:
public class Game extends AppCompatActivity {
public static EditText editText_ans;
public static TextView textView_1;
String enteredText = editText.getText().toString();
If(enteredText = 3 && textView_1 = #string/1+2){
setText.textView_1(#string/3+4)
}
If(enteredText = 7 && textView_1 = #string/3+4){
setText.textView_1("100 - 23")
I'm really stuck and i hope that you guys wanna help me.

If you wanted to change the view without button you can use method addTextChangeListner() which will notify you when when the text hasbeen change for particular edittext.
edittext.addTextChangedListener(textWatcher);
private final TextWatcher textWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else{
textView.setText("You have entered : " + editText.getText());
}
}
};

int x=0; //to keep track of qustions
private List<String> mQuestionList=new ArrayList<>(); //array of question
private List<String> mAnswerList=new ArrayList<>(); //array of question answer
displayquestion.settext(mQuestionList.get(x);//displayquestion is textview
//nextquestion is the button when user click it will first check answer and than move to next question if answer is correct
nextquestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String answer=editText.getText().toString();
if(answer.equal(mAnswerList.get(x)){
x=x+1;
displayquestion.settext(mQuestionList.get(x); //answer is correct display next quesion
}else{
//wrong answer
}
}
});

Related

Calculation with textWatcher android

In my project I have 7 EditText field.
User will only need to fill their own value in first two EditText.
I need a series calculation like this...
(after user input) subtract ed1-ed2 the result automatically show in ed3 field.
multiply ed3*2 = result show in ed4 field automatically.
multiply ed3*3= result will show in ed5 field.
multiply ed3*4=result will show in ed6 field.
Finally sum of ed4+ed5+ed6= result will show in ed7 field automatically.
So how to do it any one please help.
Thank you in Advance.
All EditText use a same TextWatcher. A simple solution like this.
EditText et1;
EditText et2;
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int num1 = Integer.valueOf(et1.getText().toString()); // Make sure you get a number
int num2 = Integer.valueOf(et2.getText().toString());
// do your calculation
}
#Override
public void afterTextChanged(Editable s) {
}
};
et1.addTextChangedListener(textWatcher);
et2.addTextChangedListener(textWatcher);

Getting EditableText from EditText in RecyclerView

I've recycler view with 0 items, I've added an option to manually add the items, my stracture is simple :
RV_Item.xml (contains EditText).
MyItem, which is an Object for RV ( contains private String Text; ).
MainActivity.java, where the stuff happen.
// My List<Object>
List<MyItem> Items = new ArrayList<>();
// For Adding, I've added FAB-Button, When Clicked, it does the following :
Items.add(new MyItem());
CheckForEmptyItems();
mAdapter.notifyItemInserted(0);
Now, When the user click the save button, i want to take all the edittext in all the items he had added, i'm doing in the following way :
for(MyItem items : Items){
Log.i("PrintingInfo", items.getText() );
}
The problem is, i'm not getting the text he entered in all EditText fields, and it's returning Null in all of them, What's the issue in this ?
So, i don't know why always i know the answer after posting, but here's how you gonna know what the user typed :
in your Adapter Class, in onBindViewHolder method, add textlistener for the EditText, here's an example :
holder.MyEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
itemList.get(position).setText(s.toString());
}
});
Hope that helps you!

How to update method to return updated information from textwatcher after pressing button

I have declared my variable, 'changed' too null so that I can check if it changes when the edittext is changed,
The problem I think is, when I press either the save button or the cancel button, it will always produce the value null, as upon clicking the button it is still null. However, I thought that the textwatcher would listen to the EditText and even if nothing was changed in the EditText it would by default change the SetChanged() to false as it provided "live updates", however clearly this isn't the case, am I doing something wrong? or am I supposed to approach it in a different way?, is there some way of refreshing it?
Advise would be greatly appreciated.
(P.S Some code was deleted to reduce the size and make it look easy on the eye, so excuse me for any missing braces. Furthermore, the activity does run properly as it shows the layout.However upon pressing any of the buttons it causes it to crash.)
public class EditNewItemActivity extends AppCompatActivity{
private Boolean changed = null;
private TextView title,content;
private Button saveBtn,cancelBtn;
private String date;
private int id;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_item);
title = (TextView) findViewById(R.id.editItemTitle);
content = (TextView) findViewById(R.id.editItemDescription);
saveBtn = (Button) findViewById(R.id.editItemSaveBtn);
cancelBtn = (Button) findViewById(R.id.editItemCancelBtn);
Bundle extras = getIntent().getExtras();
title.setText(extras.getString("title"));
content.setText(extras.getString("content"));
date = extras.getString("date");
id = extras.getInt("id");
GenericTextWatcher textWatcher = new GenericTextWatcher();
title.addTextChangedListener(textWatcher);
content.addTextChangedListener(textWatcher);
ClickEvent clickEvent = new ClickEvent();
saveBtn.setOnClickListener(clickEvent);
cancelBtn.setOnClickListener(clickEvent);
}
private class ClickEvent implements View.OnClickListener{
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.editItemSaveBtn:
save();
break;
case R.id.editItemCancelBtn:
cancel();
break;
}
}
}
private void cancel() {
if (getChanged() == null){
//This was used to simply verify that getchanged was still null.
}
if (title.getText().toString() != "" || content.getText().toString() != ""){
if (getChanged() == false) {
// if nothing has been changed let it cancel etc
}else {
}
}
}
private void save() {
if (tempTitle != "" || tempContent != "") {
if(getChanged() == true){
}
}
public Boolean getChanged() {
return changed;
}
public void setChanged(Boolean changed) {
this.changed = changed;
}
private class GenericTextWatcher implements TextWatcher{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.v("Beforetext:", s.toString());
EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(false);
}
#Override
public void afterTextChanged(Editable s) {
Log.v("afterTextChanged:", s.toString());
EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(true);
Log.v("Status:", editItem.getChanged().toString());
}
}
You had change the changed. But which you changed is in your new EditNewItemActivity not in your current page.
This is where you made mistake (beforeTextChanged and afterTextChanged in your GenericTextWatcher):
EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(false); //or true
You should just call:
setChanged(false); // or true
In fact, you should not new an activity yourself, activity must be create by the Android Framework so that it can be managed by the system.

How to extract String using AutoCompleteTextView?

I know that there are multiple posts in Stackoverflow addressing this query. However, for some reason I am still failing to extract the string from AutoCompleteTextView. I tried using the onItemClickListener for this purpose. I am unable to identify where I am going wrong.
The Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
addPurchaseItemName = (AutoCompleteTextView) findViewById(R.id.addPurchaseProductName);
vivzHelper = new VivzDatabaseAdapter(this);
String[] autoCompleteName = vivzHelper.getInventoryNameFilterBySupplierName(vivzHelper.getSupplierID(param1));
ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
addPurchaseItemName.setThreshold(1);// starts working from first char
addPurchaseItemName.setAdapter(NameAdapter);
addPurchaseItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
itemName = String.valueOf(arg0.getItemAtPosition(arg2));
}
});
}
I want to assign the value of the extracted string to itemName which is initialized at the beginning of the activity. Can some one point out where am I going wrong? I have surfed multiple resources. Maybe, I am missing something.
Note :
This code was already posted to address a an issue on IllegalArgumentException in StackOverFlow a couple of days ago. Since, the topic of that question doesn't point more specifically the problem posted here, I thought that posting a new question will make sense. Hence I hope, my question won't be down-voted or edited as duplicate
Update 01 : #Deividi Cavarzan forgot including the below line of code when editing this question
ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
Update 02 : Declaring the itemName
public class AddPurchase extends ActionBarActivity {
AutoCompleteTextView addPurchaseItemName;
String itemName;
try to get the itemName on addTextChangedListener callback
addPurchaseItemName.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
itemName = s.toString();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
if you just want to get the selected item string from auto complete, then
itemName = addPurchaseItemName.getText();
or even better-
itemName = addPurchaseItemName.getText().toString().trim();
Updated answer
instead of setting onItemClickListener , set OnItemSelectedListener , this should definitely work.
addPurchaseItemName.setOnSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
//... your stuff
itemName = addPurchaseItemName.getText().toString().trim();
Toast.makeText(getApplicationContext(), "selected value - "+itemName, Toast.LENGTH_SHORT);
}
});

Getting character count of EditText

I'm trying to get a character count of an EditText. I've looked into different properties of the EditText and TextView classes, but there doesn't seem to be a function that returns the amount of characters. I have tried to use the TextWatcher, but that is not ideal since sometimes I load a saved message into the EditText from the preferences, and TextWatcher does not count characters not typed right then.
Any help would be great!
Cheers!
Just grab the text in the EditText as a string and check its length:
int length = editText.getText().length();
EditText edittext;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//This sets a textview to the current length
textview.setText(String.valueOf(s.length());
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(mTextEditorWatcher);
In Kotlin it's very easy
<EditText_name>.Text.length

Categories

Resources