Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (retired 2014) Getting Started with Android Accessing Views in Code

Sub cattegory View and View 1 Button and Button 1?

Hello,

Code is here: package com.example.crystal.ball;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Declare our view variables
    TextView answerLabel = (TextView) findViewById(R.id.textView1);     
    Button getAnswerButton = (Button) findViewById(R.id.button1);

Is the View1 sub category to View and Button1 to Button? Like example folders. View is main folder and inside that there is Another folder named as View1 and same for Button Button is main folder and inside that Button 1?

1 Answer

Harry James
Harry James
14,780 Points

If you take a look in your res (Resources) folder (Hence the R. prefix) and then layout folder, you'll find an xml file. This file is where all of your different views and buttons are stored and created.

What you're doing here is you're linking the two sections together. So, you're making a variable in java called answerLabel and setting it equal to the TextView that has the id textView1 that can be found in this xml file (You can view the ids easily by opening this file and looking at the xml view). This means that you can use Java to modify the values of things the objects you declare in xml. Awesome, eh?

Hope it helps and any further questions I'll try my best to answer :)