Intent/Start activity starting the wrong activity? - java

For some reason, my start activity sends me to the wrong activity( sending me to add task class vs review task class) no error and following in log cat doesn't help me track down the issue. I tried using startActivityForResult(); but that did not help at all and had the same issue. It goes to createTask.class. I need to put more details but I am not sure what else to put, Maybe the put extra and putting two items packed to the instance, but I don't think that would be an issue
<?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=".MainActivity">
<TextView
android:id="#+id/amountOfTasksTextViewUIMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You Have 0 Tasks"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04000002" />
<Button
android:id="#+id/viewTaskButtonUIMain"
android:layout_width="209dp"
android:layout_height="46dp"
android:text="#string/viewTaskButtonUIMainString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998" />
<Button
android:id="#+id/createTaskButtonUIMain"
android:layout_width="209dp"
android:layout_height="46dp"
android:text="#string/createTaskButtonUIMainString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/viewTaskButtonUIMain"
app:layout_constraintStart_toStartOf="#+id/viewTaskButtonUIMain"
app:layout_constraintTop_toBottomOf="#+id/viewTaskButtonUIMain"
app:layout_constraintVertical_bias="0.04000002" />
<TextView
android:id="#+id/upCommingTasksTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/upCommingTasksTextViewUIMainActivityString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/amountOfTasksTextViewUIMain"
app:layout_constraintVertical_bias="0.18" />
<TextView
android:id="#+id/taskNameTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintStart_toStartOf="#+id/upCommingTasksTextViewUIMainActivity"
app:layout_constraintTop_toBottomOf="#+id/upCommingTasksTextViewUIMainActivity"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="#+id/dateTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintStart_toStartOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintTop_toTopOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/priorityTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toEndOf="#+id/dateTextViewUIMainActivity"
app:layout_constraintTop_toBottomOf="#+id/taskNameTextViewUIMainActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
/*
HW02
MainActivity.java
*/
package com.example.hw02;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
public class MainActivity extends AppCompatActivity {
//UI Vars
Button viewTaskButtonUIMain;
Button createTaskButtonUIMain;
TextView amountOfTasksTextViewUIMain;
TextView taskNameTextViewUIMainActivity;
TextView dateTextViewUIMainActivity;
TextView priorityTextViewUIMainActivity;
//Java Vars
ArrayList<Tasks> taskArrayList;
ArrayList<String> taskNameList;
int selectedTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//UI Vars inst
viewTaskButtonUIMain = findViewById(R.id.viewTaskButtonUIMain);
createTaskButtonUIMain = findViewById(R.id.createTaskButtonUIMain);
amountOfTasksTextViewUIMain = findViewById(R.id.amountOfTasksTextViewUIMain);
dateTextViewUIMainActivity = findViewById(R.id.dateTextViewUIMainActivity);
priorityTextViewUIMainActivity = findViewById(R.id.priorityTextViewUIMainActivity);
taskNameTextViewUIMainActivity = findViewById(R.id.taskNameTextViewUIMainActivity);
//Java Vars inst
taskArrayList = new ArrayList<Tasks>();
//create Tasks
createTaskButtonUIMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sendArray1 = new Intent(MainActivity.this,createTask.class );
sendArray1.putExtra("reviewActivityArray",taskArrayList);
startActivity(sendArray1);
}
});
viewTaskButtonUIMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] taskNames = new String[taskArrayList.size()];
for(int i = 0; i < taskNames.length; i++) {
taskNames[i] = taskArrayList.get(i).getTaskName();
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select Task")
.setItems(taskNames, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent taskDelete = new Intent(MainActivity.this, reviewActivity.class );
taskDelete.putExtra("catch1", taskArrayList);
taskDelete.putExtra("catch2",which);
startActivity(taskDelete);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
});
amountOfTasksTextViewUIMain.setText("You have "+ taskArrayList.size() + " tasks");
//This is for the Create Task
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("catch")){
taskArrayList = (ArrayList<Tasks>)getIntent().getSerializableExtra("catch");
int vartempsize = taskArrayList.size();//delete
amountOfTasksTextViewUIMain.setText("You have "+ taskArrayList.size() + " tasks");
Collections.sort(taskArrayList);
//if the arrayList isn't empty
if(taskArrayList.isEmpty()==false){
//change the text
String tempName = taskArrayList.get(0).getTaskName();
taskNameTextViewUIMainActivity.setText(tempName);
dateTextViewUIMainActivity.setText(taskArrayList.get(0).returnDate().toString());
priorityTextViewUIMainActivity.setText(taskArrayList.get(0).getPrioty());
}
}
//comming from review
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("dead")){
taskArrayList = (ArrayList<Tasks>) getIntent().getSerializableExtra("dead");
Collections.sort(taskArrayList);
}
}
private void sortLists(){
//sorts list
Collections.sort(taskArrayList);
//organize the names
taskNameList.clear();
//adds all the names in the same order as the taskArrayList
for (Tasks var : taskArrayList){
taskNameList.add(var.getTaskName());
}
}
}
<?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=".reviewActivity">
<TextView
android:id="#+id/TaskInformationTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/taskInfromationTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.13"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06999999" />
<TextView
android:id="#+id/nameTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nameTextViewUICreateTextString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/TaskInformationTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/TaskInformationTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="#+id/dateTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dateTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.050000012" />
<TextView
android:id="#+id/priorityTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/priorityTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.07999998" />
<TextView
android:id="#+id/nameTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/nameTextViewUIactivityReview" />
<TextView
android:id="#+id/dateTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintStart_toStartOf="#+id/nameTaskTextViewUIactivityReview" />
<TextView
android:id="#+id/priorityTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintStart_toStartOf="#+id/dateTaskTextViewUIactivityReview" />
<Button
android:id="#+id/cancelButtonUIactivityReview"
android:layout_width="114dp"
android:layout_height="54dp"
android:text="#string/cancleButtonUICreateTaskString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.09"
app:layout_constraintStart_toStartOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.16000003" />
<Button
android:id="#+id/deleteButtonUIactivityReview"
android:layout_width="106dp"
android:layout_height="49dp"
android:text="#string/deleteButtonUIactivityReviewString"
app:layout_constraintBottom_toBottomOf="#+id/cancelButtonUIactivityReview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.58"
app:layout_constraintStart_toEndOf="#+id/cancelButtonUIactivityReview" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.hw02;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class reviewActivity extends AppCompatActivity {
ArrayList<Tasks> taskArrayList;
int plank;
Button deleteButtonUIactivityReview;
Button cancelButtonUIactivityReview;
TextView nameTaskTextViewUIactivityReview;
TextView dateTaskTextViewUIactivityReview;
TextView priorityTaskTextViewUIactivityReview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_review);
deleteButtonUIactivityReview = findViewById(R.id.deleteButtonUIactivityReview);
cancelButtonUIactivityReview = findViewById(R.id.cancelButtonUIactivityReview);
deleteButtonUIactivityReview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
taskArrayList.remove(plank);
Intent goHomeDelete = new Intent(reviewActivity.this,MainActivity.class);
goHomeDelete.putExtra("dead",taskArrayList);
startActivity(goHomeDelete);
}
});
cancelButtonUIactivityReview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent goHomeSafe = new Intent(reviewActivity.this,MainActivity.class);
startActivity(goHomeSafe);
}
});
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("catch1")){
taskArrayList = (ArrayList<Tasks>) getIntent().getSerializableExtra("catch1");
plank = (int)getIntent().getSerializableExtra("catch2");
}
nameTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).taskName);
dateTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).returnDate().toString());
priorityTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).getPrioty());
}
}

