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.
Related
It's just a simple text with two buttons that creates toasts and change the text. I'm very new to this AS so I really don't know how to even attempt to fix this..
Any help is very appreciated...
MainActivity.java:
package com.example.david.davidisawesome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Started.");
final TextView firstText = (TextView) findViewById(R.id.firstText);
Button firstButton = (Button) findViewById(R.id.firstBtn);
Button secondButton = (Button) findViewById(R.id.secondBtn);
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: First Button Clicked.");
toastMessage("You Clicked the first button");
firstText.setText("Nice Job.");
}
});
secondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Second Button Clicked.");
toastMessage("You Clicked the second button");
firstText.setText("Good Effort.");
}
});
}
private void toastMessage(String message){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
In picture:
activity_main.xml:
Error:
It is not finding your xml elements. The error shows that you have a null exception on setting the onclick interface to the button. So start by confirming that you are inflating the proper xml and finding the proper id.
More specifically your secondBtn is not found.
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());
}
});
}
I'm building a program using some buttons. Everything is perfect and beautiful until this error popped up.
I tried as much ways as possible but it still doesn't work! Can you explain the error and the way to fix it? Here is my code:
package com.huy9515gmail.mycontact;
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.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
#BindView(R.id.edt_inputName) EditText edtName;
#BindView(R.id.btnAdd) Button btnAdd;
#BindView(R.id.edt_inputNumber) EditText edtNumber;
#BindView(R.id.rdbtn_male) RadioButton rdbtn_male;
#BindView(R.id.rdbtn_female) RadioButton rdbtn_female;
#BindView(R.id.rdbtn_others) RadioButton rdbtn_others;
#BindView(R.id.gender) RadioGroup genderSelection;
private ListView lvContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
lvContact = (ListView) findViewById(R.id.lv_contact);
final ArrayList<Contact> arrContact = new ArrayList<>();
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onclick(View v) {
//validating contact info
if (((edtName.getText().toString().trim()) == "") || (edtNumber.getText().toString().trim() == "")) {
Toast.makeText(MainActivity.this, "Invalid contact info! Please try again!", Toast.LENGTH_SHORT).show();
}
//adding contact info
else {
Contact contact = new Contact(Gender.male, "", "");
//receiving gender
boolean isMale, isFemale, isOthers;
if (rdbtn_male.isChecked()) contact.setGender(Gender.male);
if (rdbtn_female.isChecked()) contact.setGender(Gender.female);
if (rdbtn_others.isChecked()) contact.setGender(Gender.others);
//adding info
contact.setName(edtName.getText().toString());
contact.setNumber(edtNumber.getText().toString());
arrContact.add(contact);
}
}
});
CustomAdapter customAdapter = new CustomAdapter(this, R.layout.row_listview, arrContact);
lvContact.setAdapter(customAdapter);
}
}
Replace this line:
public void onclick(View v) {
With this (we just modified the capital C):
public void onClick(View v) {
It is a tricky thing, but since you're working with anonymous classes, you need to override the exact signature that the interface is giving you, and it's case-sensitive.
The implementation of the View.OnClickListener interface, requires you to implement the onClick method which receives one argument of type View.
Try to add #Override annotation if your Activity implements OnClickListener interface
And make the C capital like this:
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your codes
}
}
});
You need to do exactly what the error is telling you to do.
You have to override the onClick method.
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//validating contact info
if (((edtName.getText().toString().trim()) == "") || (edtNumber.getText().toString().trim() == "")) {
Toast.makeText(MainActivity.this, "Invalid contact info! Please try again!", Toast.LENGTH_SHORT).show();
}
//adding contact info
else {
Contact contact = new Contact(Gender.male, "", "");
//receiving gender
boolean isMale, isFemale, isOthers;
if (rdbtn_male.isChecked()) contact.setGender(Gender.male);
if (rdbtn_female.isChecked()) contact.setGender(Gender.female);
if (rdbtn_others.isChecked()) contact.setGender(Gender.others);
//adding info
contact.setName(edtName.getText().toString());
contact.setNumber(edtNumber.getText().toString());
arrContact.add(contact);
}
}
});
I was learning to make a simple login app from youtube, however, when I build the code, although the app runs quite fine but it never assigns the value of 3 to the attempt counter later in the code, only the layout is visible and hence login doesn't work. Can you please help me? If you need any other file, write it in comment, I'll upload it later. Thanks.
package com.shubham.splashscreenemulation;
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 android.widget.Toast;
public class UserLogin extends AppCompatActivity {
private static EditText username;
private static EditText password;
private static TextView attempts_left;
private static Button login_btn;
int attempt_counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
}
public void LoginButton(){
username = (EditText)findViewById(R.id.editText_user);
password = (EditText)findViewById(R.id.editText_password);
attempts_left = (TextView)findViewById(R.id.textView_attempt_count);
login_btn = (Button)findViewById(R.id.button_login);
attempts_left.setText(Integer.toString(attempt_counter));
login_btn.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v){
if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev"))
{
Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen");
startActivity(intent);
}
else
{
Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show();
attempt_counter--;
attempts_left.setText(Integer.toString(attempt_counter));
if(attempt_counter == 0)
{
login_btn.setEnabled(false);
}
}
}
}
);
}
}
You don't call the function LoginButton() in your Activity.
Here some tips : function name starts with lowercase letter like loginButton ( with camelCase ), classes names starts with uppercase letter like AppCompatActivity. This tip is for more readability for the code. Why do you declare Views as static in that function?
package com.shubham.splashscreenemulation;
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 android.widget.Toast;
public class UserLogin extends AppCompatActivity {
private static EditText username;
private static EditText password;
private static TextView attempts_left;
private static Button login_btn;
int attempt_counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
LoginButton();
}
public void LoginButton(){
username = (EditText)findViewById(R.id.editText_user);
password = (EditText)findViewById(R.id.editText_password);
attempts_left = (TextView)findViewById(R.id.textView_attempt_count);
login_btn = (Button)findViewById(R.id.button_login);
attempts_left.setText(Integer.toString(attempt_counter));
login_btn.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v){
if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev"))
{
Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen");
startActivity(intent);
}
else
{
Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show();
attempt_counter--;
attempts_left.setText(Integer.toString(attempt_counter));
if(attempt_counter == 0)
{
login_btn.setEnabled(false);
}
}
}
}
);
}
You can add LoginButton() after setContentView or remove the LoginButton method, move everything inside onCreate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
username = (EditText)findViewById(R.id.editText_user);
password = (EditText)findViewById(R.id.editText_password);
attempts_left = (TextView)findViewById(R.id.textView_attempt_count);
login_btn = (Button)findViewById(R.id.button_login);
attempts_left.setText(Integer.toString(attempt_counter));
login_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev"))
{
Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen");
startActivity(intent);
}
else
{
Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show();
attempt_counter--; attempts_left.setText(Integer.toString(attempt_counter));
if(attempt_counter == 0)
{
login_btn.setEnabled(false);
}
}
}
});
}
You can also change your setText to this attempts_left.setText(attempt_counter+"");
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.