I have an EditText field that represents an ID number. That field can either be filled programmatically, using IDField.setText(String) based on the results of a card swipe, or it can be filled manually using the keyboard.
Once the text is filled both methods (auto login--based on swipe, or manual--based on button click) both run the same sign in script. However when I go to grab the contents of the EditText field, if I edited the text manually I get an empty string returned. If the text was set programmatically then it works perfectly.
This doesn't make any sense to me. Is there a reason that editText.getText().toString() would not return the content that is visibly shown in the textbox?
XML:
<Button
android:id="#+id/btn_swipeCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/signInID"
android:layout_toLeftOf="#+id/textView1"
android:onClick="SignInStudent"
android:text="Swipe ID" />
Button Initialization:
IDField = (EditText) layout.findViewById (R.id.signInID);
LoginButton = (Button) findViewById(R.id.button1);
LoginButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { SignInStudent(); } } );
Card Swipe:
// displays data from card swiping
private Runnable doUpdateTVS = new Runnable()
{
public void run()
{
try
{
//Grab ID Number
String[] splitMSG = strMsrData.split("=");
//txtIDNumber.setText(splitMSG[2]);
IDField.setText(splitMSG[2]);
StringBuffer hexString = new StringBuffer();
hexString.append("<");
String fix = null;
for (int i = 0; i < msrData.length; i++) {
fix = Integer.toHexString(0xFF & msrData[i]);
if(fix.length()==1)
fix = "0"+fix;
hexString.append(fix);
if((i+1)%4==0&&i!=(msrData.length-1))
hexString.append(' ');
}
hexString.append(">");
myUniMagReader.WriteLogIntoFile(hexString.toString());
SignInStudent();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
Sign In Logic:
public void SignInStudent()
{
String temp = "http://wwww.tempUrl.com/signIn?ID="+ IDField.getText().toString() + "&DeviceId="+KEY;
webView.loadUrl(temp);
}
The layout is only updated during the onCreate phase of the loop. This is fired when an onResume event is called as well which explains why the fields update after you lock and unlock the device. There are a few workarounds for this such as doing more background processing and then creating a new view with correct values, or using a surfaceView that allows drawing to occur while the program is in its normal execute cycle.
For my application I either do background processing and then move to a new view, or have a view that just keeps calling itself to get the onCreate events to fire again. The solution depends on the application, but that's why the problem occurs.
Related
I pass the text through Intent to another activity, through which it is determined what will be shown to the user, dialog window with victory/defeat.
Everything seems to work well, but the problem is that when the phone screen turns off, the actions are repeated. For example, after winning, I received a dialog about what I won, after that, closing the dialog box, turned off the screen while still in the same activity, then turned the screen back on and I get the dialog box again. It seems that I put the code for showing the dialog in onResume, but I didn't.
I tried turning off the re-show of the dialog box in the following ways, which I will show below:
getIntent().removeExtra("TEXT");
boolean
By the way, about boolean, when I created a variable with this code: boolean offScreen = false, then in onCreate I checked in the if condition, and in the action set boolean to true, the dialog box again appeared, it seems that the activity doesn't pay attention to the boolean.
Please tell me how should I.
Just in case, here's the whole code:
String win_lose_select;
String text = getIntent().getStringExtra("TEXT");
win_lose_select = text;
Handler handler4 = new Handler();
handler4.postDelayed(() -> {
if (Objects.equals(win_lose_select, "win")){
youWin_DIALOG();
soundPlay(star_sound);
}
if (Objects.equals(win_lose_select, "lose")){
youWin_DIALOG();
TextView textView = you_win_dia.findViewById(R.id.textWin);
textView.setText("You lost, don't be discouraged, maybe you'll be lucky next time!\nThe defeat is counted.");
soundPlay(lose_sound);
}
if (Objects.equals(win_lose_select, "winNetwork")){
youWin_DIALOG();
TextView textView = you_win_dia.findViewById(R.id.textWin);
textView.setText("You have won, congratulations, your opponent has surrendered!\nVictory counted.");
soundPlay(star_sound);
}
},2000);
public void youWin_DIALOG() {
you_win_dia = new Dialog(this);
you_win_dia.requestWindowFeature(Window.FEATURE_NO_TITLE);
you_win_dia.setContentView(R.layout.you_win);
you_win_dia.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
you_win_dia.setCancelable(true);
getIntent().removeExtra("TEXT");
win_lose_select = "no";
Button go_to_rooms_win = (Button) you_win_dia.findViewById(R.id.go_to_rooms_win);
go_to_rooms_win.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay (tuck);
you_win_dia.dismiss();
}
});
you_win_dia.show();
Window window = you_win_dia.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
I am trying to add the text written in a textfield everytime and add it into an ArrayList. For eg- if the text "abcdef" is written in the textfield, then it should get added into the list. Again if the text "ghijkl" is written in the textfield, than that should get added.
My code for adding the text into the listview:
final ArrayList<String> mylist = new ArrayList<String>(5);
String s3=text1.getText().toString();
if(s3!=null)
g++;
else g--;
if(g>0) {
for(int i=0;i<mylist.size();i++) {
mylist.add(s3);
Toast.makeText(getApplicationContext(), mylist.get(i), Toast.LENGTH_LONG).show();
}
}
Is this the right way to do this? Also I am trying to get the list item for testing purpose, but I don't get any toast.
I assume you like to add the Content of your EditText field into the list after pressing a button, so you could do it like this:
// Member to store your Texts in list
private ArrayList<String> _list = new ArrayList<>()
// Method to call to add an entry to your list
// Either by pressing a button, removing focus of your edit text or something else
private void addTextToList()
{
String text = text1.getText().toString();
if (text != null)
{
_list.add(text);
}
}
private void showToast()
{
for(String text: _list)
{
Toast.makeText(this, mylist.get(i), Toast.LENGTH_LONG).show();
}
}
The toast is not shown because you need to pass your current activity.
Assuming your Code is in your activity, I passed this in the example above.
you have to make a button to handle this action every click i make editTex ,textView and Button
this is the action of button it every click take the value of editText and stor it in ArrayList and increse String by this value
public void getText(View v){
if(ed.getText().toString().length()!=0){
s+="\n"+ed.getText().toString();
arr.add(ed.getText().toString());
}
tv.setText(s);
}
What I need is for the textview to be added onto a linear layout where it will look organised and nice. Whenever the button is clicked again, it should replace the old textview and update it again.
At the moment, the button onclick listener will produce a textview, but I don't like it because it looks messy and unorganised.
I tried doing this:
varlinear.addView(varkebabTotal);
but it caused an error.
I have looked at other examples but they didn't explain how it will work with an onlick listener.
Here's the code for what happens when the button is clicked:
varKebab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = "Kebab Wrap";
if (menu.containsKey(name)) {
kebabquantity = list.get(name);
if (kebabquantity == null) {
kebabquantity = 0;
}
kebabquantity++;
list.put(name, kebabquantity);
//kebab qty calc
double kebabTotal = kebabquantity * menu.get(name);
overallTotal = overallTotal + menu.get(name);
varkebabTotal.setText(String.format("%s %s # £%s = £%.2f", kebabquantity.toString(), name, menu.get(name), kebabTotal));
varTotalPrice.setText(String.format("Total = £%.2f", overallTotal));
varlinear.addView(varkebabTotal); // this line caused an error
}
}
});
Edit:
The error I received is when I tested the app, and when I click the button, the app stops. It shuts itself down due to that one line: varlinear.addView(varkebabTotal);
The variables are as follows:
varkebabTotal = (TextView) findViewById(R.id.kebabTotal);
varTotal = (TextView) findViewById(R.id.TotalPrice);
varlinear = (LinearLayout) findViewById(R.id.myLinear);
one method you can try is to set varkebabTotal.setVisibility(View.GONE); when you initialize the TextView, and then on your onClick event, you can change it to varkebabTotal.setVisibility(View.VISIBLE);
So I have a situation where I populate TextView from Intent extras and then allow users to edit it. When user taps on edit Button, EditText appears with text shown in TextView.
Then user can input new value.
And finally when the save Button is pressed, new value isn't saving, instead the old value is still shown.
Here's my code:
Bundle extras = getIntent().getExtras();
if (extras != null){
textViewNazivTodoaDetails.setText(extras.getString("naslov"));
textViewDatumTodoaDetails.setText(extras.getString("datum"));
final int id = extras.getInt("id");
String oldText = extras.getString("naslov");
editTextDetaljnoIspravljanje.setText(oldText);
}
final String newText = editTextDetaljnoIspravljanje.getText().toString();
buttonDetaljnoIspravljanjeDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textViewNazivTodoaDetails.setText(newText);
}
});
}
What am I doing wrong?
You are clearly setting the text back to old text inside your onClick with the following line.
textViewNazivTodoaDetails.setText(newText);
You don't need this line inside onClick. Instead of this you should have the code to save the new value to where ever you want to save it.
The line of code where you initialized newText was called before you edited it and contains the same value as oldText. Hence you see the old text again.
Your text view already has the newly entered text.
I am trying to build an android app that gets some questions from a database and its possible answers (the answers are created dynamically as UI buttons). What I have managed so far is that once the user clicks on an answer, if the answer is right the button's colour becomes green, and if the answer is wrong the colour becomes red. What I want to achieve is in case the answer was wrong, change the button's colour to red and find the button with the correct answer and change it's colour to green. I am currently trying to do this by looping through every child element of the clicked button's parent, and comparing its text with the right answer given from the database. This is what my code looks like:
final TableRow textRow = new TableRow(this);
textRow.addView(questionText, rowParamsQuestions);
layout.addView(textRow, layoutParams);
for(final String option : currentQuestion.getOptions())
{
final Button button = new Button(this);
button.setText(option);
button.setTextSize(16);
button.setBackgroundResource(R.drawable.rounded_shape);
button.setTextColor(Color.WHITE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button clicked = (Button) v;
if (!answeredQuestions.containsKey(currentQuestion)) {
answeredQuestions.put(currentQuestion, clicked.getText().toString());
if (clicked.getText().equals(currentQuestion.getAnswer())) {
clicked.setBackgroundResource(R.drawable.right_answer);
} else {
clicked.setBackgroundResource(R.drawable.wrong_answer);
for (int i = 0; i < textRow.getChildCount(); i++) {
View child = textRow.getChildAt(i);
if (child instanceof Button) {
if (((Button) child).getText().equals(currentQuestion.getAnswer())) {
child.setBackgroundResource(R.drawable.right_answer);
}
}
}
}
Thank you very much!