Android: Display Contents of Spinner Selection in EditText - android

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.

Related

I Build a Brain Trainer app but Not Showing my TEXTVIEW when i run my app and warning is showing do not concatenate text display with setText

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.

How to play sound from string array using fragments (android)

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",
};
}

Update "Log.i" in TextView with ScrollBar

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.

Removing newline from string

Here's my issue:
I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:
Episode 1
Episode 2
Episode 3
etc.
I'm using my own adapter class that extends SimpleCursorAdapter to do this...
Since I had formatting errors I am using Android.R.layout.simple_list_item_1 and Android.R.id.text1
Basically the only reason I have a custom adapter is so I can do something like this:
textView.setText("Episode " + cursor.getString("column_for_episode_number");
The problem is, I get a list that looks like this:
Episode
1
Episode
2
Episode
3
When I try something like this(which worked in a different portion of my code):
String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");
I get the exact same list output :(
Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/
text.replaceAll(System.getProperty("line.separator"), "");
There is a mistake in your code. Use "\n" instead of "\\n"
String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);
Check if there is new line at the beginning before you replace and do the same test again:
for(int i=0; cursor.getString("blah").length()-1; i++)
{
if(cursor.getString("blah").charAt(i)=='\\n') <-- use the constant for the line separator
{
Log.i("NEW LINE?", "YES, WE HAVE");
}
}
Or use the .contains("\n"); method:
Check the xml for the width of the textview as well.
Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.
This could help you:
response = response.replaceAll("\\s+","");
It sounds like you are hitting wrapping issues rather than newline issues. Change this:
String text = "Episode " + cursor.getString("blah");
To this:
String text = "Episode" + cursor.getString("blah");
And see if that changes the output. Post your layout xml please?
this worked for my (on android 4.4):
(where body is a string with a newline entered from an EditText view on handset)
for (int i=0; i<body.length(); i++) {
if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
body = body.substring(0, i) + " " + body.substring(i+1, body.length());
}
}
have you tried
cursor.getString("blah").trim()

Displaying multiple lines of text and variables in an AlertDialog using setMessage()

I need to display multiple lines of text in an Alert Dialog. If I use multiple setMessage() methods, only the last setMessage is displayed, as shown below.
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Statistics:");
alertDialog.setMessage("No. of attempts: " + counter);
alertDialog.setMessage("No. of wins: " + counterpos);
alertDialog.setMessage("No. of losses: " + counterneg);
Is there a way to create a new line for each of these in the dialog? Like using \n in System.print.out(); method.
Thanks!
You can do something like this
String alert1 = "No. of attempts: " + counter;
String alert2 = "No. of wins: " + counterpos;
String alert3 = "No. of losses: " + counterneg;
alertDialog.setMessage(alert1 +"\n"+ alert2 +"\n"+ alert3);
You could just create one string of everything you want to show and add "\n" where you'd like the line breaks to be.
alertDialog.setMessage("No. of attempts: " + counter + "\n" +
"No. of wins: " + counterpos + "\n" +
"No. of losses: " + counterneg);
Or even better to use a StringBuilder:
StringBuilder sb = new StringBuilder();
sb.append("No. of attempts: " + counter);
sb.append("\n");
sb.append("No. of wins: " + counterpos);
sb.append("\n");
sb.append("No. of losses: " + counterneg);
alertDialog.setMessage(sb.toString());
And the best way to do it would be to extract the static texts into a string resource (in the strings.xml file). Use the %d (or %s if you want to insert strings rather than ints) to get the dynamic values in the right places:
<string name="alert_message">No. of attempts: %1$d\nNo. of wins: %2$d\nNo. of losses: %3$d</string>
And then in code:
String message = getString(R.string.alert_message, counter, counterpos, counterneg);
alertDialog.setMessage(message);
You can also insert newlines directly in the strings.xml file:
<string name="my_string_text">This would revert your progress.\n\n Are you sure you want to proceed?</string>
Kotlin simplifies the solution by:
chaining the calls of the set methods
using interpolated strings
as follows:
"AlertDialog.Builder
This Builder object to allow for chaining of calls to set methods
"
(https://developer.android.com/reference/android/app/AlertDialog.Builder)
fun alertDemo() {
var counter: Int = 5
var counterpos: Int = 2
var counterneg: Int = 3
val builder = AlertDialog.Builder(this)
.setTitle("Statistics:")
.setMessage("""
|number of
|
|attempts: $counter
|wins: $counterpos
|losses: $counterneg
""".trimMargin())
.show()
}
I had prepared a screen-shot of the result, but as I am new here, I appear to have learned that uploading screenshots may be restricted to higher level community peers. Or did I miss something? Thank you for enlighting (perhaps not only) myself :)
The screenshot comes in a nice format without showing the bars.
PS:
For the minimalists, due to chaining, we could even eliminate the redundant "val builder ="

Categories

Resources