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();
}
});