EditText.getText has a delay - java

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

Related

How to show user input from dynamic created EditTexts in TextViews into another activity?

In main activity I have button which onClick method creates EditText every time when user clicks this button. I wanted to pass user input from each EditText to TextView into another activity. But TextView shows user input only from last EditText. My code may look strange, because I tried to solve problem on my own. How can I show user input from each created EditText? I am new to programming. Thank you in advance.
Java code
EditText code
int i=1;
int numberOfCreated = 2;
public void buttonCreate(View view){ // button onClick method
for(i=1;i<numberOfCreated;i++){
LinearLayout createdEditText1Layout=findViewById(R.id.createdEditText1Layout);
editText1 = new EditText(this);
LinearLayout.LayoutParams paramsForEditText1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText1.setLayoutParams(paramsForEditText1);
editText1.setInputType(InputType.TYPE_CLASS_TEXT);
editText1.setHint(R.string.editText1Hint);
editText1.setTextSize(16);
createdEditText1Layout.addView(editText1);
}
}
Intent to pass string to second activity
public void openResult() {
String productName = editText1.getText().toString();
Intent intent2 = new Intent(this, ResultActivity.class);
intent2.putExtra("product", productName);
startActivity(intent2);
}
public void buttonResultClick(View view){
openResult();
}
Second activity
int i=1;
int numberOfButtons = 2;
for(i=1;i<numberOfButtons;i++) {
LinearLayout layoutForProductName = findViewById(R.id.layoutForProductName);
TextView productNameTextView = new TextView(this);
LinearLayout.LayoutParams layoutParamsForProductNameTextView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
productNameTextView.setLayoutParams(layoutParamsForProductNameTextView);
productNameTextView.setTextSize(40);
String productName = getIntent().getStringExtra("product");
productNameTextView.setText(productName);
layoutForProductName.addView(productNameTextView);
}
From what I can tell it's because you're adding only one String productName
You can solve this by passing a String Array aka String[]
public void openResult() {
ArrayList<String> productNames = new ArrayList<String>();
for(int y=0; y < createdEditText1Layout.getChildCount(); y++)
{
if((View)layout.getChildAt(y) instanceof TextView){
String productName = editText1.getText().toString();
productNames.add(productName);
}
}
Intent intent2 = new Intent(this, ResultActivity.class);
intent2.putStringArrayListExtra("product", productNames);
startActivity(intent2);
}
And then in the second Activity you would recalled like
int i=1;
int numberOfButtons = 2;
for(i=1;i<numberOfButtons;i++) {
LinearLayout layoutForProductName = findViewById(R.id.layoutForProductName);
TextView productNameTextView = new TextView(this);
LinearLayout.LayoutParams layoutParamsForProductNameTextView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
productNameTextView.setLayoutParams(layoutParamsForProductNameTextView);
productNameTextView.setTextSize(40);
String productName = getIntent().getExtras().getStringArrayList("product").get(i);
productNameTextView.setText(productName);
layoutForProductName.addView(productNameTextView);
}

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.

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

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)

create dynamic textview and grid view or list view dynamically

#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.table1);
lv =getListView();
category_main = "";
new Getitems().execute(category_main);
sub = (Button) findViewById(R.id.submit);
sub.setOnClickListener(this);
b =(Button) findViewById(R.id.start);
b.setOnClickListener(this);
itemList = new ArrayList<HashMap<String, String>>();
}
#Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.start: try {
Log.d("start","click");
onbuttonclick();
} catch (JSONException e) {
e.printStackTrace();
}
break;
case R.id.submit:
Log.d("submi","click");
onsubmitclick();
break;
}
}
private void onsubmitclick() {
Log.d("submi inside","click");
EditText et = (EditText) findViewById(R.id.EditText05);
EditText et1 = (EditText) findViewById(R.id.EditText06);
TextView tx= (TextView) findViewById(R.id.name);
String json = "{'Order':[";
if(!et.equals(str)){
Log.d("json", "json");
String stra = "{'Table_id':"+Second.a+",'item_name':'"+tx.getText()+"','Item_quantity':'"+et.getText()+"','Item_additionaldetails':'"+et1.getText()+"'}";
json = json+stra;
}
json = json + "]}";
Log.d("json", json);
}
private void onbuttonclick() throws JSONException {
TableRow[] tr = new TableRow[arry1.length];
final TextView[] tx = new TextView[arry1.length];
GridLayout tl = (GridLayout) findViewById(R.id.tablelayout1);
tl.setRowCount(arry1.length*2);
tl.setColumnCount(1);
final Button but = new Button(S1.this);
b.setText("hi");
gl = new GridLayout[arry1.length];
for (i = 0; i < arry1.length; i++) {
final String cat = arry1[i].toString();
tx[i] = new TextView(S1.this);
tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tx[i].setAllCaps(true);
tx[i].setTextSize(15);
tx[i].setText(cat);
tr[i] = new TableRow(S1.this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i],new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tl.addView(tr[i],new GridLayout.LayoutParams());
tl.addView(gl[i], new GridLayout.LayoutParams());
}
b.setVisibility(View.GONE);
}
Here is my code for creating textviews dynamically.
and i am successful in this.
Now i want to add grid view or listview beneath every text view like the following image :
The same way i want to show text view and display grid or list view on click and this all should be made on bases of arry1 named array's length.
All i want is to create grid or list view dynamically.

