• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with Java?

Alinor95

Lurker
Hello. I'm new to the forum and to programming aswell. I'm trying to learn how to make an app so I started with a tutorial online, but I'm having a problem. Here's a link of the tutorial https://www.raywenderlich.com/56109/make-first-android-app-part-2
On the section "Accessing Views From Within Java" when I add "android:text= @String/hello_world"" I get an error. On the bottom it says I should be using a @String but when I do the preview window displays the @String/" aswell. What's that about?
Second, I can't rename the string after changing hello_world to something else. Why is the tutorial even saying I should rename it? What will that do?
Lastly, in MainActivity.java above onCreate I have to insert a line of code. The tutorial doesn't explain why I need to do that. Where should the line go, the code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wherever I insert the code and press Alt+Enter I mess up the whole thing...
Sorry that these questions are annoying and basic, I just really want to learn to make apps and I'm trying to figure things out alone.
Thanks.
P.S. one more thing, in "Buttons and Listeners" there is a part saying:
"Then there’s text, for which you need to add a line in strings.xml such as the following:
<string name="button">Update The TextView</string>"
when I do that I get an error in strings.xml
 
I think you've got some problems with your "" characters.
Can you post your layout file, but use [code][/code] tags so it appears in a nicely formatted way.
 
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >

    <TextView
        android:id="@+id/main_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="@string/hello_world"
        android:layout_alignParentBottom="true"
   
        />
</LinearLayout>

@String/hello_world gets highlighted and I get a rendering problem because of it...
 
In MainActivity.java the code is
Code:
package com.example.user.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
so I've no idea where to insert the "TextView mainTextView;" line above onCreate.
 
Do you know what @String/hello_world means? Your tutorial does not explain this, and I think it may have even missed out a step.

The @String notation is a kind of shorthand. It tells the system to look for a "string" resource. All such resources are in the file strings.xml. Placing all your literal strings in this file helps when you need to internationalise your app. Because all the strings are in one file, they can easily be translated to another language.

Code:
<string name="hello_world">Hello world</string>

You see this is the problem with step-by-step tutorials, which don't fully explain what's happening. It's great to quickly get an app running, but you really need to achieve full understanding of what's going on.

I would recommend you get a good book, or find a decent website which takes you through the basics, because IMO the one you're using isn't very good.
 
I thought that might be the case. I am planning on getting a book soon but until then I would like to learn some of the basics. If you could recommend a tutorial which is better that would be great.
Thanks.
 
Well there are many online tutorials, but I personally got started with the 'Hello Android' book, because my preference is for book based learning, rather than purely online. Since then I've consulted a combination of online resources to fill in the gaps, or accumulate more knowledge about certain aspects.
I think the bottom line is (particularly with step-by-step guides) to analyse what they're telling you to do, and as I say, fully understand what it is you're typing in.

Have you had a look at the official Android developer website?

https://developer.android.com/training/basics/firstapp/index.html
 
I have come across the website, but I wanted to find something with more visual examples. Still, I will give it a shot. Thanks.
 
Back
Top Bottom