I am trying to open another activity according to the sap number entered by the user. But even before the next activity gets opened I get an exception, illegal state exception and thread exiting with uncaught exception.
This is the java file from where I am calling the other activities-
package ai.app;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
public class homepage extends Activity {
SQLiteDatabase db;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
String sap = getIntent().getExtras().getString("sapno");
tv=(TextView)findViewById(R.id.textView1);
db=openOrCreateDatabase("aidatabase.db",MODE_PRIVATE,null);
Cursor c=db.rawQuery("select * from master where sapno='"+sap+"'", null);
Boolean s;
s=c.moveToFirst();
String a=c.getString(c.getColumnIndex("department"));
tv.setText("Welcome to "+a+" Department");
}
public void call (View v)
{
switch(v.getId())
{
case R.id.view:
{
Toast.makeText(this, "view done", Toast.LENGTH_LONG).show();
Intent i=new Intent(this, viewdetails.class);
startActivity(i);
finish();
break;
}
case R.id.enter:
{
Intent i = getIntent();
String sap = i.getStringExtra("sapno");
if(sap.equals("102"))
{
System.out.println("Inside cargo");
Intent i1=new Intent(this, cargo.class);
startActivity(i1);
finish();
}
else if(sap.equals("103"))
{
System.out.println("Inside hcc");
Intent i1=new Intent(this, hcc.class);
startActivity(i1);
finish();
}
else if(sap.equals("104"))
{
System.out.println("Inside trimmers");
Intent i1=new Intent(this, trimmers.class);
startActivity(i1);
finish();
}
else if(sap.equals("105"))
{
System.out.println("Inside ops 1");
Intent i1=new Intent(this, ops.class);
System.out.println("Inside ops 2");
startActivity(i1);
finish();
}
else if(sap.equals("107"))
{
System.out.println("Inside routing");
Intent i1=new Intent(this, routing.class);
startActivity(i1);
finish();
}
}
}
}
}
For example if ops section gets selected, the exception is thrown.
Following is the java code of ops.java
package ai.app;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
public class ops extends Activity
{ #Override
protected void onCreate(Bundle savedInstanceState)
{ System.out.println("inside oncreate");
try { System.out.println("inside try1");
super.onCreate(savedInstanceState);
System.out.println("inside try2");
setContentView(R.layout.opssection); }
catch(Exception e)
{ Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show(); }
}
}
The xml file for the same is -
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="3dp"
android:text="Remarks" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="200dp"
android:text="FOB Revised2" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="160dp"
android:text="FOB Revised1" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="120dp"
android:text="Fuel on Board" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="1dp"
android:layout_y="0dp"
android:background="#BD3112"
android:text=" OPS Section "
android:textColor="#FFFFFF"
android:textSize="20dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="110dp"
android:ems="10" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="150dp"
android:ems="10" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="190dp"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="40dp"
android:text="Enter Trip Fuel" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="30dp"
android:ems="10" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="70dp"
android:ems="10" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="80dp"
android:text="RTOW" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="102dp"
android:layout_y="248dp"
android:background="#BD3112"
android:text=" Update "
android:textColor="#FFFFFF" />
</AbsoluteLayout>
Similar exception is being thrown on other activities as well, kindly let me know where I am going wrong... Thanks
Not all exceptions are under Exception class.change:
catch(Exception e)
To catch more exceptions types using Throwable class:
catch(Throwable e)
Related
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);
}
I am new to app development, I am stuck on this following example in my book.
Can some one please help me that why my play button is not working? After clicking on the play button the game should start and it should navigate to next activity.
Main page 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="com.packtpub.mathgamechapter3a.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Math Game"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="30sp" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="#+id/buttonPlay"
android:layout_marginTop="28dp"
android:layout_below="#+id/imageView"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High Scores"
android:id="#+id/button2"
android:layout_below="#+id/buttonPlay"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quit"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignRight="#+id/buttonPlay"
android:layout_alignEnd="#+id/buttonPlay"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i;
i = new Intent(this, GameActivity.class);
startActivity(i);
}
}
Game page where it should navigate
<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="com.packtpub.mathgamechapter3a.GameActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartA"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x"
android:id="#+id/textOperator"
android:layout_alignTop="#+id/textPartA"
android:layout_centerHorizontal="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartB"
android:layout_alignTop="#+id/textOperator"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="="
android:id="#+id/textView4"
android:layout_below="#+id/textOperator"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:textSize="70sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/buttonChoice1"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="99dp"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/buttonChoice2"
android:layout_alignTop="#+id/buttonChoice1"
android:layout_centerHorizontal="true"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/buttonChoice3"
android:layout_alignTop="#+id/buttonChoice2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="40sp" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GameActivity extends Activity implements View.OnClickListener{
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//The next line loads our UI design to the screen
setContentView(R.layout.activity_game);
//Here we initialize all our variables
int partA = 9;
int partB = 9;
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer - 1;
int wrongAnswer2 = correctAnswer + 1;
/*Here we get a working object based on either the button
or TextView class and base as well as link our new objects
directly to the appropriate UI elements that we created previously*/
TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//Now we use the setText method of the class on our objects
//to show our variable values on the UI elements.
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partA);
//which button receives which answer, at this stage is arbitrary.
buttonObjectChoice1.setText("" + correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
buttonObjectChoice1.setOnClickListener(this);
buttonObjectChoice2.setOnClickListener(this);
buttonObjectChoice3.setOnClickListener(this);
}//onCreate ends here
#Override
public void onClick(View view) {
//declare a new int to be used in all the cases
int answerGiven=0;
switch (view.getId()) {
case R.id.buttonChoice1:
//initialize a new int with the value contained in buttonObjectChoice1
//Remember we put it there ourselves previously
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
//is it the right answer?
if(answerGiven==correctAnswer) {//yay it's the right answer
Toast.makeText(getApplicationContext(),
"Well done!", Toast.LENGTH_LONG).show();
}else{//uh oh!
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice2:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice3:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Sorry that's wrong",
Toast.LENGTH_LONG).show();
}
break;
}
}
}
i checked your code it is correct .
I think you forget to register your activity in manifests
please check that
well you need to do a if statement on the being clicked on.
if(view.getId() == R.id.buttonPlay)
{
Intent intent = new Intent(getBaseContext(), GameActivity.class);
startActivity(intent);
}
also make sure game activity is registered in the manifest file.
Intent in = getIntent();
is missing..
the 2nd activity should contain this line
also add this
if(view.getId() == R.id.buttonPlay)
{
Intent inte = new Intent(MainActivity.this, GameActivity.class);
startActivity(intent);
}
and do register in the manifest file
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));
}
}
I have a main activity (mottoscreen) after which an activity called circles opens up then from that activity I want to open up either one of the other two activities(sc_activity and or_activity). I have written the code for it but on clicking the buttons present in circles activity the next activity isn't showing up.
Circles Java file-
package com.apsdevelopers.mr.meteout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
public class circles extends mottoscreen {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.circles);
}
public void onButtonClick(View v)
{
if (v.getId() == R.id.GOsc)
{
Intent I = new Intent(circles.this, sc_activity.class);
startActivity(I);
}
else if (v.getId() == R.id.GOor)
{
Intent j = new Intent(circles.this, or_activity.class);
startActivity(j);
}
}
}
Sc_activity Java file-
package com.apsdevelopers.mr.meteout;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class sc_activity extends mottoscreen
{
EditText name, ph, address, mass, thing;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sc_activity);
name = (EditText)findViewById(R.id.name);
EditText p = (EditText)findViewById(R.id.pin);
String pinc = p.getText().toString();
final int apsnumber= Integer.parseInt("8763597264");
if (pinc.equals("753001") || pinc.equals("753002") || pinc.equals("753003") || pinc.equals("753004") || pinc.equals("753005") || pinc.equals("753006") || pinc.equals("753007") || pinc.equals("753008") || pinc.equals("753009")) {
ph = (EditText)findViewById(R.id.ph);
address = (EditText)findViewById(R.id.address);
mass = (EditText)findViewById(R.id.mass);
thing = (EditText)findViewById(R.id.thing);
msg = (Button)findViewById(R.id.msg);
msg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String number = ph.getText().toString();
String message1 = name.getText().toString();
String message2 = address.getText().toString();
String message3 = mass.getText().toString();
String message4 = thing.getText().toString();
Intent i = new Intent(getApplicationContext(), sc_activity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(String.valueOf(apsnumber), null, number+message1 + message2 + message3 + message4, pIntent, null);
Toast.makeText(getApplicationContext(), "Form sent successfully ! , now click on DONE",
Toast.LENGTH_LONG).show();
}
});
}
else
{
Toast.makeText(getApplicationContext(), "ERROR: WE DONOT COVER THE PINCODE ENTERED BY YOU, PLZ ENTER A VALID PINCODE OF (CTC, ODISHA)",
Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
public void onButtonClick(View v)
{
if (v.getId() == R.id.msg)
{
Intent I = new Intent(sc_activity.this, th_activity.class);
startActivity(I);
}
}
}
Sc_activity XML file-
<?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="#9acef6fe"
android:id="#+id/sc_activity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="NAME"
android:ems="10"
android:id="#+id/name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/ph"
android:layout_below="#+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="PHONE NUMBER"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.-5kg, 100 bottles... "
android:id="#+id/textView12"
android:layout_below="#+id/mass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.- metal scrap+newspapers, bottles+tyres...+"
android:id="#+id/textView11"
android:layout_below="#+id/thing"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/address"
android:text="ADDRESS"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_below="#+id/pin"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thing"
android:text="THING"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_below="#+id/address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/mass"
android:text="AMOUNT"
android:imeOptions="actionDone"
android:textColor="#d4375a5c"
android:layout_below="#+id/textView11"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/pin"
android:layout_marginTop="36sp"
android:text="PINCODE"
android:textColor="#d4375a5c"
android:layout_below="#+id/ph"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND MESSAGE"
android:id="#+id/msg"
android:background="#e33a9179"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/thing"
android:layout_alignRight="#+id/textView11"
android:layout_alignEnd="#+id/textView11" />
</RelativeLayout>
Or_activity Java file-
package com.apsdevelopers.mr.meteout;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class or_activity extends mottoscreen
{
EditText nam, pho, addres, mas, thinge;
Button msg2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.or_activity);
nam = (EditText) findViewById(R.id.nam);
EditText p = (EditText) findViewById(R.id.pin1);
String pinc = p.getText().toString();
final int apsnumber= Integer.parseInt("8763597264");
if (pinc.equals("753001") || pinc.equals("753002") || pinc.equals("753003") || pinc.equals("753004") || pinc.equals("753005") || pinc.equals("753006") || pinc.equals("753007") || pinc.equals("753008") || pinc.equals("753009")) {
pho = (EditText) findViewById(R.id.ph);
addres = (EditText) findViewById(R.id.address);
mas = (EditText) findViewById(R.id.mass);
thinge = (EditText) findViewById(R.id.thing);
msg2 = (Button) findViewById(R.id.msg2);
msg2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String number = pho.getText().toString();
String message1 = nam.getText().toString();
String message2 = addres.getText().toString();
String message3 = mas.getText().toString();
String message4 = thinge.getText().toString();
Intent i = new Intent(getApplicationContext(), or_activity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(String.valueOf(apsnumber), null, number+message1 + message2 + message3 + message4, pIntent, null);
Toast.makeText(getApplicationContext(), "Form sent successfully ! , now click on DONE",
Toast.LENGTH_LONG).show();
}
});
}
else
{
Toast.makeText(getApplicationContext(), "ERROR: WE DONOT COVER THE PINCODE ENTERED BY YOU, PLZ ENTER A VALID PINCODE OF (CTC, ODISHA)",
Toast.LENGTH_LONG).show();
}
}
public void onButtonClick(View v)
{
if (v.getId() == R.id.msg2)
{
Intent I = new Intent(or_activity.this, th_activity.class);
startActivity(I);
}
}
}
Or_activity XML file-
<?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="#9acef6fe"
android:id="#+id/or_activity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="NAME"
android:ems="10"
android:id="#+id/nam"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/pho"
android:text="PHONE NUMBER"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_below="#+id/nam"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/pin1"
android:text="PINCODE"
android:layout_below="#+id/pho"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="43dp"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/addres"
android:text="ADDRESS"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/mas"
android:text="AMOUNT"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_marginTop="25dp"
android:layout_below="#+id/textView11"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.- toys , clothes ... "
android:id="#+id/textView11"
android:layout_below="#+id/thinge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thinge"
android:text="THING"
android:imeOptions="actionDone"
android:textColor="#d4375a5c"
android:layout_below="#+id/addres"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND MESSAGE"
android:id="#+id/msg2"
android:background="#e33a9179"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView11"
android:layout_alignEnd="#+id/textView11"
android:layout_toRightOf="#+id/thinge"
android:layout_toEndOf="#+id/thinge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.-5 books,2 cricket bats..."
android:id="#+id/textView12"
android:layout_below="#+id/mas"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Please guide me where I did a mistake in the code.
Can you please check the following in your code:
Your circles.xml has two Buttons with id GOsc and GOor?
In your circles.xml you have set android:onClick="onButtonClick"for both buttons?
If both of the above result to yes, can you please paste your circles.xml file here as well?
Here I am creating a simple Droid LoginApp, which simply tells login successfull or not. But the if statement is not giving me the desired result.
Following is the code : MainActivity.java
package com.example.login1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
public EditText t1,t2;
public Button b1;
String user,pass;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText) findViewById(R.id.editText2);
b1= (Button) findViewById(R.id.button1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View arg0)
{
user = t1.getText().toString();
pass = t2.getText().toString();
if (((user.equals("user")) && (pass.equals("user"))))
{
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
t1.setText("");
t2.setText("");
}
}
}
activity_main.xml
<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"
android:gravity="fill_vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:layout_marginTop="28dp"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="53dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:clickable="true"
android:onClick="onClick"
android:text="Login" />
</RelativeLayout>
While I run the App, on clicking the Login button, the if statement does not give a correct result.. I simply don't know where is the error.
Well, you should compare strings with "equals()" and not with "==".
Replace:
user == "user"
With:
user.equals("user")
And also, I think there's no need for this extra line you have:
if (arg0.findViewById(R.id.button1)==b1)
^ Why are you checking this?
use this code, for click command
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
user = t1.getText().toString();
if (user.equals("user")){
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
}
}
}