set text in textview with text from an edittext in another class - android

Im completely new to Android so apologies if this is a silly question. But my problem is this:
I have created a number of classes for each of the pages in my app and I would like to update the text in a textview of a particular class from the text in an edittext field from another class.
To be more specific, I have a login page and I want the username (input by the user in an edittext box) to be transferred to a textfield in the logged in page. Currently I am trying to achieve this by using a click listener for the log in button in the log in page:
public void sign_in_click(View view) {
EditText tv1 = (EditText) findViewById(R.id.txt_input_uname);
String username=tv1.getText().toString();
LoginDetails unamede=new LoginDetails();
unamede.setuname(username);
Intent myIntent = new Intent(view.getContext(), customer.class);
startActivityForResult(myIntent, 0);
}
So the click listener initialises a new class variable I have defined in another class like so:
public class LoginDetails {
public String uname;
public void setuname(String username){
uname=username;
}
public String getuname(){
return uname;
}
}
and it gives uname the username from the edittext box in the login page.
Then I have in the logged in page under oncreate:
LoginDetails unamed= LoginDetails();
String username=unamed.getuname();
tv1.setText(username);
but the text in the textview box doesnt get anything written to it. Now I wont be surprised if I'm doing this completely wrong but any advice would be much appreciated. thanks

What i would suggest is put the user's log in information into a SharedPreference. To transfer the date to another activity.
For example...
SharedPreferences myPrefs = getSharedPreferences("USER_INFO", 0);
myPrefsEdit.putString("USER_ID", username=tv1.getText().toString(););
//This is the username you get from edittext
myPrefsEdit.putString("PASSWORD", password);
//this is the user password you get from edittext
myPrefsEdit.commit();
In your next activity.
Get reference to your SharePreference like...
SharedPreferences info = getSharedPreferences("USER_INFO, 0);
String username = info.getString("USER_ID", "DOESNT EXIST");
String userpassword = info.getString("PASSWORD", "DOESNT EXIST");
This should do it

You could try putting the username to activity intent extra (when you don't want to persist it in the shared preferenced right now):
Intent myIntent = new Intent(view.getContext(), customer.class);
myIntent.putExtra("uname", username);
startActivityForResult(myIntent, 0);
And then in onCreate:
tv1.setText(getIntent().getStringExtra("uname"));

Related

How to put EditText info into a Intent variable

I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.

How to keep username in header

My database have two table, which is login and details.
login is used to store username (PK), and password and details is to store the username (FK), user's photo, details..
I want to keep the username shown in header so I can get the username and store it in details table.
How try to use bundles get extras() method to get the username and shown in header, but can't work if I intent back to those page.
Any idea how can I fix the username in header?
Or what should I do to ensure the user's details can store in the correct user database when user click "store button".
If you are calling Activity1 --- > Activity2
You can send the UserName by this method
Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("UserName ", UserName );
startActivity(intent);
To retrive the Extra() in Activity2, you need this code
String UserName = (String) getIntent().getSerializableExtra("UserName ");
Hope this helps
Below Edit for Better Understanding
public class Activity2 extends Activity {
private String UserName;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_2);
UserName= (String) getIntent().getSerializableExtra("UserName");
Log.i(Tag, "UserName: "+ UserName);
}
// you can call this method from click or where ever you want
private void AnyMethod()
{
Intent intent = new Intent(getBaseContext(), Activity3.class);
intent.putExtra("UserName ", UserName );
startActivity(intent);
}
}
You can have static object in the activity that object contains username and details of the user. When you intent back then you can assign current object to that static object. In this way you will be able to get the same user on previous activity.
Let's say you have an activity A from which you intent to the details activity. Create a public static user object that contains all the details of that user in A. When you intent back from details activity then assign A.user = current user object. In this way you can have the same user after you intent back and you can show username on the header on.

onClick - Outputting data onto new page or layout?

