Android Studio How to print on screen a variable content (number) - java

private EditText InputWiek; //First input
private EditText InputTspocz; //Second input
private TextView textout;
float Wiek = InputWiek;
float Tspocz = InputTspocz;
int Tmax = 220-Wiek;
int RT = Tmax-Tspocz;
int Tburn = 70*RT/100+Tspocz;
public void buttonOnClick(View v) {
Button button=(Button) v;
InputWiek = (EditText) findViewById(R.id.idWiek);
InputTspocz = (EditText) findViewById(R.id.idTspocz)
textout = (TextView) findViewById(R.id.txtOutput;
textout.setText(Tburn.getText())); //A little scrap here :/
}
}

you can use String.valeuOf(Tburn)

Related

Java/Android : First tiny app answer always "false"

I try to create my first tiny app but i have a problem.
My tiny math app always say my answer is false.I don't understand why.
This is my first app, i don't know where i'm wrong.
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
Answer always "False"
I'm a new student in the dev world.
The problem with code is that you are comparing string with integer, that's why it always returns false as java is strictly typed language.
problem code:
if(str.equals(result)){...}
possible solutions:
if( str.equals(""+result)){...}
or
str.equals(String.valueof(result)) // best solution
or
if(result==Integer.parseInt(str)){...}
Here is corrected code:
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(""+result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
As azurefrog comment says, you're comparing a String to an int. You will need to transform str to an int or result to a String. You can for example do:
String str=question.getText().toString();
if (str.equals(String.valueOf(result))) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}

Android new class TextWatcher cannot tran value from mainActivity

