Click on Imageview and open fullscreen-image in new Activity - java

I have a MainActivity with 4 imageviews placed in 4 different card views.
What I would like to is, when clicking on one of these cardviews/imageviews it shows the image in fullscreen in a new activity.
I guess I could create 4 new activities and place a fullscreen imageview in each one of those, referencing to the selected image in MainActivity. But this approach does not seem so smooth.
I would prefer one "imageActivity" and then pass the selected imageview. Could this be done with passing the parameter for the resource id?

There can be a good solution than what I am going to offer but as even I'm learning and if it's just about 4 cardviews, I would've done the following.
Create a public static variable in MainActivity
public static Drawable resId;
set onClickListener for every Imageview like this.
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image1;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image2;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
ImageView image3 = (ImageView) findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image3;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
ImageView image4 = (ImageView) findViewById(R.id.image4);
image4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image4;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
or on the cardviews, see here or here.
Create a fullscreenactivity and put its name in those intents.
Then in the java file of this fullscreenactivity, get the reference of layout/imageview and do this.
ViewGroup viewGroup = findViewById(R.id.fullscreenLayout);
viewGroup.setBackgroundDrawable(MainActivity.resId);
OR
ImageView imageView = findViewById(R.id.fullscreenImageview);
imageView .setBackgroundDrawable(MainActivity.resId);
You can use either the imageView or the viewGroup.
And yes, Don't forget to add id of layout in XML file of fullscreenactivity or create a imagview with size same as parent layout.
If anything is confusing/not helpful, I'll help.

Related

How to get the filename of ImageView in android?

ImageView imageView = (ImageView) itemView.findViewById(R.id.imageViewMain);
imageView.setImageResource(images[position]);
Objects.requireNonNull(container).addView(itemView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Toast.makeText(view.getContext(), imageView.getthefilename,
Toast.LENGTH_SHORT).show();
As you can see in the imageView onClick event, there I want to show the name of the image loaded current in the imageview.
I have come up with the answer.
The following code rename will return the filename
TypedValue value = new TypedValue();
imageView.getResources().getValue(resourceId, value, true);
String resname = value.string.toString().substring(13, value.string.toString().length());

OnClickListener in RecyclerView (ImageView)

I am trying to do this. In the CardView of a RecyclerView, if you tap on the image, a new activity must open with a bigger version of the image. Like tapping on the image of the profile pic of Whatsapp.
So a I thought I will do it with a OnClicklisterner on the Image.
I get this error message while running it:
error: incompatible types: cannot be converted to Activity
Some help would really be appreciated, I am still new to Android and Java, and I am a bit stuck.
Here is the code I've tried so far:
#Override
public void onBindViewHolder(final MyViewTwoHolder holder, int position) {
holder.tvBrand.setText((CharSequence) modelTwoArrayList.get(position).getBrand());
holder.tvImage.setImageResource(Integer.parseInt(String.valueOf(modelTwoArrayList.get(position).getImage())));
holder.tvImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, modelTwoArrayList.get(position).getImage());
Intent i = new Intent(view.getContext(), DetailsActivity.class);
view.getContext().startActivity(i, options.toBundle());
}
});
The position in this line gives me the problem:
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, modelTwoArrayList.get(position).getImage());
Thank you for your time in advance.
My code now:
#Override
public void onBindViewHolder(final MyViewTwoHolder holder, int position) {
holder.tvBrand.setText((CharSequence) modelTwoArrayList.get(position).getBrand());
holder.tvImage.setImageResource(Integer.parseInt(String.valueOf(modelTwoArrayList.get(position).getImage())));
holder.tvImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation((Activity) ctx, holder.tvImage, "imageAnimation");
Intent i = new Intent(ctx, DetailsActivity.class);
i.putExtra("image_url", String.valueOf(holder.tvImage));
ctx.startActivity(i, options.toBundle());
}
});
And my DetailsActivity:
public class DetailsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
getIncomingIntent();
}
private void getIncomingIntent(){
String imageUrl = getIntent().getStringExtra("imageAnimation");
setImage(imageUrl);
}
private void setImage(String imageUrl){
ImageView image = findViewById(R.id.beer_logo);
}}
1) Make sure that your first parameter this is reference of Activity.
2) Make sure that your second parameter modelTwoArrayList.get(position).getImage() is ImageView of list. It seems that this is not so. It must be like this holder.tvImage
3) Provide third string parameter "imageAnimation"
All in all your code will be look something like this
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MyActivity.this, holder.tvImage, "imageAnimation");
Intent i = new Intent(MyActivity.this, DetailsActivity.class);
MyActivity.this.startActivity(i, options.toBundle());
You are having problem because ActivityOptions.makeSceneTransitionAnimation is expecting an Activity but when you do this you are giving it your Adapter.
To solve the problem change the constructor of your adapter to take the activity in
MyAdapter(Context context,Arg1,Arg2){
this.myActivityContext = context;
... other stuffs
}
Then when you initialise adapter in your Activity
MyAdapter myAdapter = new MyAdapter(this,arg1,arg2)
Then
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(myActivityContext, modelTwoArrayList.get(position).getImage());