This is probably quite basic but I've only needed a use for a feature like this now. I have a button that carries out a calculation when I click on it. The output is a number. I want to put that number into a TextView on a different layout. On it's own page basically.
I can already get what I want on the same page. Just do the whole
TextView.setText();
Can anyone help me put the data onto it's own page? So when I click the button it carries out the calculation AND opens this new page to put the answer on it?
I tried putting the TextView in a new layout file and calling it via a findViewById but that gave me a force close.
Any solutions? Thanks.
EDIT: Here's the code, I'm trying to display the time on another page. See below
public void getWakeUpTime (View v) {
LocalTime localtime = new LocalTime();
LocalTime dt = new LocalTime(localtime.getHourOfDay(), localtime.getMinuteOfHour());
LocalTime twoHoursLater = dt.plusHours(2);
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
Text1.setText("Time: " + twoHoursLater.toString(formatter));
}
Right now it displays on the same page under the TextView Text1.
You can pass integer to next activity using this code:
String num;
Intent i = new Intent(this, NextActivity.class);
i.putExtra("value", num);
startActivity(i);
You can retrieve the data on next activity as shown below:
Intent i = getIntent();
String num = i.getStringExtra("value");
textview.setText("Number is: "+num);
So brother, here you want is that you have calculated a value in 1st page and you want to show it on the 2nd page am I right?
For this brother you need to pass some data(i.e. Bundle) while calling the next activity.
Here's a similar Application that I built few months back. It passes the message created in one activity to another. Hope this works for you.
The following code is from the first activity where message is created and it is bundled and sent to the next activity.
public void yourMethod(View view ){
// Fetching the message to be sent to the next activity.
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
// Remember this constant(or any string you can give). to be used on other activity.
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
And this is what I did on the onCreate Method of the Activity which was called by the previous Activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
// save the passed message as string so that it can be displayed anywhere in this activity.
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Hope My Program give you enough light to your solution. Hope that satisfies you.
Thanks brother.
You need to send the data as an Extra. Starting Another Activity has all the information you need.

How to ensure the data remain in an activity after clicking a button to another page then return to current page?

I've an application where a user creates events. The user need to retrieve a certain name from an activity which is a ListView of names list.
I'm having an issue with making sure that a name should remain in an activity after clicking a date button which links to another activity(calendar activity), then return back to the current activity.
My codes of the 3 pages:
Create_Events.java - codes for getting a certain name from ListView activity and the btnDate onClickListener which links to the another activity(calendar activity)
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
String date = bundle.getString("date");
txtDate.setText(date);
}
Bundle b = getIntent().getExtras();
if(b != null)
{
String name = bundle.getString("name");
txtName.setText("Create an event for:" +name);
}
buttonDate = (Button) findViewById(R.id.btnDate);
buttonDate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent calIntent = new Intent(Create_Events.this, Calendar_Event.class);
startActivity(calIntent);
}
});
ContactsList.java -- the ListView of the names which is passed to the Create_Events page.
Cursor cursor = null;
cursor = (Cursor) l.getItemAtPosition(position);
Intent intent = new Intent(ContactsList.this, Create_Events.class);
intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
startActivity(intent);
I need help with this. Any help provided will be greatly appreciated. Thanks in advance! =)
you can get this behavior by saving you current screen state,
you can either use shared preferences or other ways (xml,data base, ..),
this way before you leave the activity (onPause) you save any information you need..
and on (onResume) if the information exists (its not the first time the activity loads),
collect the data and put it on screen..
if this is too much for you and you only need the name string to save,
try doing this :
How to declare global variables in Android?
hope it helps...
okay what i understand from your question is you want to retain your data on screen after coming back from another activity.
like Activity A--> Activity B--> Activity A
so, set in menifest file for activity A
android:launchmode="singletop"
and, when you are coming back from Activity B to Activity A
set
Intent intent=new Intent(ActivtyB.this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
you can use SharedPreferences to store the name while use bundle to store the date.
From contactLists.java add these codes
private void SavePreferences(String key, String value)
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences()
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String name = sharedPref.getString("name", "");
}
Then set the name to the textView which will show on the listView. And then load the preference in the create_events page and the name will be shown even when you go to another activity.
Do inform me if you still have any questions. (:

Capturing login name and using it in next activity

I have a login page; when I successfully login, the logged person's name should come on the next activity like so:
Welcome xyz
How can I do that?
Do something like this and always remember Phil's comment
In Login Activity
String userName;
if(login_ok){
final Intent i = new Intent(Login.this, Welcome.class);
i.putExtras("userName",userName);//sending checked value to next activity
startActivity(i);
}
in welcome Activity
TextView view1 = (TextView)findViewById(R.id.textView1);
Bundle bundle = getIntent().getExtras();
String user=bundle.getString("userName"));
view1.setText("Welcome "+user);

Categories

Resources