I am new to android, i was following some tutorial and at this point, app keeps crashing when i click the login button.
When i run the app and click the login button, the app crashes and in the logcat, it shows this error of java RuntimeException.
This is my logcat, loginactivity.xml and loginactivity.java.
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.ugtunda/com.example.ugtunda.LoginActivity}:
java.lang.ClassCastException:
androidx.appcompat.widget.AppCompatTextView cannot be cast to
com.rey.material.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2884)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1614)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6524)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to
com.rey.material.widget.TextView
at com.example.ugtunda.LoginActivity.onCreate(LoginActivity.java:48)
at android.app.Activity.performCreate(Activity.java:7044)
at android.app.Activity.performCreate(Activity.java:7035)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2759)
Here is my login_activity_XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/login"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/login_applogo"
android:layout_width="300dp"
android:layout_height="100dp"
android:src="#drawable/applogo"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
/>
<EditText
android:id="#+id/login_phone_number_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/login_applogo"
android:background="#drawable/input_design"
android:padding="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:hint="Phone Number"
android:inputType="number"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="17sp"
android:textStyle="bold"
/>
<EditText
android:id="#+id/login_password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/login_phone_number_input"
android:background="#drawable/input_design"
android:padding="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="6dp"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="17sp"
android:textStyle="bold"
/>
<LinearLayout
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/login_password_input"
android:layout_marginTop="5dp"
>
<com.rey.material.widget.CheckBox
android:id="#+id/remember_me_chkb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Material.Drawable.CheckBox"
android:text="Remember Me"
android:textColor="#color/colorPrimaryDark"
app:cbd_strokeColor="#color/colorPrimaryDark"
android:gravity="center_vertical"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginLeft="17dp"
/>
<TextView
android:id="#+id/forget_password_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="80dp"
/>
</LinearLayout>
<Button
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linear_layout_1"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#drawable/buttons"
android:padding="17dp"
android:text="Login"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/admin_panel_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/login_button"
android:layout_alignParentEnd="true"
android:layout_marginLeft="80dp"
android:layout_marginEnd="23dp"
android:text="I'm an Admin"
android:textAppearance="#android:style/TextAppearance.Material.Small"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/not_admin_panel_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm not an Admin"
android:layout_alignParentStart="true"
android:layout_below="#+id/login_button"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="80dp"
android:layout_marginEnd="23dp"
android:layout_marginStart="25dp"
android:visibility="invisible"
/>
</RelativeLayout>
And lastly my loginactivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.ugtunda.Models.Users;
import com.example.ugtunda.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rey.material.widget.CheckBox;
import com.rey.material.widget.TextView;
import io.paperdb.Paper;
public class LoginActivity extends AppCompatActivity
{
private EditText InputPhoneNumber, InputPassword;
private Button LoginButton;
private ProgressDialog loadingBar;
private TextView AdminLink, NotAdminLink;
private String parentDbName = "Users";
private CheckBox chkBoxRememberMe;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton = findViewById(R.id.login_button);
InputPassword = findViewById(R.id.login_password_input);
InputPhoneNumber = findViewById(R.id.login_phone_number_input);
AdminLink = findViewById(R.id.admin_panel_link);
NotAdminLink = findViewById(R.id.not_admin_panel_link);
loadingBar = new ProgressDialog(this);
chkBoxRememberMe = findViewById(R.id.remember_me_chkb);
Paper.init(this);
LoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
LoginUser();
}
});
AdminLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
LoginButton.setText("Login Admin");
AdminLink.setVisibility(View.INVISIBLE);
NotAdminLink.setVisibility(View.VISIBLE);
parentDbName = "Admins";
}
});
NotAdminLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
LoginButton.setText("Login");
AdminLink.setVisibility(View.VISIBLE);
NotAdminLink.setVisibility(View.INVISIBLE);
parentDbName = "Users";
}
});
}
private void LoginUser()
{
String phone = InputPhoneNumber.getText().toString();
String password = InputPassword.getText().toString();
if (TextUtils.isEmpty(phone))
{
Toast.makeText(this, "Please write your phone number...", Toast.LENGTH_SHORT).show();
}
else if (TextUtils.isEmpty(password))
{
Toast.makeText(this, "Please write your password...", Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle("Login Account");
loadingBar.setMessage("Please wait, while we are checking the credentials.");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
AllowAccessToAccount(phone, password);
}
}
private void AllowAccessToAccount(final String phone, final String password)
{
if(chkBoxRememberMe.isChecked())
{
Paper.book().write(Prevalent.USER_PHONE_KEY, phone);
Paper.book().write(Prevalent.USER_PASSWORD_KEY, password);
}
final DatabaseReference RootRef;
RootRef = FirebaseDatabase.getInstance().getReference();
RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.child(parentDbName).child(phone).exists())
{
Users usersData = dataSnapshot.child(parentDbName).child(phone).getValue(Users.class);
if (usersData.getPhone().equals(phone))
{
if (usersData.getPassword().equals(password))
{
if (parentDbName.equals("Admins"))
{
Toast.makeText(LoginActivity.this, "Welcome Admin, you are logged in Successfully...", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(LoginActivity.this, AdminCategoryActivity.class);
startActivity(intent);
}
else if (parentDbName.equals("Users"))
{
Toast.makeText(LoginActivity.this, "logged in Successfully...", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
Prevalent.currentOnlineUser = usersData;
startActivity(intent);
}
}
else
{
loadingBar.dismiss();
Toast.makeText(LoginActivity.this, "Password is incorrect.", Toast.LENGTH_SHORT).show();
}
}
}
else
{
Toast.makeText(LoginActivity.this, "Account with this " + phone + " number do not exists.", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
It looks like you're defining standard Android components in the XML layout, then attempting to inflate them into custom components (the com.rey.material package). Try going into your imports and deleting the com.rey.material lines. Then when your IDE prompts you to import the TextView and Checkbox, make sure you're importing the versions from android.widget, not com.rey.material. Alternatively, you could change your XML file to specify the com.rey.material components if you wanted to be using those instead of the default ones.
You used the wrong import so you need to
replace this import
import com.rey.material.widget.TextView;
with
import android.widget.TextView;
OR
You can use the corresponding view in the XML
Replace
<TextView
android:id="#+id/forget_password_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="80dp"
/>
with
<com.rey.material.widget.TextView
android:id="#+id/forget_password_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="80dp" />
in your XML file
Looks like you are using the AppCompat Button in your XML, but using the material button in your java code.
So they need to be the same.
First you need to use the right import for whatever view you're using, if you want to use the AppCompat button then import like this
import android.widget.Button;
If you want to use the Material button, then use this import:
import com.rey.material.widget.Button;
Same thing goes for the TextView.
Hope this helps!
androidx.appcompat.widget.AppCompatTextView cannot be cast to
com.rey.material.widget.TextView
In your xml, you're using the SDK TextView but in your code, you want the com.rey.material.widget one.
--> Change one of these
Related
I am implementing an OTP Verification system and have 3 EditText fields in my layout, and one of them is hidden until the user clicks a button.
Here is the layout file:
registration_acivity_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"
tools:context=".RegistrationActivity">
<ImageView
/>
<TextView
/>
<TextView
/>
<TextView
/>
<EditText
android:id="#+id/mobile"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_below="#+id/regText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:clickable="true"
android:focusable="true"
android:hint="Enter Mobile Number"
android:inputType="phone"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/regText" />
<EditText
android:id="#+id/pin_code"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="#+id/mobile"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:autofillHints="Enter Area Pin Code"
android:clickable="true"
android:focusable="true"
android:hint="Enter Area Pin Code"
android:inputType="number"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mobile" />
<TextView
android:id="#+id/verify_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pin_code"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Verify mobile number with OTP"
android:textAlignment="center"
android:textColor="#696969"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pin_code" />
<Button
android:id="#+id/get_otp_button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/verify_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:alpha="1"
android:backgroundTint="#00FA9A"
android:text="Get OTP"
android:textAlignment="center"
android:textColor="#696969"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/verify_text" />
<EditText
android:id="#+id/gen_otp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="#+id/verify_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:alpha="0"
android:clickable="true"
android:focusable="true"
android:hint="Enter OTP"
android:inputType="number"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="15sp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/verify_text" />
<Button
android:id="#+id/verify_otp_button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/get_otp_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:alpha="0"
android:backgroundTint="#00FA9A"
android:text="Verify OTP"
android:textAlignment="center"
android:textColor="#696969"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/gen_otp" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem I am having is that when the third otp EditText is shown it doesn't show the virtual keyboard or gets focused when clicked. Instead I have to first click one of the previous two EditTexts for it to show the virtual keyboard.
What can I do to make the virtual keyboard show on clicking the OTP field at once?
Here is the java file.
RegistrationActivity.java:
package com.example.covislot;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.FirebaseException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class RegistrationActivity extends AppCompatActivity {
EditText mobileField, pinField,otpField;
Button getOTP, verifyOTP;
String mobileNumber, pinCode, OTP;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallback;
FirebaseAuth auth;
private String verificationCode;
SharedPreferences sp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_activity);
sp = getSharedPreferences("login",MODE_PRIVATE);
StartFirebaseLogin();
mobileField = findViewById(R.id.mobile);
pinField = findViewById(R.id.pin_code);
otpField = findViewById(R.id.gen_otp);
getOTP = findViewById(R.id.get_otp_button);
verifyOTP = findViewById(R.id.verify_otp_button);
getOTP.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (TextUtils.isEmpty(mobileField.getText().toString())) {
// when mobile number field is empty
// displaying a toast message.
Toast.makeText(RegistrationActivity.this, "Please enter a valid phone number.", Toast.LENGTH_SHORT).show();
} else {
mobileNumber = "+91" + mobileField.getText().toString();
}
if (TextUtils.isEmpty(pinField.getText().toString())) {
// when pin number field is empty
// displaying a toast message.
Toast.makeText(RegistrationActivity.this, "Please enter a valid Pin code.", Toast.LENGTH_SHORT).show();
} else {
pinCode = pinField.getText().toString();
}
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber(mobileNumber) // Phone number to verify
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(RegistrationActivity.this) // Activity (for callback binding)
.setCallbacks(mCallback) // OnVerificationStateChangedCallbacks
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
});
verifyOTP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (TextUtils.isEmpty(otpField.getText().toString())) {
// when otp field is empty
// displaying a toast message.
Toast.makeText(RegistrationActivity.this, "Please enter a valid OTP.", Toast.LENGTH_SHORT).show();
} else {
OTP = otpField.getText().toString();
}
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCode, OTP);
SigninWithPhone(credential);
}
});
}
private void SigninWithPhone(PhoneAuthCredential credential) {
auth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
sp.edit().putBoolean("logged", true).apply();
sp.edit().putString("phone", mobileNumber).apply();
sp.edit().putString("pinCode", pinCode).apply();
startActivity(new Intent(RegistrationActivity.this,SignedInActivity.class));
finish();
} else {
Toast.makeText(RegistrationActivity.this,"Incorrect OTP",Toast.LENGTH_SHORT).show();
}
}
});
}
private void StartFirebaseLogin() {
auth = FirebaseAuth.getInstance();
mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
Toast.makeText(RegistrationActivity.this,"verification completed",Toast.LENGTH_SHORT).show();
}
#Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(RegistrationActivity.this,"verification failed",Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationCode = s;
Toast.makeText(RegistrationActivity.this,"Code sent",Toast.LENGTH_SHORT).show();
getOTP.animate().alpha(0f).setDuration(500);
otpField.setVisibility(View.VISIBLE);
otpField.animate().alpha(1f).setDuration(500);
verifyOTP.setVisibility(View.VISIBLE);
verifyOTP.animate().alpha(1f).setDuration(600);
}
};
}
}
Put this line when you want to give it focus:
otpField.requestFocus();
Also, to show the soft keyboard explicitly, you can do like below:
otpField.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager im = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(otpField, InputMethodManager.SHOW_IMPLICIT);
}
},200);
I am creating my first app which is when I press a button I simply move to the next page, but every time I press the button my app crashes (Ignore the username and password texts they don't do anything yet). I am trying to move from login to home page.
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Login">
<ImageView
android:id="#+id/imageView"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_marginTop="59dp"
app:srcCompat="#drawable/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/username"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:textSize="16dp" />
<EditText
android:id="#+id/password"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#id/username"
android:layout_alignStart="#id/username"
android:layout_alignLeft="#id/username"
android:layout_marginTop="38dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_pass"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName"
android:textSize="16sp" />
<Button
android:id="#+id/login"
android:layout_width="321dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="207dp"
android:background="#color/colorAccent"
android:onClick="loginButton"
android:text="Login" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="139dp"
android:text="Register Here"
android:textColor="#fff"
android:textSize="14sp" />
<TextView
android:id="#+id/attempts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="74dp"
android:text="Incorrect Attempts: "
android:textColor="#fff"
android:textSize="14sp" />
</RelativeLayout>
Login.java
package com.example.chorehelpers;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void onClick (View v){
switch (v.getId()){
case R.id.login:
loginButton(v);
break;
}
}
public void loginButton(View v) {
Intent i = new Intent (Login.this, HomePage.class);
startActivity(i);
}
}
activity_home_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".HomePage">
</RelativeLayout>
HomePage.java
package com.example.chorehelpers;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class HomePage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
}
}
Please Help.
why are u using this method
public void onClick (View v){
switch (v.getId()){
case R.id.login:
loginButton(v);
break;
}
remove it and rerun the app , Hope it woks
**Please try to update Login.java class like this,**
package com.example.chorehelpers;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity {
private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent x = new Intent(LoginActivity.this, HomePageActivity.class);
startActivity(x);
}
});
}
}
Clicking on register makes the app crash when the email and password fields are empty, but when the name field is empty it works properly what seems to be the problem?
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.zachmarcelo.softcash.LoginActivity"
android:background="#fff"
>
<!-- Login progress -->
<ImageView
android:layout_width="match_parent"
android:layout_height="47dp"
android:layout_marginBottom="20dp"
android:visibility="visible"
app:srcCompat="#drawable/icon_ewallet_" />
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColorHint="#color/colorHint" >
<EditText
android:id="#+id/reg_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="text"
android:textSize="20sp"
android:textColor="#android:color/background_dark"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextLabel"
android:textColorHint="#color/colorHint">
<EditText
android:id="#+id/reg_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#android:color/darker_gray"
android:hint="Email"
android:maxLines="1"
android:singleLine="true"
android:textColor="#android:color/background_dark"
android:inputType="textEmailAddress"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextLabel"
android:textColorHint="#color/colorHint">
<EditText
android:id="#+id/reg_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#android:color/background_dark"
android:hint="Password"
android:imeActionId="6"
android:imeActionLabel="#string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="#android:color/background_dark" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/register"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#color/colorAccent"
android:gravity="center"
android:paddingLeft="20sp"
android:paddingRight="20sp"
android:text="Register"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>
<TextView
android:id="#+id/signin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Already have an account?"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="12dp" />
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
code:
package com.example.zachmarcelo.softcash;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.view.WindowManager;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
public class RegisterActivity extends AppCompatActivity implements
View.OnClickListener {
private EditText reg_password,
reg_username,
reg_email;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
progressBar = new ProgressBar(getApplicationContext());
reg_password = findViewById(R.id.reg_password);
reg_email = findViewById(R.id.reg_email);
reg_username = findViewById(R.id.reg_username);
progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.register).setOnClickListener(this);
findViewById(R.id.signin).setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() != null) {
Intent i = new Intent(RegisterActivity.this, HomeActivity.class);
startActivity(i);
finish();
}
}
private void registerUser() {
final String username = reg_username.getText().toString().trim();
final String email = reg_email.getText().toString().trim();
String password = reg_password.getText().toString().trim();
if (username.equals("")) {
reg_username.setError(getString(R.string.input_error_name));
reg_username.requestFocus();
return;
}
if (email.equals("")) {
reg_email.setError(getString(R.string.input_error_email));
reg_email.requestFocus();
return;
}else{
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
reg_email.setError(getString(R.string.input_error_email_invalid));
reg_email.requestFocus();
return;
}
}
if (password.equals("")) {
reg_password.setError(getString(R.string.input_error_password));
reg_password.requestFocus();
return;
}else{
if (password.length() < 8) {
reg_password.setError(getString(R.string.input_error_password_length));
reg_password.requestFocus();
return;
}
}
progressBar.setVisibility(View.VISIBLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
User user = new User(
username,
email
);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, getString(R.string.registration_success), Toast.LENGTH_LONG).show();
Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(i);
finish();
} else {
Toast.makeText(RegisterActivity.this, getString(R.string.registration_failed), Toast.LENGTH_LONG).show();
}
}
});
} else {
Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.register:
registerUser();
break;
case R.id.signin:
Intent i = new Intent(this, LoginActivity.class);
startActivity(i);
finish();
break;
}
}
}
error log:
09-03 13:25:24.942 15875-15875/com.example.zachmarcelo.softcash E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zachmarcelo.softcash, PID: 15875
android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
at android.content.res.TypedArray.getColor(TypedArray.java:492)
at android.widget.TextView.<init>(TextView.java:955)
at android.widget.TextView.<init>(TextView.java:872)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:75)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:71)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.widget.Editor.showError(Editor.java:505)
at android.widget.Editor.onFocusChanged(Editor.java:1237)
at android.widget.TextView.onFocusChanged(TextView.java:9821)
at android.view.View.handleFocusGainInternal(View.java:6631)
at android.view.View.requestFocusNoSearch(View.java:10890)
at android.view.View.requestFocus(View.java:10869)
at android.view.View.requestFocus(View.java:10836)
at android.view.View.requestFocus(View.java:10778)
at com.example.zachmarcelo.softcash.RegisterActivity.registerUser(RegisterActivity.java:74)
at com.example.zachmarcelo.softcash.RegisterActivity.onClick(RegisterActivity.java:143)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
and this is what is supposed to be happening: (works perfectly in name text but not in other edittexts)
screenshot of mobile app
any help will be appreciated :D
My guess is that it has something to do with what you put into #style/TextLabel.
When you have an error with password or email you request focus programmatically. Which is fine, however when that happens something in your style is looking for a color resource which doesn't exist. That's what's causing the error.
The error seems to be for getColor() method of textview, your primary color has some problem, please check that.
I'm new for android java coding. I'm try to do menu list where just have tick box, and once tick the items, n press next, it should go to view layout and show items and the total of the item selected, then press next it should open details page where user must put their details n press send button to send via email. I don't know how to call the items from menu to Cart and to confirmation class.
This is menu.java .
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
Button btnorder;
Button btnback;
Button btnlinkcart;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
btnlinkcart = (Button) findViewById(R.id.button1);
btnback = (Button) findViewById(R.id.button2);
// back button click event
btnback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
}
});
// Link to Cart Screen
btnlinkcart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ViewActivity.class);
startActivity(i);
finish();
}
});
}
}
This is Cart.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ViewActivity extends Activity {
Button btnconfirm;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
btnconfirm = (Button) findViewById(R.id.button1);
// Link to Cart Screen
btnconfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ConfirmActivity.class);
startActivity(i);
finish();
}
});
}
}
This is cart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/wallpaper" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Confirm" />
</RelativeLayout>
This is menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Back" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:text="Pizza (Large) RM30.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox1"
android:layout_marginTop="20dp"
android:text="Pizza (Mediume) RM20.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox2"
android:layout_marginTop="20dp"
android:text="Pizza (Personal) RM10.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox3"
android:layout_marginTop="20dp"
android:text="Chicken Wings RM12.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox4"
android:layout_marginTop="19dp"
android:text="Garlic Bread RM6.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox5"
android:layout_marginTop="20dp"
android:text="Soft Drink (Large) RM5.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox6"
android:layout_marginTop="19dp"
android:text="Soft Drink (Medium) RM4.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Make the variable you want to get in the menu class static, then in the cart class you can get them like this:
menu.variableFromMenuClass
public static ArrayList<YourObject> selectItemList= new ArrayList<YourObject>();
on selectItem(){
//each Item you add here
selectedList.add(selectedObject);
}
on disselectItem(){
//remove if exist in list
selectedList.remove(disselectedOject);
}
sendemail(){
send your public static selecteditem list.
}
This is the algorithm you have to write simple.
I'm just wondering if anyone can help me. I've created a login page which requests a user to enter their username and password. Once the details are correct it should move to my home page? I'm just wondering how I would do this. Thank you in advance
Here is my login xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#808080"
android:orientation="vertical" >
<!-- Login Title -->
<TextView
android:layout_width="222dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="#string/login_title"
android:textColor="#FFFF00"
android:textSize="24sp" />
<!-- Login Image -->
<!-- layout for Username -->
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="-75dp"
android:src="#drawable/logov2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:gravity="center_horizontal"
android:text="#string/pleaselogin"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_weight="0.83"
android:text="#string/username"
android:textColor="#FFFF00"
android:textSize="24sp" />
<EditText
android:id="#+id/username1"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#FFFFFF"
android:ems="10"
android:inputType="text"
android:textSize="24sp" >
<requestFocus />
</EditText>
</LinearLayout>
<!-- layout for Password -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="67dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_weight="1.0"
android:text="#string/password"
android:textColor="#FFFF00"
android:textSize="24sp" />
<EditText
android:id="#+id/password1"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#FFFFFF"
android:inputType="textPassword" />
</LinearLayout>
<Button
android:id="#+id/login1"
android:layout_width="150sp"
android:layout_height="50dp"
android:layout_marginLeft="200dp"
android:background="#FF0000"
android:text="#string/login"
android:textColor="#FFFFFF"
android:textSize="28sp" />
</LinearLayout>
Login class
package com.example.workplease;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.workplease_v2.R;
public class LoginActivity extends Activity {
String userName, passWord;
EditText username, password;
Button login;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// UI elements gets bind in form of Java Objects
username = (EditText)findViewById(R.id.username1);
password = (EditText)findViewById(R.id.password1);
login = (Button)findViewById(R.id.login1);
// now we have got the handle over the UI widgets
// setting listener on Login Button
// i.e. OnClick Event
login.setOnClickListener(loginListener);
}
private OnClickListener loginListener = new OnClickListener() {
public void onClick(View v) {
//getting inputs from user and performing data operations
if(username.getText().toString().equals("test") &&
password.getText().toString().equals("test")){
// responding to the User inputs
Toast.makeText(getApplicationContext(), "Login Successfully !!!", Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "Login Not Successful !!!", Toast.LENGTH_LONG).show();
}
};
}
Home page XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#808080"
android:orientation="vertical"
tools:context=".Home" >
<TextView
android:id="#+id/title"
android:layout_width="220dp"
android:layout_height="100dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFF00"
android:textSize="28sp" />
<ImageButton
android:id="#+id/exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="40dp"
android:src="#drawable/exercisesv2"
android:background="#null" />
<ImageButton
android:id="#+id/routines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="#drawable/routines"
android:background="#null" />
<ImageButton
android:id="#+id/logbook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="#drawable/logbook"
android:background="#null" />
<ImageButton
android:id="#+id/qrscanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="#drawable/qrscanner1"
android:background="#null" />
<TextView
android:id="#+id/Copyright"
android:layout_width="206dp"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="70dp"
android:text="#string/copyright"
android:textColor="#FFFF00"
android:textSize="14sp"
android:background="#null" />
<ImageView
android:id="#+id/Logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="-550dp"
android:src="#drawable/logov2" />
</LinearLayout>
Home class
package com.example.workplease;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import com.example.workplease_v2.R;
public class Home extends Activity {
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
AddListenerOnButton();
AddListenerOnButton1();
}
public void AddListenerOnButton(){
final Context context = this;
imageButton = (ImageButton) findViewById(R.id.exercise);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Exercises.class);
startActivity(intent);
}
});
}
public void AddListenerOnButton1(){
final Context context = this;
imageButton = (ImageButton) findViewById(R.id.routines);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Routines.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
To start a new Activity you call startActivity(Intent intent)
Intent intent = new Intent(this, Home.class);
startActivity(intent);
So your loginListener button OnClickListener would perform this action:
public void onClick(View v) {
if(username.getText().toString().equals("test") &&
password.getText().toString().equals("test")){
// responding to the User inputs
Toast.makeText(getApplicationContext(), "Login Successfully !!!",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, Home.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Login Not Successful !!!",
Toast.LENGTH_LONG).show();
}
}