I have the problem on android app development, I have a class to TextWatcher.
but the textWatcher cannot use the method of getSelectionStart(), getSelectionEnd(), length(), findViewById(mInputLayout), setText(s) and setSelection(tempSelection).
and the mainActivity cannot tran the value into customTextWatcher.
Could you help me please.
public class customTextWatcher implements TextWatcher {
CharSequence temp;
int editStart;
int editEnd;
String mEditText; //EditText
String mInputLayout; //Input Layout
int tempLength; // Set the EditText length to limit it
//private String activity; // Set the Activity of the class to use this class
String errorText; // When out of boundary with EditText , it will show the errorText
// Constructor for this Class,
// Get the params from main Class
public customTextWatcher(String mEditText, String mInputLayout, int tempLength, String errorText) {
this.mEditText = mEditText;
this.mInputLayout = mInputLayout;
this.tempLength = tempLength;
//this.activity = activity;
this.errorText = errorText;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
temp = s;
}
#Override
public void afterTextChanged(Editable s) {
// String selectedText = mEditText.getText().substring(editStart, editEnd);
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
if (tempLength.length() > 10) {
TextInputLayout til = (TextInputLayout)findViewById(mInputLayout);
til.setErrorEnabled(true);
til.setError(errorText);
s.delete(editStart - 1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
}
mEditTextLastName = (EditText) findViewById(R.id.input_lastName);
mEditTextFirstName = (EditText) findViewById(R.id.input_firstName);
mEditTextHomeAddress = (EditText) findViewById(R.id.input_homeAddress);
mEditTextCountry = (EditText) findViewById(R.id.input_country);
mEditTextPhoneCode = (EditText) findViewById(R.id.input_PhoneCode);
mEditTextMobile = (EditText) findViewById(R.id.input_mobile);
mEditTextOtherPhone = (EditText) findViewById(R.id.input_otherPhone);
mEditTextEmail = (EditText) findViewById(R.id.input_email);
mEditTextPassword = (EditText) findViewById(R.id.input_password);
mEditTextReComfirmPassword = (EditText) findViewById(R.id.input_reConfirmPassword);
mEditTextWechat = (EditText) findViewById(R.id.input_wechat);
/**
* mTextInputLayout findById of layout TextInputLayout
*/
mTextInputLayout_LastName = (TextInputLayout) findViewById(R.id.lastNameLayout);
mTextInputLayout_FistName = (TextInputLayout) findViewById(R.id.firstNameLayout);
mTextInputLayout_HomeAddress = (TextInputLayout) findViewById(R.id.homeAddressLayout);
mTextInputLayout_Country = (TextInputLayout) findViewById(R.id.countryLayout);
mTextInputLayout_PhoneCode = (TextInputLayout) findViewById(R.id.phoneCodeLayout);
mTextInputLayout_Mobile = (TextInputLayout) findViewById(R.id.mobileLayout);
mTextInputLayout_OtherPhone = (TextInputLayout) findViewById(R.id.otherPhoneLayout);
mTextInputLayout_Email = (TextInputLayout) findViewById(R.id.emailAddressLayout);
mTextInputLayout_Password = (TextInputLayout) findViewById(R.id.passwordLayout);
mTextInputLayout_ReComfirmPassword = (TextInputLayout) findViewById(R.id.reConfirmPasswordLayout);
mTextInputLayout_Wechat = (TextInputLayout) findViewById(R.id.wechatLayout);
mEditTextLastName.addTextChangedListener(new customTextWatcher(mEditTextLastNam,mTextInputLayout_LastName,20,"error : Can't over 20's character!"));
That is because you moved your TextWatcher into separate class.
If your TextWatcher is inner class within your Activity, you can access that Activity (Context). One solution is to define callback interfaces in your TextWatcher and implement it in Activity. By doing so, you will be able to set your Activity as callback for TextWatcher and access Activity's methods.

How to sort an ArrayList of Objects (Player Names and Player Scores)

I try solve this but I don't know what I'm doing wrong, has passed 7 days for trying a lot of things, I need help solve it please and I think the mistake is on my created class.
I have 4 players with 4 text view and 4 other text view for the score of the players. Also I have made a new other 4 textviews for the place1, place2, place3, place4. What I want to do is that I need to sort scores and display them in place1, place2, place3, place4 along with the name of that player who has that score.
What I did until now is that I created my class into the main activity above the onCreate section
public class MainActivity extends AppCompatActivity {
//my created class
class JucatorScor {
public String Name;
public int Scor;
public JucatorScor (String sName, int iScor) {
this.Name = sName;
this.Scor = iScor;
}
}
//begin oncreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//end oncreate
}
And I also have a button, when I click it, read my values from the text view name of players, score of players and after sorting the array display place1, place2, place3, place4 like this:
//begin button click
public void sorteazaCLICK (View view) {
//initialization place1, place2, place3, place4 where i will put the sorting value
TextView varPozitia1 = (TextView) findViewById(R.id.text_loc1);
TextView varPozitia2 = (TextView) findViewById(R.id.text_loc2);
TextView varPozitia3 = (TextView) findViewById(R.id.text_loc3);
TextView varPozitia4 = (TextView) findViewById(R.id.text_loc4);
//initialization name of players
TextView varNumeJucator1 = (TextView) findViewById(R.id.text_numeJuc1);
String sNameJuc1;
sNameJuc1 = varNumeJucator1.getText().toString();
TextView varNumeJucator2 = (TextView) findViewById(R.id.text_numeJuc2);
String sNameJuc2;
sNameJuc2 = varNumeJucator1.getText().toString();
TextView varNumeJucator3 = (TextView) findViewById(R.id.text_numeJuc3);
String sNameJuc3;
sNameJuc3 = varNumeJucator1.getText().toString();
TextView varNumeJucator4 = (TextView) findViewById(R.id.text_numeJuc4);
String sNameJuc4;
sNameJuc4 = varNumeJucator1.getText().toString();
//initialization scores
TextView varScorJucator1 = (TextView) findViewById(R.id.text_scorJuc1);
String t1;
t1 = varScorJucator1.getText().toString();
TextView varScorJucator2 = (TextView) findViewById(R.id.text_scorJuc2);
String t2;
t2 = varScorJucator2.getText().toString();
TextView varScorJucator3 = (TextView) findViewById(R.id.text_scorJuc3);
String t3;
t3 = varScorJucator1.getText().toString();
TextView varScorJucator4 = (TextView) findViewById(R.id.text_scorJuc4);
String t4;
t4 = varScorJucator1.getText().toString();
//transform the values from text view scores into integer
int iScorJuc1;
iScorJuc1 = Integer.parseInt(t1);
int iScorJuc2;
iScorJuc2 = Integer.parseInt(t2);
int iScorJuc3;
iScorJuc3 = Integer.parseInt(t3);
int iScorJuc4;
iScorJuc4 = Integer.parseInt(t4);
ArrayList<JucatorScor> arrJucatorScor = new ArrayList<>(4);
//building manually the array list
arrJucatorScor.add(new JucatorScor(sNameJuc1, iScorJuc1)); //position 0
arrJucatorScor.add(new JucatorScor(sNameJuc2, iScorJuc2)); //position 1
arrJucatorScor.add(new JucatorScor(sNameJuc3, iScorJuc3)); //position 2
arrJucatorScor.add(new JucatorScor(sNameJuc4, iScorJuc4)); //position 3
Collections.sort(arrJucatorScor, new Comparator<JucatorScor>() {
//sorting my object array depending on scores
#Override
public int compare(JucatorScor o1, JucatorScor o2) {
return Integer.valueOf(o1.Scor).compareTo(o2.Scor);
}
});
//here display them from bigger to smaller according to eatch place1, place2, place3, place4
varPozitia1.setText(String.valueOf(arrJucatorScor.get(3)));
varPozitia2.setText(String.valueOf(arrJucatorScor.get(2)));
varPozitia3.setText(String.valueOf(arrJucatorScor.get(1)));
varPozitia4.setText(String.valueOf(arrJucatorScor.get(0)));
} //end button click
The problem is that when I click the button don't put on place1, place2, place3, place4 the name and the score sorted... just appear some thing like in this photo:
Here you use the toString() method of JucatorScor to render the objects :
varPozitia1.setText(String.valueOf(arrJucatorScor.get(3)));
varPozitia2.setText(String.valueOf(arrJucatorScor.get(2)));
varPozitia3.setText(String.valueOf(arrJucatorScor.get(1)));
varPozitia4.setText(String.valueOf(arrJucatorScor.get(0)));
What you see in your screen is a default implementation toString() provided by a base class.
Either you implement toString() with the wished text
Or the other solution is to perform the rendering in a helper method and that also sets the suitable String in the TextView:
public set renderTopJucatorScor(JucatorScor jucatorScor, int ranking, TextView varPozitia){
varPozitia.setText("Loc" + ranking + " = " + jucatorScor.getName() + jucatorScor.getScore());
}
Then you can call the helper method like that :
renderTopJucatorScor(varPozitia1, arrJucatorScor.get(3), 1);
renderTopJucatorScor(varPozitia2, arrJucatorScor.get(2), 2);
renderTopJucatorScor(varPozitia3, arrJucatorScor.get(1), 3);
renderTopJucatorScor(varPozitia4, arrJucatorScor.get(0), 4);
After discovering ( with the help of user "davidxxx" ), some mistakes into my initialization of textviews, finnaly i have managed to solve the problem. Also i added into my created class a method: toString.
The correct solution is this:
//my created class
class JucatorScor {
public String Name;
public int Scor;
public JucatorScor (String sName, int iScor) {
this.Name = sName;
this.Scor = iScor;
}
//this is the toString added to diplay name and score
#Override
public String toString() {
return Name + " " + Scor;
}
}
and the exectution code from the button click
//begin button click
public void sorteazaCLICK(View view) {
//initialization of textvies for place1 to place 4
TextView varPozitia1 = (TextView) findViewById(R.id.text_loc1);
TextView varPozitia2 = (TextView) findViewById(R.id.text_loc2);
TextView varPozitia3 = (TextView) findViewById(R.id.text_loc3);
TextView varPozitia4 = (TextView) findViewById(R.id.text_loc4);
//initialization for name of players
TextView varNumeJucator1 = (TextView) findViewById(R.id.text_numeJuc1);
String sNameJuc1;
sNameJuc1 = varNumeJucator1.getText().toString();
TextView varNumeJucator2 = (TextView) findViewById(R.id.text_numeJuc2);
String sNameJuc2;
sNameJuc2 = varNumeJucator2.getText().toString();
TextView varNumeJucator3 = (TextView) findViewById(R.id.text_numeJuc3);
String sNameJuc3;
sNameJuc3 = varNumeJucator3.getText().toString();
TextView varNumeJucator4 = (TextView) findViewById(R.id.text_numeJuc4);
String sNameJuc4;
sNameJuc4 = varNumeJucator4.getText().toString();
//initialization of scores
TextView varScorJucator1 = (TextView) findViewById(R.id.text_scorJuc1);
String t1;
t1 = varScorJucator1.getText().toString();
TextView varScorJucator2 = (TextView) findViewById(R.id.text_scorJuc2);
String t2;
t2 = varScorJucator2.getText().toString();
TextView varScorJucator3 = (TextView) findViewById(R.id.text_scorJuc3);
String t3;
t3 = varScorJucator3.getText().toString();
TextView varScorJucator4 = (TextView) findViewById(R.id.text_scorJuc4);
String t4;
t4 = varScorJucator4.getText().toString();
//transformation scores into integer values
int iScorJuc1;
iScorJuc1 = Integer.parseInt(t1);
int iScorJuc2;
iScorJuc2 = Integer.parseInt(t2);
int iScorJuc3;
iScorJuc3 = Integer.parseInt(t3);
int iScorJuc4;
iScorJuc4 = Integer.parseInt(t4);
ArrayList<JucatorScor> arrJucatorScor = new ArrayList<>(4);
//manually construct of the object array list
arrJucatorScor.add(new JucatorScor(sNameJuc1, iScorJuc1)); //position 0
arrJucatorScor.add(new JucatorScor(sNameJuc2, iScorJuc2)); //position 1
arrJucatorScor.add(new JucatorScor(sNameJuc3, iScorJuc3)); //position 2
arrJucatorScor.add(new JucatorScor(sNameJuc4, iScorJuc4)); //position 3
Collections.sort(arrJucatorScor, new Comparator<JucatorScor>() {
//the sort of the object list by their scores
#Override
public int compare(JucatorScor o1, JucatorScor o2) {
return Integer.valueOf(o1.Scor).compareTo(o2.Scor);
}
});
//here are displayed in the position place1 to place4 from bigger score to smaller score
varPozitia1.setText(String.valueOf(arrJucatorScor.get(3)));
varPozitia2.setText(String.valueOf(arrJucatorScor.get(2)));
varPozitia3.setText(String.valueOf(arrJucatorScor.get(1)));
varPozitia4.setText(String.valueOf(arrJucatorScor.get(0)));
}//end button click
Thanks very much for the help.

Spinner value is not captured

Please, do not mark this as duplicate if you are not sure
I have three spinners and a botton. When the botton is clicked, the program makes a calculation depending on the value of the three spinners. Then this value passes two another activity and it shows in an editText. Here is my code:
Main
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
capturarTexto();
}
private void capturarTexto() {
Button button_calc = (Button) findViewById(R.id.button_calc);
button_calc.setOnClickListener(get_edit_view_button_listener);
}
private Button.OnClickListener get_edit_view_button_listener = new Button.OnClickListener() {
public void onClick(View v) {
EditText edit_text = (EditText) findViewById(R.id.textBox1);
String edit_text_value = edit_text.getText().toString();
StringTokenizer st = new StringTokenizer(edit_text_value);
int num_words = st.countTokens();
Spinner espec = (Spinner) findViewById(R.id.espec);
String espec_value = espec.getSelectedItem().toString();
Spinner lengor = (Spinner) findViewById(R.id.lista_origen);
String lengor_value = lengor.getSelectedItem().toString();
Spinner lengdest = (Spinner) findViewById(R.id.lista_destino);
String lengdest_value = lengdest.getSelectedItem().toString();
double precio = 0;
if(espec_value .equals("Medicina")){
if (lengor_value .equals("ES") && lengdest_value .equals("EN")){
precio = num_words * 0.12;
}
if (lengor_value .equals("ES") && lengdest_value .equals("FR")){
precio = num_words * 0.12;
}
if (lengor_value .equals("ES") && lengdest_value .equals("DE")){
precio = num_words * 0.12;
}
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
intent.putExtra("precio",precio);
startActivity(intent);
}
};
}
Main2
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
int precio =(int) intent.getExtras().getInt("precio");
TextView txtCambio = (TextView) findViewById(R.id.textView4);
txtCambio.setText("Precio Total: "+ precio + " €");
}
After testing it, the value passed in this line of code:
intent.putExtra("precio",precio)
is allways 0. But if I change it to this:
intent.putExtra("precio",num_words)
it passes correctly the total number of words. This makes me think that the script is not entering in the first if(espec_value .equals("Medicina")) and then, it is not making any calculation.
Does anyone have an idea of how to solve this problem?
Thank you for your time
You are sending Double value and accessing Integer value.
Change the line in Main3Activity.
double precio = intent.getExtras().getDouble("precio");
If you want to parse double value to int then add one more line
int p = (int) precio;

EditText.getText has a delay

I have this code:
void sendMessage(){
EditText messageText = (EditText) findViewById(R.id.editText3);
String messageString = messageText.getText().toString();
LinearLayout chatLayout = (LinearLayout) findViewById(R.id.chatView);
TextView chatMessage = new TextView(this);
chatMessage.setText(messageString);
chatLayout.addView(chatMessage);
messageText.setText("");
scrollChatDown();
/*
int arraySize = messages.size();
messages.set(arraySize + 1, chatMessage);
*/
}
When I call the function sendMessage(); by a button, it gives an empty TextView, when I call the function again, it gives me a TextView with the text.
Output
I did what jiotman said but it didn't work, now I have this
void sendMessage(){
TextView chatMessage = new TextView(this);
EditText messageText = (EditText) findViewById(R.id.editText3);
String messageString = messageText.getText().toString();
LinearLayout chatLayout = (LinearLayout) findViewById(R.id.chatView);
chatLayout.addView(chatMessage);
chatMessage.setText(messageString);
messageText.setText("");
scrollChatDown();
/*
int arraySize = messages.size();
messages.set(arraySize + 1, chatMessage);
*/
}
As I see, you`r trying to implement kind of list with item population.
I`d use bubble list view for this purpose, here is a simple tutorials how to do it.
http://javapapers.com/android/android-chat-bubble/
http://blog.booleanbites.com/2012/12/android-listview-with-speech-bubble.html

Categories