Displaying a non-static string on an Android app? - java

I'm making a calculator-type application and it will have a resulting string that is generated by user-given inputs. This is my first android app, so I am not too familiar with the SDK. In main.xml I can make the static, non-changing strings, but how can I similarly display a string that can change and has no set value?

You just do it in your Java code. As you don't provide any snippet of your code, let's suppose you want to give text to the EditText that holds the calculations. In that case you will have to do this:
editText.setText(stringOfCalculation);
In this case, stringOfCalculation is an String that you have carefully formated by using the calculator buttons.

Related

Properly split a long string to be read by Talkback

I have a long string in my android app that looks like this:
<string name="imprint_text" translatable="false">
...long text here...
</string>
The problem is that Talkback is only able to read the whole string from the beginning, indifferent of where on the screen you touch. What is the best way for this to be split into different paragraphs? Should I just create different strings (imprint_text1, imprint_text2...) and bring them in the correct order or is there a better way to do this?
Unfortunately, I don't think that this is possible. Keep in mind that there's also a limit of 4000 characters if I'm not mistaken. My suggestion would be to use TextToSpeech. You can start it when the user opens the screen or when they tap on anywhere (probably the first one would be better, but it depends on the app). It has a lot of options for setting the speed and even a playSilence method.

How to format xml and java code as text inside TextView

I am creating an Android Tutorial Application. I am planning to use Tabs to display the java and the xml code used for that topic.
I thought of using TextView and applying the Java and XML code as text to it.
However I am failing to format the Java code, plus I am not able to add xml as text at all as it has tags which are not supported.
Below are snaps from another App and the desired output is what I am looking for.
Any help or suggestions are welcomed.
You could display your Code in your TextView with HTML and style/format it with CSS.
Try this,
Suppose you have HTML code like this
<h2>Title</h2><br>
<p>description here</p>
Then in android you can use below code
TextView tvTrial=(TextView)findViewById(R.id.tvTrial);
String html="<h2>Title</h2><br> <p>description here</p>";
tvTrial.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
Now any thing you want to display in you view you can store it the string and set it.

How do you display a string on Android?

Edit: it turned out the XML Viewer wasn't the emulator, and does not actually run code.
So, I'm starting my first app in Android Studio 1.1.0, and I realized that I can't even manage to display a string (created in the java file).
I did all research I could, but no matter what snippets of code I copy/paste, it never works.
I tried copy/pasting the snippet of code from the first answer here: Android Eclipse: Change the text in the app to a string created withing the program and it doesn't work. The findViewById method is in red, and so is my_text_view :
String displaythisgoddammit = "display this goddammit";
private TextView text = (TextView) findViewById(R.id.my_text_view);
text.setText(displaythisgoddammit);
And my textview (in activity_main.xml) is:
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/displaythisgoddammit"/>
It can't find the string, no matter what I do. Apparently I must put it in the strings.xml file, but then how do I edit it...? In the question I linked, the snippet of code apparently worked for a string in the java file.
Here is my main file (I'm not sure if it's really relevant though, it's just snippets of code I copy/pasted): http://pastebin.com/7uuHyJWP I put the string at line 144, should I put it elsewhere?
Some help please? I've been researching for 5 hours but none of the solutions I tried work...
The issue with your code is on the line when you use
TextView text = (TextView) findViewById(id.my_text_view);
It is occuring because when you define a TextView or any other object by it's Id you need to use R.id.my_text_view.
So, to fix it simply change it to
TextView text = (TextView) findViewById(R.id.my_text_view);
In your code please check the reference to the TextView that you are making.
It should be findViewById(R.id.my_text_view);
A few things you should check:
Inside a method, you should not be using the word private, as you are not defining a class level variable.
You need to use R. before the id, to signify you are retrieving a resource.
You need to make sure that the Text View is inside activity_main.xml.
In addition to all of that, you have a lot of things wrong in your pastebin file, I highly recommend you avoid just copy and pasting code, because it is never that simple.
You need to remove all access modifiers that are not at the class level, and clean up snippets like this:
String contentAsString;
public void onCreate;(Bundle savedInstanceState;) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
txtView=(TextView)findViewById(R.id.my_text_view);
txtwidth = (TextView)findViewById(R.id.viewwidth);
hello="This is my first project";
mybtn.setOnClickListener(this);
txtView.setText(hello);}
There are brackets and semi colons all over the place, and not to mention this code is written after a return statement, so you won't even hit this line of code.
NOTE
Other things that I've found that are wrong:
You have an extra closing bracket after the DownloadWebpageTask which will cause some compilation errors.
You override onCreate twice in HTTPExample
It looks like the code you're having trouble with should be inside onCreate.
In other words, try something like this.
Also, inside your XML where you have the line android:text="#string/displayThis, the #string is basically saying you want to get the string 'displayThis' from strings.xml. If you don't have, or don't want to put 'displayThis' in strings.xml, you need to remove that attribute and just set the text programatically.

Unicode character ("\u232B") does not display on button

I'm building the keyboard of a calculator application for Android.
I'm using the unicode but the application did not display the button "erase to left"
static String[][] screen2L ={{"sin","asin","sinh","asinh","sind","asind","\u232B","AC"},
{"cos","acos","cosh","acosh","cosd","acosd","log2","gamma"},
{"tan","atan","tanh","atanh","tand","atand","log10","ln"}};
thanks
Unicode characters are not supported in all fonts. Check here to see the supported fonts for \u232B
Instead of using the character, make an image of the character and set it as the buttons background. A post that can help with that has already been answered here: How to make button with custom background image...
Also, as for using strings in java to print in your GUI, it's better practice to use xml for this. The buttons individual values would be stored to the app instead of having to assign them every time the app is run. I would write out instructions on how this is done, but the android developers guide that can be found here gives much better instructions than I could.

showing Alphabets in spinner

Howdy,
I am writing a GRE app on android.i am new in this arena.
I want to show all the alphabets in the spinner control. what is best way to do it and how can I do it. Can some share the code.
Like in .NET , i would have filled an array with all the chars A-Z. then have called toChar and bind it with combo control.
I dont how to did the same think java and android ?
Do a search first, Anyway here is the tutorial
http://developer.android.com/resources/tutorials/views/hello-spinner.html

Categories