My Android "Hello World" app doesn't say "Hello" - android

I created a Hello World application, and the system generated most of the Android language below. When running the app without the System.out statement, there is no "Hello" displayed in the emulator. Then, using the Eclipse tutorial, I read that I can add the System.out.println statement to main. Again the app runs, but there is no output.
What am I not understanding here?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
System.out.println =" Hello world!"
/>
</LinearLayout>

I had this same problem, turns out all I had to do was be more patient.
The initial load time of the AVD seems like FOREVER... and in Eclipse before it actually looks like the device is booting up it just says "ANDROID_" against a fully black screen for quite a while. I thought it wasn't loading my text "Hello, Android" correctly, but it was actually just a load screen and I didn't wait long enough. (Yes, I now feel like a moron and a noob.)
If you're following along with the Android Developers Hello World Tutorial, and it doesn't seem like it's working, if you've never loaded the AVD before... maybe just wait longer?

Beginners' questions are fine, but boy, this really is a beginner question :) As for your problem, it can be a few things. What's probably happened is that you've adapted the basic "Hello, Android" tutorial which defines the TextViews in code, to make it display using an XML file. However, when you did that, you didn't change the code to use that XML file, and instead it's trying to display your old TextView. Also, "System.out.println="hello world!" won't do anything when in your XML file - you need to put statements like that in the code itself. In fact, offhand I can't remember if System.out.... even does anything in Android - debugging lines should be issued using Log.d("some title", "your message"), as that outputs to the Android specific logging device.
Anyway, it'd be easier to help solve your problem if you showed a bit more of your code. Try to make sure it's formatted properly, e.g. indenting code lines by four spaces. You can preview your post before you submit your edited version in the lower window to make sure it looks right.

Try this.
Hardcoded in [your_layout].xml
...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Hello world" />
or this
Use reference in [your_layout].xml
...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#strings/hello" />
In res/values/strings.xml
<resources>
<string name="hello">Hello world</string>
</resources>
That should help you get started a bit. A few thing to remember, System.out.println doesn't do a thing in Android, especially in XML file.
When addressing text with "#string/hello", it means that the application will look for string name "hello" inside strings.xml.
Anyway, you should try Android tutorial to get start.

One thing is for sure, putting println in your layout resource file won't do anything. This code(if it may be called so) is NOT executed. This is just definition of view. If at all, this statement should be called from your Activity class implementation(the java file) to be executed.

Related

Cannot Understand this code functionality

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.bignerdranch.android.geoquiz.CheatActivity">
<TextView
android:id="#+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
tools:text="Answer"/>
I am a novice in programming. I started with Big Nerd Ranch programming book. I came across this code. In the book it is stated as:
"This namespace allows you to override any attribute on a widget for the
purpose of displaying it differently in the Android Studio preview. Since TextView has a text attribute,
you can provide a literal dummy value for it to help you know what it will look like at runtime. The
value “Answer” will never show up in the real app. Handy!"
What does that actually mean? I am completely new. I know this is foolish question, please help me in this.
Thanks to that line
xmlns:tools="http://schemas.android.com/tools"
you can use in all your XML something like that
tools:text="Answer"
Thanks to that line
xmlns:android="http://schemas.android.com/apk/res/android"
you can use in all your XML element the android attribute, for example
android:id="#+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
The book you're reading is a good book, keep continue and don't give up!
Tools is basically a collection of extra properties you can add to your TextView that assist you when designing layouts in Android Studio. In this particular example, tools:text allows you to put a fake value into your TextView which will only show up in the the layout preview in Android Studio.
This will allow you to see what a TextView looks like when designing your layout in Android Studio, but you don't have to worry about removing that dummy text from your layout when you build a "real" version of your app for a phone.
See also: Tools Attribute Reference

Displaying a TextView immediately after another

I am a first time developer for Android, so you can say I've been learning as I was developing. For most of my code that doesn't have to do with the XML layout, I had no problem patching my rookie mistakes. With that said, my rookie mistakes has caught up to me in regards to two TextViews when I initially designed them with the GUI interface designer (my major rookie mistake).
My display_city tv and display_today_date tv seem to have a symbiotic relationship with each other. Removal of either one would crash the app. They seem so dependent on each other that changing each other's positioning is impossible (at least from the myriad of things I have tried such as setting layout margins).
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip"
android:layout_above="#+id/display_today_date"
android:layout_below="#+id/get_data"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/display_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/display_pollen_type"/>
My question is - how do I simply position display_today_date immediately after my display_city? When I first started this Android app, I relied a lot on the GUI builder. That was my first rookie mistake, which resulted in this symbiotic relationship I explained.
Currently this is what my app looks like:
I have tried changing display_today_date's layout to android:layout_below="#+id/display_city. This results in a crash. I checked logcat, but it did not give me relevant information to the reason of the crash within the XML file.
P.S. get_data is my TextEdit box.
You already have the city to show above the date with the line android:layout_above="#+id/display_today_date". You can't have 2 views in a relative layout each reference the other, or it won't be able to figure out what to do. If you don't want to put the city above the date, delete that line then add the code to place it where you want.
You could use a LinearLayout with the orientation set to horizontal. That way there is no reference to another view. So if you delete one the other one won't cause the app to crash.

