hello there i'm a new beginner in android development. I tried several times to figure out my issue but i couldn't please help me.I created a simple login screen once the user enters the username and password I simply want to display it in the next activity. I used putExtra and getExtra but the values are null all the time.
This is my code
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
data = (TextView)findViewById(R.id.txt);
Intent i = this.getIntent();
u = i.getStringExtra("username");
p = i.getStringExtra("password");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
You've Passed the Data to the next Intent Correctly using PutExtra but in the new intent when you've used getStringExtra you've used another String variable which is completely different from what you've passed.Try this example it should work.You should use i.getStringExtra("uname"); and i.getStringExtra("pass"); instead of what you have passed.
public class Data1 extends AppCompatActivity {
EditText username;
EditText password;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data1);
username = (EditText)findViewById(R.id.edusername);
password = (EditText)findViewById(R.id.edpassword);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
}
});
}
}
public class Data2 extends AppCompatActivity {
TextView data;
String u;
String p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data2);
data = (TextView)findViewById(R.id.txt);
// Intent i = getIntent();
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
}
}
You a passing the value using
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
and getting value using
u = i.getStringExtra("username");
p = i.getStringExtra("password");
here you must be use same key as you passed in intent so you have to use "uname" instead of "username" and "pass" instead of "password"..
example :
FirstActivity.java
i.putExtra("key", "value");
secondActivity.java
i.getStringExtra("key");
You are getting null values because strings are not same in put and get method.And values are always stored in same key.So change your code in get method like as-
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");`
Related
I am newbie at this javascript and android stuff.
I'm tried to add two number taken from "EditView" and display it in other activity.
But it's didn't show up anything.
here are the code.
public void lihathasil(View v) {
thn1 = (EditText) findViewById(R.id.datat1);
thn2 = (EditText) findViewById(R.id.datat2);
thn3 = (EditText) findViewById(R.id.datat3);
thn4 = (EditText) findViewById(R.id.datat4);
thn1cus = Integer.parseInt(thn1.getText().toString());
thn2cus = Integer.parseInt(thn2.getText().toString());
thn3cus = Integer.parseInt(thn3.getText().toString());
thn4cus = Integer.parseInt(thn4.getText().toString());
ab = thn1cus + thn2cus + thn3cus + thn4cus;
Intent i = new Intent(getApplicationContext(), GeneralReport.class);
i.putExtra("pertama",ab);
startActivity(i);
}
And here are the code in other activity to view the result
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general_report);
tam1=(TextView) findViewById(R.id.tampilsatu);
val1=getIntent().getExtras().getString("pertama");
tam1.setText(val1);
}
when i run it's display nothing.
can anyone help to solved it.
thanks
You pass the integer value in one activity and get the string value in other activity.
Other activty code :
val1=getIntent().getExtras().getIntExtra("pertama",0);
tam1.setText(String.valueof(val1));
In your second Activity change this
val1=getIntent().getExtras().getString("pertama");
to this
val1=getIntent().getIntExtra("pertama", 0);
and change this
tam1.setText(val1);
to this
tam1.setText(String.valueOf(val1));
// Assuming val1 is defined as integer
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 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.
i have to activities:
activity 1 with twoo edit text and
activity 2 with a list view.
Everytime i fill the forms in activity1 and press the button "send" i want that all i have wrote in the two edit texts go in one row of the list view of activity 2.
I have tried and it is the result but i don't know how i have to continue:
this is the code of activity1 when the button is pressed:
buttonSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "Button Send Report Clicked......");
object=object.getText().toString();
description = editDescription.getText().toString();
getCoordinates();
Intent i=new Intent(FirstReporting.this, MyList.class);
i.putExtra("object", object);
i.putExtra("description", description);
startActivity(i);
}
});
this is the code of activity2:
arrayAdapterListReports arrayAdapter = new ArrayAdapterListReports(this, R.layout.item_list_reports, listReports);
listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(arrayAdapter);
and then?
In that other activity you can do this to get the values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String objStr = extras.getString("object");
String descStr = extras.getString("description");
// Do what you want with these now ...
}
Check out the Receiving Simple Data from Other Apps guide.
In your second Activity, you can retrieve the Intent used to start Activity2 using getIntent().
You can then use that Intent object to get your data like so:
Intent i = getIntent();
Bundle extras = i.getExtras();
String obj = extras.getString("object");
String desc = extras.getString("description");
I'm trying to make the button (R.id.buttonring) call any phone number entered in the textview called tvTelefon. However, when i click that button, my phone is trying to call "w4126848130,511290,549" instead of the phone number entered in the textview. Any ideas how come? I don't receive any error messages so I'm clueless! Thanks!
public class Activity2 extends Activity {
public static final String SPARAD_DATA = "sparadData";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
SharedPreferences sp = getSharedPreferences(SPARAD_DATA, Context.MODE_PRIVATE);
TextView tvRubrik = (TextView)findViewById(R.id.textViewrubrik);
tvRubrik.setText(sp.getString("rubrik", "Rubrik saknas"));
TextView tvNamn = (TextView)findViewById(R.id.textViewnamn);
tvNamn.setText(sp.getString("namn", "Namn saknas"));
TextView tvText = (TextView)findViewById(R.id.textViewtext);
tvText.setText(sp.getString("text", "Text saknas"));
final TextView tvTelefon = (TextView)findViewById(R.id.textViewtelefon);
tvTelefon.setText(sp.getString("telefon", "Telefon saknas"));
TextView tvPris = (TextView)findViewById(R.id.textViewpris);
tvPris.setText(sp.getString("pris", "Pris saknas"));
Button r = (Button)findViewById(R.id.buttonring);
r.setOnClickListener (new View.OnClickListener() {
Intent call = new Intent(Intent.ACTION_DIAL);
#Override
public void onClick(View v){
call.setData(Uri.parse("tel:" +tvTelefon));
startActivity(call);
}
});
}
}
Slight change might help you:
call.setData(Uri.parse("tel:" +tvTelefon));
It should be:
call.setData(Uri.parse("tel:" +tvTelefon.getText().toString()));
you are passing an object reference, get the string you put into it.
Actually, how I understand your posted code you can just take the phone number straight from your preferences. Try this:
call.setData(Uri.parse("tel:" + sp.getString("telefon", "")));
Instead of:
call.setData(Uri.parse("tel:" +tvTelefon));