If anyone could assist me with this little hiccup i'm having i would be grateful. Thanks in advance.
I have an app structure made from fragments and activities...
The main fragment is called "Headlines" - this displays to the user a list of words in a clickable list.
The detail view page for that is called "Articles" and the activity holding the data which is a list of words is called "Greetings".
Pretty much - in the articles activity;
I have: a List of words, the translated form of the words, synonyms etc and they are all stored using string arrays.
Once a word is selected, it launches the "details activity page" displaying more information on the selected word.
I want to be able to input a sound button here that allows the user to hear the translated word when pressed.
Note:
I have already placed the image button in the xml file for the final output, so it is viewable on whenever a user selects a word. I just need to program the button to be functional now.
The code below is where i have the list of words stored:
package com.example.sample;
public class GreetingsWords {
static String[] Headlines = {
"Hello",
"Goodbye",
"Good Morning",
"Good Night",
};
static String[] Articles = {
//word 1
"'Hello' \n\n\n " +
"Translation: Yow! \n\n" +
"Pronouncation: [Yoh'ow] \n\n" +
"Synonyms: Greetings!, Hello!, Hi, Hey \n\n",
//word 2
"'GoodBye' \n\n\n " +
"Translation: Lata \n\n" +
"Pronouncation: [lay'ta] \n\n\n" +
"Synonym: Likkle More. \n\n" +
"Pronouncation: [lickle- mor']",
//word 3
"'Good Morning' \n\n\n " +
"Translation: Mawnin \n\n" +
"Pronouncation: [Maw-ning] ",
//word 4
"'Good Night' \n\n\n " +
"Translation: Nighty Nights \n\n",
};
}
Related
sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));
This Line show warning you see in pic..
Use String.format();
sumTextView.setText(String.format("%1$d + %2$d", a, b));
With this you can format a string correctly with multiple variables, no matter whether they are strings or integers. This example takes the value of variable a and replaces the placeholder %1$d with it. Same goes for the other variable.
take an string copy whole line in it, then show string in setText
String str = (Integer.toString(a) + " + " + Integer.toString(a));
sumTextView.setText(str);
1. The First String Says that do not concate string with setText property.
String txt = String.valueOf(a) + " + " + String.valueOf(b);
sumTextView.setText(str);
2. Second warning says that your program have possibility to crash or genearte an exception in case if value of a or b is null or not an integer.
So check condition if(a!=null and b!=null) then display text in if condition.
I have a TextView in my application. I want to align 2 separate text one ontop of the other.
But have no idea how. Any suggestions ?
Text code that is to be displayed:
String[] product_showcase_list = new String[] {
"Product Name 产品名称 : Office Chair 1 " +
"Product Serial 产品号码 :0C012345",
...
}
code that is used to call out String text:
label_col_1_datatab3.setText(product_showcase_list[cnt]);
Currently, what is being displayed on the screen:
Product Name 产品名称 : Office Chair 1 Product Serial 产品号码 :0C012345
How it is needed to be displayed:
Product Name 产品名称 : Office Chair 1
Product Serial 产品号码 :0C012345
Try adding "\n" after your number one. In your String
"Product Name 产品名称 : Office Chair 1 \n" +
"Product Serial 产品号码 :0C012345"
Or Use two TextViews in a LinearLayout or ListView
Within your loop, change as below.
String temp = product_showcase_list[cnt] + "\n"
label_col_1_datatab3.setText(temp);
Hope it helps!
I'm trying to finish the 'backbone' of my app in the next 3 weeks, however, one of the few obstacles I stutter at is saving data. I've had a look at saving data internally, but there is limited tutorials from what I can find of reading and writing multiple lines to files in the apps cache directory.
Basically what I'm trying to do is save the values stored inside a fragment. This fragment resets all its values when the user clicks a button and changes text to match a page number. (A number of duplicates that contain various values.) I would do multiple fragments, however, thought it would be beneficial to use just one fragment to minimize storage space needed.
I've only got round to writing to the files, and created two methods to manage this which are then called on the click of a button. One creates these files and the other writes to them. Unfortunately I'm inexperienced using adb and could only find that the files are created, but don't know if they are being correctly written to. Is there any chance someone could review this and possibly assist with re-reading the files? Help is much appreciated.
The two methods (Warning: A great number of lines ahead):
public void createEmptyFiles() {
try {
outputTempExerciseFileE1 = File.createTempFile("temp_exercise_1",
".txt", outputTempExerciseDir);
outputTempExerciseFileE2 = File.createTempFile("temp_exercise_2",
".txt", outputTempExerciseDir);
outputTempExerciseFileE3 = File.createTempFile("temp_exercise_3",
".txt", outputTempExerciseDir);
} catch (IOException e) {
e.printStackTrace();
Log.w("rscReporter", "Encountered an error when creating empty files!");
}
}
public void writeTemporaryFiles() {
try {
if (counterAnotherExercise == 1) {
writerTemp = new FileWriter(outputTempExerciseFileE1);
writerTemp
.write(editTextExerciseName.getText().toString() + "\n"
+ counterNoSets + "\n" + counterRepsPerSet
+ "\n" + counterMeanRepTime + "\n"
+ counterMeanRepTimeRefined + "\n"
+ counterSetInterval);
writerTemp.close();
} else if (counterAnotherExercise == 2) {
writerTemp = new FileWriter(outputTempExerciseFileE2);
writerTemp
.write(editTextExerciseName.getText().toString() + "\n"
+ counterNoSets + "\n" + counterRepsPerSet
+ "\n" + counterMeanRepTime + "\n"
+ counterMeanRepTimeRefined + "\n"
+ counterSetInterval);
writerTemp.close();
} else if (counterAnotherExercise == 3) {
writerTemp = new FileWriter(outputTempExerciseFileE3);
writerTemp
.write(editTextExerciseName.getText().toString() + "\n"
+ counterNoSets + "\n" + counterRepsPerSet
+ "\n" + counterMeanRepTime + "\n"
+ counterMeanRepTimeRefined + "\n"
+ counterSetInterval);
writerTemp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Any of the text files should look like:
editTextExerciseName
counterNoSets
counterRepsPerSet
counterMeanRepTime
counterMeanRepTimeRefined
counterSetInterval
Where the two methods are called:
// In a switch statement as there are around 15 buttons
case R.id.button_another_exercise_foreground:
// Increases page number in fragment
counterAnotherExercise++;
// This then checks the page number and changes text
checkPageNo();
// Writing to files is called, files were created in onCreateView()
writeTemporaryFiles();
// Resets all the counters, giving the imitation it is a completely new fragment
counterReset();
// default array exercise is then set to the page number which is then displayed as title
// For example: Exercise 1, Exercise 2, Exercise 3...
textViewExerciseTitle.setText(defaultArrayExercise);
break;
I only know the basics of Java and Android, for myself this is ambitious, however, you gotta learn somewhere! Additional suggestion for saving values are welcomed.
You don't really need files as you are only writing and then reading a handful of fixed data. Use SharedPreferences like this:
to write:
PreferenceManager.getDefaultSharedPreferences(YourActivity.this).edit().putString("editTextExerciseName", "my exercise").commit();
to read:|
PreferenceManager.getDefaultSharedPreferences(YourActivity.this).getString("editTextExerciseName");
I have in my layout.xml a TextView with "id = txtLog".
Where do the test results from my application using:
Log.i("Result:", "Value of x = " + x);
for show result in LogCat.
It is possible to show these results "Log.i" within the TextView?
Note: I left a space at the bottom of my application to show the TextView.
Like a console.
I would like to display these messages on TextView.
If possible create a scroll bar and display every time I use Log.i
I am a beginner, do not know if it is possible. Yet thanks.
I would think
myTextView.setText(myTextView.getText() + "Value of x = " + x + "\n");
would work.
EDIT:
Also, to make the TextView scrollable, you need to set a movement method like so:
myTextView.setMovementMethod(new ScrollingMovementMethod());
EDIT 2:
If you want the information to go to both Log.i and a TextView, then you need a method that holds a reference to the TextView you want to update.
public static void LogToView(TextView myTextView, String title, String message) {
Log.i(title, message);
myTextView.setText(myTextView.getText() + title + ": Value of x = " + x + "\n");
}
Put that in whatever class or in your Activity class. Use it instead of Log.i and the message will be passed to both.
I am working on an android project and came to a halt in my design process. I am a beginning java programmer and also new to the android sdk so please bear with me... On my main screen, it prompts the user to make 6 selections from separate spinner drop down menus. Each of the 6 spinners contain the same StringArray. What I want to do is display the 6 different spinner selections within an EditText field on another screen when a 'submit' button is clicked. I have the submit button listener set up correctly along with a new activity and intent to switch the layout to the output screen. What I don't understand is how to take the spinner(s) and display them into the text fields. I have tried setting up 6 individual SetOnItemSelectedListener methods but unsure if that is allowed. Help, please and thank you!
I sugggest you setup your spinners with a simple ArrayAdapter like so:
String[] selections = new String[] { "Selection 1", "Selection 2", "Selection 3", "Selection 4" };
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(mySpinner1.getContext(), android.R.layout.simple_spinner_item, selections);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner1.setAdapter(myAdapter);
Follow the same concept for all 6 spinners. And then when you fetch their values like so:
String value1 = mySpinner1.getSelectedItem().toString();
String value2 = mySpinner2.getSelectedItem().toString();
String value3 = mySpinner2.getSelectedItem().toString();
String value4 = mySpinner2.getSelectedItem().toString();
String value5 = mySpinner2.getSelectedItem().toString();
String value6 = mySpinner2.getSelectedItem().toString();
Now you can concatenate these string as needed and display them in your text view like so:
myTextView.setText(value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5 + "," + value6);
Hope that helps. Have fun.