Search code examples
javaandroidbuttontextviewandroid-studio-4.0

how do I add a reset button for my two TextViews in Android Studio


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/away"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="@string/away"
        android:gravity="center"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="@string/_0"
        android:textSize="20sp" />

    <Button
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="50dp"
        android:onClick="score1"
        android:text="@string/score" />

    <TextView
        android:id="@+id/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="50dp"
        android:text="@string/home"
        android:textSize="20sp"
        android:gravity="center"/>

    <TextView
        android:id="@+id/score2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="50dp"
        android:text="@string/_0"
        android:textSize="20sp" />

    <Button
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="score2"
        android:text="@string/score" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:textColor="#ff1100"/>

</LinearLayout>

I searched it on multiple websites but I still can't the answer. I also tried using the__setText("")it

didn't work as well as the the if() method. I have also tried the intent method also doesn't seem to

work, and many other codes , please help me because I am stuck at this bit , I know that it is simple but

I just can't find it. Thank you


Solution

  • The following codes will set the TextView score to blank when Button one is clicked

    First approach

    To use the android:onClick="score1" in your XML

    Add this to your activity

    TextView mTvScore1 = (TextView) findViewById(R.id.score)
    
    public void score1(View v) {
        mTvScore1.setText("");
    }
    

    Second approach

    Button mBtn = (Button) findViewById(R.id.one)
    TextView mTvScore1 = (TextView) findViewById(R.id.score)
    
    mBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mTvScore1.setText("");
        }
    });
    

    Activity code

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView mTvScore1 = (TextView) findViewById(R.id.score);
            Button mBtn = (Button) findViewById(R.id.one);
    
            mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    score1(v);
                }
            });
        }
    
        public void score1(View v) {
            mTvScore1.setText("");
        }
    }