In my main page I have 3 different buttons.
Login (loginB)
Create a new account (createB)
Terms of conduct (termsB)
So far it has worked just fine, but for some reason I have now faced a problem with the create new account button. It did go to the right page before but now it goes to a wrong page. It should be going to the CreateAccount -page but it is going to the Menu -page.
I have the java code in the MainActivity were the buttons are here:
public class MainActivity extends AppCompatActivity {
Button loginB;
Button termsB;
Button createB;
EditText emailL,passwordL;
FirebaseAuth fAuth;
ProgressBar progressB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createB = findViewById(R.id.createB);
createB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openCreateAccount();
}
});
termsB = findViewById(R.id.termsB);
termsB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openTermsOfConduct();
}
});
emailL = findViewById(R.id.emailL);
passwordL = findViewById(R.id.passwordL);
fAuth = FirebaseAuth.getInstance();
loginB = findViewById(R.id.loginB);
progressB = findViewById(R.id.progressB);
loginB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailL.getText().toString().trim();
String password = passwordL.getText().toString().trim();
if(TextUtils.isEmpty(email)){
emailL.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(password)){
passwordL.setError("Password is required.");
return;
}
if(password.length() <6){
passwordL.setError("Password must be at least 6 characters.");
}
progressB.setVisibility(View.VISIBLE);
//authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Menu.class));
}
else{
Toast.makeText(MainActivity.this, "Error " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressB.setVisibility(View.INVISIBLE);
}
}
});
}
});
}
public void openCreateAccount(){
Intent intent = new Intent(this, CreateAccount.class);
startActivity(intent);
}
public void openTermsOfConduct(){
Intent intent = new Intent(this, TermsOfConduct.class);
startActivity(intent);
}
}
Main Activity 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:orientation="vertical"
tools:context=".MainActivity"
android:background="#drawable/tausta">
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:fontFamily="sans-serif"
android:paddingVertical="30dp"
android:text="Login"
android:textAlignment="center"
android:textColor="#000"
android:textSize="90sp"
android:layout_marginBottom="25dp"
/>
<EditText
android:id="#+id/emailL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="30dp"
android:textColor="#fff"
android:textSize="25sp"
android:hint="Email"
android:textColorHint="#fff"
/>
<EditText
android:id="#+id/passwordL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:inputType="textPassword"
android:layout_marginBottom="30dp"
android:textColor="#fff"
android:textSize="25sp"
android:hint="Password"
android:textColorHint="#fff"
/>
<Button
android:id="#+id/loginB"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Login"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="25dp"
android:background="#drawable/button_bg"
/>
<Button
android:id="#+id/createB"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Create a new account"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="25dp"
android:background="#drawable/button_bg"
/>
<Button
android:id="#+id/termsB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Terms and conditions"
android:layout_marginHorizontal="30dp"
android:textColor="#0070ff"
android:textAlignment="center"
android:background="#android:color/transparent"
/>
<ProgressBar
android:id="#+id/progressB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
</LinearLayout>
CreateAccount java:
public class CreateAccount extends AppCompatActivity {
Button registerB;
EditText nameR,emailR,pnumberR,passwordR;
FirebaseAuth fAuth;
ProgressBar progressB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account2);
nameR = findViewById(R.id.nameR);
emailR = findViewById(R.id.emailR);
pnumberR = findViewById(R.id.pnumberR);
passwordR = findViewById(R.id.passwordR);
progressB = findViewById(R.id.progressB);
fAuth = FirebaseAuth.getInstance();
//check if user registered
if(fAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),Menu.class));
finish();
}
registerB = findViewById(R.id.registerB);
registerB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailR.getText().toString().trim();
String password = passwordR.getText().toString().trim();
if(TextUtils.isEmpty(email)){
emailR.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(password)){
passwordR.setError("Password is required.");
return;
}
if(password.length() <6){
passwordR.setError("Password must be ata least 6 characters.");
return;
}
progressB.setVisibility(View.VISIBLE);
//register the user to FireBase
fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(CreateAccount.this, "User created.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Menu.class));
}
else{
Toast.makeText(CreateAccount.this, "Error " +task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
CreateAccount 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:orientation="vertical"
tools:context=".CreateAccount"
android:background="#drawable/tausta"
>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:fontFamily="sans-serif"
android:paddingVertical="20dp"
android:text="Create new account"
android:textAlignment="center"
android:textColor="#000"
android:textSize="40sp"
android:layout_marginBottom="25dp"
/>
<EditText
android:id="#+id/nameR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full name"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="#+id/emailR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="#+id/pnumberR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone number"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="#+id/passwordR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>
<Button
android:id="#+id/registerB"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Register"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="25dp"
android:background="#drawable/button_bg"
/>
<ProgressBar
android:id="#+id/progressB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
</LinearLayout>
I guess you should definitely check(R.layout.activity_create_account2)and whether it is the correct xml for the CreateAccount.java
P.S. While posting questions, always mention the correct name of the xml too.
The following code might be the cause:
if(fAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),Menu.class));
finish();
}
Can you check if it's going into the if block?
Related
So basically I have attached my addimage.java code and mainpage.xml code below. whenever the user will click on addimage button he can select image and dynamically add images in my mainpage.xml file. So basically its like I want to transfer I image from activity to activity on a button click.
I tried doing that still I am stuck up a this specific point, where the user can selects image from gallery but still I don't know how will it get uploaded to mainpage.xml file.
Any help is highly appreciable.
this is my addimage.java code
public class addimage extends AppCompatActivity {
private ImageView imageView;
private Button addimage;
private ProgressBar progressBar;
android.content.Intent data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addimage);
imageView = (ImageView) findViewById(R.id.imageView);
addimage = (Button) findViewById(R.id.addimage);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1);
}
});
addimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (imageView != null) {
/*uploadToFirebase(imageView);*/
} else {
Toast.makeText(getApplicationContext(), "PLEASE SELECT IMAGE", Toast.LENGTH_SHORT).show();
}
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/* super.onActivityResult(requestCode, resultCode, data);*/
switch (requestCode) {
case 2://2 is from startActivityForResult(galleryIntent, 2);
try {
Uri selectedImage = data.getData(); //get photo uri
imageView.setImageURI(selectedImage);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
}
this is my mainpage.xml code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:id="#+id/mainpage"
android:layout_height="match_parent"
tools:context=".mainpage">
<LinearLayout
android:orientation="vertical"
android:background="#color/Pink"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<TextView
android:layout_width="950dp"
android:layout_height="550dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="5dp"
android:layout_marginBottom="0dp"
android:alpha="0.5"
android:background="#color/purple_200"
android:text=" BEGINNERS CALLIGRAPHY"
android:textAllCaps="false"
android:textColor="#color/DarkBlue"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="210dp"
android:layout_marginBottom="7dp"
android:text="PHOTOS"
android:textSize="20dp"
android:background="#color/white"
android:textColor="#color/purple_700"/>
<Button
android:id="#+id/button1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="52dp"
android:layout_marginBottom="7dp"
android:text="VIDEOS"
android:textSize="20dp"
android:background="#color/white"
android:textColor="#color/purple_700"/>
</RelativeLayout>
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="949dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
android:background="#color/Purple">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview_image"
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center_horizontal"
android:src="#drawable/b1" />
</LinearLayout>
</androidx.cardview.widget.CardView/>
<!-- Row 2 -->
<!-- Column 1 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview_image2"
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b3" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Column 2 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview_image3"
android:layout_width="190dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b4" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Row 2 -->
<!-- Column 1 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview_image4"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b5" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<ImageView
android:id="#+id/expanded_image"
android:layout_marginRight="30dp"
android:layout_width="350dp"
android:layout_height="900dp"
android:visibility="invisible"
android:layout_gravity="center"/>
<!--android:layout_gravity="center"-->
</LinearLayout>
</ScrollView>
If more code is required please comment below.
try to add code to this addimage class:
public class addimage extends AppCompatActivity {
private ImageView imageView;
private Button addimage;
private ProgressBar progressBar;
android.content.Intent data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addimage);
imageView = (ImageView) findViewById(R.id.imageView);
addimage = (Button) findViewById(R.id.addimage);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1);
}
});
addimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (imageView != null) {
/*uploadToFirebase(imageView);*/
} else {
Toast.makeText(getApplicationContext(), "PLEASE SELECT IMAGE", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1://1 is from startActivityForResult(galleryIntent, 2);
try {
Uri selectedImage = data.getData(); //get photo uri
imageView.setImageURI(selectedImage);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
So....i am trying to make this button change from main activity to Disciplinas_Activity. But whenever I try to run the app and click the button, the app crashes.
Keep in mind i have other 3 button completly identical ,(other then the fact that they direct to different activities), in the same activity and they all work with the same base code.
Here is the .xml code for Main Activity
<TextView
android:id="#+id/textView_emailnotverified"
android:layout_width="172dp"
android:layout_height="34dp"
android:background="#color/white"
android:text="Email não Verificado!"
android:textColor="#E41F1F"
android:textSize="18dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.066"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.516" />
<TextView
android:id="#+id/textView_studentEmail"
android:layout_width="237dp"
android:layout_height="40dp"
android:text="Email do aluno"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.856"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.395" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="332dp"
android:layout_height="168dp"
android:scaleX="2"
android:scaleY="1.3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.028"
app:srcCompat="#drawable/etpr" />
<TextView
android:id="#+id/welcommingtextview5"
android:layout_width="337dp"
android:layout_height="39dp"
android:text="ESCOLA TÉCNICA E PROFISSIONAL DO RIBATEJO"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.459"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15" />
<TextView
android:id="#+id/ETPRTitle5"
android:layout_width="258dp"
android:layout_height="30dp"
android:text="BEM-VINDO À ETPR"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.105" />
<Button
android:id="#+id/Button_LOGOUT"
android:layout_width="114dp"
android:layout_height="59dp"
android:text="Logout"
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.934" />
<Button
android:id="#+id/button_Testes"
android:layout_width="108dp"
android:layout_height="47dp"
android:text="Testes"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.867"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.644" />
<Button
android:id="#+id/button_pdf"
android:layout_width="121dp"
android:layout_height="41dp"
android:text="PDF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.894"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.753" />
<Button
android:id="#+id/button_historia"
android:layout_width="191dp"
android:layout_height="47dp"
android:text="Sobre o criador"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.149"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.76" />
<ImageView
android:id="#+id/imageView_perfil"
android:layout_width="122dp"
android:layout_height="118dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.055"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.376"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView_studentname"
android:layout_width="236dp"
android:layout_height="33dp"
android:text="Nome do aluno"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.857"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.329" />
<Button
android:id="#+id/button_emailverification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verificar agora"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.88"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.527" />
<Button
android:id="#+id/changeprofileBTN"
android:layout_width="245dp"
android:layout_height="41dp"
android:text="Mude a imagem de perfil"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.897"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.453" />
<Button
android:id="#+id/button_disciplinas"
android:layout_width="194dp"
android:layout_height="55dp"
android:text="Disciplinas"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.106"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.644" />
Here is my .java code for main activity
public class MainActivity extends AppCompatActivity {
TextView fullname, email, verifymessage;
FirebaseAuth fAuth;
FirebaseFirestore fstore;
String userID;
Button resendVerification, LogoutBTN, changeprofileBTN, tests, createrInfo, pdfdatabase, buttonDisciplinas;
ImageView profileImage;
StorageReference storageReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullname = findViewById(R.id.textView_studentname);
email = findViewById(R.id.textView_studentEmail);
LogoutBTN = (Button) findViewById(R.id.Button_LOGOUT);
tests =(Button) findViewById(R.id.button_Testes);
createrInfo =(Button) findViewById(R.id.button_historia);
pdfdatabase =(Button) findViewById(R.id.button_pdf);
buttonDisciplinas = findViewById(R.id.button_disciplinas);
buttonDisciplinas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Disciplinas_Activity.class));
}
});
tests.setOnClickListener(new View.OnClickListener() {// menu dos testes
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),Testes_Activity.class));
}
});
createrInfo.setOnClickListener(new View.OnClickListener() {//página sobre a história da app
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),AboutMe_Activity.class));
}
});
pdfdatabase.setOnClickListener(new View.OnClickListener() {//pdf database
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),PDF_Activity.class));
}
});
LogoutBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginScreen.class));
finish();
}
});
profileImage = findViewById(R.id.imageView_perfil);
changeprofileBTN = findViewById(R.id.changeprofileBTN);
fAuth = FirebaseAuth.getInstance();
fstore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
StorageReference profileRef = storageReference.child("utilizadores/"+fAuth.getCurrentUser().getUid()+"/perfil.jpg");
profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(profileImage);
}
});
resendVerification = findViewById(R.id.button_emailverification);
verifymessage = findViewById(R.id.textView_emailnotverified);
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fstore.collection("utilizadores").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot documentSnapshot, #Nullable FirebaseFirestoreException error) {
fullname.setText(documentSnapshot.getString("NomeCompleto"));
email.setText(documentSnapshot.getString("Email"));
}
});
FirebaseUser user = fAuth.getCurrentUser();
if (!user.isEmailVerified()){
verifymessage.setVisibility(View.VISIBLE);
resendVerification.setVisibility(View.VISIBLE);
resendVerification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(v.getContext(),"Email de verificação enviado.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d("tag","Erro: Email de verificação não enviado " + e.getMessage());
}
});
}
});
}
changeprofileBTN.setOnClickListener(new View.OnClickListener() { //mudar imagem de perfil
#Override
public void onClick(View v) {
Intent openGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGalleryIntent,1000);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 ){
if (resultCode == Activity.RESULT_OK){//se o resultado for igual então abre galeria
Uri imageUri = data.getData();
//profileImage.setImageURI(imageUri); //insere imagem escolhida na galeria
uploadImageToFirebase(imageUri);
}
}
}
private void uploadImageToFirebase(Uri imageUri) { //upload da imagem para base de dados
StorageReference fileReference = storageReference.child("utilizadores/"+fAuth.getCurrentUser().getUid()+"/perfil.jpg");
fileReference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(profileImage);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this,"Erro", Toast.LENGTH_SHORT).show();
}
});
}
}
Here is .xml code for DisciplinasActivity
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/Relative_Layout_Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="32dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/Menu_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disciplinas"
android:textColor="#color/white"
android:textSize="22sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Menu_Title"
android:layout_marginTop="6dp"
android:text="ETPR"
android:textColor="#color/white"
android:textSize="14sp">
</TextView>
<TextView
android:id="#+id/Name_Data"
android:textColor="#color/white"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/user_icon"
android:text="Nome">
</TextView>
<TextView
android:id="#+id/Email_Data"
android:textColor="#color/white"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Name_Data"
android:layout_toStartOf="#id/user_icon"
android:text="Email">
</TextView>
<ImageView
android:id="#+id/user_icon"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_alignParentRight="true"
app:srcCompat="#drawable/cara">
</ImageView>
</RelativeLayout>
<GridLayout
android:id="#+id/MainGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#+id/Relative_Layout_Title"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="2">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutSdac"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/sdac_icon"/>
<TextView
android:layout_width="56dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="SDAC"
android:textColor="#color/black"
android:textSize="18sp">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutEletronica"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/eletronica_icon"
app:srcCompat="#drawable/eletronica_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="Eletrónica"
android:textColor="#color/black"
android:textSize="18sp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutCD"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/cd_icon"
app:srcCompat="#drawable/cd_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="CD"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutIMEI"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/imei_icon"
app:srcCompat="#drawable/imei_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="IMEI"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<Button
android:id="#+id/ExitButton"
android:layout_marginTop="20dp"
android:layout_width="121dp"
android:layout_height="69dp"
android:layout_gravity="center"
android:layout_marginBottom="#+id/RelativeLayout"
android:text="Sair"
android:textColor="#color/white"
android:textSize="16dp" />
</LinearLayout>
Here is my .Java code for DisciplinasActivity
public class Disciplinas_Activity extends AppCompatActivity {
GridLayout gridPrincipal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disciplinas);
gridPrincipal = (GridLayout)findViewById(R.id.MainGrid); //identifica qual a grelha
//ação
setSingleEvent(gridPrincipal);
}
private void setSingleEvent(GridLayout gridPrincipal) {
for (int i =0;i<gridPrincipal.getChildCount();i++)
{
CardView cardView = (CardView)gridPrincipal.getChildAt(i);
final int finalI = i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),TeacherLoginScreen.class));//muda para Disciplina de Sdac
}
});
}
}
}
What am i missing...? if all the other 3 buttons work...why doesnt this one work? (I am a total newbie but i
I really need the help)
Consider your code
button = (Button)findViewById(R.id.button1);
You do not need to explicitly cast widgets anymore unless it's a special case like a RadioButton from a resource ID.
Please check that the button you're clicking is actually the button you set all this up for.
Add
android:clickable="true";
android:focusable="true";
to your buttons in the XML layout.
I'm guessing ConstraintLayout is creating this problem, you shouldn't come across this issue if you use LinearLayout.
In your .xml code for DisciplinasActivity you have some mistakes on the dimensions that you are using:
<GridLayout
...
android:id="#+id/MainGrid"
...
android:layout_marginBottom="#+id/Relative_Layout_Title" <!-- E.G 20dp -->
>
<Button
android:id="#+id/ExitButton"
...
android:layout_marginBottom="#+id/RelativeLayout" <!-- E.G 20dp -->
... />
For margins, padding, etc you have to use a dimension, like dp,in,mm,etc not a reference to other view, or you could use dimmens.xml maybe you are confusing android:layout_marginBottom with android:layout_constraintBottom_toBottomOf or another property. Check for this in all the elements in your .xml
This answer is due to this log that you posted:
ComponentInfo{studying.app.tkappv6/studying.app.tkappv6.Disciplinas_Activity}:
java.lang.UnsupportedOperationException: Can't convert to dimension:
type=0x12
In the MainActivity
Change the to "v.getContext()" istead of "MainActivity.this"
tests.setOnClickListener(new View.OnClickListener() {// menu dos testes
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), Testes_Activity.class));
}
});
createrInfo.setOnClickListener(new View.OnClickListener() {//página sobre a história da app
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), AboutMe_Activity.class));
}
});
pdfdatabase.setOnClickListener(new View.OnClickListener() {//pdf database
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),PDF_Activity.class));
}
});
LogoutBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(v.getContext(),LoginScreen.class));
finish();
}
});
I was Looking into the code and i've never used this method, but it's strange for me so it's good to pay attention, I think is wrong to set a "Void", if the method is not going to receive any value, but since is a #Override method if it was that way from the beginning you don't need to change;
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(v.getContext(),"Email de verificação enviado.", Toast.LENGTH_SHORT).show();
}
Give a try and let's see if works!
I'm new to android studio. Currently I'm working on a medlabtut app on android for over 3 months now. I'm having an issue on the login button on the MedLabStartUpScreen that actually supposed to launch the login screen when clicked on. Rather it crashes the app completely and shows a runtimeException at my //Connection Hooks "progressbar = findViewById(R.id.login_progress_bar);" of my login.java class file, that android.widget.ProgressBar cannot be cast to android.widget.RelativeLayout. I need your help please
My Login layout activity
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".Common.LoginSignup.Login"
android:orientation="vertical"
android:background="#fff"
android:padding="25dp"
android:transitionName="transition_login">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/logo_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Welcome Back! Login Here"
android:textSize="40sp"
android:transitionName="logo_text"
android:fontFamily="#font/bungee"
android:textColor="#000"
android:layout_marginTop="20dp"/>
<TextView
android:id="#+id/slogan_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In to continue"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:fontFamily="#font/muli_black"
android:transitionName="logo_desc"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp">
<com.hbb20.CountryCodePicker
android:id="#+id/login_country_code_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ccp_autoDetectCountry="true"
app:ccp_showFlag="true"
app:ccp_showNameCode="true"
app:ccp_showFullName="true"
android:padding="5dp"
android:background="#drawable/black_border"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_below="#+id/login_country_code_picker"
android:id="#+id/login_phone_number"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:hint="#string/phone_number"
android:textColorHint="#color/Black"
app:boxStrokeColor="#color/Black"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="#color/Black"
app:hintTextColor="#color/Black"
app:startIconDrawable="#drawable/field_phone_number_icon"
app:startIconTint="#color/Black">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/login_phone_number_editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="phone" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_password"
android:layout_below="#+id/login_phone_number"
android:hint="#string/password"
app:startIconDrawable="#drawable/field_password_icon"
app:startIconTint="#color/Black"
android:transitionName="password_tran"
app:passwordToggleEnabled="true"
app:passwordToggleTint="#color/Black"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/login_password_editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/muli_bold"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout
android:id="#+id/forget_password_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/login_password"
android:layout_marginTop="10dp">
<CheckBox
android:id="#+id/remember_me"
style="#style/Widget.AppCompat.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/remember_me" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/forget_password"
android:background="#00000000"
android:layout_alignParentEnd="true"
android:onClick="callForgetPassword"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<Button
android:id="#+id/letTheUserLogin"
android:layout_below="#+id/forget_password_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="#000"
android:text="#string/login"
android:onClick="letTheUserLoggedIn"
android:textColor="#fff"
android:transitionName="button_tran" />
<RelativeLayout
android:id="#+id/login_sign_google_box"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp"
android:layout_margin="5dp"
android:background="#drawable/rounded_rectangle"
android:layout_below="#+id/letTheUserLogin">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/signin_with_google_icon"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIGN IN WITH GOOGLE"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:fontFamily="#font/muli_bold"
android:layout_margin="5dp"
android:textColor="#000"/>
<Button
android:id="#+id/google_signIn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent" />
</RelativeLayout>
<Button
android:id="#+id/signup_screen"
android:layout_below="#+id/login_sign_google_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="Create Account"
android:layout_centerHorizontal="true"
android:elevation="0dp"
android:layout_margin="5dp"
android:textColor="#000"
android:transitionName="login_signup_tran"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_centerInParent="true"
android:background="#drawable/white_circle"
android:elevation="10dp">
<ProgressBar
android:id="#+id/login_progress_bar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
<ImageView
android:id="#+id/social_fb"
android:layout_below="#+id/signup_screen"
android:layout_width="50dp"
android:layout_height="45dp"
android:src="#drawable/social_facebook_icon"
android:layout_marginLeft="130dp" />
<ImageView
android:id="#+id/social_tw"
android:layout_below="#id/signup_screen"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="150dp"
android:src="#drawable/social_twitter_icon" />
<ImageView
android:id="#+id/social_wh"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_below="#id/signup_screen"
android:layout_marginLeft="250dp"
android:src="#drawable/social_whatsapp_icon" />
</RelativeLayout>
</LinearLayout>
And the code for login.java class file
public class Login extends AppCompatActivity {
CountryCodePicker countryCodePicker;
Button callSignUp, login_btn;
TextView logoText, sloganText;
TextInputLayout username, phoneNumber, password;
RelativeLayout progressbar;
CheckBox rememberMe;
EditText phoneNumberEditText, passwordEditText;
private GoogleSignInClient mGoogleSignInClient;
private final static int RC_SIGN_IN = 123; // request code
Button verify;
private FirebaseAuth mAuth;
#Override
protected void onStart() {
super.onStart();
FirebaseUser user = mAuth.getCurrentUser();
if (user!=null){
Intent intent = new Intent(getApplicationContext(), MedLabDashboard.class);
startActivity(intent);
}
}
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
createRequest();
//when the user clicks the google button,call the signIn method
findViewById(R.id.google_signIn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
signIn();
}
});
//Connection Hooks
countryCodePicker = findViewById(R.id.country_code_picker);
phoneNumber = findViewById(R.id.login_phone_number);
progressbar = findViewById(R.id.login_progress_bar);
callSignUp = findViewById(R.id.signup_screen);
image = findViewById(R.id.logo_image);
logoText = findViewById(R.id.logo_name);
sloganText = findViewById(R.id.slogan_name);
password = findViewById(R.id.login_password);
login_btn = findViewById(R.id.Login_btn);
rememberMe = findViewById(R.id.remember_me);
phoneNumberEditText = findViewById(R.id.login_phone_number_editText);
passwordEditText = findViewById(R.id.login_password_editText);
//Check whether phone number and password is already saved in Shared Preference or not
SessionManager sessionManager = new SessionManager(Login.this, SessionManager.SESSION_REMEMBERME);
if (sessionManager.checkRemeberMe()){
HashMap<String,String> rememberMeDetails = sessionManager.getRememberMeDetailFromSession();
phoneNumberEditText.setText(rememberMeDetails.get(SessionManager.KEY_SESSIONPHONENUMBER));
passwordEditText.setText(rememberMeDetails.get(SessionManager.KEY_SESSIONPASSWORD));
}
callSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Login.this, SignUp.class);
Pair[] pairs = new Pair[7];
pairs[0] = new Pair<View, String>(image, "logo_image");
pairs[1] = new Pair<View, String>(logoText, "logo_name");
pairs[2] = new Pair<View, String>(sloganText, "logo_desc");
pairs[3] = new Pair<View, String>(username, "username_tran");
pairs[4] = new Pair<View, String>(password, "password_tran");
pairs[5] = new Pair<View, String>(login_btn, "button_tran");
pairs[6] = new Pair<View, String>(callSignUp, "login_signup_tran");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
try {
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
//Setting Auto Google Sign In request
private void createRequest() {
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
//SignIn with google method that'll pop-up user google accounts (Intent)
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
//Google Sign in was successful, authenticate with firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e){
//Sign in failed, update UI appropriately
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
Intent intent = new Intent(getApplicationContext(), MedLabDashboard.class);
startActivity(intent);
} else {
Toast.makeText(Login.this, "Sorry authentication failed", Toast.LENGTH_SHORT).show();
}
}
});
}
//login the user in app
public void letTheUserLoggedIn(View view) {
//Check the internet connection
CheckInternet checkInternet = new CheckInternet();
if (!isConnected(Login.this)) {
showCustomDialog();
}
//validate user and password
if (!validateFields()) {
return;
}
progressbar.setVisibility(View.VISIBLE);
//get value from fields
String _phoneNumber = phoneNumber.getEditText().getText().toString().trim();
final String _password = password.getEditText().getText().toString().trim();
if (_phoneNumber.charAt(0) == '0') { //if the user uses proceeding zero (0)
_phoneNumber = _phoneNumber.substring(1);
}
final String _completePhoneNumber = "+" + countryCodePicker.getFullNumber() + _phoneNumber;
//Remember me checkbox
if (rememberMe.isChecked()){
SessionManager sessionManager = new SessionManager(Login.this,SessionManager.SESSION_REMEMBERME);
sessionManager.createRememberMeSession(_phoneNumber, _password);
}
//database query if user exist or not
Query checkUser = FirebaseDatabase.getInstance().getReference("Users").orderByChild("phoneNo").equalTo(_completePhoneNumber);
checkUser.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) { //if the data has arrived or exists
phoneNumber.setError(null); //if there's any error on the phoneNumber, remove
phoneNumber.setErrorEnabled(false); //if there is error space, set to false
String systemPassword = dataSnapshot.child(_completePhoneNumber).child("password").getValue(String.class);
//if password exist and matches with users password, then get other fields from firebase database
if (systemPassword.equals(_password)) {
password.setError(null); //if there's any error on the password, remove
password.setErrorEnabled(false); //if there is error space, set to false
String _fullname = dataSnapshot.child(_completePhoneNumber).child("fullName").getValue(String.class);
String _username = dataSnapshot.child(_completePhoneNumber).child("username").getValue(String.class);
String _email = dataSnapshot.child(_completePhoneNumber).child("email").getValue(String.class);
String _phoneNo = dataSnapshot.child(_completePhoneNumber).child("phoneNo").getValue(String.class);
String _password = dataSnapshot.child(_completePhoneNumber).child("password").getValue(String.class);
String _dateOfBirth = dataSnapshot.child(_completePhoneNumber).child("date").getValue(String.class);
String _gender = dataSnapshot.child(_completePhoneNumber).child("gender").getValue(String.class);
//Create a session
SessionManager sessionManager = new SessionManager(Login.this, SessionManager.SESSION_USERSESSION);
sessionManager.createLoginSession(_fullname, _username, _email, _phoneNo, _password, _dateOfBirth, _gender);
startActivity(new Intent(getApplicationContext(), MedLabDashboard.class));
Toast.makeText(Login.this, _fullname + "\n" + _email + "\n" + _phoneNo + "\n" + _dateOfBirth, Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
} else {
progressbar.setVisibility(View.GONE);
Toast.makeText(Login.this, "Password does not match!", Toast.LENGTH_SHORT).show();
}
} else {
progressbar.setVisibility(View.GONE);
Toast.makeText(Login.this, "No such user exist", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
progressbar.setVisibility(View.GONE);
Toast.makeText(Login.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
//check internet connection
private boolean isConnected(Login login) {
ConnectivityManager connectivityManager = (ConnectivityManager) login.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiConn = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileConn = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifiConn != null && wifiConn.isConnected()) || (mobileConn != null && mobileConn.isConnected())) {
return true;
} else {
return false;
}
}
private void showCustomDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setMessage("Please connect to the internet to continue");
builder.setCancelable(false)
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(getApplicationContext(), MedLabStartUpScreen.class));
finish();
}
});
}
private boolean validateFields() {
String _phoneNumber = phoneNumber.getEditText().getText().toString().trim();
String _password = password.getEditText().getText().toString().trim();
if (_phoneNumber.isEmpty()) {
phoneNumber.setError("Phone number cannot be empty");
phoneNumber.requestFocus();
return false;
} else if (_password.isEmpty()) {
password.setError("Password cannot be empty");
password.requestFocus();
return false;
} else {
password.setError(null);
password.setErrorEnabled(false);
return true;
}
}
public void callForgetPassword(View view){
startActivity(new Intent(getApplicationContext(), ForgetPassword.class));
}
#Override
public void onBackPressed() {
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
finish();
}}
MedLabStartUpScreen Activity screen
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="#color/White"
android:padding="30dp"
tools:context=".Common.LoginSignup.MedLabStartUpScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="#drawable/splash_screen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="120dp"
android:fontFamily="#font/muli_black"
android:text="#string/medlab_heading"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/Black"
android:textSize="36sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="#font/muli_light"
android:text="#string/medlab_tab_line"
android:textAlignment="center"
android:textColor="#color/Black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp">
<Button
android:id="#+id/login_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:onClick="callLoginScreen"
android:layout_weight="1"
android:background="#color/colorPrimaryDark"
android:text="#string/login"
android:textColor="#color/Black"
tools:ignore="ButtonStyle"
android:transitionName="transition_login"/>
<Button
android:id="#+id/signup_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#color/colorPrimaryDark"
android:text="#string/sign_up"
android:textColor="#color/Black" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/how_we_work"
android:textColor="#color/Black"
android:layout_marginTop="20dp"
android:background="#00000000"/>
</LinearLayout>
</ScrollView>
MedLabStartUPScreen.java class that has the login button
public class MedLabStartUpScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_med_lab_start_up_screen);
}
public void callLoginScreen(View view){
Intent intent = new Intent(getApplicationContext(), Login.class);
Pair[] pairs = new Pair[1];
pairs[0] = new Pair<View,String>(findViewById(R.id.login_btn), "transition_login");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MedLabStartUpScreen.this, pairs);
startActivity(intent, options.toBundle());
}
else {
startActivity(intent);
}
}
}
On around line 7 you declare this variable:
RelativeLayout progressbar;
You then correctly go on to initialise this variable with:
progressbar = findViewById(R.id.login_progress_bar);
The issue is that in the xml, the view (widget) with this ID is a ProgressBar, but the type declaration for progressbar in the Java code is RelativeLayout. Your code is therefore trying to cast the ProgressBar in the layout file into a RelativeLayout, which can't be done.
You can fix this by changing the declaration on line 7 to:
ProgressBar progressbar;
(And then adding the necessary import at the top of your Java file: import android.widget.ProgressBar;).
Hope this helped.
After press SignUp button, Display error : unfortunately app has stopped
LogCat error : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lolacupcakes/com.example.lolacupcakes.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
SignUpActivity: Java Code
public class SignUpActivity extends AppCompatActivity {
EditText emailId, password;
Button btnSignUp;
TextView tvSignIn;
FirebaseAuth mFirebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.emailIDSignUp);
password = findViewById(R.id.pwdIDSignUp);
btnSignUp = findViewById(R.id.btnIDSignUp);
tvSignIn = findViewById(R.id.textView);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailId.getText().toString();
String pwd = password.getText().toString();
if(email.isEmpty())
{
emailId.setError("Please Enter Email Id");
emailId.requestFocus();
}
else if (pwd.isEmpty())
{
password.setError("Please Enter Email Id");
password.requestFocus();
}
else if (email.isEmpty() && pwd.isEmpty())
{
Toast.makeText(SignUpActivity.this, "Fields are Empty",Toast.LENGTH_SHORT).show();
}
else if (!(email.isEmpty() && pwd.isEmpty()))
{
mFirebaseAuth.createUserWithEmailAndPassword(email,pwd).addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful())
{
Toast.makeText(SignUpActivity.this, "SignUp Unsuccessful, Please Try Again",Toast.LENGTH_SHORT).show();
}
else
{
startActivity(new Intent(SignUpActivity.this,HomeActivity.class));
}
}
});
}
else
{
Toast.makeText(SignUpActivity.this, "Error Occurred!",Toast.LENGTH_SHORT).show();
}
}
});
tvSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(i);
}
});
}
}
SignUpActivity 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"
tools:context=".SignUpActivity">
<Button
android:id="#+id/btnIDSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="132dp"
android:text="Sign Up" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:src="#drawable/logo_lola" />
<EditText
android:id="#+id/pwdIDSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="206dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/emailIDSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="269dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="87dp"
android:text="Already have an account? Sign in here." />
</RelativeLayout>
HomeActivity Java Code:
public class HomeActivity extends AppCompatActivity {
Button btnlogout;
FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnlogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Intent intoMain = new Intent(HomeActivity.this, SignUpActivity.class);
startActivity(intoMain);
}
});
}
}
HomeActivityXML:
<?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=".HomeActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Welcome"
android:textSize="50dp"
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" />
<Button
android:id="#+id/btnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="424dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
You missed initializing btnlogout. Add this line in HomeActivity onCreate before setting View.OnClickListener
btnlogout = findViewById(R.id.btnLogout);
You need to create btnSignUp object before setonlicklistener for button
btnSignUp = findViewById(R.id.btnLogout);
You need to create the btnSignUp object before setonclicklistener.
I am writing a simple app which is working fine but the issue I have is that I am using a password condition to trigger a button click if entered correctly.
The issue is that my source code is saying 'onButtonClick' is never used and when I manually press that button in the app, it suddenly force closes and crashes. Anyone know what I am doing wrong. I am extending Activity, at the start of the source code. Should I be extending AppCompatActivity?
public class Gvoice extends Activity implements OnClickListener{
ListView lv1;
static final int check = 1111;
Button b1;
Button b_home;
EditText a1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gvoice);
lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
b1 = (Button)findViewById(R.id.GVoice);
a1 = (EditText) findViewById(R.id.editTextHome);
b1.setOnClickListener(this);
//This now handles an automatic press of the bVoice button 1 second after the activity is opened
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
b1.callOnClick();
}
}, 1000);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.BHome) {
String str = a1.getText().toString();
//Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'xxx' which is not case sensitive
if (str.toLowerCase().contains("home")) {
Intent userintent = new Intent(Gvoice.this, PocketSphinxActivity.class);
startActivity(userintent);
} else {
Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
}
}
}
public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
startActivityForResult(i1, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
a1.setText((String) lv1.getItemAtPosition(0)); //Get the first phrase in the first row of list view
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
b_home.performClick();
}
}, 500); //Automatically click the 'Blogin' button after 500ms
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Update: Below is the xml file. Please note that onButtonClick has been added to the xml file but still it force closes the app when the button is clicked using the condition statement:
<?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="#ececec">
<ImageView
android:layout_width="100dip"
android:layout_height="100dip"
android:background="#drawable/patient_two"
android:id="#+id/pimage"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Patient Name: Joe Blogs"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Cause of Injury: Car crash"
android:id="#+id/textView2"
android:layout_below="#+id/pimage"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date of Birth:"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Gender:"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_alignStart="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Occupation:"
android:id="#+id/textView5"
android:layout_below="#+id/textView4"
android:layout_alignStart="#+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Address:"
android:id="#+id/textView6"
android:layout_below="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medical History"
android:id="#+id/textView7"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView6"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Heart attack"
android:id="#+id/textView8"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView7"
android:layout_alignStart="#+id/textView6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Arthritis"
android:id="#+id/textView9"
android:layout_below="#+id/textView8"
android:layout_alignStart="#+id/textView8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tests Completed"
android:id="#+id/textView10"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView9"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="X-Ray"
android:id="#+id/textView11"
android:layout_below="#+id/textView10"
android:layout_alignStart="#+id/textView9"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="#+id/textView12"
android:layout_below="#+id/textView11"
android:layout_alignStart="#+id/textView11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tests Due"
android:id="#+id/textView14"
android:layout_below="#+id/textView12"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="#+id/textView15"
android:layout_below="#+id/textView14"
android:layout_alignStart="#+id/textView12"
android:layout_marginTop="15dp" />
<ListView
android:layout_width="150dp"
android:layout_height="50dp"
android:id="#+id/lvVoiceReturn1"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter"
android:id="#+id/Blogin1"
android:onClick="onButtonClick"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/bVoice1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
android:id="#+id/bVoice1"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TFusername1"
android:layout_alignParentStart="true"
android:hint="Speech to Text" />
You can 4 method handle button click :
method 1 :
public class Mtest extends Activity {
Button b1;
public void onCreate(Bundle savedInstanceState) {
...
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(myhandler1);
...
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
};
}
method 2 :
class MTest extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
...
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(this);
...
}
#Override
public void onClick(View v) {
}
}
method 3 in xml and android:onClick="HandleClick" :
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="HandleClick" />
public class MTest extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void HandleClick(View view) {
}
}
method 4 :
public class MTest extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do stuff
}
});
}
}
The issue was to do with initially not adding onButtonClick in the xml file and also not correctly assigning the button labels in the java file.
lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
b1 = (Button)findViewById(R.id.GVoice);
a1 = (EditText) findViewById(R.id.editTextHome);
c1 = (Button)findViewById(R.id.BHome);
b1.setOnClickListener(this);
The following corrects the issues and everything is working fine now. Hope this can help others in the future