This is the interface of an application i am trying to succeed the last days.
It is just a simple counter, of a card game. I have managed to create the buttons, do the counting and show it in a different textview, stoping in a limit i wanted.
The thing is, that i want the scores of each round separetely, to be shown until the end of the game, like a list i draw in ms paint for preview.
I have managed to put the score in the 1st round on the txt1 and txt2 fields, but i cant figure out, how to put the 2nd round on txt3 and txt4 field, the 3rd on txt5 and txt6 field etc.
This is the code i ve created, of the void that puts the txt1 and txt2 into that fields.
private void setScore() {
Double skor1 = Double.parseDouble(editText1.getText().toString());
Double skor2 = Double.parseDouble(editText2.getText().toString());
txt1.setText(Double.toString(skor1));
txt2.setText(Double.toString(skor2));
while (telikoOmada1 < 64 && telikoOmada2 < 64) {
skor1 = Double.parseDouble(editText1.getText().toString());
skor2 = Double.parseDouble(editText2.getText().toString());
telikoOmada1 = telikoOmada1 + skor1;
telikoOmada2 = telikoOmada2 + skor2;
telikoTxt1.setText(Double.toString(telikoOmada1));
telikoTxt2.setText(Double.toString(telikoOmada2));
editText1.setText(null);
editText2.setText(null);
break;
}
}
Related
I have a "for-loop" in Kotlin which is going to run my code 6 times.
I also have a textView on the app and want to see these 6 results shown there.
I can easily println() the results.
However, If I set the text of textView to these results, it only gets the last result.
What I like to do printing out all 5 results in textView (suggestedNums ) as each result is a separate line.
Is it even possible?
Any help appreciated.
Thanks.
for (i in 1..6) {
val s: MutableSet<Int> = mutableSetOf()
//create 5 numbers from numbers
while (s.size < 5) {
val rnd = (numbers).random()
s.add(rnd)
}
// remove all 5 random numbers from numbers list.
numbers.removeAll(s)
// sort 5 random numbers and println
println(s.sorted())
// set suggestedNums text to "s"
suggestedNums.text = s.sorted().toString()
}
You can do it in 2 ways
replace
suggestedNums.text = s.sorted().toString()
with
suggestedNums.text = suggestedNums.text.toString() + "\n" + s.sorted().toString()
Create a string and append the results with "\n" and set the text outside the for loop
I have a TextView and I can search some words, so that they're marked.
The TextView is named textView and it shows the text, I have an EditText Etxt to get the searched word and a Button to start the search. The code above is the code of the search. The app marks all found words big and italic. And I have a TextView text_total which shows the number of found words.
The problem: But if there is a searched word in the text below the shown screen, you must scroll and find the marked word:
int total = 0;
String word_search = Etxt.getText().toString().trim().toLowerCase();
String fullTxt = textView.getText().toString();
String[] array = fullTxt.split("\n");
String word;
StringBuilder st = new StringBuilder();
for (int i = 0; i < array.length; i++) {
word = array[i];
if (word.toLowerCase().contains(word_search)) {
String abc = word.trim();
st.append("<b><i>" + abc + "</i></b>");
total++;
} else {
st.append(word);
}
st.append("<br>");
}
textView.setText(Html.fromHtml("" + st));
text_total.setText("Ergebnisse: " + total);
Now, I have a problem because the text is too long to see all search results. I want that I have a 'back' and a 'next' button and the view goes to the next result if I click the next button and that the the found word goes automatically to the shown screen.
Does anyone know how to code this?
That's very important. Thanks for help!
Try this.
Put all the finded words in a list
List words= new ArrayList();
Add the words to the list
words.add(word_search);
Finally in next and previous buttons, you need to check the positions of the words in the list
words.get(position)
See how in this link
I am using Double data type for a variable in my Android app.
It simply takes a number and shows whatever percentage increase from that number would be:
for example 1,000,000 plus 1000 % = 1.1E7
The problem is I don't want an exponent display (the E), I want it to be in decimal.
This is a code snippet of the area which when the user clicks a Calculate button the info is displayed in an editText (Textbox)
enter code here
Button calc2 = (Button)findViewById(R.id.button1);
calc2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText number1 = (EditText)findViewById(R.id.editText1);
EditText number2 = (EditText)findViewById(R.id.editText2);
EditText number3 = (EditText)findViewById(R.id.editText3);
double editText1 = Double.parseDouble(number1.getText().toString());
double Pluspercent = Double.parseDouble(number2.getText().toString());
double editText3 = Double.parseDouble(number3.getText().toString());
double Result1 = 0 ;
double Result2 = 0;
Result1 = Pluspercent * 0.01 ;
Result2 = editText1 * Result1;
editText3 = editText1 + Result2 ;
number3.setText(editText3 + "");
}
});
enter code here
The code works but displays with the E. Could you show me what code to use to simply get it to display the result as in decimal. The decimal result should be 11,000,000
No need to worry about commas at the moment.
Some languages use a Decimal data type which would take care of this problem I think. Anyone know why Android do not have this?
I looked at
http://lecturesnippets.com/android-variables-data-types/
which shows a list of the data types, but Double seems to be the biggest container and uses the Exponent thing I don't want.
Thanks for any help.
Al
You're looking for a DecimalFormat object. You want to pass your double into the DecimalFormat.format( ... ) method to get a StringBuffer, and then append the rest of the text you'd like to display to that StringBuffer before you pass it to your EditText.
You'll be particularly interested in this method:
public StringBuffer format (double value, StringBuffer buffer, FieldPosition position)
I have a couple of EditTexts arranged on rows and columns.Those EditTexts contain product name,quantity and price and a TextView that shows the total in real time(calculates it each time you write on one of the EditTexts)
I've setup a a button on each row that when clicked sets visibility of the row(3EditTexts for product name,price and quantity) to GONE.
My problem is that after i set the visibility to GONE,though there are no more EditTexts it still calculates their values from before being GONE.
My question now is,what happens when the EditTexts are set to visibility.GONE ?
My app calculates in real time,so when something happens to an EditText,he calculates again..but it's like the values are still there...Isn't this supposed to be the difference between invisible and gone ?
I'll show you the way i calculate(it is called even after you press the X button to erase the EditTexts,not only when you change values inside EditTexts)
public void calculeaza() {
totaltest = 0;
prod = new String[allprod.size()];
pret = new String[allpret.size()];
cant = new String[allcant.size()];
for (int m = 0; m < allprod.size(); m++) {
prod[m] = allprod.get(m).getText().toString();
if (prod[m].matches("")) {
prod[m] = " - ";
}
}
for (int j = 0; j < allcant.size(); j++) {
cant[j] = allcant.get(j).getText().toString();
if (cant[j].matches("")) {
cant[j] = Float.toString(0);
}
}
for (int k = 0; k < allpret.size(); k++) {
pret[k] = allpret.get(k).getText().toString();
if (pret[k].matches("")) {
pret[k] = Float.toString(0);
}
}
for (int l = 0; l < allpret.size(); l++) {
Float temp = Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]);
totaltest = totaltest + temp;
TextView totalf = (TextView) findViewById(R.id.total);
totalf.setText(String.format("Total: %.2f", totaltest));
}
}
Lines Straight from Android dev site..
View.GONE This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.
i.e it retains EditText object even after Gone..
You can reinitialise edittext if you dont want it to retain its value...or setText = ""
Above quoted is the only difference...
Hope this helps...
I'm not really seeing in the code you posted anything I can use to answer this question, but there does appear to be some confusion as to what setVisibility does:
INVISIBLE elements are not seen on the page, but they still take up space (there's a hole where they would be)
GONE elements have no visible effect on the screen, from the user's perspective they aren't there. However they are still part of the view.
If you want to remove the object from the view, then you need to call removeView() on its parent.
It may still take up memory after it has been removed from the view, in case your code has kept references to it in any variables.
It may still take up memory after there are no further references to it, at least until the garbage collector gets around to it.
I'm hoping the rather generalized statements above help clarify the situation.
I'm kinda new to Android, now I'm developing a very simple game, the logic is described as following: The user sees a 10-digit value after pressing a "Ready" Button. After 5 seconds, the value changes to
* * * * * * * and the user has to enter it in a text field (type "number") below. Under that text field there are 2 Buttons: "Check" and "Give a hint". The check Button compares the user value to original value and changes a TextView according to the result("Correct"/"Incorrect"). The hint Button should show 3 random digits in the original value.
I have some questions on that small application:
I'm not sure which type I have to use to show the original value to the user: a
TextView, a text field or something else?
How do I force the app to show 3 random digits from the original value in the case
when user presses the hint Button?
Any help is appreciated.
1). I'm not sure which type I have to use to show the original value to the user: a TextView, a text field or something else?
For this TextView will do.
2). How do I force the app to show 3 random digits from the original value in the case when user presses the hint button?
Hopefully you are using int for storing 10 digit value (original value)
Make an array of int having size 10
int[] digits = new int[10];
Use for loop to separate all 10 digits from the 10 digit number.
int number = 1234567891
for(int i = 0; i < 10; i++){
digits[i] = number % 2;
number = number / 10;
}
This will give you array of 10 int values
Make object of java.util.Random class and fetch random values from 0-9
Random r = new Random();
int pos1 = r.nextInt(9);
nextInt(int n) returns a pseudo-random uniformly distributed int in the half-open range [0, n).
These values are positions from which you are going to retrieve digit from array
So this will give you random position between 0 and 9
Just make sure that each time you generate random value from Random, it should not be equal to previously generated values e.g. if you are generating random position for second digit you should check that it should be equal to first
you can show all 3 digits(fetched from 3 randomly generated positions) in TextView as a hint
Hope this will solve your problem
Edited Part
Set visibility of Textview to View.GONE
something like this
private TextView tv;
tv = (TextView)findViewById(R.id.textView01);
tv.setVisibility(View.GONE);
Button btn = (Button)findViewById(R.id.button01);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View view){
tv.setVisibility(View.VISIBLE);
}
});