Defined method is never called - android

I am creating a method to validate my login fields (user name and password). I have created a method and call it on click event but it is not working.
package com.boyzcorn.android.fyp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/** Called when the activity is first created. */
public class login extends Activity {
EditText eText = (EditText)findViewById(R.id.uid);
EditText eText2 = (EditText)findViewById(R.id.editText2);
Button btnSubmit = (Button)findViewById(R.id.sbtn);
Button btnSignup = (Button)findViewById(R.id.signupbtn);
/* I think there is some problem with my method definition but i am not
getting it. */
public void validation(EditText username,EditText pass) {
if (username.getText().toString().equals("") ||
pass.getText().toString().equals("")) {
Toast.makeText(
getApplicationContext(),
"Fill Empty Fields",Toast.LENGTH_SHORT
).show();
} else {
Intent i = new Intent(login.this,order_pushing.class);
startActivity(i);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
validation(eText,eText2);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(login.this,signup.class);
startActivity(i);
}
});
}
}

Put this code under Activity{
EditText eText;
EditText eText2;
Button btnSubmit;
Button btnSignup;
and initialize it in OnCreate after setContentView(R.layout.login);
eText = (EditText)findViewById(R.id.uid);
eText2 = (EditText)findViewById(R.id.editText2);
btnSubmit = (Button)findViewById(R.id.sbtn);
btnSignup = (Button)findViewById(R.id.signupbtn);
Hope This will work

You are defining your buttons before setting the content view. Move the following lines into the onCreate(), after setContentView(R.layout.login):
EditText eText = (EditText)findViewById(R.id.uid);
EditText eText2 = (EditText)findViewById(R.id.editText2);
Button btnSubmit = (Button)findViewById(R.id.sbtn);
Button btnSignup = (Button)findViewById(R.id.signupbtn);

Related

getText is not working in Android studio

package com.example.mediosa.mediosa;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.firebase.client.Firebase;
public class registerActivity extends AppCompatActivity {
private Button buttonSignIn, buttonRegister;
private EditText editTextUsername;
private EditText editTextEmail;
private EditText editTextPassword;
private EditText editTextDOB;
private EditText editTextName;
private RadioButton radioButtonMale, radioButtonFemale;
private Firebase rootRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
rootRef = new Firebase(FIREBASE_URL");
buttonSignIn = (Button)findViewById(R.id.buttonSignIn);
editTextDOB = (EditText)findViewById(R.id.editTextDOBReg);
editTextUsername = (EditText)findViewById(R.id.editTextUsernameReg);
editTextEmail = (EditText)findViewById(R.id.editTextEmailReg);
editTextPassword = (EditText)findViewById(R.id.editTextPasswordReg);
editTextName = (EditText)findViewById(R.id.editTextNameReg);
radioButtonFemale =(RadioButton)findViewById(R.id.radioButtonFemale);
radioButtonMale = (RadioButton)findViewById(R.id.radioButtonMale);
buttonRegister = (Button)findViewById(R.id.buttonRegister);
buttonSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(registerActivity.this ,
LoginActivity.class));
}
});
final String Name = editTextName.getText().toString();
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(registerActivity.this, Name ,
Toast.LENGTH_SHORT).show();
}
});
}
}
I am new to Android and was working on a project.
In this code (Part of that project) in am unable to read data from user as name. The compiler shows no error And the code works correctly but when i try to show the data in the Name string using toast, it is completely blank.
I need this name and other field to store data in Firebase DB, but it was not working and i found out there was some problem in reading data.
Would be really great if anyone can help me, thanks.
The problem is that you are not updating the value of Name , meanwhile there is no need for a variable if you only want to display the text. Here is how you display the text.
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(registerActivity.this,
editTextName.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
Try putting getText() inside the OnClickListener:
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String Name = editTextName.getText().toString();
Toast.makeText(registerActivity.this, Name ,
Toast.LENGTH_SHORT).show();
}
});
This is because you call getText() inside onCreate(). see where it is placed. Thus when the Activity is being created, the text inside EditText is empty.

Trying to create a login screen With Adndoid SDK 2.3.3

I am Completely new to Android SDK and very new to writing code in general. I have been monkeying with the software and following a tutorial on how to make a login for my app but I just can't seem to get it to work. Below is my .Java Code. I can tell that the error is in line 32-42, I don't know why I it does not detect .settext or .SetOnClickListner. Any help would be great.
package com.example.inventory;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private EditText User;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
User = (EditText) findViewById(R.id.etName);
Password = (EditText) findViewById(R.id.etPassword);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button) findViewById(R.id.btnLogin);
}
Info.setText("No of attemps remaining: 5");
Login.SetOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
validate(Name.getText().toString(), Password.getText().toString());
}
};
}
private void validate(String userName, String userPassword) {
if ((userName.equals("Admin")) && (userPassword.equals("Pass"))) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}else{
counter--;
if (counter== 0){
Login.setEnabled(false);
}
}
}
}
Your .setText() code is outside of the onCreate method. There is not a process to call the .setText() method. Just move the code inside of the bracket above it. Same goes for your OnClickListeners. You were also missing a parenthesis at the close of your OnClickListener.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
User = (EditText) findViewById(R.id.etName);
Password = (EditText) findViewById(R.id.etPassword);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button) findViewById(R.id.btnLogin);
Info.setText("No of attemps remaining: 5");
Login.SetOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view){
validate(Name.getText().toString(), Password.getText().toString());
}
});
}