After creating many TextViews and giving them IDs, I am trying to open an activity with their information. How do I do that?

So I can successfully create an android Text View using java and set it a value and an ID. But my problem is that after I create them and gave them values and id, I have no control over them. After clicking on them, I want the new page to have the data that was stored in that Text View.
Here is how I created and set value and ID to my Text Views:
textView = new TextView(SellActivity.this);
// put the data in the text view
textView.setText(data);
// give it an id
textView.setId(Integer.parseInt(getStrID));
//place it nicely under one another
textView.setPadding(0, 50, 0, 0);
// if clicked any of the textviews, open the offer page
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setGetStrID(getStrID);
Intent intent = new Intent(SellActivity.this, OfferActivity.class);
startActivity(intent);
}
});
// add the text view to our layout
linearLayout.addView(textView);
You just have to pass the data to the new intent,
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SellActivity.this, OfferActivity.class);
intent.putExtra("TextViewContent", textView.getText());
startActivity(intent);
}
});
Then in onCreate function of OfferActivity.java,
String s = getIntent().getStringExtra("TextViewContent");
Now the string s will contain the value of your textView.
You can put the data in the intent when you are opening a new activity
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setGetStrID(getStrID);
Intent intent = new Intent(SellActivity.this, OfferActivity.class);
intent.putExtra("mydata", textView.getText());
startActivity(intent);
}
});
In the oncreate of OfferActivity receive this data as string
String mydata = intent.getStringExtra("mydata");

setImageResources from an activity.java to change the image resource from another activity.java

I am making two activities, FirstActivity.java which contain a listView with onClickListener that start the SecondActivity.java and change the image resource of an ImageView that contained in SecondActivity.java when clicked. I set this code in FirstActivity.java :
Button button1 = (Button) findViewById(R.id.button1)
final ImageView image1 = (ImageView) findViewById(R.id.image1)
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
image1.setImageResource(R.drawable.imagexxx);
}
});
}
But when I run the application and press the button, the application crashed. Am I doing it wrong?
Button button1 = (Button) findViewById(R.id.button1)
final ImageView image1 = (ImageView) findViewById(R.id.image1)
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set Image
image1.setImageResource(R.drawable.imagexxx);
//then start another activity
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
}
});

OnClicklistener not working

I load the image programmatically and set onclicklistener but it's not working if I click the image.
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.one);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
How to do this?
First figure out if the imageView is registered to listen to onClick events. Put a toast or a Log.d message to find out if the control is going to the onClick method. If you still have issues, refer to the below link.
To set an ImageView programatically, follow this:
Adding image programmatically to RelativeLayout
You need to set View.onClick.... so replace this code
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
with below
ImgBook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TEST","in onclick");
}
});
try this code..
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.ic_launcher);
LinearLayout lyt=new LinearLayout(this);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Params.setMargins(6, 0, 6, 0);
lyt.addView(ImgBook);
setContentView(lyt);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});

Categories