If statement will not change text view when taking progress from seekBar - java

I am trying to take the value of a seekBar and if that value is within a certain range to change the textView.
if (olivine.getProgress()==50) {
rock.setText("Olivine rich");
};
This is what I have tried but it does not change the textView.
olivine.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar bar) {
int value = bar.getProgress();
}
public void onStartTrackingTouch(SeekBar bar) {
}
public void onProgressChanged(SeekBar bar,
int paramInt, boolean paramBoolean) {
olivineper.setText("" + paramInt + "%");
}
});
The above puts the seekBar value into a textView. I have multiple seekBars in the activity if that makes a difference.

Why don't you put your logic into the OnSeekBarChangeListener?
public void onProgressChanged(SeekBar bar, int paramInt, boolean paramBoolean) {
if (paramInt==50) {
rock.setText("Olivine rich");
};
}

Related

How to set constant animation after EditText reach target length?

I want to set animation on ImageView when user put more than 3 characters. I wrote a code to do this. Main problem is when I keep putting more than 3 characters the animation starts again from 0 position always when new character is added, so it doesn't looks good.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_user_first);
initViews();
imageButtonNext.setEnabled(false);
horizontalScrollView.post(() -> horizontalScrollView.scrollTo(0, 0));
horizontalScrollView.setOnTouchListener((v, event) -> true);
editTextEnterName.addTextChangedListener(nameTextWatcher);
pulse = AnimationUtils.loadAnimation(NewUserFirst.this, R.anim.pulse);
imageButtonNext.setOnClickListener(view -> {
intent = new Intent(this, NewUserSecond.class);
startActivity(intent);
});
}
public void buttonAnimation(){
if(buttonBoolean){
imageButtonNext.setAnimation(pulse);
imageButtonNext.setColorFilter(ContextCompat.getColor(NewUserFirst.this, R.color.accent));
imageButtonNext.setEnabled(true);
} else {
imageButtonNext.clearAnimation();
imageButtonNext.setColorFilter(ContextCompat.getColor(NewUserFirst.this, R.color.gray));
imageButtonNext.setEnabled(false);
}
}
private final TextWatcher nameTextWatcher = 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) {
nameInput = editTextEnterName.getText().toString().trim();
if (nameInput.length() >= 3) {
buttonBoolean = true;
} else {
buttonBoolean = false;
}
}
#Override
public void afterTextChanged(Editable editable) {
buttonAnimation();
}
};
So the question is, what changes I need to make in my code to set animation, and do not renew it every time when add next character.
Here is the answer:
public void buttonAnimation(){
if(buttonBoolean){
if (!imageButtonNext.isEnabled()) {
imageButtonNext.setAnimation(pulse);
imageButtonNext.setColorFilter(ContextCompat.getColor(this, R.color.accent));
imageButtonNext.setEnabled(true);
}
} else {
imageButtonNext.clearAnimation();
imageButtonNext.setColorFilter(ContextCompat.getColor(this, R.color.gray));
imageButtonNext.setEnabled(false);
}
}
We need to add if statement in animation method. Program check animation is currently enabled, when yes, program do nothing. Result is animation triggered once live his own life until we modify edittext to length less than 3.

How to disable button with multiple edit text using Textwatcher?

I am trying to disable my button if my input edit texts are empty. I am using text watcher for this. To test it out , i have only tried with only two edit texts to start.
However, my button stays enabled no matter what.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
fnameInput = findViewById(R.id.et_firstname);
lnameInput = findViewById(R.id.et_lastname);
numberInput = findViewById(R.id.et_phone);
emailInput = findViewById(R.id.et_email);
nextBtn = findViewById(R.id.btn_next);
fnameInput.addTextChangedListener(loginTextWatcher);
lnameInput.addTextChangedListener(loginTextWatcher);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
launchNextActivity();
}
});
}
Text watcher method
private TextWatcher loginTextWatcher = 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) {
String firstNameInput =firstNameInput.getText().toString().trim();
String lastNameInput = lastNameInput.getText().toString().trim();
// tried doing it this way
nextBtn.setEnabled(!firstNameInput.isEmpty() && !lastNameInput.isEmpty());
//What i've also tried
if(firstNameInput.length()> 0 &&
lastNameInput.length()>0){
nextBtn.setEnabled(true);
} else{
nextBtn.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
I expect the button to be disabled if one or all inputs are empty and enabled when all input fields are filled out.
create a method check all condition there like
private void checkTheConditions(){
if(condition1 && condition2)
nextBtn.setEnabled(true)
else
nextBtn.setEnabled(false)
}
call this method from afterTextChanged(Editable s) method
Let us consider this case for 2 EditTexts only as for now.
define 2 global CharSequence as below
CharSequence fName="";
CharSequence lName="";
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
fnameInput = findViewById(R.id.et_firstname);
lnameInput = findViewById(R.id.et_lastname);
numberInput = findViewById(R.id.et_phone);
emailInput = findViewById(R.id.et_email);
nextBtn = findViewById(R.id.btn_next);
fnameInput.addTextChangedListener(textWatcher);
lnameInput.addTextChangedListener(textWatcher2);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
launchNextActivity();
}
});
}
then you have to define different textwatcher for each of your Edittext
then inside each of these textWatcher assign values to CharSequence defined above
private 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) {
fName=s;
validate(); //method to enable or disable button (find method below)
}
#Override
public void afterTextChanged(Editable s) {
}
};
now textWatcher2
private TextWatcher textWatcher2 = 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) {
lName=s;
validate(); //method to enable or disable button (find method below)
}
#Override
public void afterTextChanged(Editable s) {
}
};
now write validate method
void validate(){
if (fName.length()>0 && lName.length()>0){
nextBtn.setEnabled(true);
}else {
nextBtn.setEnabled(false);
}
}
Oh! You did a small mistake. Use OR condition instead of AND. So your code should be
nextBtn.setEnabled(!firstNameInput.isEmpty() || !lastNameInput.isEmpty());
And TextWatcher will only notify when you will manually change the inputs of EditText. So TextWatcher will not wark at starting. So at first in onCreate method you should manually check those EditText feilds.
Edit:
Android new DataBinding library is best suitable for this purpose.