Why is there blank text appearing in the toast output when i have a string?

As far as i know this will store the user and pass in the variables i made.Then get the text and convert it to a string.Now if that is happening how come only a blank bubble appears when i call it?
package com.set.ultimax.login;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button logIn;
EditText user;
EditText pass;
logIn = (Button) findViewById(R.id.btn);
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
final String userValue = user.getText().toString();
final String passValue = pass.getText().toString();
logIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(Main.this,userValue, Toast.LENGTH_LONG).show();
Toast.makeText(Main.this,passValue, Toast.LENGTH_LONG).show();
}
});
}
}
You have to get the text from the EditTexts at the time you click the button, instead of getting it on onCreate.
Try this:
logIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
final String userValue = user.getText().toString();
final String passValue = pass.getText().toString();
Toast.makeText(Main.this,userValue, Toast.LENGTH_LONG).show();
Toast.makeText(Main.this,passValue, Toast.LENGTH_LONG).show();
}
});
Don't take values in onCreate, do it in the listener and don't show two toasts consecutively at they will overlap. Something like this:
logIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText user = (EditText) findViewById(R.id.username);
EditText pass = (EditText) findViewById(R.id.password);
String userValue = user.getText().toString();
String passValue = pass.getText().toString();
Toast.makeText(Main.this,"User: " + userValue + " Pass: " + passValue, Toast.LENGTH_LONG).show();
}
});

Android application - login account

i am working with my project and i find it hard to go to my main activity which is diary after I log-in..The problem is that I can't display a message saying I successfully logged in and after that proceed to creating my diary..what should I do? here's my code..
login.class
package com.gomez.android;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class login extends Activity{
//Declare views
private EditText uname;
private EditText pword;
private Button btnlogin;
private Button btncancel;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set Activity Layout
setContentView(R.layout.login);
//Get EditText and Button References
uname = (EditText)findViewById(R.id.username);
pword = (EditText)findViewById(R.id.password);
btnlogin = (Button)findViewById(R.id.login_enter);
btncancel = (Button)findViewById(R.id.cancel);
//set Click Listener
btnlogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Check Login
final String username = uname.getText().toString();
final String password = pword.getText().toString();
try{
if(username.length()>0&& password.length()>0)
{
dbuser users = new dbuser(login.this);
users.open();
if (users.Login(username, password))
{
Toast.makeText(login.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
Intent i = new Intent(login.this, firstpage.class);
startActivity(i);
}
else{
Toast.makeText(login.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
}
users.close();
}
}catch(Exception e)
{
Toast.makeText(login.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
btncancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//close application
finish();
}
});
}
}
please help me..thanks
You can use Asynctask class
Asynctask
In the onPostExecute method, you can show your notification then start your activity.

reset button in signup page is not working in android

I tried to make a signup page in android where I use a reset button that should clear all fields in the page. Please see the code below and correct it as my code is not working.
Button btnreset = (Button) findViewById(R.id.btnreset);
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
this is an signup page and fields are first name,last name,user id,password.when user click on reset button it clear all the fields who's filled the user.I'm give a complete source code to you please check this:
package com.boyzcorn.android.fyp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
public class signup extends Activity{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.signup);
Button b = (Button) findViewById(R.id.btnClick2);
Button btnreset = (Button) findViewById(R.id.btnreset);
final EditText eText1 = (EditText)findViewById(R.id.firstname);
final EditText eText2 = (EditText)findViewById(R.id.lastname);
final EditText eText3 = (EditText)findViewById(R.id.userid);
final EditText eText4 = (EditText)findViewById(R.id.password);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
{
if(eText1.getText().toString().equals("") ||eText2.getText().toString().equals("") || eText3.getText().toString().equals("") ||eText4.getText().toString().equals(""))
{
Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
}
else
{
Intent i = new Intent(signup.this,login.class);
startActivity(i);
}
}
}
});
}
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
public void onClick(View arg0) {
}
}
You've only defined the function; you're not calling it. You will need to execute restartActivity(signup.this) from the OnClickListener.
Also, your intent will likely not execute, because its parent has been finished. Perhaps rearranging the lines of code might help, but a better solution would be having the parent activity start it. Try replacing the click listener with:
btnreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Activity act = signup.this;
Intent intent = new Intent(act, act.getClass());
act.startActivity(intent);
act.finish();
}
}

Categories

Resources