How do you change scenes using EditText in Android Studio? - 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
}

Related

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);
}
}
});

Android Studio: EditText is not updating

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));
}
});
}

TextInputEditText set hint start animated if edittext is not empty

I wanted to make my hint to not animate if the edit text is empty. If user start typing the hint will move up, but if the edit text is empty, the hint will return into the edit text.
I have tried
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.length() == 0){
textInputLayout4.setHintAnimationEnabled(false);
} else {
textInputLayout4.setHintAnimationEnabled(true);
}
}
but the hint does not act like how I wanted it to be.
Instead of beforeTextChanged(), try using afterTextChanged():
#Override
public void afterTextChanged(Editable s) {
if(s.toString().isEmpty()){
textInputLayout4.setHintAnimationEnabled(false);
} else {
textInputLayout4.setHintAnimationEnabled(true);
}
}

Android studio - how to access EditText from within inner class?

Hey guys pretty new to android studio, like the title says I've tried to put the text within the EditText into a var called "username" which I want to be able to use within my inner class but I don't know how to make it accessible from within.
here's what I do with the EditText, within the onCreate: (KeyListener is the name of my inner class)
KeyListener KL_Instance = new KeyListener();
EditText input_text = (EditText) findViewById(R.id.inputText);
input_text.setOnKeyListener(KL_Instance);
String username = input_text.getText().toString();
And here's what i'm trying to do within the inner class:
public class KeyListener implements View.OnKeyListener
{
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent)
{
if (keyCode == KeyEvent.KEYCODE_AT)
{
Toast.makeText(MainActivity.this, "Do not type #", Toast.LENGTH_LONG).show();
}
//if the enter key is pressed, then check to see if username is 8 or more chars
if (keyCode == keyEvent.KEYCODE_ENTER)
{
//if it is, display it
if(username.length => 8)
{
Toast.makeText(MainActivity.this, "Hi, " + username, Toast.LENGTH_LONG).show();
}else //else, ask for a username of atleast 8 chars
{
Toast.makeText(MainActivity.this, "Please enter a username that is 8 or more chars", Toast.LENGTH_LONG).show();
}
}
return false;
}
}
But it says "cannot resolve symbol 'username'" and I cant access "input_text" within that class either so If someone could help me out here it would be greatly appreciated,
Thanks in advance :)
If you want store the value of the EditText you will need to update it with your listener and why use a KeyListener and why not just a TextWatcher like in this post here
EDIT :
You should do something like that
input_text.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged (Editable s){
username = s.toString();
}
#Override
public void beforeTextChanged (CharSequence s,int start,
int count, int after){
}
#Override
public void onTextChanged (CharSequence s,int start,
int before, int count){
}
});
TextWatcher is an implementation of the Design Pattern Observer so this code will be triger each time someone use the EditText, you have to deal with this by doing your logic in the TextWatcher like calling service or something else in charge of handle the username.

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