ArrayList doesn't detect first input

I have an ArrayList in which the input of dynamically added EditTexts are referenced. I iterate the ArrayList so that I can get all the values of the EditTexts and add them together. However, I do have one non-dynamically added EditText that is already laid out in my layout.xml.
The problem is that whenever I click my Calculate Button and the only EditText on the screen is the already laid out EditText and it has an input of 1 or any other number, my Calculate Button shows the total input as 0. Whenever I add 1 or more editTexts dynamically, the total input includes the already laid out EditText after I click the Calculate Button.
I need to get the correct total input even if the only input is the non-dynamic editText.
int count = 1;
double gradeValue;
List<EditText> allEd = new ArrayList<EditText>();
List<Spinner> allSp = new ArrayList<Spinner>();
EditText editText1;
EditText editText2;
EditText tempText1;
EditText tempText2;
Spinner spinner1;
Spinner spinnerTemp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonAdd = (Button) findViewById(R.id.button1);
Button buttonDel = (Button) findViewById(R.id.button2);
Button buttonCalc = (Button) findViewById(R.id.button3);
spinner1 = (Spinner) findViewById(R.id.spinner1);
String[] options = new String[13];
options[0] = "A+";
options[1] = "A";
options[2] = "A-";
options[3] = "B+";
options[4] = "B";
options[5] = "B-";
options[6] = "C+";
options[7] = "C";
options[8] = "C-";
options[9] = "D+";
options[10] = "D";
options[11] = "D-";
options[12] = "F";
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, options);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner1.setAdapter(spinnerArrayAdapter);
allEd.add(editText2);
allSp.add(spinner1);
buttonAdd.setOnClickListener(this);
buttonDel.setOnClickListener(this);
buttonCalc.setOnClickListener(this);
}
#SuppressWarnings("deprecation")
public void onClick(View v) {
TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
switch(v.getId()){
case R.id.button1:
if(count != 16){
count++;
// Create the row only when the add button is clicked
TableRow tempRow = new TableRow(MainActivity.this);
EditText tempText1 = new EditText(MainActivity.this);
EditText tempText2 = new EditText(MainActivity.this);
TextView tempTextView = new TextView(MainActivity.this);
Spinner spinnerTemp = new Spinner(MainActivity.this);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
TextView textView3 = (TextView) findViewById(R.id.textView3);
tempTextView.setText(count + ".");
tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tempText1.setLayoutParams(editText1.getLayoutParams());
tempText2.setLayoutParams(editText2.getLayoutParams());
tempTextView.setLayoutParams(textView3.getLayoutParams());
tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
tempText2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
tempText2.setId(count);
spinnerTemp.setLayoutParams(spinner1.getLayoutParams());
spinnerTemp.setId(count);
String options[] = { "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" };
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, options);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinnerTemp.setAdapter(spinnerArrayAdapter);
allEd.add(tempText2);
allSp.add(spinnerTemp);
tempRow.addView(tempTextView);
tempRow.addView(tempText1);
tempRow.addView(tempText2);
tempRow.addView(spinnerTemp);
tableLayout1.addView(tempRow);
} else {
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
alertDialog.setTitle("Error");
alertDialog.setMessage("You can only have up to 16 rows!");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
break;
case R.id.button3:
int calculation = 0;
for(int i = 0; i < allEd.size(); i++) {
EditText totalUnits = allEd.get(i);
try {
int units = Integer.parseInt(totalUnits.getText().toString());
calculation += units;
}catch (Exception e) {
//ignore
}
}
double grade = 0;
for(int i = 0; i < allSp.size(); i++) {
double gradeValue = calcGradeValue(allSp.get(i).getSelectedItemPosition());
try {
double calculation1 = (gradeValue) * (Integer.parseInt(allEd.get(i).getText().toString()));
grade += calculation1;
}catch (Exception e) {
//ignore
}
}
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
alertDialog.setTitle("Your calculated GPA");
alertDialog.setMessage("Your calculated GPA is: " + (grade));
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
I think you need to take a look at where your adding the tempText2 editText to your list. Check that it isn't as a result of adding the dynamic editTexts.
Look at where you add the following
allEd.add(tempText2);
allEd.add(editText2);
It could be that your only adding the tempText2 as a result of adding a dynamic field. As such when you don't add a dynamic field the arraylist will be empty. resulting in 0. When you do add a dynamic field this will result in the calculation working.
Difficult to say this is it without more code.
I think that maybe u dont initilized value editText2, and u try to add null object to list, but I really cant say anything without any source code.

Categories