I am working on a Android App on Android Studio. The app will show several phrases randomly when a button is pressed.
I already have a simple script for that, using a String variable which contains all the possible texts.
However, I want each text to appear as follows:
"text 1
- Text1 a"
But I am not able to add the break on the string variable I created.
I currently have it as:
final TextView textOne = (TextView) findViewById(R.id.textView);
Button motivateYou = (Button) findViewById(R.id.button);
final String[] misFrases = {"Texto 1", "texto 2", "Texto 3", "Texto 4"};
motivateYou.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random randGen = new Random();
final int rando = randGen.nextInt(4);
textOne.setText(misFrases[rando]);
}
});
Is this a good way to create the list to add the format?
Or should I add the format on the String file on the res folder and then call the string from there?
As a side comment, I am planning to have lists of 100+ texts
Let me know your comments
What you want to do is this:
myTextView.setText(Html.fromHtml("Text1<br/>next line"));
Use Html formatted strings inside of textviews. This should cover most of usecases.
If you want a lot of strings inside of your app, I'd recommnd using an array inside arrays.xml
If you want to port the app to another language, it makes it a lot easier, if you have all strings in one place.
Furthermore you shorten your code inside the Class, since you will have over hundred strings in those lists.
Related
I'm working on a project in Android Studio where I have one EditText where the user will insert one word at a time, 10 times. Everytime the user writes a new input and clicks on the button it goes to a different TextView than the previous ones and different from the next ones.
How can I put the different inputs into the specific (different) TextViews?
Every TextView has a different sequencial ID like, word1, word2, etc.
I haven't done java in a long time, so I'm having problems with logic. I tried to do the following but the app crashes.
gameword = (EditText) findViewById(R.id.wordj);
public void onClick(View v) {
printwords(gameword.getText().toString());
}
});
public void printwords(String word) {
String[] array = new String[10];
TextView[] positions = new TextView[10];
for (int i=0; i < 10; i++){
array[i] = word;
positions[i].setText(array[i]);
}
}
}
You're creating a new array of TextView's and String every time the button is clicked. Change your code to the code below and it should work.
Make your int[] textViews , String[] array & int i = 0 class variables and then initialize them in onCreate() after setContentView()
In above code, int[] textViews is the array of ID's of TextView's from your activity.
After doing that, change your code to following:
gameword = (EditText) findViewById(R.id.wordj);
public void onClick(View v) {
array[i] = gameword.getText().toString();
YourActivity.this.findViewById(textViews[i]).setText(array[i]);
i++;
}
});
If my question is unclear, please let me know, I am quite new in this area.
I did the following:
I freezed and optimized tensorflow model
It is a RNN lstm model , getting a sentence then saying if that positive or negative
I successfully added all the things in android studio
Now I have a editbox to enter my sentence , then touch Run button, then this run button should fill the label if this sentence was positive or negative,
Would you please give me some instructions that if there is a predefined function I should use,
or I have to write down that by myself, if this is the case, how,
Thanks in advance for helping.
Update
This is something I have tried but it seems quite incorrect:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inferenceInterface = new TensorFlowInferenceInterface();
inferenceInterface.initializeTensorFlow(getAssets(), MODEL_FILE);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final EditText editNum1 = (EditText) findViewById(R.id.editNum1);
inferenceInterface.fillNodeInt(INPUT_NODE, INPUT_SIZE, editNum1);
inferenceInterface.runInference(new String[] {OUTPUT_NODE});
int[] resu = {0, 0};
inferenceInterface.readNodeInt(OUTPUT_NODE, resu);
final TextView textViewR = (TextView) findViewById(R.id.txtViewResult);
textViewR.setText(Float.toString(resu[0]) + ", " + Float.toString(resu[1]));
}
});
}
Edit2
there is FillNodeInt in TensorFlowInferenceInterface,
what I thought is that, it is going to tie my input in the editbox to the input node in my model,
so the first parameter is input node, the second is the size, and the third should be the input for example I fill in the text box,
but there is no function to have the third parameter as String,
they are int or float or byte(like example above which is Int), or something like buffer
can someone please explain which method I can use
I am new to Android App Development. I am try to view my database using a Textview in my activity.
Here is my setText() java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewlogs);
TextView tv = (TextView) findViewById(R.id.tvSqlinfo);
LogsDB info = new LogsDB(this);
info.open();
ArrayList<String> data = info.getData();
info.close();
tv.setText(data);
}
I seem to be getting and error at tv.setText(data);. Stating "The method setText(CharSequence) in the type TextView is not applicable for the arguments (ArrayList)" Then when i do the recommended fix it changes
tv.setText(data)
to
tv.setText((CharSequence) data);
Then when I test the application I get an error stating that it cannot be cast.
What do I need to change to be able to view my database in the textview?
Any advice and help would be greatly appreciated.
Thanks
If you want to keep it simple you can use
tv.setText(data.toString());
instead of
tv.setText(data);
It will show something like this:
([field1],[field2],...)
You probably want to take each String out of the ArrayList and add them to a single String object then add that to your TextView. Something like
String text = "These are my database Strings ";
for (int i=0; i<data.size(), i++)
{
text = text.concat(data.get(i)); // might want to add a space, ",", or some other separator
}
tv.setText(text);
and you can separate the Strings however you want them to be displayed.
I started with JAVA about 2 months ago by myself so I'm sorry if I write something stupid : pp
I think all my questions was answered here but this one I didn't find exactly what I want. My question is:
I have an app with a single EditText and a Button, the users enter a text and the button will analyze it.
I also have 2 boxes, one with apple and orange, another with lemon and potato.
If users type: "I want apple", the program will say: "It is inside Box 1".
But the user can type whatever he want but the food's name will never change, it will be apple, orange, potato or lemon. So how can I say to the program: If (MyEditText contains "apple"), show box 1, else if (MyEditText contains "lemon") show box 2?
I'm doing this app because I want to learn more and more about Android Development. Hope I was clear.
Sorry about my English
I am not android developer but based on this answer you can try something like
if(myEditText.getText().toString().contains("apple")){//...
I am assuming that you wan't the input to to be processed when the user clicks the button. You can do something like this
btnOK.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
String userInput = MyEditText.getText().toString();
if(userInput.contains("apple")){
//Show it is inside box 1
}else if(userInput.contains("orange")){
//Show it is inside box 2
}
}
});
Here btnOK refers to your button whatever you called it.
As far as displaying the information, I am not sure about what you mean by "I have 2 boxes." If by boxes you mean textView which already has the text ("it is in box 1") ("it is in box 2") in it. You can simply change the visibility attribute. Depending on condition
textView1.setVisibility(View.VISIBLE); //textview containing ("it is in box 1")
textView2.setVisibility(View.INVISIBLE); //textView containing ("it is in box 2")
Or you can just display the result by populating a textView. textView.setText("Your text");
Hope this helps.
You can use indexOf method to check if a specific string exists in the text. Alternatively You can use contains method. If you would prefer to ignore the case, convert it all to upper case and compare like:
EditText myEditText = (EditText) findViewById(R.id.edittext);
String myEditTextValue = myEditText.getText().toString();
String valueInUpperCase=myEditTextValue.toUpperCase()
if(valueInUpperCase.contains("APPLE")) {
// show box 1
} else if(valueInUpperCase.contains("LEMON")) {
// show box 2
}
See Java Docs for String
Hope this helps you.
Try this
String enteredText = editText.getText().toString();
if(enteredText.contains("apple")){
......
}
else if(enteredText.contains("orange")){
......
}
Hope this helps.
I am trying to switch the android:text of a textview with various strings that are to be dynamically retrieved from my strings.xml. When the user presses the button on the grid, it will return a unique identifier. I want to use this identifier to dynamically load a string into the Pronoun textView.
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView Pronoun = (TextView) findViewById(R.id.Prounoun);
chosen = ((TextView) v).getText();
//Inserts the identifier of what button was pressed into the local string chosen".
Pronoun.setText("R.string.Pronoun_" + chosen);
}
}
When the button 'a' is pressed, 'chosen' holds the letter 'a', when joining this to the string naming format "Pronoun_", resulting in "Pronoun_a". The intention was to call upon the string at this location. In reality, the text is literally "R.string.Pronoun_a" instead of retrieving the actual string content of Pronoun_a. If the button 'b' is pressed, I want the string content of 'Pronoun_b' to show in the textView. I am new to java, merely trying to recreate a technique I learned in another language. Is there a way to do this?
Use this (with your actual package name):
String resKey = "your.apps.package:string/Pronoun_"+chosen;
int resId = getResources().getIdentifier(resKey, null, null);
Pronoun.setText(resId);
Also see the API Docs.
Note: As AND_DEV already mentioned, you'll have to use ActivityName.this.getResources().
instead of
Pronoun.setText("R.string.Pronoun_" + chosen);
try below line
Pronoun.setText(ActivityName.this.getString(R.string.Pronoun_) ++ chosen);
it will work in activity context, but in onclick i think you should use ActivityName.this. you can also use context variable if you already created. here ActivityName is you activity name of which ur code is written .