Issue
I am new to Android Studio and have been pulling my hair out for the past hour trying to figure this out.
So I have a MainActivity java class which has two separate onClick methods each with an intent that opens a certain activity depending on the button (in activity_main.mxml) pressed.
For whatever reason, the newUser.setOnClickListener() will not open SignupActivity.
The strangest part is that if I change the destination activity in that same block to SplashActivity, it works. This tells me that it's a problem with my SignupActivity and its corresponding.xml file maybe.
Any help is greatly appreciated.
Here are the files in my project:
MainActvitiy.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
// Declare buttons
Button existingUser, newUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
// Define buttons
existingUser = findViewById(R.id.btnExistingUser);
newUser = findViewById(R.id.btnNewUser);
// Existing user button listener
existingUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SignupActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
// New user button listener
newUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SignupActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
}
}
SignupActivity.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class SignupActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_login);
}
// Launch MainActivity when back button is pressed
public void launchMain (View v) {
// Launch Log-in activity
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
LoginActivity.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_login);
}
// Launch MainActivity when back button is pressed
public void launchMain (View v) {
// Launch Log-in activity
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
activity_signup.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/gradient_background"
tools:context=".SignupActivity">
<!-- Welcome Back Text -->
<TextView
android:id="@+id/txtWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="330dp"
android:fontFamily="sans-serif-light"
android:text="Create User"
android:textColor="@color/white"
android:textSize="60sp"
android:textStyle="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- User ID Field -->
<EditText
android:id="@+id/edtTxtPromptUserID"
android:layout_width="332dp"
android:layout_height="69dp"
android:layout_marginTop="468dp"
android:backgroundTint="@color/white"
android:hint="@string/prompt_userID"
android:inputType="text"
android:selectAllOnFocus="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Password Field -->
<EditText
android:id="@+id/edtTxtPromptPassword"
android:layout_width="332dp"
android:layout_height="69dp"
android:layout_marginTop="8dp"
android:backgroundTint="@color/white"
android:hint="@string/prompt_password"
android:inputType="textPassword"
android:selectAllOnFocus="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtTxtPromptUserID" />
<!-- Register Button -->
<Button
android:id="@+id/btnRegister"
android:layout_width="242dp"
android:layout_height="83dp"
android:layout_marginTop="40dp"
android:backgroundTint="#00A36C"
android:text="Register"
android:textSize="30sp"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtTxtPromptPassword" />
<!-- Back button -->
<Button
android:id="@+id/btnBack2"
android:layout_width="134dp"
android:layout_height="57dp"
android:layout_marginTop="24dp"
android:onClick="launchMain"
android:text="Back"
android:textColor="@color/white"
android:textSize="20dp"
android:backgroundTint="@color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnRegister" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have gone back and looked at all my files to make sure they are unique and consistent with naming, but I haven't been able to find the reason why it won't work.
Solution
change in signup Activity
setContentView(R.layout.activity_login);
to
setContentView(R.layout.activity_signup);
Answered By - Kamran Khan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.