0% found this document useful (0 votes)
175 views

Activity - Main Cannot Be Resolved or Is Not A Field Treehou

This document contains code for an Android application that uses a crystal ball to provide random text answers. When the user clicks a button, a random answer is generated by the crystal ball class and displayed on the screen. The code imports Android classes, extends the activity class, overrides required methods, declares and initializes view variables from the layout file, and sets an onClickListener on the button to update the text view with a new answer from the crystal ball on each click.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views

Activity - Main Cannot Be Resolved or Is Not A Field Treehou

This document contains code for an Android application that uses a crystal ball to provide random text answers. When the user clicks a button, a random answer is generated by the crystal ball class and displayed on the screen. The code imports Android classes, extends the activity class, overrides required methods, declares and initializes view variables from the layout file, and sets an onClickListener on the button to update the text view with a new answer from the crystal ball on each click.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

package com.example.

crystalball;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private CrystalBall mCrystalBall = new CrystalBall();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View variables and assign them the Views from the layout file
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String answer = mCrystalBall.getAnAnswer();
// Update the label with our dynamic answer
answerLabel.setText(answer);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
//Declare our view variables
final TextView anwserLabel = (TextView) findViewById(R.id.text1);
Button getAnwserButton = (Button) findViewById(R.id.button1);
getAnwserButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String anwser = "Yes";
anwserLabel.setText(anwser);
}
});
}

@Override

public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

R.layout.activity_list_item

setContentView(R.layout.activity_main);

getMenuInflater().inflate(R.menu.main, menu);

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Enlighten me!" />
</RelativeLayout>

text1

final TextView anwserLabel = (TextView) findViewById(R.id.text1);

final TextView anwserLabel = (TextView) findViewById(R.id.text1);

text1

package com.zigax.crystalball2;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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
final TextView anwserLabel = (TextView) findViewById(R.id.text1);
Button getAnwserButton = (Button) findViewById(R.id.button1);
getAnwserButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String anwser = "Yes";
anwserLabel.setText(anwser);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

You might also like