how to get value thats displayed in the Emulator? - java

I am writing a small android "Hello world" application and i would really like to write a small test case . i have written a small test case , have a look below :
public void testMyFirstTestTextView_labelText() {
final String expected = getContext().getResources().getString(R.string.hello_world);
final String actual = getContext().getText().toString();
assertEquals(expected, actual);
}
the expected variable gets instantiated with the value from strings.xml
but for the actual value , i am struggling to understand what methods or snippet i should write in order to get the value of the displayed string .
so how do i get the value of the text displayed in the emulator:
as of now the below statement is wrong:
final String actual = getContext().getText().toString();
i am really new to android , can somebody help me with this please .
if you check the last peice of code in the snippet :
assertEquals(expected, actual);
it checks(using jUnit i think) weather the value of expected is the same as actual.
once again to repeat my question in the above snippet , how do i get/retrive the value of the displayed text ? I.E. on the below line :
final String actual = getContext().getText().toString();

it should be yourTextViewObject.getText() not getContext().getText()
TextView textView = (TextView)findViewById(R.id.hello_world);//Assuming your id in the xml page is textView1
String actual = textView.getText().toString();
This gets the value of an entry hello_world from strings.xml.
String expected = getContext().getResources().getString(R.string.hello_world);
And This gets the value from the TextView.
String actual = textView.getText().toString();
From the android documentation's example
mFirstTestText = (TextView) mFirstTestActivity
.findViewById(R.id.my_first_test_text_view);
String actual = mFirstTestText.getText().toString();

You have a exactly the same example there at the Set Up Your Test Fixture part. Just analyse the TextView with something like this:
final String actual = label.getText().toString();
final String expected = getContext().getResources().getString(R.string.hello_world);
assertEquals(actual, expected)

If your textView is called label you can use something like this:
String s= label.getText().toString();

Related

How Do I Update TextView Result On App Launch

