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
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.
I want to move an adapter object from one activity to another.
My main activity:
Intent intent = new Intent(MainActivity.this, ActivityTest.class);
intent.putExtra("Key1", position);
intent.putExtra("key2", toyAdapter);
startActivity(intent);
My second activity:
Intent intent = getIntent();
int position = intent.getIntExtra("Key1", 0);
ToyAdapter toyAdapter = (ToyAdapter) intent.getSerializableExtra("Key2");
I know something's wrong but I am pretty new to this and therefore I am not sure what's the right way to move that Adapter Object to my second activity. Thanks
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.
I am making an Android application containing more than 60 buttons. Each button responds to an Activity. Each Activity contains a sound file sourced from a raw file, a TextView and an image. Is there any way that I can use Intent parameters for each button?
For example:
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile", "FirstKeyValue");
myIntent.putExtra("image", "SecondKeyValue");
myIntent.putExtra("text", "ThirdKeyValue");
startActivity(myIntent);
//Make method let suppose
public void sendMysong(String songname,String imgUrl,String text)
{
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile",songname);
myIntent.putExtra("image",imgUrl);
myIntent.putExtra("text",text);
}
// Now in receiving Activity receive intent data
Bundle extras = getIntent().getExtras();
if (extras != null) {
String fname=extras.get("soundfile");
int resID=getResources().getIdentifier(fname, "raw", getApplicationContext().getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
}
Sixty button in single layout is not a good design pattern, you must use either grid or List view.
Using List View
http://developer.android.com/design/building-blocks/lists.html
Using Grid View
http://developer.android.com/guide/topics/ui/layout/gridview.html
As far as the current problem is concerned
You can create a generalized Function as given below
public boolean startActivityFoo(String value1,String value2,String value3)
{
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile",value1);
myIntent.putExtra("image",value2);
myIntent.putExtra("text",value3);
startActivity(myIntent);
}
I am calling an Activity B from Activity A, which contains a Video View using the following code :
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intent, 0);
I am using Intent.FLAG_ACTIVITY_NO_ANIMATION to avoid transition animation while new activity is being called. But its not working for me and a black screen is coming while the transition. Is there any way to avoid this transition animation and black screen, so that the user will not come to know that the video view is being called in a new screen?
Try calling:
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intent, 0);
overridePendingTransition(0,0); //0 for no animation
if you want to do it for all activities then do it in this way:
switching activities without animation
Just assign style with no animation to each activity in manifest.
Or through code do it in this way:
Disable activity slide-in animation when launching new activity?
Took me quite some time to figure it out...
to support overriding transition when coming back from another Activity:
use overridePendingTransition in your Activity onResume.
override fun onResume() {
super.onResume()
// disable transition when coming back from an activity
overridePendingTransition(0, 0)
}