Reputation: 58
How do I get my text field to change when the application launches and how do I get it to change again when I click the button?
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myText=(TextView)findViewById(R.id.myActivityTextView);
myText.setText("Launch Text Change");
}
public void myButtonclicked(View v){
TextView myText=(TextView)findViewById(R.id.myActivityTextView);
myText.setText("Button Change Text");
}
activity_main.xml
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text"
android:id="@+id/myButton"
android:layout_below="@+id/myText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="myButtonclicked"/>
Upvotes: 3
Views: 225
Reputation: 690
The TextView
you have in your xml has an id of myText
. Change your lookup of
TextView myText=(TextView)findViewById(R.id.myActivityTextView);
to
TextView myText=(TextView)findViewById(R.id.myText);
and it should work.
Upvotes: 2
Reputation: 15668
You are looking for the wrong id. Here is the correct way
TextView myText=(TextView)findViewById(R.id.myText);
myText.setText("Launch Text Change");
Upvotes: 1