Reputation: 13
I've been trying to change textviews at run-time based on which button is selected on screen. I'm running into a problem though: Everytime a button is pressed, the correct textview changes, but the previous textview resets to what is given out in the layout xml. For example, if button one is pressed, score1 says 5. But then when button 2 is pressed next, score1 goes back to zero and score2 is set to 5. Any suggestions? I've hit a wall.
This is in the main activity:
public void updateInfo(View v){
setContentView(R.layout.main_layout);
if (v.getId() == R.id.button1){
TextView mytextview1 = (TextView) findViewById(R.id.Score1);
mytextview1.setText("5");
}else if (v.getId() == R.id.button2){
TextView mytextview1 = (TextView) findViewById(R.id.Score2);
mytextview1.setText("5");
}else if (v.getId() == R.id.button3){
TextView mytextview1 = (TextView) findViewById(R.id.Score3);
mytextview1.setText("5");
}else if (v.getId() == R.id.button4){
TextView mytextview1 = (TextView) findViewById(R.id.Score4);
mytextview1.setText("5");
}
Here is the xml code for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pastel" >
<Button
android:id="@+id/letterButton"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_button"
android:text="@string/first_letter"
android:freezesText="true"
android:textSize="90sp"
android:textStyle="bold"
android:typeface="normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:orientation="vertical" >
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="updateInfo"
android:text="@string/button_text"
android:background="@drawable/rect_button" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1.1" />
<TextView
android:id="@+id/Score1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textStyle="bold" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/Score3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="19dp"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textStyle="bold" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1.1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="updateInfo"
android:text="@string/button_text"
android:background="@drawable/rect_button" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="10" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:orientation="vertical" >
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="updateInfo"
android:text="@string/button_text"
android:background="@drawable/rect_button" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1.1" />
<TextView
android:id="@+id/Score2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textStyle="bold" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/Score4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="19dp"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textStyle="bold" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1.1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="updateInfo"
android:text="@string/button_text"
android:background="@drawable/rect_button" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Upvotes: 0
Views: 351
Reputation: 8357
The problem is that you're calling setContentView(R.layout.main_layout);
in updateInfo
, therefore you re-initialize your whole view everytime a button is clicked, you need to call setContentView(R.layout.main_layout);
only once in your onCreate
method.
your code should be like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
public void updateInfo(View v){
if (v.getId() == R.id.button1){
TextView mytextview1 = (TextView) findViewById(R.id.Score1);
mytextview1.setText("5");
}else if (v.getId() == R.id.button2){
TextView mytextview1 = (TextView) findViewById(R.id.Score2);
mytextview1.setText("5");
}else if (v.getId() == R.id.button3){
TextView mytextview1 = (TextView) findViewById(R.id.Score3);
mytextview1.setText("5");
}else if (v.getId() == R.id.button4){
TextView mytextview1 = (TextView) findViewById(R.id.Score4);
mytextview1.setText("5");
}
Upvotes: 1