How to pass integer to a new activity in Android? - java

I'm new to android. I am taking a string and a numerical value as input from the MainActivity.
On pressing the button, the following method is called which invokes the second activity.
Am I passing the values correctly?
If yes, how do I receive both these values for use in the second activity and then print them ?
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
intent.putExtra("lol",message);
startActivity(intent);
}
P.S. I know Im not passing the integer at all. I dont know how to do that. Please help!

From the new Activity you should call getIntent(): https://developer.android.com/reference/android/app/Activity.html#getIntent()
Once you have the Intent you have to call getIntExtra(String, int): https://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String,%20int).
In your case, from the new Activity something like:
Intent intent = getIntent();
int lol = intent.getIntExtra("lol", 0);
Or shorter:
int lol = getIntent().getIntExtra("lol", 0);
That '0' is the default value for the int if the extra "lol" does not exist.

You just use this one for integer values
In second activity....
int i = getIntent().getIntExtra(SendingStringName, 0);
(or)
String progress = getIntent().getStringExtra(SendingStringName);
(or)
ArrayList< String> progress = getIntent().getStringArrayListExtra(name);

To send the data:
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
Bundle param = new Bundle();
param.putString("greeting",message);
param.putInteger("NumberInteger",i);
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtras(param);
startActivity(intent);
To receive them:
In Class DisplayMessageActivity.java do:
Intent it = getIntent();
Bundle param = it.getExtras();
String capturedMessage= param.getString("greeting");
Integer captured_I = param.getInteger("NumberInteger");

Related

Can't pass string value

I want to create a simple calulator, and I get value of second activity of 0 no matter what I write in text View.
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
startActivity(intent);
2 activity :
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int wpis = 0;
if (extras != null) {
wpis = extras.getInt("wpis1");
}
int wpis2 = 0;
if (extras != null) {
wpis2 = extras.getInt("wpis2");
}
TextView tv = findViewById(R.id.result);
tv.setText(String.valueOf(wpis) + String.valueOf(wpis2));
As mention in the comment. You also need to link the Bundle variable with the Intent variable. See the follow link: https://zocada.com/using-intents-extras-pass-data-activities-android-beginners-guide/
//create a Bundle object
Bundle extras = new Bundle();
//Adding key value pairs to this bundle
//there are quite a lot data types you can store in a bundle
extras.putString("USER_NAME","jhon Doe");
extras.putInt("USER_ID", 21);
extras.putIntArray("USER_SELCTIONS", [1, 2, 3, 4, 5]);
...
//create and initialize an intent
Intent intent = new Intent(this, NextActivity.class);
//attach the bundle to the Intent object
intent.putExtras(extras);
//finally start the activity
startActivity(intent);
So your code needs to be:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);
Just change your code to:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);
You forgot about putExtras() method!

How to add/pass multiple values to Intent object?

i have two EditText objects in my first activity. i want both their values when i go to the next activity.
Lets assume the EditText objects are inp1, inp2 and they can only accept numbers.
please mention how i can add their values to int Intent object and how i will extract their values in my next activity's .java file.
Here we go, your code will look like,
Sender Side:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName1", intValue1);
myIntent.putExtra("intVariableName2", intValue2);
startActivity(myIntent);
Receiver Side:
Intent mIntent = getIntent();
int intValue1 = mIntent.getIntExtra("intVariableName1", 0);
int intValue2 = mIntent.getIntExtra("intVariableName2", 0);
Hope it helps.
Use this code
Intent intent = new Intent(first.this, Second.class);
Bundle extras = new Bundle();
extras.putString("value1",String.valueof(inp1.getText().toString()));
extras.putString("value2",String.valueof(inp2.getText().toString()));
intent.putExtras(extras);
startActivity(intent);
Then in your second Activity onCreate()
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String value1 = extras.getString("value1");
String value2 = extras.getString("value2");
To make thing easier and reusable you can make your own intent like this
public class MyIntent extent Intent{
private static final String FIRST_VALUE;
private static final String SECOND_VALUE;
public MyIntent(Context context, String firstValue, String secondValue){
super(context,MySecondActivity.class);
putExtra(FIRST_VALUE, firstValue);
putExtra(SECOND_VALUE, secondValue);
}
public String getFirstValue(){
getStringExtra(FIRST_VALUE);
}
public String getSecondValue(){
getStringExtra(SECOND_VALUE);
}
}
Sender:
startActivity(new MyIntent(this,"FirstString", "SecondString"));
Receiver Side:
MyIntent myIntent = (MyIntent)getIntent();
String firstValue = myIntent.getFirstValue();
String secondValue = myIntent.getSecondValue();

Android app crashing on using editText

