Pass the textvalue between two xml - android

I have a "Login" xml in layout. And I have another xml is the "List".
In my Application, the user login into the application with e-mail address and password than show their mail list. I did it like that:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LoginScreen();
}
// I am receiving the email and password on the main layout for login page
public void LoginScreen()
{
setContentView(R.layout.main);
EditText emailTxt = (EditText) findViewById(R.id.txtMail);
EditText passwordTxt = (EditText) findViewById(R.id.txtPassword);
String email= epostaTxt.getText().toString();
String password = parolaTxt.getText().toString();
// After receive, call the MailList() for connection and getting the list
MailList()
}
// and I use this email and password again into the MailList() for connection with server then receive the mail list
public void MailList()
{
setContentView(R.layout.list);
EditText emailTxt = (EditText) findViewById(R.id.txtMail);
EditText passwordTxt = (EditText) findViewById(R.id.txtPassword);
String email= epostaTxt.getText().toString();
String password = parolaTxt.getText().toString();
Sending emain and password to the server etc...
}
This just for now but this code repeat will be continue . I want to get this email and password from Edittxt just one time and use it all the methods. And this code doesn't work also, Maillist doesn't show the list because cannot connect
How can I do that?

If I unterstand it well, you simply want to pass data between your Activities, here might be some help : In Android: How do I get variables/data from one screen to another?
EDIT: My new answer, hope it's the good one:
Simply create two variables to store the date?
public class LoginScreen extends Activity{
private String email;
private String password;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LoginScreen();
}
// I am receiving the email and password on the main layout for login page
public void LoginScreen()
{
setContentView(R.layout.main);
EditText emailTxt = (EditText) findViewById(R.id.txtMail);
EditText passwordTxt = (EditText) findViewById(R.id.txtPassword);
email= emailTxt.getText().toString();
password = passwordTxt.getText().toString();
// After receive, call the MailList() for connection and getting the list
MailList()
}
// and I use this email and password again into the MailList() for connection with server then receive the mail list
public void MailList()
{
setContentView(R.layout.list);
//Sending emain and password to the server etc...
//just use the email and password stored above
yourFunctionToSend(email,password);
}
}

Related

the data is submitted in firebase once in each run

I am inserting data into my firebase realtime database. I wrote a code for it. however, when i run the program, first thing it will insert and submit the data correctly, but when I move in the same run to other activity for example to login, and came back to insert other data, it will not insert it.
when I want to insert a data in my firebase, each time I must re-install the program and install it again and do it at the first,
Here is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_request_page);
firebaseAuth=FirebaseAuth.getInstance();
Title = (EditText) findViewById(R.id.Title);
Content = (EditText) findViewById(R.id.Content);
insert =(Button) findViewById(R.id.insert);
status=(EditText)findViewById(R.id.statustxt);
edittxtDate =(EditText) findViewById(R.id.edittxtDate);
edittxtTime =(EditText) findViewById(R.id.edittxtTime);
edittxtExpDate=(EditText) findViewById(R.id.edittxtExpDate);
imagebtnDate = (ImageButton) findViewById(R.id.imagebtnDate);
imagebtnTime = (ImageButton) findViewById(R.id.imagebtnTime);
imagebtnExpDate=(ImageButton) findViewById(R.id.imagebtnExpDate);
location = (Spinner) findViewById(R.id.location);
typeof=(Spinner)findViewById(R.id.typeof);
database = FirebaseDatabase.getInstance();
ref1 = database.getReference("requests");
insert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnInsert1();
}
});
private void getValues(){
request1.setTitle(Title.getText().toString());
request1.setContent(Content.getText().toString());
request1.setDate(edittxtDate.getText().toString());
request1.setTime(edittxtTime.getText().toString());
request1.setLocation(location.getSelectedItem().toString());
request1.setTypeof(typeof.getSelectedItem().toString());
request1.setExpDate(edittxtExpDate.getText().toString());
request1.setStatus(status.getText().toString());
}
private void btnInsert1(){
database = FirebaseDatabase.getInstance();
ref1 = database.getReference("requests");
DatabaseReference ref2=ref1.push();
String id=ref2.getKey();
request1 =new requests();
getValues();
ref1.child(id).setValue(request1);
Toast.makeText(FillRequestPage.this,"Data inserted....",Toast.LENGTH_SHORT).show();
}
my problem was from the firebase its self, the access is denied , I just edited the Rules So the user can read and write to my database and it works!

