Pass variable value to another TextView Activity - java

As the step value of a variable of an Activity for a TextView other Activity? Can Help?
Thank you!
((See explanation on the picture))

Intent activityTwo = new Intent(this, Activity2.class);
activityTwo.putIntExtra("key", sumSettlement);
startActivity(activityTwo);
Now, in Activity2:
if(getIntent() != null) {
textView.setText(String.valueOf(getIntent.getExtra("key"));
}

You can use of local broadcast receiver.
First register receiver in Activity B
//in onCreate Method
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event-name"));
// It will be called whenever an Intent
// with an action named "my-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
// show this message in textview
}
};
In Activity A
//broadcast this
Intent intent = new Intent("my-event-name");
intent.putExtra("message", Integer(sumSettlment).toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

the easiest way is to use Intent: in first Activity
Intent intent =new Intent(CurrentClass.this,DisClass.class);
intent.putExtra("myTextValue",textView.getText().toString());
startActivity(intent);
in the dist activity do the following :
String myValue=getIntent().getExtra().getString("myTextValue");
textView.setText(myValue);

Related

I am trying to put a intent to my app so i can navigate

I am trying this intent so I can navigate from one activity to another activity but I'm getting this error `
final Button btnAdd = findViewById(R.id.addEmp);
btnAdd.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(v.getId() == R.id.addEmp){
Intent intent = new
Intent(getCallingActivity(),AddEmployee.class);
startActivity(intent);
}
}
});`
there is a red line under
(getCallingActivity(),AddEmployee.class);
there error says
cannot resolve constructor
Is anything wrong with this
(getCallingActivity(),AddEmployee.class);
statement?
You need to change your line from
Intent intent = new Intent(getCallingActivity(),AddEmployee.class);
to this:
Intent intent = new Intent(YourActivityName.this,AddEmployee.class);
OR
Intent intent = new Intent(getApplicationContext(),AddEmployee.class);
Edit
Whats wrong with getCallingActivty()?
getCallingActivity() returns ComponentName while intent constructor requires Context as a first argument.
Hope this will work

Android Studio: How to send, and recieve a Class in diferent Activities

Im making a quiz game with questions on diferent topics
For Example i have activities for these topics: Flags, Capitals, Population, Economy, Continent, etc.
And i have one single ResultActivity to obtain the Score of the quiz.
The ResultActivity has a PLAY AGAIN button.
On the FlagsActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "FlagsActivity");
startActivity(intent);
On the CapitalActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "CapitalActivity");
startActivity(intent);
etc.....
On the ResultActivity i have this code:
activity = getIntent().getStringExtra("NAME_ACTIVITY");
public void playAgain(View view){
if(activity.equals("FlagsActivity")){
Intent intent = new Intent(getApplicationContext(), FlagsActivity.class);
startActivity(intent);
}
if(activity.equals("CapitalActivity")){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
if(activity.equals("PopulationActivity")){
Intent intent = new Intent(getApplicationContext(), PopulationActivity.class);
startActivity(intent);
}
if(activity.equals("EconomyActivity")){
Intent intent = new Intent(getApplicationContext(), EconomyActivity.class);
startActivity(intent);
}
if(activity.equals("ContinentActivity")){
Intent intent = new Intent(getApplicationContext(), ContinentActivity.class);
startActivity(intent);
}
}
Basically im sending an Intent with a String containing the name of the activity, then on the Result Activity evaluating with "if" the String = That activity name, start the activity.
What i want to do is someting like this:
On the Flags Activity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(FlagsActivity.class);
startActivity(intent);
On the CapitalActivity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(CapitalActivity.class);
startActivity(intent);
On the Result Activity:
activity = getIntent();
public void playAgain(View view){
Intent intent = new Intent(getApplicationContext(), activity);
startActivity(intent);
}
So that i can create as many quiz activities without having to create an "if" statement on the ResultActivity for it to work.
You can pass the class name as a string and use reflection to look up a class object for that type. Something like this:
Class classToLoad = Class.forName(getIntent().getStringExtra("NAME_ACTIVITY"));
Intent intent = new Intent(getApplicationContext(), classToLoad);
startActivity(intent);
I would pass the fully qualified class name to avoid errors.
You could pass the String of activity name to the Result activity, and get its corresponding class name using reflection:
String strActivity = "com.package.FlagsActivity";
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("activity_name", strActivity );
startActivity(intent);
String activityName = getIntent().getStringExtra("activity_name");
Class<?> myClass = Class.forName(activityName );
Intent myIntent = new Intent(getApplicationContext(), myClass);
Something like this.
In the Results Activity
public static void toActivity(Context context, final Class ActivityToOpen){
//whatever awesome code you would like to perform
Intent intent = new Intent(context, ActivityToOpen);
startActivity(intent);
}
In the originating Activity (Flags Activity for example)
ResultsActivity.toActivity(FlagsActivity.this, FlagActivity.class);
Check for typos... I kinda winged this :)
You should be able to call from any originating Activity without if statement.
When you receive the data
String activity;
Bundle bundle = getIntent().getExtras();
if (bundle != null){
activity = bundle.getString("NAME_ACTIVITY");
}

How to get value of a TextView which is in another activity?

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");
}
...
}

sending a data to another activity with putExtra and doesn't work java android

I used intent to send a primarykey of my app to another activity but it debug my application I don't know why?
I'm sure that I put the syntax corrrectly and I tried many way I used bundle object but the same result the application has stopped
Activity iden_espvol
Intent intent=new Intent(iden_espvol.this,verf_tel_espvol.class);
intent.putExtra("TEL",tel.getText().toString());
startActivity(intent);
finish();
Activity verf_tel_espvol
Intent intent = getIntent();
String sent=intent.getStringExtra("TEL");
Try this code.. and i hope you define both activity in manifest file..
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(LayoutActivity.this,MainActivity.class);
intent.putExtra("TEL","65421321");
startActivity(intent);
finish();
}
});
and in second activity onCreate() method
Intent intent = getIntent();
String sent=intent.getStringExtra("TEL");
Log.d("Data",sent);
make sure value get only oncreate or onResume method because first activity finished that time call second activity this method.

Adding a list of scan results from Wifi to an intent and retrieving from a broadcast receiver?

I want to add a list as a parameter to pass into an intent and then receive it from a broadcast listener, but I'm having some trouble. I cannot figure out how to put this List into the Intent as an extra, or retrieving the list from it. I can get into the broadcast receiver.
//In my Main File: Everthing is registered and working.
IntentFilter startUsingScanResults = new IntentFilter("StartUsingScanResults");
c.registerReceiver(serviceConsume.ScanResultReceiver, startUsingScanResults);
List<ScanResult> scanResults = Some values;
Intent intent = new Intent();
intent.setAction("StartUsingScanResults");
// Then Need to put the List<ScanResults> into the intent.
// ie: intent.putExtra("MyResults", scanResults);
Context.sendBroadcast(intent);
// My broadcast receiver that should have the list inside it.
public BroadcastReceiver ScanResultReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
// Need something here to get the list
// ie: List<ScanResult> scanResults = extras.getBundle("MyResults");
}
};
Hopefully I am clear with this question. I just need to put the list into and get the List from the bundle (or intent).
A ScanResult is in the format of ["","","","","","",""] if that helps. So I guess it might be similar to a multidimensional array.
Any help is appreciated! Thanks
I've figured it out. I like simple solutions, and this is pretty much as simple as it gets.
intent.putParcelableArrayListExtra("ScanResults", (ArrayList) scanResults);
And Add this to the Broadcast receiver
ArrayList scanResults = extras.getParcelableArrayList("ScanResults");
So the end result is:
//In my Main File:
IntentFilter startUsingScanResults = new IntentFilter("StartUsingScanResults");
c.registerReceiver(serviceConsume.ScanResultReceiver, startUsingScanResults);
List<ScanResult> scanResults = Some values;
Intent intent = new Intent();
intent.setAction("StartUsingScanResults");
intent.putParcelableArrayListExtra("ScanResults", (ArrayList<? extends Parcelable>) scanResults);
Context.sendBroadcast(intent);
// And my broadcast receiver
public BroadcastReceiver ScanResultReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
ArrayList<ScanResult> scanResults = extras.getParcelableArrayList("ScanResults");
}
};
Hopefully this helps out someone in a similar situation.

Categories

Resources