Android Layout Editor Freaks Out on Question Mark

This is strange, yet I see it all the time as I have lots of reasons to display just a simple question mark in this app.
When editing with the Eclipse graphical layout editor, everything is fine...until I tell a TextEdit or a Button to display just a question mark. Here's my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/question_mark" />
</LinearLayout>
And the string is defined in res/values/strings.xml. The relevant line is:
<string name="question_mark">\u003f</string>
The error message I get is:
Missing styles. Is the correct theme chosen for this layout? Use the
Theme combo box above the layout to choose a different layout, or fix
the theme style references.
Couldn't find theme resource for the current theme
Change the text, and the error message goes away.
You can see that in my struggles, I'm even trying to use the unicode version of a question mark. And yes, \? doesn't work either.
Note that this only happens when the graphical layout editor is set to API 7 or greater.
Now the graphical layout editor displays the question mark properly, and the emulator and my phone display the question mark without any problems. I'm just annoyed with the error message taking up 1/4 of my screen for all my layouts (and obscuring other error messages that may crop up).
Any suggestions?
First, test this again on the ADT 21 release that shipped today.
If the problem continues, create a sample project that demonstrates the issue, and post it along with step-by-step instructions on the Android issue tracker.
Well, this is a really crappy hack, but it kind of works--if you stand on your head!
You can use the unicode \u00bf as in
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u00bf" />
It's not a normal question mark, but it's close enough for a hack and gets that annoying error message out of your hair. And it might even increase the humor quotient of your app!
But PLEASE, if anyone out there has a real fix, post it!
Wow, I entered a bug report...[time marches on]...finally, I get a few emails as the Google team starts to look at it.
Today, I received some good news. It looks like the bug has been fixed (and they found a few related bugs, which have been fixed as well). The fix will be in the next release, Version 21.1 Preview 2. You can read the official details here.
Looking forward to it!

stop xml entity from producing square

i have the following in my xml file and it gives me a square ⃠
<?xml version="1.0" encoding="utf-8"?>
...
<Button android:text="&#8416" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
...
the character is the "no symbol"/prohibited symbol but it shows up like a square...other symbols show up fine like euro €.
question is how do i get the symbol to show up and if i cant, how can i know what symbols will and wont work....is there a master list somewhere
Ok so due to various reasons it won't show up and there are other posts on this.
Why not use a left drawable in the image by:
android:drawableLeft="#drawable/myicon"
Same result and you don't have to worry about character encoding and fonts.
Sometimes it depends on the Font you are using. I had a link to look that up. I'll get back here as soon as I find it.
Update:
Here you go - http://www.fileformat.info/info/unicode/font/index.htm

Null Pointer Exception when inflating from XML (Not using TabActivity)

I'm currently trying to debug my Android App. I've built out the first screen and it's using a Tab Host. I had made a few minor modifications to TabHost to add a few extra methods (full source to follow) to work with some other parts of my App.
However, when I try to run the following I get a null pointer exception on Inflate and it doesn't really tell me anything about why. I downloaded the full Android source for my SDK and added it in eclipse to step through the inflation process and find if I could get any more info about the error that way.
I found exactly what line was causing the error (source included below) but when stepping through the debugger something strange happened. The logic is an if/else case where if == true it should return a New Parser() object, I ran that code and it successfully stepped through the constructor of Parser, yet after returning from that, the next instruction stepped right out of the statement group to the line below returning null.
The constructor appears to have run properly (it's located in XmlBlock.java in case you're wondering) yet it still steps to the next line and returns null and I have no idea why this is. I've searched for solutions but so far the only similar problem was encountered by someone using a TabActivity which is not suitable for my Application. I need Tabs and their views to be generated by objects I've defined within my project (similar to IOS style View Controllers) and not separate processes via using Intents to launch new Activities.
Can someone please have a look at my source and tell me what I'm doing wrong here because no one else can seem to tell me what the problem is? Here's another direct link to the source involved: http://pastebin.com/9VRE2UGW
Okay, well it looks like my REAL problem here was that I wasn't looking in the Android DDMS perspective for my Debug info. I'd originally thought the Java Debug perspective would give me all the necessary info. The real cause of this exception was elsewhere in the program. Now that I'm reading the logcat I should be able to solve the rest of this on my own since that was my real problem (inadequate information).
I suspect this line is wrong:
TabBarController UITabBarController =
(TabBarController)interfaceBuilder.inflate(R.layout.tab_bar , null);
Because your R.layout.tab_bar represents a TabHost:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/tabhost">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
android:layout_alignParentBottom="true" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabcontent">
</FrameLayout>
</RelativeLayout>
</TabHost>

Categories

Resources