Open new class/activity on button click in android

How to open another activity on button click? So now my app has one main window ( when you run the app ). Then there is a menu whit some buttons and when I click on some button is open another activity for this button.
So I have one form which user must enter his details but now I want to click on button continue and open another activity where he must enter some more info. Now when this button is clicked the info goes into database.
I know how to add more activities on normal page like main where I have only buttons but on this one I don't really know. Here is the code
public class Reservation extends Activity {
String Name;
String Email;
String Phone;
String Comment;
String DateTime;
String numberOfPeople;
private EditText editText1, editText3, editText2, editText4, txtDate, numberOfPeoples; //, txtTime;
private Button btnMenues, btnCalendar, btnTimePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation);
editText1 = (EditText) findViewById(R.id.personName);
editText3 = (EditText) findViewById(R.id.personPhone);
editText2 = (EditText) findViewById(R.id.personEmail);
editText4 = (EditText) findViewById(R.id.personComment);
txtDate = (EditText) findViewById(R.id.datePicker);
numberOfPeoples = (EditText) findViewById(R.id.numberOfPeoples);
btnCalendar = (Button) findViewById(R.id.btnCalendar);
//btnTimePicker = (Button) findViewById(R.id.btnTimePicker);
btnMenues = (Button) findViewById(R.id.continueWithReservation);
btnMenues.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Reservation.this, ReservationSecond.class);
intent.putExtra(Name, Name.getBytes().toString());
intent.putExtra(Email, Email.getBytes().toString());
intent.putExtra(Phone, Phone.getBytes().toString());
intent.putExtra(Comment, Comment.getBytes().toString());
intent.putExtra(DateTime, DateTime.getBytes().toString());
intent.putExtra(numberOfPeople, numberOfPeople.getBytes().toString());
startActivity(intent);
}
});
}
So what I can put in setOnClickListener to go on next activity?
Update:
If I change this
public void onClick(View v) {
Name = editText1.getText().toString();
Phone = editText3.getText().toString();
Email = editText2.getText().toString();
Comment = editText4.getText().toString();
DateTime = txtDate.getText().toString();
numberOfPeople = numberOfPeoples.getText().toString();
new SummaryAsyncTask().execute((Void) null);
}
to this
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Contacts.class);
startActivity(intent);
}
How to keep the information that user is added so far for next activity and save in DB after he finish with second activity?
You can use an Intent and then call startActivity() with that Intent:
myBtn.setOnClickListener() {
public void onClick() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
Android Docs give a pretty good explanation
EDIT
To address the question you gave in the comment, the easiest way to pass information between Activities is to use extras like:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(NAME, VALUE);
startActivity(intent);
There are multiple versions of putExtra to accomodate passing different types of values
Another way to store information would be to use SharedPreferences. Here is a simple example
You could also create a public class called, for example, Storage and have your data be represented as static member(s) of this class (accessed like Storage.MY_DATA) so it can be accessed from any point in your application.

How to check Shared Prefs when app loads?

I am trying to remember a user's login credentials so that after he/she logs into the application the first time, each subsequent time, it automatically logs him/her in and starts the main activity.
Right now I have two activities the Login activity and the Main app activity. When a user types in his/hers credentials, it verifies them against the DB on my server in an AsyncTask. Now I am using Shared Prefs to store the username and password and each time the app is started I want to read those prefs. If they are empty, then open the Login activity/screen, otherwise open the Main activity/screen.
This works....but it takes about 3-5 seconds before it opens the Main activity. So it starts out on the Login activity and then after 3-5 seconds it goes to the Main activity. The following code is in the Login activity....where should I put it so that it does not open the Login activity unless the prefs are blank?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_login);
//keep screen on while activity is running
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
//get username and password from preferences if they exist
sharedPrefs = getSharedPreferences(PREFERENCES, getApplicationContext().MODE_PRIVATE);
if (sharedPrefs.contains(UserId) && sharedPrefs.contains(Password))
{
inputEmail.setText(sharedPrefs.getString(UserId, ""));
inputPassword.setText(sharedPrefs.getString(Password, ""));
new MyAsyncTask().execute(sharedPrefs.getString(UserId, ""),
sharedPrefs.getString(Password, ""));
}
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
Editor editor = sharedPrefs.edit();
editor.putString(UserId, email);
editor.putString(Password, password);
editor.commit();
new MyAsyncTask().execute(email, password);
}
});
}
Thanks for the help!

