I want the user to input these fields in a editText(s) and then login to the site using these fields which were previously entered (in a different activity). Also, how do I automatically click login without the user having to click it. By this, I mean in one activity, enter the login credentials and auto-login to the website in the background without the user needing to see a browser window or login using the website.
Preferably, I want the user to simply enter the URL to that page, the login info, and then eventually I want to extract information from the webpage without the user ever having to see a webview...
Furthermore, here is a link to a question posted by my colleague that expresses the same problem: Android link navigation and box input on internet
if you want to "automatically" login the user without having to click login you would have to implement a listener on text changed on the editText
something like this:
editText = (EditText)findViewById(R.id.myEditText);
editText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
//validate the user here
}
but honestly I wouldnt recommend this since you would have be query-ing your server to check everytime the user enters/deletes a single letter. And if I'm right that is not efficient nor and uses a lot of resources (i.e. battery)
And for getting the parameters from the activity previously created use a Bundle on the previous activity and send the extras to the new activity (login activity) and then use the extras on your onCreate, like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
....
}
Related
I am new to Android.I am building an app which will send/retrieve data to/from a server.So what I did is I created a login page and gave credentials for each user.So,once the user logs in,I want the user to be logged in until he signs out.I mean the log in screen should not be visible the next time the user opens the app.So, I have done that using SharedPreferences in android.
private EditText mobile,password;
SharedPreferences sharedPreferences;
mobile = (EditText) findViewById(R.id.mobileET);
password = (EditText) findViewById(R.id.passwordET);
String mob = mobile.getText().toString();
String pass = password.getText().toString();
sharedPreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(MOBILE,mob);
editor.putString(PASSWORD,pass);
editor.commit();
And when the user comes to the next screen,if the user clicks on Back button,the user should not be taken to the Login screen. So, I have used the below code in the next Activity which is the activity that comes after a user is logged in.
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
So,this is working fine and when the user clicks on the next Back button,the app is closed and if the user opens the app again,the Second activity is shown.
So,what I want is to for the Second Activity to be loaded with some data from the server whenever the app is opened.The Second Activity should fetch data from the server automatically when the app is opened.Earlier,what I did was I ran a PHP Script to fetch data from the server when the Login button is clicked. But now,since the Login screen will appear only once I am not able to figure out how to fetch the data and reload it with the latest details every time the app is opened.Also there are some validations to be done like the user can input data only once per day.So,when the app is opened,it should also check for that and if the user has already given his/her input for the day,he/she should be redirected to a "Thank You" screen.And if again the user opens the app the next day,it should open the Second Activity where the user can give his input.So how do I do these validations in the Second Activity which comes after the Login Activity. Can anyone please help me with this?
you have to call the service on second activity's on create method after setContentView() and fetch the data from the shared preferences and pass that data to the php script and catch response and show the data to the apropreate location.
and the credentials you want to give for one day duration then check the entry with the system date and validate it. if it is belong to the same date then it will pass to the thank you page otherwise it allow user to move to second activity.
I have made a login app in android .I want to display the name of the user in everytab of my app.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final TextView txtuser=(TextView)findViewById(R.id.txtUser);
txtuser.setText("Welcome "+getIntent().getStringExtra(DataBase_Server.NAME));
}
Whenever the user login his name does appear but when he clicks on some different tab and then redirects to the home tab the name of the user disappears
If the name no longer appears, it means that the intent returned by getIntent() has changed and no longer contains the name extra.
It would be better if you store the name after fetching it from the intent. How you store it depends on how long you want it to persist. You could use a preference or a database to store it, and then remove the value when the user is logged out.
What is the best approach for navigating between "windows" in an android app?
I say "windows" because I dont the proper terminology in java. I just started.
Lets say the first screen the user sees is a username and password input with a button. On successful login, it shows a whole new "window" with relevant logged in information?
I attempted putting 2 EditTexts and a Button inside a view (using the Main.xml graphical layout tab[eclipse]) in main.xml, but it did not like that.
This is a excerpt from the page http://developer.android.com/guide/topics/fundamentals.html
Activities
An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.
Here's an tutorial on how to switch between activities: http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
Don't for get to add every activity to AndroidManifest.xml!
You can consider activities as being "windows" for android.
Create different layout XMLs for your different activities and on a button click from activity A start activity B and close A... to give an example:
// in activity A for the button click:
public void onButtonClick(View view) {
Intent intent = new Intent(this, B.class);
activity.startActivity(intent);
activity.finish();
}
// in activity B you have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.B); //using your B.xml layout
}
The Android way of doing it is to use an Activity for each 'window', as you call them. You move between Activities by using startActivity().
You shouldn't have any problem putting 2 edittexts & a button inside an xml layout, so the problem you're having is probably related to something else. Have you checked the log output for errors? Something that gets a lot of newcomers is that you have to declare every Activity in your mainfest file, otherwise Android will not load it.
The "windows" in Android are called Activities and you move between them with Intents.
Intent intent = new Intent(GroupPickerActivity.this, SmsActivity.class);
startActivity(intent);
There's a lot to understand, I'd recommend going through some tutorials before diving right in. For each "window" (called content views in android), there should be an underlying Activity.
The Labs taught here from a college course at Cal Poly SLO helped me get very familiar with Android quickly.
Intent intent = new Intent(this,
otherclassname.class);
intent.putExtra("userid", userfield); //sends the userid
startActivity(intent);
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.showhistoricweek);
senduserid = getIntent().getIntExtra("userid", 0); //gets the userid
I have a form application, in this application the user clicks on a Menu option(M1) to go to a form (Frm1). This is done using Intent.
The form (Frm1) have two buttons, a back button and a submit button. When the user clicks the submit button the form is submitted, when the back button is clicked the user reaches the menu option(done using Intent).
Now, When the user clicks the back button(he goes to M1) with a half filled form, he must be able to continue with the task when he returns back( to Frm1).
My logic was to collect all the values in the form to a bundle and carry it along the way from Frm1 to M1 and vice versa.
Is this a good approach, got any better idea?
Thanks in advance..!
If data entered in the form (Frm1) is used in your Menu Activity (M1), then obviously you should use bundles and send it between Activities.
Else it may be unwanted logic of working with forms data in Menu Activity.
Just imagine, that you will create new wonderful Activity before M1 in your app (dashboard or something similar to it). Now you have to pass your bundle to first activity, because else you will lose form's state, when user close Menu Activity. It's not good, anyway.
So, if you can encapsulate this logic in Form Activity, I recommend you to make it.
You can use SharedPreferences or Singleton storage. If your form's data is strings, numbers and flags - SharedPreferences is easy, good and safe solution.
Using a bundle is exactly the right way to do this, though if the user has pressed the back button, are you sure they want to keep the data they entered? You will have to provide some way to clear the form.
You can use either Bundle or SharedPreferences.
But better to go for SharedPreferences because Bundle is restricted to the session where as shared preferences is not.
Another option would be to save the form data in the SharedPreferences. The difference between saving it in a bundle and this is that the data will persist upon an app shutdown. Saving the data into a bundle will only last during the application's lifecycle
I have a setup view where the user can enter their name and email and click done when they are finished which navigates them to another activity1. When they are in activity1, and they hit the soft back button on the phone, it takes them screen where they entered their name and email. However, the name and email EditText fields are blank.
In the view where the name and email are entered I looked to see if onStart, onCreate, OnPause, onResume,... where being called after the back button was hit, but they are not.
Do you know how I can make it so that the EditText fields have the information that was previously entered.
Well, you could store the data in a singleton class, and when they go back to the activity, it checks if those values exist and populate the EditText.
Here's how you can override the onresume:
#Override
public void onResume() {
super.onResume();
// do whatever you need to do here
}