Android Spinner to change text size doesn't save state

private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int p = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (p < 20) {
sb.setProgress(p);
}
}
});
}
So the problem is that the code I have above changes the text size of a text view. However when going back and forth the state isn't saved so the text size is then reset. I am really new to android and would appreciate any guidance or help on how I can achieve this. I basically have a tutorial app and the text needs to change from the settings menu which has a seekbar allowing the user to select what text size they want. Any Ideas?
You can do either in this way :
1)you can save the value of p in shared pref and when you come next time you will check the value of p and set it on textview
2)you can save the value in onSavedInstance,but the problem is that when user kill the app from background you are going to loose the value
if you have set sb.setMax(20); then you dose not need to
if (p < 20) {
sb.setProgress(p);
}
code is useless user cant seek more then 20
you have code like that
private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(CONTEXT);
int p = SP.getInt("key", p);
tutorialText.setTextSize(p);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
sb.setProgress(p);
Editor edit = SP.edit();
edit.putInt("key", p);
edit.commit();
}
});
}

Rating bar, how to get the number of stars?

I got an edit text and also a rating bar in my program. I am checking if the user has more than 3 characters in the edit text and for rating bar it should have something greater than 0.5 star (basically to check if they at least clicked on any of them).
final EditText commentbox = (EditText)findViewById(R.id.comment);
final RatingBar rating = (RatingBar)findViewById(R.id.rating);
final Button feedbackbutton = (Button)findViewById(R.id.submit);
final TextWatcher 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) {
}
#Override
public void afterTextChanged(Editable s) {
if(commentbox.length() >= 3 && rating.getNumStars() >=0.5){
feedbackbutton.setEnabled(true);
feedbackbutton.setBackgroundColor(Color.parseColor("#369742"));
} else {
feedbackbutton.setEnabled(false);
feedbackbutton.setBackgroundColor(Color.parseColor("#ffcacaca"));
}
}
};
commentbox.addTextChangedListener(watcher);
rating.setOnRatingBarChangeListener((this));
So as you can see it has to meet those condition in order for a button to become enabled and also to change its background colour. However soon as I write more than 3 characters in the edit text, the button enables itself and changes its colour. It's not looking for the rating bar condition.
Can someone help me please
To get rating from rating value:
float ratingValue = rating.getRating();
Add listener :
rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar arg0, float rateValue, boolean arg2) {
// TODO Auto-generated method stub
Log.d("Rating", "your selected value is :"+rateValue);
}
});
feedbackbutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
//Getting the rating and displaying it on the toast
String rating=String.valueOf(rating.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
Hope it solves your problem.

update seekbar using edittext and decimal numbers

ok let me explain:
i have a seekbar and a edittextbox, the seekbar only goes from 0 to 100, when the user used the seekbar and advance in the editbox it also show the progress, if the user input a number from 0 to 100 the seekbar automatically updates to the number in the edit textbox.
here is the code:
EditText num1;
SeekBar Sb1;
num1=(EditText)findViewById(R.id.hoja1E2);
Sb1=(SeekBar) findViewById(R.id.seekBar1);
Sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
//---change the font size of the EditText---
num1.setText(String.valueOf(progress));
}
});
num1.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) {
try{
//Update Seekbar value after entering a number
Sb1.setProgress(Integer.parseInt(s.toString()));
num1.setSelection(num1.getText().length());
} catch(Exception ex) {}
}
});
now my problem is i whant to do the same thing but this time the bar only goes from o to 1, i change a part of the code of the seekbar where i divide the progress whit 100 this way it show in the edittextbox decimal number, but i cant figure out how to introduce decimal number and update the seekbar if some can help my
This should work:
int progress = Math.round(Float.parseFloat(s.toString()) * 100f);
Sb1.setProgress(progress);
NOTE: You need to do a lot of validation

Categories

Resources