I am new to Android and I'm trying to make an app that sends some data from a secondary activity to the initial one and after the data is sent the second activity is closed. Problem is I get this error when compiling
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String java.lang.Object.toString()' on a null object reference
public void sendData(View view)
{
Bundle bundle = new Bundle();
String msg = new String("");
String msg2 = new String("");
Intent intent1;
intent1 = new Intent(this, MainActivity.class);
EditText editText1 = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
msg = editText1.getText().toString();
msg2 = editText2.getText().toString();
bundle.putString(extra_message,msg);
bundle.putString(extra_message2,msg2);
intent1.putExtras(bundle);
startActivity(intent1);
finish();
}
I've seen other posts related to this. I've tried to initialize the strings that capture the value of the editText yet it says its null and causes my app to crash. Below I have the code that conjures my listview and some items and the point is to add another element from the second activity:
ListView list = (ListView) findViewById(R.id.listViewMain);
String[] itemsToBeDisplayed = {"Vezi Doctor", "Cumparaturi", "Facturi", "Datorie"};
arrayList=new ArrayList<>(Arrays.asList(itemsToBeDisplayed));
adapter=new ArrayAdapter<String>(this,R.layout.list_items,R.id.txtitem,arrayList);
list.setAdapter(adapter);
Intent intent = getIntent();
Bundle bundle = this.getIntent().getExtras();
String new_note = bundle.getString(Add_note.extra_message);
String notes = bundle.getString(Add_note.extra_message2);
arrayList.add(new_note);
It is within the onCreate Method. The rest of the code works just fine.
I think: In order to pass data, such as bundles, you need to also set a variable name, not just a value. What about :
intent1.putExtras("blabla",bundles);

Getting data from an Intent

I am doing a project (Android app) for uni and after a bit of help I have a login screen as per the code below:
//Do once the "Login" button is clicked
public void onClick(View view)
{
//get the users name and password
EditText editName = (EditText) findViewById(R.id.txtUserName);
String name = editName.getText().toString();
//EditText editPassword = (EditText) findViewById(R.id.txtUserPassword);
//String password = editPassword.getText().toString();
//create an Intent object and pass it the name and password
Intent intent = new Intent(this, UserLoggedInScreen.class);
intent.putExtra("userName", name);
//intent.putExtra("userPassword", password);
startActivity(intent);
}
I have commented out the the password bit for now just to get the username bit working. The aim is to click the button and put the text input into textUserName by the user into the String name. Then pass that through to activity UserLoggedInScreen.
Then in UserLoggedInScreen collect the data:
public class UserLoggedInScreen extends Activity
{
TextView welcomeUser;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.userloggedinscreen);
//get the Intent Object from LapMasterActivity
Intent intent = getIntent();
//get the data from the Intent Object
String userName = intent.getStringExtra("userName");
//String userPassword = intent.getStringExtra("userPassword");
welcomeUser = (TextView) findViewById(R.id.txtUserName);
welcomeUser.setText(userName);
}
When I try running it and clicking the button in the opening activity I get the usual "Unfortunately UserLoggedInScreen has stopped working".
I think this bit might be the error:
welcomeUser = (TextView) findViewById(R.id.txtUserName);
I tried changing txtUserName to userName but that didn't help either.
Thanks.
First Thing As Bappy said Register Your activity so that it can be detected at runtime.
<activity android:name="UserLoggedInScreen">
</activity>
Second thing
welcomeUser = (TextView) findViewById(R.id.txtUserName);
EditText editName = (EditText) findViewById(R.id.txtUserName);
You are using same id, are you doing it intentionally??
Pleas Check id of TextView of UserLoggedInScreen.
have you maked activity in AndroidManifast for Second Class........
<activity android:name="UserLoggedInScreen"></activity>

Put 2 extras in intent

I've made an intent and i've put inside 2 extras
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra(LABEL_NAME_DEFAULT, lblNameDefault);
intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault);
In my other activity i retrieve the info from them like this
//Get name from the label
Intent intent = getIntent();
String lblNameDefault = intent.getStringExtra(MainActivity.LABEL_NAME_DEFAULT);
String lbl2NameDefault = intent.getStringExtra(MainActivity.LABEL_2_NAME_DEFAULT);
//Set current name to editText
EditText labelNameDefault = (EditText)findViewById(R.id.set_name);
EditText label2NameDefault = (EditText)findViewById(R.id.set_name2);
labelNameDefault.setText(lblNameDefault, TextView.BufferType.EDITABLE);
label2NameDefault.setText(lbl2NameDefault, TextView.BufferType.EDITABLE);
The problem is that i receive to both labelNameDefault and label2NameDefault the result from LABEL_2_NAME_DEFAULT.
Can i only pass one extra?
How can i pass them both?
By default the value of labelName is "Counter 1" and the value of label2Name is "counter 2"
If i comment out intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault); the first label name is ok.
It looks like LABEL_2_NAME_DEFAULT is overwriting LABEL_NAME_DEFAULT
You could pass a 'bundle' of extras rather than individual extras if you like, for example:-
Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("LABEL_NAME_DEFAULT",lblNameDefault);
extras.putString("LABEL_2_NAME_DEFAULT",lbl2NameDefault);
intent.putExtras(extras);
startActivity(intent);
Then in your Activity that your triggering, you can reference these like so:-
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Or (if you prefer):-
Bundle extras = getIntent().getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Hope this helps! :-)
You can pass two (or more) extras in an intent. You need to make sure that LABEL_2_NAME_DEFAULT and LABEL_NAME_DEFAULT does not have the same value, though.
activity one
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra("LABEL_NAME_DEFAULT", lblNameDefault);
intent.putExtra("LABEL_2_NAME_DEFAULT", lbl2NameDefault);
activity two
username_string = getIntent().getExtras().getString("LABEL_NAME_DEFAULT");
password_string = getIntent().getExtras().getString("LABEL_2_NAME_DEFAULT");

Categories

Resources