My Activity Login has edittext's value first entered by username and second value by password. I'm trying to pass this values of editText from Activity Class to nonActivityClass(Java Class).
Does exist any way to do this?
In MyLoginAcitivy:
yourButtonLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String username = yourEditTextUsername.getText().toString();
String password = yourEditTextPassword.getText().toString();
Intent intent = new Intent(this,nonActivityClass.class);
intent.putExtra("username",username);
intent.putExtra("password",password);
startActivity(intent);
}
});
In nonActivityClass:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAY_OUT);
Intent intent = getIntent();
if(intent != null){
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
}
}
I do not know why you want to do that. But you can do this using Preferences.
At your Activity save your data to Pref and load them at your Class
It depends on what you want to do with the data, I'm not sure what your issue is. You can use a Class with a Static method which can accept username and passwords then do your checking there or what ever operation you want after which you can return the result. There are other options to try out. Just like database classes which are not 'Activity classes', you can also pass values to them.
As in any normal class in Java with constructors:
User newuser = new User(uname, pwd); // in your activity
// in your class
public User(String uname,String pwd){
this.uname = uname;
this.pwd = pwd;
}
Please also consider to learn java basics before use it in android.
Related
In the code below (log in code), I have one button that if it clicked, it will start two intent. One intent is to use to open A class and the other is to use to send a string value to B class. But when the button clicked, the second intent not just sending the string value but also open the B class. The code supposedly to just send the string value to B class and open A class.
How to fix this?
package com.example.project;
public class login extends AppCompatActivity {
DatabaseReference reff;
EditText kode, pass;
Button masuk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FirebaseApp.initializeApp(this);
kode = (EditText)findViewById(R.id.kode);
pass = (EditText)findViewById(R.id.pass);
masuk = (Button)findViewById(R.id.masuk);
masuk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(TextUtils.isEmpty(kode.getText().toString()) || TextUtils.isEmpty(pass.getText().toString())){
Toast.makeText(login.this, "isi kode alat atau password", Toast.LENGTH_LONG).show();
}
else {
final String value = kode.getText().toString().trim();
reff = FirebaseDatabase.getInstance().getReference().child(value);
final String password = pass.getText().toString().trim();
// Read from the database
reff.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String passin = dataSnapshot.child("pass").getValue().toString();
if (passin.equals(password)){
Intent intent = new Intent(login.this, MainActivity.class);
startActivity(intent);
Intent send = new Intent(login.this, addmember.class);
send.putExtra("kode", value);
startActivity(send);
}
else if(!passin.equals(password)){
Toast.makeText(login.this, "password salah", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
}
});
}
Here, you can't start activity because you are trying to start two activities at same time. This is not possible and if possible this approach is not too good. In safe side, you can use EventBus library that used for pushing event as data and subscriber that receive that data. This is the right way to do what you want here.
I am building an app where you can upload images to my company server
My problem is that I have a login screen with Email, Password and Client ID(in LoginActivity)
Now The data you enter there is passed to another Activity(CameraActivity) and that is used to build a URL with Uri.Builder.
The data is being saved within the edittext boxes BUT it is only being passed(using intents) to the other activity on the button click so, you have to go back to the login screen and click the button to reSubmit the data for every upload and everytime you start the app(The Data is stored within the textboxes but it is not being passed to the other activity without the button click), for the URI.Builder to work and give the entered information to the URL.
I want the client to enter that information ONCE and click the Button ONCE and then it is stored and used everytime regardless of the app being exited etc.
Login Activity, This activity has the edittext boxes and the entered information is being saved but not passed to the other activity without the button click
public class LoginActivity extends AppCompatActivity {
String ID = "id";
String EMAIL = "email";
String PASS = "pass";
private SharedPreferences mPreference;
EditText email, password, id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
id.setText(mPreference.getString(ID, ""));
email.setText(mPreference.getString(EMAIL, ""));
password.setText(mPreference.getString(PASS, ""));
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EMAIL =email.getText().toString();
PASS =password.getText().toString();
ID =id.getText().toString();
mPreference.edit().putString(ID,ID).apply();
mPreference.edit().putString(EMAIL, EMAIL).apply();
mPreference.edit().putString(PASS, PASS).apply();
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", ID);
intent.putExtra("email", EMAIL);
intent.putExtra("password", PASS);
startActivity(intent);
}
});
}
}
As suggested in the question comment, SharedPreferences is a good option.
SharedPreferences getPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
void setEmail(Context context, String email) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putString("EMAIL", email);
editor.apply();
}
String getEmail(Context context) {
return getPreferences(context).getString("EMAIL", "");
}
Above is an example of how you can put things and retrieve things from the shared preferences. Since you have more than an email to store (user ID, etc...) you could even store an object as a gson so you would have to call only one function. But of course this is your call.
You can start the CameraActivity if data exist in SharedPreferences.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
//Check if data exist
if(mPreference.contain(ID))
{
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", mPreference.getString(ID, ""));
intent.putExtra("email", mPreference.getString(EMAIL, ""));
intent.putExtra("password", mPreference.getString(PASS, ""));
startActivity(intent);
}
//Rest of the code
}
Cheers :)
I'm having a problem when trying to get data from "EditText" in Android.
The app crashes immediately when I try to assign "EditText" value to a String variable (I used toString() method).
The problem is in the two lines after the onCreate method.
Here is my code:
public class MainActivity extends AppCompatActivity {
EditText em;
EditText pw;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText em = (EditText) findViewById(R.id.email1);
EditText pw = (EditText) findViewById(R.id.password1);
mAuth = FirebaseAuth.getInstance();
}
// When I remove those two lines the app runs normally
String email = em.getText().toString();
String password = pw.getText().toString();
void login(View v){
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(MainActivity.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();
String Logged = user.getEmail().toString();
Intent intent = new Intent(MainActivity.this, afterLogin.class);
intent.putExtra("email", Logged);
startActivity(intent);
finish();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(MainActivity.this, "Wrong email/password.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Why are these statements outside?
String email = em.getText().toString();
String password = pw.getText().toString();
You are trying to assign these before onCreate was executed.
Extracting data at class level is similar to doing this in constructor.
Think about it.. constructor runs first, then the onCreate executes.
So, you are essentially running
null.getText().toString(); //because em and pw were still null
Instead of doing this, please move these statements to login method or any other event method you might want to create.
You're declaring and initializing String email and String password before onCreate so the EditTexts are null. Move the String initialization into your onCreate like this:
EditText em;
EditText pw;
String email;
String password;
onCreate {
EditText em = findViewById(R.id.email1);
EditText pw = findViewById(R.id.password1);
email = em.getText().toString;
password = pw.getText().toString;
}
Of course, the EditTexts will probably be empty at that point, so you'll probably want to get their values later, or use a TextWatcher.
I want to make an app which plays sounds with different amount of times between sounds but I don't know how to do it.
The amount of times are defined by the user in EditText in a setting activity and then the songs are played in another activity so I need to link the EditText value in the other activity.
Here is my code to transform the content of my EditText into a variable but I don't know how to use it in my other interface :
EditText myEdit = (EditText) findViewById(R.id.editText1);
String myEditValue = myEdit.getText().toString();
time = Integer.parseInt(myEditValue);
It would be even better if someone can tell me how to transfer my EditText value in an enum but if you only know how to transfer between activities and not from an activity to an enum it will be ok.
Here is my enum code :
public enum phaseTimer {
WAITING("APPUYEZ", valueInEditText1),
LOADING("ATTENTE", valueInEditText2),
ON_YOUR_MARK("A VOS MARQUES", valueInEditText3),
READY("PRET", valueInEditText4),
GO("PARTEZ", valueInEditText5);
public String message;
public int time;
phaseTimer(String message, int time) {
this.message = message;
this.temps = temps;
}
public String getMessage() {
return message;
}
}
Intent i = new Intent(this, theotheractivityname.class);
i.putExtra("myEditValue",myEditValue );
startActivity(i);
from the other activity, you can get your string with
String newString;
Bundle extras = getIntent().getExtras();
newString= extras.getString("myEditValue");
This is a simple way here:
Intent intent = new Intent(getBaseContext(), YourClass.class);
intent.putExtra("EXTRA_SESSION_ID", yourVariable);
startActivity(intent);
Access that intent on next activity
String s = getIntent().getStringExtra("EXTRA_SESSION_ID");
Credit to this answer: How do I pass data.....
I have the default ParseUser class "User". The problem is that when I am trying to insert data into a new column in that class "updatePOST" using querying but it does not update.
That column shows "undefined". Plus I want that column "updatePOST" to hold only the latest string. I have also tried giving manually the objectId but noting happens.
Here is the code
public class ChangeAccount extends AppCompatActivity {
private ImageView imageView;
private EditText editText;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_account);
imageView = (ImageView) findViewById(R.id.profile_pic_change);
editText = (EditText) findViewById(R.id.changeUpdatePost);
button = (Button) findViewById(R.id.change_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ParseUser currentUser = ParseUser.getCurrentUser();
String objectId = currentUser.getObjectId();
final String updatePost = editText.getText().toString();
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.getInBackground((String) objectId, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if(e == null){
parseObject.put("updatePOST",updatePost);
parseObject.saveInBackground();
}
}
});
Intent i = new Intent(ChangeAccount.this,Account.class);
startActivity(i);
}
});
}
I have created a new "Update" class. And have put data in it. Also the above query is working fine for that class. The values are updating. So basically only the problem is with the default User class.
Any help will be appreciated. Thanks
I ran into a similar issue with the Installation object. Can you try adding the column into the User table in the parse dashboard? Creating that new data column along with the right data type worked for me.
**Update 5/4/16 **
Once you get the current ParseUser from ParseUser.getCurrentUser() you can immediately put key/value pairs on ParseUser. You don't need to construct another ParseQuery to get the User object. Please see the put method in the reference docs. Once you've set the desired key/value pair, you can then call saveInBackground() right away.