Android, Codes and Tips

Just in

11/24/2019

How to add Reset button on android Studio Games

How to add Reset button on android Studio Games
After working and searching for hours and you can't get the solution you are looking for. You are welcome to the home of Android Developer. If you have being using Android Studio for writing Java apps, Java codes is everything sensitive raging from capital letters, punctuation errors, misspelled of words and plugin updates.

Android Studio comes up with new updates always which depreciated the older once. A code you write today, use it and develop an app may be outdated and depreciated tomorrow. If you copy somebody's code or forked a code from github, there are chances that the code which worked earlier won't work again unless you do serious editing.

1 Design your Button on XML file

<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#f00"
tools:context=".MainActivity">


<Button
android:id="@+id/reset"
android:layout_width="163dp"
android:layout_height="50dp"
android:layout_marginBottom="3dp"
android:background="#0f0"
android:text="Reset"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>




2. Declare the button present under

public class MainActivity extends AppCompatActivity {

private Button reset;


3. Set the OnClick activity under Oncreate method

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

reset = findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
           Intent t= new Intent(MainActivity.this,MainActivity.class);
           startActivity(t);
           finish();
          }
    });

No comments:

Post a Comment