I am pulling data from a Website to my application. I want the TextView to display the result I want from the website immediately as the user launches the app. However html codes make the result look weird some times and I am trying to correct it. I have the codes that will do what I am trying to do. I just can't figure out how to get it to do everything automatically at app launch. It needs to pull the code from the website and if it receives any special symbols within the string I want it to correct it as soon as the app launches. Here is an example...
TextView tv = (TextView) findViewbyId(R.id.my_textview_result);
tv.setText(resultFromWebsite);
The result it pulled: You u0026 Me Forever!
The result I want: You & Me Forever! My app should correct that.
Here is my correction code...
public void symbolTextFilter(TextView myTv) {
String getData = tv.getText().toString();
if (getData.contains("u0026") {
String replace = getData.replace("u0026", "&");
myTv.setText(replace);
}
Now on my onCreate Method
TextView tv = (TextView) findViewbyId(R.id.my_textview_result);
tv.setText(resultFromWebsite);
symbolTextFilter(tv);
It will not make that correction. It will if I put the symbolTextFilter(tv) in a onClickListener button though. I don't want to assign the correction in a button. I want it automatically. My guess is, everything that I have in the onCreate is happening too fast for corrections to be made. How do I fix that? Thanks in advance!
Try this:
tv.setText(symbolTextFilter(resultFromWebsite))
You should use the method symbolTextFilter to handle the string only:
public void symbolTextFilter(String input) {
if (input.contains("u0026") {
return input.replace("u0026", "&");
} else {
return input
}
Nevermind, I got it! I'm not sure where the "\" came from because it wasn't in the original string that it pulled before the correction. I fixed it with this...
public String symbolTextFilter(String input) {
if (input.contains("u0026") {
return input.replace("\\" + "u0026", "&");
} else {
return input
}

Failed to get string from autocompletebubbletext

i'm using autocompletebubbletext library (https://github.com/FrederickRider/AutoCompleteBubbleText) which display the list of items to chose from in a list and allow in same time to chose the items from the editetxt..
My problem is as follow:
after the user choses a number of items(=Multiple inputs) .. i want to display a text as an output when clicked on a button (depending on the items chosen of course) as explained in this picture: (https://i.imgur.com/QQuzFvl.png)..
but i got stucked in getting the string of itemsChosen from the edittext
FIRST: i am not sure which return value to use!!
SECOND: i assumed i should use "checkedIds" and I've tried A lot of solution in internet , i've been trying different ideas all day, from what i have tried: ( Ps: i used a toast to see if the methods did work)
edittext.getText().toString() > nothing appears in Toast
i have tried to turn the setHash to String[]: then turning the String[] to one string like:
content=editText.getCheckeditems();//getcheckeditems returns checkedIds which is = new HashSet<String>()
String[] BLANA= content.toArray(new String[content.size()])
data= TextUtils.join(",",BLANA);
it didnt work, in Toast i got"[]"
For the MainActivity.Java (i have the same as here):
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/samplelist/src/main/java/com/mycardboarddreams/autocompletebubbletext/samplelist/SampleActivity.java
For MultiSelectEditText.java (i Have same as here) :
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/library/src/main/java/com/mycardboarddreams/autocompletebubbletext/MultiSelectEditText.java
WHAT is the solution? (to get a string so i can use it later)
PS: if there is another way(another library or methode) to get what i want to achieve in the first place , i would love to try it..
EDIT: THIS IS A CODE THAT LOOKS PROMISING BUT DIDN'T WORK!
in MultiSelectEditText.java
public String datachosen(){
String [] blan= checkedIds.toArray(new String[0]);
StringBuilder builder = new StringBuilder();
for (String string : blan) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(string);
}
String DATATORETURN = builder.toString();
return DATATORETURN;
}
in MAINACTIVTY.JAVA
MultiSelectEditText editText = (MultiSelectEditText)findViewById(R.id.auto_text_complete);
content=editText.datachosen();
Toast.makeText(DecisionTree.this, content,
Toast.LENGTH_LONG).show(); // TOAST INCLUDED IN A BUTTON OF COURSE
OUTPUT: TOAST SHOWS NOTHING!
Solved it ..
i intialize the edit text before on create ..and defin it later after onCreate()..
and got string with the normal edittext.getText().toString(); method!
Simple but was hard to detect the problem!

Contains method in Java doesn't exist?

I am trying to make a java script for a mobile app check if a string contains specific characters.
if(fn.contains(" ")){
}
The fn variable is declared twice in the following:
private EditText fn;
final EditText fn = (EditText) findViewById(R.id.fn);
Not sure why this doesn't work. In android studio, the contains function highlights red as it doesn't exist. You can click here to see what I am talking about. Is there another method than this?
That is an EditText not a string you must do first:
var str = fn.getText().toString()
if(str.indexOf(" ") !== -1;){ . . . }

Android : Get string for its number

I want to show sentences for its number.
Getting number with EditText, and sentences are in string.xml
Name of strings are
sen_(number)
ex: sen_1, sen_25
I tried to make the code to String, so I did like this.
(sentence_num is getString of EditText) (sen_1 is "Hello, world!")
String getTxtString = "getText(R.string.sen_" + sentence_num + ")";
TextView scrambled_sen = (TextView)findViewById(R.id.scrambled_sentence);
scrambled_sen.setText(getTxtString);
But it shows getText(R.string.sen_1), not "Hello, world!".
How can I make it show string with its number?
I want to put getTxtString for a java code, not a String.
You could use getResources().getIdentifier() to get the string but a better and non-complicated way is through a switch case or the if else method.
Something like
If(editText value equals something){
return getString(R.string.sen_1);
}
Minimizes the errors that you could cause through the first getIdentifier() method too.

Android String Edit and Array's value positioning

I have two problem. Please try to solve...
No 1.
Suppose I have two string:-
String one = "Android is awesome";
String two = "Android is";
Now my question is, can I get awesome by comparing those two string like we do with int.
int abc = 50;
int def = 35;
int ghi = abc - def;
output :-
ghi = 15;
So, this is what we do basically with int, long, double... Is it possible also with String?
No 2.
Now, suppose again I have a string-array
ArrayList<String> list = new Arraylist()<>;
list.add("beautiful");
list.add("awesome");
list.add("cool");
Now, if string-comparing is possible, then suppose I have got a new String three from comparing one and two.
So, here,
String three = "awesome";
Now, again, I am using if-statement
if(list.contains("awesome")){
**Problem here starts. See the commented texts**
//Here if **awesome** is found in **list**, so can I get the position of **awesome** in the **list**?
}
Forgive me for a big and rough question. Please help me. I am really so sad with this problem.
If you want the remaining string by comparing two strings, try the following
String one = "Android is awesome";
String two = "Android is";
if(one.contains(two)){
Log.i("remaining string ",one.split(two)[0].replace(" ",""));
Log.i("remaining string ",one.split(two)[1].replace(" ",""));
}else{
Log.i("remaining string ","");
}
Then use the remaining string to find the index from list.Try the following ,
list.indexOf(remainingstring);

Categories

Resources