android pass the username and password in edittext of another activity

I am developing android application with login. When the user logged in i want to pass both username and password to another activity in another edittext. The passed username and password should be displayed in the another edittext of another activity. the password should look as it is (not revealing characters). I know that it may sound like weird but is that possible? Just give me an answer i know you might ask why should i display this thing to another edittext but i have my own purpose.
This is what i need to pass to another edittext
inputusername = (EditText) findViewById(R.id.loginUsername);
inputPassword = (EditText) findViewById(R.id.loginPasswod);
imgLogin1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String username = inputusername.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(username, password);
// check for login response
try {
if (json.getString(KEY_PSUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_PSUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_PUSERNAME), json.getString(KEY_PUID), json_user.getString(KEY_PCREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), PatientHomeActivity.class);
dashboard.putExtra(KEY_PUSERNAME,username);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
EDIT
Here's where i have passed the data
private static final String TAG_PUSERNAME = "username";
private static final String TAG_PASSWORD = "password";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Start and bind the imService
**/
startService(new Intent(Login.this, IMService.class));
setContentView(R.layout.login_screen);
setTitle("Login");
Bundle i = getIntent().getExtras();
Button loginButton = (Button) findViewById(R.id.login);
cancelButton = (Button) findViewById(R.id.cancel_login);
usernameText = (EditText) findViewById(R.id.userName);
usernameText.setText(i.getString(TAG_PUSERNAME));
passwordText = (EditText) findViewById(R.id.password);
passwordText.setText(i.getString(TAG_PASSWORD));
That's the next activity where the data should be passed.
Use Bundle to pass the value from current activity to next acivity
Current Activtiy to pass data
Intent i = new Intent (BundleActivity.this,datapass.class);
Bundle b =new Bundle();
b.putString("Username","your Username editext value");
b.putString("Password","your Password editext value");
i.putExtras(b);
startActivity(i);
In Next activity to receive data
Bundle b = getIntent().getExtras();
String Username= b.getString("Username");
String Password= b.getString("Password");
For passing data from onr activity to other you can use intents or shared preference.
For setting password as non revealing character in edit textbox :Set this android:inputType="phone" to editbox of your second activity in which you are showing password in Xml Layout OR from your activity you can also dynamically set this as userPassword.setInputType(129)(it will make text into password ).Hope it will help.

transfer data of 5 activity pages 26 edit text fields and calculate in last android activity

my problem is related to android application so far i was able to create 5 different activities with variables and onclick button to pass on next activity...
public class Abc extends Activity{
Button one2five;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA, tB, tC, tD, tE, tF;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
one2five = (Button) findViewById(R.id.btp1);
edtA = (EditText) findViewById(R.id.etA);
tA = edtA.getText().toString();
edtB = (EditText) findViewById(R.id.etB);
tB = edtB.getText().toString();
edtC = (EditText) findViewById(R.id.etC);
tC = edtC.getText().toString();
edtD = (EditText) findViewById(R.id.etD);
tD = edtD.getText().toString();
edtE = (EditText) findViewById(R.id.etE);
tE = edtE.getText().toString();
edtF = (EditText) findViewById(R.id.etF);
tF = edtF.getText().toString();
one2five.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openg2j = new Intent("com.sport.sport.G2J");
startActivity(openg2j);
}
});
}
}
Now with this as a base i created 4 more pages and total 26 different edittext fields.
My objective is:-
get all edit text in one calculate page.
perform claculation and post result in on One page.
Problem:-
get user text all decimal numbers
user get prompt if a field is left blank with highlighted field.
if a user press back previous activity should have previously entered values.
can any one help me to solve this issue..
thanks
monika
If you want to save the datas throughout the application then use "SHARED PEFERENCES"
Saving the values:
SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF",0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(String.valueOf(key), value);//store int values, can use string,arrays,etc
editor.commit();
Load the saved values:
SharedPreferences getProgramPrefs = this.getSharedPreferences(
"MY_SHARED_PREF", MODE_WORLD_READABLE);
int Index = getProgramPrefs.getInt(String.valueOf(key), -1);
and for this "a user press back previous activity should have previously entered values", you can try with finish() or by loading the preference values
finish();
With various methodology you can handle it
1- either define all the Field at application Level this will keep alive all the field alive untill and unless application is alive
Pros: Once defined use through out the application
cons: once app close data lost.
2- pass value in intent handle it in next activity and then combine field of previous and current activity and pass it on to next activity
Pros: use Fields in back and forth activity
cons: once app close data lost.
3- Save field in shared preferences and use it any time either app is alive or not
pros: data is saved once committed.
4- you can use ArrayList and add data in every current to next activity context switching
so its up to you with which scenario you want to use it
and if you want to have data in previous activity
you can override method onbackpressed and perform your functionality as accordingly
Sample code Using Method 2
Modified code in the question
public class Abc extends Activity{
Button one2five;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA, tB, tC, tD, tE, tF;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
one2five = (Button) findViewById(R.id.btp1);
edtA = (EditText) findViewById(R.id.etA);
tA = edtA.getText().toString();
edtB = (EditText) findViewById(R.id.etB);
tB = edtB.getText().toString();
edtC = (EditText) findViewById(R.id.etC);
tC = edtC.getText().toString();
edtD = (EditText) findViewById(R.id.etD);
tD = edtD.getText().toString();
edtE = (EditText) findViewById(R.id.etE);
tE = edtE.getText().toString();
edtF = (EditText) findViewById(R.id.etF);
tF = edtF.getText().toString();
one2five.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openg2j = new Intent("com.sport.sport.G2J");
openg2j .putExtra("field1Data", tA);
openg2j .putExtra("field2Data", tB);
openg2j .putExtra("field3Data", tC);
openg2j .putExtra("field4Data", tD);
openg2j .putExtra("field5Data", tE);
startActivity(openg2j);
}
});
}
this is how you can send data from current class to next Class
}
Edited :
in 2ndActivity you can get that data like ,
String field5Data = getIntent().getStringExtra("field5Data") ;
I think you have to create ArrayList<String> data = new ArrayList<String>(); for each activity when onClick(). Press "add all data" to ArrayList, pass this ArrayList into intent, put extra, on 2,3,4 activity you get previous activity data from getIntent() add your current activity data and pass on next activity and last activity do your calculation with previous and current data.
/**First Activity**/
ArrayList<String> data = new ArrayList<String>();
#Override
public void onClick(View v) {
data.clear();
data.add(tA);
--------------
--------------
data.add(tZ);
Intent intent = new Intent(SecondActivity.class, FirstActivity.this);
intent.putStringArrayListExtra("IN_PREVIOUS_DATA", data);
startActivity(intent);
}
/**Second Activity**/
ArrayList<String> data = new ArrayList<String>();
#Override
public void onClick(View v) {
data.clear();
data.addAll((ArrayList<String>)getIntent().getStringArrayListExtra("IN_PREVIOUS_DATA"));
data.add(tA);
--------------
--------------
data.add(tZ);
Intent intent = new Intent(ThirdActivity.class, SecondActivity.this);
intent.putExtra("IN_PREVIOUS_DATA", data);
startActivity(intent);
}

Categories

Resources