user758945
user758945

Reputation: 21

Android UI Beginner question

I'm new to Android/java, coming from C#/Visual Studio. It's not a too hard jump from C# to java while coding the logic, but with the UI, I'm having problems.

I've now added a simple TextView to my main.xml

<?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:layout_width="wrap_content" android:editable="true" android:layout_height="wrap_content" android:id="@+id/textView1" android:text="TextView">
</TextView>
</LinearLayout>

Now I wanted to access the textView1 by code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.textView1 = (TextView)this.findViewById(android.R.id.textView1);
}
private TextView textView1;

but I get the error: textView1 cannot be resolved.

What am I doing wrong?

Thanks

James

Upvotes: 0

Views: 646

Answers (4)

Houcine
Houcine

Reputation: 24181

try this

    private TextView textView1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.textView1 = (TextView)this.findViewById(R.id.textView1);
        textView1.setText("Hello World !!!");
   }

Upvotes: 1

Nipun Gogia
Nipun Gogia

Reputation: 1856

Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);

android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .

and set the background color to #ffffff in xml .... it seems more attractive..

Upvotes: 2

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);

android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .

Upvotes: 1

Graham Borland
Graham Borland

Reputation: 60681

Instead of android.R.id.textView1, just use R.id.textView1. The android prefix is for resources from Android itself, rather than from your project.

Upvotes: 1

Related Questions