This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 4 years ago.
I am very new to Android Studio, and have found myself stuck in this concept. I am attempting to pass Price + Name data to a Cart activity upon a button press (Add to Cart).
After attempting intent methods, it seems that after pressing "Add to Cart", the cart is opened with the data, but the data is not saved in the new activity for more additions.
Right now I have the following:
Button button = (Button) findViewById(R.id.addcart);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//How to pass information here
}
});
I am hoping to pass textView6 and textView7 to the cart activity. If possible, I would be interested in passing the image as well! Any start on this would be appreciated. Thank you!
To pass data trhoug activities you can use the same intent you use to open the new activity. You can set extras like this:
Intent i = new Intent(context, CartActivity.class);
i.putExtra("price", textView6.getText().toString());
i.putExtra("name", textView7.getText().toString());
startActivity(i);
And then in onCreate() of the just created activity you can retrieve this data getting the intent used to open this activity and getting its extras:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent data = getIntent();
String price = data.getStringExtra("price");
String name = data.getStringExtra("name");
}
Hope this help.
When you create your Intent you have to do :
Intent i = new Intent(this, ToClass.class);
i.putExtra("someName", findViewById(R.id.textView6 ).getText().toString());
i.putExtra("someName2", findViewById(R.id.textView7 ).getText().toString());
startActivity(i);
And then in your second Activity, use :
Intent intent = getIntent();
String someName= intent.getStringExtra("someName");
String someName2= intent.getStringExtra("someName2");
You say your using an intent, but what are you doing with the values? Are you saving them somewhere in the CartActivity?
For the image just pass a reference or name of the image. No need to pass the whole PNG... and again use an intent putExtra() call.
Related
I have 3 activities.
The first activity is a map (using Google Map API). In this activity, I also have a button that will direct to second activity.
In Second activity, it's just for additional information but there is a button. When this button is clicked, it will be directed to third activity.
In third activity, there's only a button that will bring a result to first activity (maps).
This is how I assigned my first activity's button:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
And I also used ActivityResultLauncher in first activity like so:
ActivityResultLauncher<Intent> intentLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if(result.getResultCode() == Activity.RESULT_OK){
String data = result.getData().getStringExtra("SIMPAN");
Log.d(TAG, "berhasil");
}
}
);
In my third activity's button, I assigned it like this:
third_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("SIMPAN", "Simpan");
setResult(Activity.RESULT_OK, intent);
finish();
startActivity(new Intent(ThirdActivity.this, FirstActivity.class)); // When the button is clicked, it will get back to first activity and bringing the result
}
});
But, turns out the first activity didn't get the result. I assume that I missing this particular code after using ActivityResultLauncher:
Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
intentLaunch.launch(intent);
However, it makes the application starts from third activity instead of first activity. It does receive the result (the log was showing) but it's only one time. How can I resolve this?
(Sorry for my bad english)
This is an overkill.
Simply:
Intent intent = getIntent();
String simpan = intent.getStringExtra("SIMPAN");
in your onCreate class
registerForActivityResult is used when you get a result from outside the app, such as getting a picture from the camera.
In my project I have such situation, that one activity should transfer the value to another. And depending of this value should be chosen needable menu element. I tried to do it with the help of bundle, but I don't know how to choose the needable element of menu. Can I access to menu item with the help of this number or I can access only with the help of the id?
As you may already now, you can start another activity with Intent which also allows you to transfer some small amounts of data like String or Integer (which you need) with .putExtra() property.
So, for your problem you will do something like this:
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Intent intent = new Intent(this, DisplayMessageActivity.class);
int menuOption = 1; // or whichever menu option you want
intent.putExtra(EXTRA_MESSAGE, menuOption);
startActivity(intent);
In another activity, you will read that value like this:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
You can read all about here in official Android documentation.
Try this
if you are using activity you can pass the data on click using intent like this
create_new_bank.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(BankDetailsActivity.this, AddBankDetailsActivity.class);
intent.putExtra("id",id);
intent.putExtra("bank_Name", bankname);
intent.putExtra("holder_name", holdername);
intent.putExtra("acc_no", accountno);
startActivity(intent);
and get the data next activity eg (AddBankDetailsActivity.this) like this:
/* using get string intent method get intent value*/
private void getStringIntent() {
Intent intent = getIntent();
String bank_id = intent.getStringExtra("id");
String accountName = intent.getStringExtra("holder_name");
String accountNo = intent.getStringExtra("acc_no");
String bankName= intent.getStringExtra("bank_Name");
Note: make sure your putExtra key value same in getStringExtra key value
it helps you
I know how to show text with a button click on the same page, but my question is (since I couldn't find anything on Google): Is it possible when you click a button, that the text shows up on another activity?
Yes, you can
In your FirstActivity execute this when button is clicked:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data","Messsage to be sent");
startActivity(intent);
In your SecondActivity inside onCreate():
String someData = getIntent().getStringExtra("data");
yourTextView.setText(someData);
If you need pass values between the activities you use this:
String name="aaaa";
Intent intent=new Intent(Main_Activity.this,Other_Activity.class);
intent.putExtra("name", name);
startActivity(intent);
And this code to recovery data on new Activity:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
You can use Intent for this. Intent is used to move on other activity from first activity.
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
So 1 have 2 ImageButtons, clothesButton1 with an image declared in xml, and imageButton2 which is blank. Both are in seperate activities.
Upon clicking clothesButton1, i want to move the image in clothesButton1 to imageButton2 using Bitmap. clothesButton1 will become blank afterwards.
Here's my code in Java for clothesButton1:
final ImageButton clothesButton1 =(ImageButton)findViewById(R.id.clothesBtn1);
clothesButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clothesButton1.buildDrawingCache();
Bitmap bitmapclothes = clothesButton1.getDrawingCache();
Intent intent = new Intent();
intent.putExtra("BitmapClothes", bitmapclothes);
}
});
In my second activity (for imageButton2):
final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapClothes");
imageButton2.setImageBitmap(bitmap);
However the moving function isn't working and I really have no idea where I am wrong. Any help is greatly appreciated.
The error is because of Intent. There are two types of Intent in android Explicit and implicit intents. When you create an Intent with a Context and a Class object, you are creating an explicit intent. You use explicit intents to start activities within your application.When an activity in your application wants to start an activity in another application, you create an implicit intent. In Your case it is Explicit Intent use Intent intent=new Intent(getApplicationContext(), secondActivityName.class).
In your case
Intent intent=new Intent(getApplicationContext(),secondActivityName.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);
To understand more about intent read this tutorial.
I think the problem is that you didn't enabled the drawing cache on clothesButton1.
clothesButton1.setDrawingCacheEnabled(true);
Please refer:https://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29
I think that you can achieve this easily with Activity Transition. Take a good look at it :)
The Start an activity with a shared element section might be what you want.
First,getDrawingCache() returns null;second the way you invoke other activity is not correct!
try this :
clothesButton1.setDrawingCacheEnabled(true);
clothesButton1.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
clothesButton1.layout(0, 0, clothesButton1.getMeasuredWidth(), imageButton.getMeasuredHeight());
clothesButton1.buildDrawingCache(true);
Bitmap bitmapclothes = Bitmap.createBitmap(clothesButton1.getDrawingCache());
clothesButton1.setDrawingCacheEnabled(false);
Intent intent = new Intent(NowActivity.this,SecondActivity.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);
Reference:Android View.getDrawingCache returns null, only null
I ask if I can pass to an other application some data using intent. If it's possible, how can I do clicking a button and passing to an other application?
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intention = new Intent(?????);
startActivity(intention);
}
});
To pass data from one activity to another you need to add it to the Intent. E.g,
Intent intention = new Intent(this, DestClass.class);
int value = 10;
intention.putExtra("KEY", value);
startActivity(intention);
and in your DestClass's onCreate(), you get it from the Intent with,
Bundle extras = getIntent().getExtras();
if (extras!= null) {
extras.getInt("KEY");
}
To send it to another application. Similarly you create an Intent as shown in the Android docs.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Note that in this case the Android system allows apps to register to receive Intents and if there are multiple apps that have registered for these Intents then the system lets the user choose which App they would like to handle the Intent.