Related

My activity layout isn't showing when I run the app

I am currently building an Android app in Android Studio, written in Java, a symptom checker application where upon starting the application, user will be required to choose whether they want to open a patient account or a doctor account. For the patient account option, I managed to see the layout when I run the app but when I try to pick the doctor account option, the app does not show the layout. This is weird considering the fact that the code does not show any error, even the logcat too shows the process is perfectly fine.
Here is the activity class and the xml for the doctor account option, also with the main class. I am sorry if somehow the way I post this might be confusing, this is my first time asking a question here.
DoctorRegistrationActivity.java
`package com.example.jeusain;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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.TextView;
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.DatabaseReference;
public class DoctorRegistrationActivity extends AppCompatActivity {
private TextView goToLogin;
private Button doctorRegisterButton;
private EditText doctor_name, university, placeofpractice, medicalregistrationnumber, doctor_email, doctor_password;
//firebase initialisation
private FirebaseAuth mAuth;
private FirebaseAuth mUser;
private DatabaseReference reference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_registration);
doctorRegisterButton = findViewById(R.id.doctor_registerbutton_page1);
doctor_name = findViewById(R.id.doctor_fullname);
university = findViewById(R.id.graduated_uni);
placeofpractice = findViewById(R.id.place_of_practice);
medicalregistrationnumber = findViewById(R.id.registration_number);
doctor_email = findViewById(R.id.doctor_email);
doctor_password = findViewById(R.id.doctor_password);
goToLogin = findViewById(R.id.loginFromDoctor);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),DoctorProfileActivity.class));
finish();
}
doctorRegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
performValidations();
Intent goToLogin = new Intent(DoctorRegistrationActivity.this, LoginActivity.class);
startActivity(goToLogin);
}
});
goToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openLogin();
}
});
}
public void openLogin(){
Intent login = new Intent(DoctorRegistrationActivity.this, LoginActivity.class);
startActivity(login);
}
private void performValidations(){
String getDoctorname = doctor_name.getText().toString();
String getUniversity = university.getText().toString();
String getPlaceofPractice = placeofpractice.getText().toString();
String getMedicalregistrationnumber = medicalregistrationnumber.getText().toString();
String getDoctoremail = doctor_email.getText().toString();
String getDoctorpassword = doctor_password.getText().toString();
if (TextUtils.isEmpty(getDoctorname)){
doctor_name.setError("Name is required!");
return;
}
if (TextUtils.isEmpty(getUniversity)){
university.setError("University is required!");
return;
}
if (TextUtils.isEmpty(getPlaceofPractice)){
placeofpractice.setError("Place of Practice is required!");
return;
}
if (TextUtils.isEmpty(getMedicalregistrationnumber)){
medicalregistrationnumber.setError("Medical registration number is required!");
return;
}
if (TextUtils.isEmpty(getDoctoremail)){
doctor_email.setError("Email is required!");
return;
}
if (TextUtils.isEmpty(getDoctorpassword)){
doctor_password.setError("Password is required!");
return;
}
if (doctor_password.length()<8){
doctor_password.setError("Password must be 8 or more characters!");
return;
}
//register the user in firebase
mAuth.createUserWithEmailAndPassword(getDoctoremail, getDoctorpassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(DoctorRegistrationActivity.this, "User Created!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),DoctorProfileActivity.class));
}
else{
Toast.makeText(DoctorRegistrationActivity.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}`
activity_doctor_registration.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="#a0db8e"
tools:context=".DoctorRegistrationActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/oval_background_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title_DRP_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="You're a registered medical practitioner?"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/subtitle_DRP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="Please fill in the form with relevant information"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="#+id/title_DRP_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_DRP_1" />
<EditText
android:id="#+id/doctor_fullname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/subtitle_DRP1"
app:layout_constraintStart_toStartOf="#+id/subtitle_DRP1"
app:layout_constraintTop_toBottomOf="#+id/subtitle_DRP1" />
<EditText
android:id="#+id/graduated_uni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Graduated university"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/doctor_fullname"
app:layout_constraintStart_toStartOf="#+id/doctor_fullname"
app:layout_constraintTop_toBottomOf="#+id/doctor_fullname" />
<EditText
android:id="#+id/place_of_practice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Place of Practice"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/graduated_uni"
app:layout_constraintStart_toStartOf="#+id/graduated_uni"
app:layout_constraintTop_toBottomOf="#+id/graduated_uni" />
<EditText
android:id="#+id/registration_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Medical Registration Number"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/place_of_practice"
app:layout_constraintStart_toStartOf="#+id/place_of_practice"
app:layout_constraintTop_toBottomOf="#+id/place_of_practice" />
<EditText
android:id="#+id/doctor_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="E-mail address"
android:inputType="textEmailAddress"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/registration_number"
app:layout_constraintStart_toStartOf="#+id/registration_number"
app:layout_constraintTop_toBottomOf="#+id/registration_number" />
<EditText
android:id="#+id/doctor_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/doctor_email"
app:layout_constraintStart_toStartOf="#+id/doctor_email"
app:layout_constraintTop_toBottomOf="#+id/doctor_email" />
<Button
android:id="#+id/doctor_registerbutton_page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="REGISTER"
app:layout_constraintEnd_toEndOf="#+id/doctor_password"
app:layout_constraintStart_toStartOf="#+id/doctor_password"
app:layout_constraintTop_toBottomOf="#+id/doctor_password" />
<TextView
android:id="#+id/loginFromDoctor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login here"
tools:layout_editor_absoluteX="280dp"
tools:layout_editor_absoluteY="597dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
`
MainActivity.java
`package com.example.jeusain;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button choose_patient_account, choose_doctor_account;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
choose_patient_account = (Button) findViewById(R.id.patient_reg);
choose_doctor_account = (Button) findViewById(R.id.doctor_reg);
choose_patient_account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openPatientRegister();
}
});
choose_doctor_account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDoctorRegister();
}
});
}
public void openPatientRegister() {
Intent goToPatientRegister = new Intent(getApplicationContext(), PatientRegistrationActivity.class);
startActivity(goToPatientRegister);
}
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(getApplicationContext(), DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
}`
Use:
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(this, DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
Instead of
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(getApplicationContext(), DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
Kristy Welsh's answer doesn't solve your problem then try specifying Activity name instead of "this"
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(MainActivity.this,DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}

Error in mainactivity java file before that these are separate activities but when i connect two activities i got error

1.Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
2.First I added my two activities then I got this error
Below is XML code:-
Exact what line i got this error please give the solution with code.
<RelativeLayout 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"
tools:context=".MainActivity2">
<TextView
android:id="#+id/textview"
android:layout_width="346dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Text to Speech"
android:textColor="#F32269"
android:textSize="28dp"
android:textStyle="bold" />
<SeekBar
android:id="#+id/seek_bar_speed"
android:layout_width="389dp"
android:layout_height="28dp"
android:layout_below="#id/textview"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:layout_marginEnd="4dp"
android:progress="50" />
<TextView
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_alignBottom="#id/textview"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="1dp"
android:layout_marginTop="41dp"
android:layout_marginBottom="-29dp"
android:gravity="center"
android:text="Speed"
android:textColor="#color/black"
android:textSize="20dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="363dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_marginStart="0dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="2dp">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" Enter Text "
android:textColor="#000000"
android:textColorHint="#51FA2B"
android:textSize="20dp"
tools:layout_conversion_absoluteHeight="45dp"
tools:layout_conversion_absoluteWidth="89dp"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="179dp" />
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="418dp"
android:layout_height="71dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginTop="74dp"
android:layout_marginEnd="-7dp"
android:layout_marginBottom="134dp"
android:text="Text to speech"
android:textSize="18dp"
android:textStyle="bold"/>
</RelativeLayout>
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.SeekBar;
public class MainActivity2 extends AppCompatActivity {
TextView textView;
private SeekBar mseekbar;
TextToSpeech t1;
EditText ed1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.editText);
b1 = findViewById(R.id.button);
textView = findViewById(R.id.text);
mseekbar = findViewById(R.id.seek_bar_speed);
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.US);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
float speed = (float) mseekbar.getProgress() / 50;
t1.setSpeechRate(speed);
Toast.makeText(MainActivity2.this.getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause() {
if (t1 != null) {
t1.stop();
}
super.onPause();
}
}```

