Hi guys I am having trouble using a button on android studio. Currently, when I click on the button there is no response at all to the click. I am using onClickListener() and it doesn't work so I tried to use the onClick method in XML and write your own function but that just leads to the crash of my app. Please let me know if there are any problems with my code and if you know what the problem is thanks.
XML FILE:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="#fff"
android:elevation="0dp"
android:text="NEW USER? SIGN UP"
android:textColor="#000"
android:id="@ id/signup_btn" />
Login.class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button button = (Button) findViewById(R.id.signup_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Login.this, SignUp.class);
startActivity(i);
}
});
}
CodePudding user response:
Try change your code like this :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// In XML you use AppCompatButton so make same AppCompatButton too in your class
AppCompatButton button = (AppCompatButton) findViewById(R.id.signup_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Login.this, SignUp.class);
startActivity(i);
}
});
}
CodePudding user response:
Change this line:
Button button = (Button) findViewById(R.id.signup_btn);
To this:
AppCompatButton button = (AppCompatButton) findViewById(R.id.signup_btn);
And make sure both activities (SignUp and Login) are declared in the manifest.