I have 2 activities and 2 classes.
In my Main class, when i click submit button it will start another activity. here is the code.
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, NewActivity.class));
newActivity.setViewValues(fNameET.getText().toString(), lNameET.getText().toString(), mInitialET.getText().toString(), "Female", "birthday", addressET.getText().toString(), cNumberET.getText().toString());
}
The newActivity is an object of the other activity and the setViewValues is the method of it.
This doesn't work, this is how i do it in java gui. Maybe something is missing.
Could anyone help me with this?
You should pass the data like this.
MainActivity.java
Intent intent = new Intent(MainActivity.this, NewActivity.class));
intent.putExtra("firstName",fvalue);
intent.putExtra("lastname",lname);
......
startActivity(intent);
NewActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutname);
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstName");
String lastName = intent.getStringExtra("lastname");
}
Related
This is my MainActivity.java and I want the results in a text view of another activity? How can I achieve it? Can you Show me with an example please.
public class MainActivity extends AppCompatActivity {
private Button scan_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan_btn=(Button)findViewById(R.id.btnQr);
final Activity activity =this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
intentIntegrator.setPrompt("Scan");
intentIntegrator.setCameraId(0);
intentIntegrator.setBeepEnabled(false);
intentIntegrator.setBarcodeImageEnabled(false);
intentIntegrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null){
if (result.getContents()==null){
Toast.makeText(this,"You cancelled scanning",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this,result.getContents(),Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
This is my Second Activity. Where I want to show the result.
public class DetailActivity extends AppCompatActivity {
private TextView qrResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
qrResult= findViewById(R.id.qrResult);
}
}
If you want I can post my Layout file as well. Thankyou.
You need to create a new Intent object, and add it extra data with intent.putextra(). This method can take a String object as an argument. You need to specify a unique key for that string.Then start the new activity. For example
Intent i = new Intent(context, nextactivity.class)
i.putextra(“stringKey”,yourSstring)
startActivity(i)
Then, in the second activity, you need to get the intent that started that activity (with getIntent), you can use it as early as onCreate.
The getIntent function returns the intent object that started the new activity.
When you have the new intent, you can get the extra string you passed from the old activity, with intent.getStringExtra(“stringKey”)
This allows you to pass simple data between activities. Make sure to use the same key.
You can put data into the intent from your main acivity and the get the intend from the second activity for the data.
For example:
In your MainActivity.class
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("result", "Your result text here");
startActivity(intent);
In Your DetailsActivity.class:
Intent intent = getIntent();
String result = intent.getStringExtra("result");
qrResult.setText(result);
You can even send any type of object through intent. Please google it for further information.
I want to get the value of a TextView which is defined in another activity? I am using android studio.
You should use intents for passing values to another activities.
in first activity:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("YOURKEY", yourTextViewValue);
startActivity(intent);
Access that intent on next activity
String textViewValue= getIntent().getStringExtra("YOURKEY");
First activity (which has TextView)
SecondActivity.launchActivity(this, textView.getText().toString())
Second Activity (which need the text value)
public static void launchActivity(Context context, String textViewValue) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("textValueKey", textViewValue)
context.startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent() != null) {
String textViewValue = getIntent().getStringExtra("textValueKey");
}
...
}
I have a "Play Now" button for a simple android game. When I click the button it calls start, but it doesn't do anything.
Here is start():
public void start(View view) {
Intent myIntent = new Intent(this, Game.class);
startActivity(myIntent);
}
and Game.java:
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
Also, I didn't forget to put it into the manifest
<activity android:name=".Game"></activity>
I'm new to android and this is all very confusing. I tried putting an intent filter although I probably did it wrong.
I looked at this How to switch between screens? but it didn't work for me.
You are finishing the activity just when you create it (onCreate). Try deleting or commenting finish(); and good luck!
remove following lines, we use them with startActivityForResult , after removing it should work other than this everything is fine
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
Actually,your start function is working fine.But the problem is with onCreate() method in Game activity.You are calling finish() method in this which is killing the activity.Get rid of this method and then check.One thing more,I don't understand what is the purpose of setResult in your context.It is actually used for startActivityForResult() method.Refer to this link for further information:
https://developer.android.com/training/basics/intents/result.html
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
//Intent intent = new Intent();
//setResult(RESULT_OK, intent);
//finish();
}
}
Having issues with sending multiple pieces of data (in this case, three arrays(two int, one string)) over to a second activity page.
I am unsure of how this is done. What I would like to know is how to send these arrays in one Start Activity method, if that is possible. My current code is:
public void onClickGoToTeamSummary(View view)
{
Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);
Intent intentTeamPoints = new Intent(MainActivity.this, ResultsActivity.class);
Intent intentTeamGoals = new Intent(MainActivity.this, ResultsActivity.class);
intentTeamNames.putExtra("footballClubs", myTeams);
intentTeamPoints.putExtra("clubPoints", pointsAttained);
intentTeamGoals.putExtra("clubGoals", goalsScored);
startActivity(intentTeamPoints);
startActivity(intentTeamNames);
startActivity(intentTeamGoals);
}
I had tried:
startActivity(intentTeamPoints, intentTeamNames, intentTeamGoals);
to no avail. To help, my getIntent in the next activity looks like this:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intentClubNames = getIntent();
String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");
Intent intentClubPoints = getIntent();
int[] team_points = intentClubPoints.getIntArrayExtra("clubPoints");
Intent intentTeamGoals = getIntent();
int[] club_goals = intentTeamGoals.getIntArrayExtra("clubGoals");
}
The code itself works provided only one startActivity is used. I would like to know how to pass all my arrays into the second activity page through one activity if anyone can help me.
Try this..
You can send all values in single Intent
public void onClickGoToTeamSummary(View view)
{
Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);
intentTeamNames.putExtra("footballClubs", myTeams);
intentTeamNames.putExtra("clubPoints", pointsAttained);
intentTeamNames.putExtra("clubGoals", goalsScored);
startActivity(intentTeamNames);
}
and
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intentClubNames = getIntent();
String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");
int[] team_points = intentClubNames.getIntArrayExtra("clubPoints");
int[] club_goals = intentClubNames.getIntArrayExtra("clubGoals");
}
You don't use multiple Intents but multiple extras.
// create your Intent as normal
Intent myIntent = new Intent(MainActivity.this, ResultsActivity.class);
// then you can add multiple extras
myIntent.putExtra("footballClubs", myTeams);
myIntent.putExtra("clubPoints", pointsAttained);
myIntent.putExtra("clubGoals", goalsScored);
startActivity(myIntent);
Then receiving them would be the same. You would just receive the one Intent and use the key as normal for each extra Array.
I am trying to switch activity's with the use of a button.
Skillz.java
Button b2 =(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent();
String packageName="marco.skillz.app";
String className="marco.skillz.app.act2";
myintent.setClassName(packageName, className);
startActivity(myintent);
}
});
act2.java
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
}
When the app run in the emulator I get the following error:
The application "app name" (process marco.skillz.app) has stopped unexpectedly.
FIXED!! I feel so stupid i had android:name=".act1" when it should be android:name=".act2".
Thanks for all your input :P
Please check like this
public void onClick(View v) {
Intent myintent = new Intent(Skillz.this,act2.class);
startActivity(myintent);
}
Add act2 activity in the manifest file
Try this Skillz.java in oncreate
Button b2 =(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener()
{public void onClick
(View v) {
Intent i = new Intent(getApplicationContext(), act2.class);
startActivity(i);
}
});