Dynamically Remove EditText Field in LinearLayout

My problem is that once my editText fields are created, I don't know how to remove them one by one with the other button in my app. I don't really understand the android:id element as it relates to setID() in my java code. I guess my main question is, how do I figure out how to target dynamically created editText views with their id's if I don't know their id's. Here is my code, any help is really appreciated:
Java:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="#+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="+"
android:textColor="#android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="#+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="-"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="DECIDE!"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="#android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
There is no relationship between android:id and your setID.
Check this answer to unsderstand more about that.
Anyway, one approach to achieve to remove a generated line could be based on the children index of your LinearLayout like this:
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}

What should i do to move to another activity when only special conditions are met

I am making a quiz app. I want the exit page to be displayed when the next button is pressed after last question has been answered. I have a activity_questions.xml and a activity_exit.xml. The xml for activity_questions.xml is :
<?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"
tools:context="com.example.android.crystal.questions">
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-thin"
android:padding="10dp"
android:text=""
android:textColor="#440027"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text3"
android:layout_margin="30dp"
android:background="#9775AA"
android:hint="Answer"
android:inputType="text"
android:padding="10dp"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edit2"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/answer"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onAnswerClick"
android:padding="2dp"
android:text="Okay" />
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onHintClick"
android:padding="2dp"
android:text="Hint" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="5dp"
android:orientation="vertical"
android:layout_marginTop="26dp"
android:layout_below="#+id/ll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/tickcross"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/ll"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/wierdtick" />
<TextView
android:id="#+id/correctornot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tickcross"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Correct!"
android:textColor="#440027"
android:textSize="30dp" />
<Button
android:id="#+id/nextbutton"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next"
android:onClick="onNextClick"
/>
</LinearLayout>
</RelativeLayout>
And the java is:
package com.example.android.crystal;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class questions extends AppCompatActivity {
Random r = new Random();
private boolean done;
private int QuestionNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
String[] questions = getResources().getStringArray(R.array.Questions);
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
}
public void onFinishClick(View view){
Intent intent = new Intent(this, exit.class);
startActivity(intent);
}
public static void hideKeyboardFrom(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public void answersubmitted() {
findViewById(R.id.tickcross).setVisibility(View.VISIBLE);
TranslateAnimation animation = new TranslateAnimation(0, 0, 2000, 0);
animation.setDuration(1000);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
findViewById(R.id.tickcross).startAnimation(animation);
findViewById(R.id.correctornot).setVisibility(View.VISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.VISIBLE);
}
public void onHintClick(View view) {
String[] hints = getResources().getStringArray(R.array.Hints);
Toast toasty = Toast.makeText(getApplicationContext(), hints[QuestionNo], Toast.LENGTH_SHORT);
toasty.show();
}
public void onAnswerClick(View view) {
if (done == false) {
hideKeyboardFrom(this);
String answer = ((EditText) findViewById(R.id.edit2)).getText().toString();
String[] answers = getResources().getStringArray(R.array.Answers);
String correctAnswer = answers[QuestionNo];
correctAnswer = correctAnswer.toUpperCase();
answer = answer.toUpperCase();
if (answer.equals(correctAnswer)) {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("CORRECT!");
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.wierdtick));
answersubmitted();
} else {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("Correct Answer: " + correctAnswer);
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.weirdcross));
answersubmitted();
}
done = true;
}
}
public void onNextClick(View view){
if (done){
String[] questions = getResources().getStringArray(R.array.Questions);
if (QuestionNo < (questions.length - 1)) {
QuestionNo += 1;
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
EditText et = (EditText) findViewById(R.id.edit2);
et.setText("");
done = false;
}
else {
onFinishClick();
}
}
}
}
But the onFinishClick() gives an error
Thanks in advance for your help
What should i do to move to another activity when only special conditions are met
Basically, an if-statement and an Intent.
Raw code with no variables added:
if(condition(s)){
Intent i = new Intent(this, TargetActivity.class);
//Put arguments into the intent if you need them...
startActivity(i);
}
You can put it in a thread, in an onClickListener, anywhere and any way you want to execute the code.
As to the error you are receiving, I cannot help you with that until you supply the stacktrace from Logcat
You can do like this:
mNextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mQuestionCount < questionList.size()){
// set next question
} else {
// call finish
}
}
});

How to do enable a button and change its colour when textfield has some value?

I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?
I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.
I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.
package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});
login_activation_code.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}
#Override
public void afterTextChanged(Editable s) {
}
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);
}
}
});
login_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
The XML file is below
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
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="io.kaapi.kaapimobileassistant.Activities.LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="#drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:src="#drawable/ic_info_outline_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="#style/Widget.AppCompat.Button"
android:text="Activate"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />
<LinearLayout
android:id="#+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setClickable(false);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
} else {
login_button.setClickable(true);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
}
}

Categories

Resources