Android Studio: EditText is not updating - android-studio

I created a EditText and a Text Widget. The app should divide the written Number by 2 and show the result. But it also should be updated all the time. So if i write 4 then it should say 2 and if I write 10 it should say 5. Without a Button to confirm.
Have you an idea how to do so?
Sincerely, tobi

you could use TextWatcher and use onTextChange
EditText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
String value= EditText.getText().toString();
int finalValue=Integer.parseInt(value)/2;
TextView.setText(String.valueOf(finalValue));
}
});
}

Related

How to display decimals on android studio

Hello I have a problem between the double and int
my code "works" when I put the variables in int but I do not have the numbers after the decimal point as soon as I put in duplicate it puts the result to me at 0 and I do not see why
MyCartAdpter
public class MyCartAdapter extends RecyclerView.Adapter<MyCartAdapter.ViewHolder> {
Context context;
List<MyCartModel> list;
double totalAmount =0;
...
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
...
holder.totalPrice.setText(String.valueOf(list.get(position).getTotalPrice()));
//total amount pass to cart activity
totalAmount = (totalAmount + list.get(position).getTotalPrice());
Intent intent = new Intent("MyTotalAmount");
intent.putExtra("totalAmount",totalAmount);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
MyCartModel
public class MyCartModel {
...
double totalPrice;
...
}
public MyCartModel(..., double totalPrice, ...) {
...
this.totalPrice = totalPrice;
...
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
and my CartActivity
public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
double totalBill = intent.getIntExtra("totalAmount",0);
overAllAmount.setText("Montant Total :"+totalBill+"€");
}
};
thank you for your help because for 24 hours I have been looking
overAllAmount.setText(String.format("%.2f",totalBill));
Could also put the textviews input type to numberdecimal
android:inputType="numberDecimal"
Happy coding

How to listen to EditText changes and change ImageView Visibility at runtime?

Is there a way that I can for example make an ImageView to appear only after an InputEditText is not null? So before the InputEditText has some value, ImageView will not appear (hidden or invisible).
Of course.
in the image view's xml add the attribute:
android:visibility="gone"
or
android:visibility="invisible"
to make it invisible by default
You can see a demonstration of the difference between the two, here: http://tips.androidgig.com/invisible-vs-gone-view-in-android/#:~:text=This%20view%20is%20invisible%2C%20and,what%20is%20the%20actual%20difference.
Create a listener in your activity's onCreate() method, to track changes in your InputEditText, and change the visibility of your imageView, based on the text in your input
assuming your InputEditText view ID is "myInput", and your ImageView view ID is "myImage":
InputEditText myInput = findViewById(R.id.myInput);
myInput.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0){
ImageView myImage= findViewById(R.id.myImage);
myImage.setVisibility(View.VISIBLE);
}
}
});

TextWatcher in Android not setting values in other edittexts

I have three edittext values for which i am using textwatcher to update data.
I want that if i enter any value in anyone of the edit text then according to that value it should calculate and then update the edittext in which i entered the value automatically and after that set the value in other specif edit text and save the final value in string if need to send to any server.
//My Code
public class Land_details extends AppCompatActivity {
EditText land_acre_edit;
EditText land_kanal_edit;
EditText land_marla_edit;
Button land_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_land_details);
land_acre_edit = findViewById(R.id.land_acre_edit);
land_kanal_edit = findViewById(R.id.land_kanal_edit);
land_marla_edit = findViewById(R.id.land_marla_edit);
land_button = findViewById(R.id.save_land_detail);
land_acre_edit.addTextChangedListener(mytext_watcher);
land_marla_edit.addTextChangedListener(mytext_watcher);
land_kanal_edit.addTextChangedListener(mytext_watcher);
}
private TextWatcher mytext_watcher = 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) {
try {
String check_marla = land_marla_edit.getText().toString();
String check_kanal = land_kanal_edit.getText().toString();
String check_acre = land_acre_edit.getText().toString();
if (check_marla != null) {
if ((Integer.valueOf(check_marla) > 8)) {
check_acre =(String.valueOf(Integer.valueOf(check_kanal) + (Integer.valueOf(land_marla) / 20)));
check_marla=( String.valueOf(Integer.valueOf(land_marla) % 20));
check_acre = String.valueOf(check_acre) + (Integer.valueOf(check_kanal) / 8);
check_kanal = String.valueOf(Integer.valueOf(check_kanal) % 8);
land_marla_edit.setText(check_marla);
land_kanal_edit.setText(check_kanal);
land_acre_edit.setText(check_acre);
}}
}catch (NumberFormatException e){
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
I have tried to set my try catch code in aftertextChanged but at first it was giving me numberformatexception as soon as i enter a text above my contion my app crash but after that i tried using try catch to catch a numberformatexception after that i am able to add number above my condition and even after a long wait nothing happen can any one explain where i am going wrong.

How do you change scenes using EditText in Android Studio?

In my project, I am trying to change to different scenes depending on the number typed into an EditText. For example, it will launch as giving you 1 random number, and the EditText is default at 1, but if you want to have several random numbers at once, it will change scenes to go to a different layout.
I know to change scenes you use;
startActivity(new Intent(MainActivity.this, OtherActivity.class));
My trouble is with the if statement, if I use
if (value == 2);
It returns to me as "Cannot resolve symbol 'if(boolean)'"
Thank you for your time,
Lane
I think, by "Scenes" you mean to say you want to switch to a particular "activity" according to the number you type in the Edittext. If this is what you want to achieve, then you should add a text watcher to your edittext and monitor the value that is entered in the edittext onTextChange.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() > 0) {
//do something like showing a toast or changing the activity
if (charSequence.toString().equalsIgnoreCase("1")) {
//do something
} else if (charSequence.toString().equalsIgnoreCase("2")) {
//do something
}
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Remove semicolon ; from your if(value == 2); statement
It should be like:
if (value == 2) {
//do something here
}

onKeyUp is not firing when I press most keyboard keys

In my Android application, I would like to perform some logic each time a key on the keyboard is pressed, for example, I want to evaluate if they typed a "magic phrase".
To do so, I overrode the method onKeyUp in my Activity and experimented to see if it was firing by making it fire a Toast.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Toast toast = Toast.makeText(this, "Key Down", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
return super.onKeyUp(keyCode, event);
}
The problem is, the Toast is not showing when I press numbers or letters on the keyboard. It is only showing up when backspace is pressed. Why is this?
How do I get to work for all keys?
Ended up solving this by adding a TextChangedListener to my EditText field.
Here's my simple code, which just makes a little toast appear with the digit count:
EditText passcodeInput = (EditText) findViewById(R.id.passcodeInput);
passcodeInput.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable editable) {
digitsPressed++;
Toast.makeText(Passcode.this, Integer.toString(digitsPressed), Toast.LENGTH_LONG).show();
}
});

Resources