How to get the filename of ImageView in android? - java

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());

Related

How can I get the id of element in array of imageView Android

I have an array of image Views which is generate dynamically on the run by the user
this is the code:
LinearLayout picLL = (LinearLayout) findViewById(R.id.cityInfoLN);
ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//lp.add
lp.setMargins(32, 8, 32, 8);
myImage.setLayoutParams(lp);
myImage.setImageBitmap(bitmap1);
myImages[CityImageCount] = new ImageView(this);
myImages[CityImageCount].setId(CityImageCount);
myImages[CityImageCount] = myImage;
myImages[CityImageCount].setLayoutParams(lp);
myImages[CityImageCount].setImageBitmap(bitmap1);
myImages[CityImageCount].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View v1 = inflater.inflate(R.layout.image_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(v1);
Button btn = (Button) v1.findViewById(R.id.editBTN);
final TextView editText = (TextView) v1.findViewById(R.id.textView);
editText.setText("هل تريد تعديل أم حذف الصورة");
builder.setCancelable(true);
final AlertDialog alert = builder.create();
alert.getWindow().setGravity(Gravity.CENTER);
alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alert.show();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
// EditIndexs[]
// String name = v.getResources().getResourceName(v.getId());
// String name2 = context.getString(v.getId());
// String name3 = getString(view1.getId());
// String name4 = getResources().getString(view1.getId());
EditIndex = CityImageCount;
int xxx = view1.getId();
showFile5();
alert.cancel();
}
});
}
});
picLL.addView(myImages[CityImageCount]);
My problem is when the user click on any of the images to edit or delete it. How can I know which image is clicked?
First, you didn't correctly set the image view with this code. It's because you set the ImageView to the array twice with different ImageView (Please read the comment in the code:
// You're creating a ImageView
ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(32, 8, 32, 8);
myImage.setLayoutParams(lp);
myImage.setImageBitmap(bitmap1);
// You're setting a new ImageView to array
myImages[CityImageCount] = new ImageView(this);
// then you set the Id
myImages[CityImageCount].setId(CityImageCount);
// But then you discard it by setting the array item with myImage
// so you're discarding the id.
myImages[CityImageCount] = myImage;
So, you need to set the ImageView to array like this:
ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(32, 8, 32, 8);
myImage.setLayoutParams(lp);
myImage.setImageBitmap(bitmap1);
myImages[CityImageCount] = myImage;
myImages[CityImageCount].setId(CityImageCount);
(Note: Instead of array, you can use ArrayList)
Second, you need to use generateViewId() or ViewCompat.generateViewId() when setting the view id. You can't manually set view id with simple loop counter id like this:
myImages[CityImageCount].setId(CityImageCount);
it should be something like this:
// View.generateViewId() is only available from api 17
myImages[CityImageCount].setId(View.generateViewId());
// Use ViewCompat if you need to support API < 17
// myImages[CityImageCount].setId(ViewCompat.generateViewId());
Third, you need to handle the image click by checking the id:
myImages[CityImageCount].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// Check the id
String id = v.getId();
...
}
In case you need specific key for the image, you can use HashMap instead of Array like this:
Map<String, ImageView> map = new HashMap<String, ImageView>();
...
ImageView myImage = new ImageView(this);
map.put("Id1",myImage);
Last, separate your logic to something like this:
private ImageView generateImageView() {
ImageView myImage = new ImageView(this);
// set the ImageView properties here.
...
return myImage;
}
private void addImageViewToList(ImageView imageView) {
// Add ImageView to the array
}
// hold previous click listener for ImageView.
private View.OnClickListener mImageViewClickListener;
// Get View.OnClickListener for ImageView, create it if not yet initialized.
private View.OnClickListener getImageViewClickListener() {
if(mImageViewClickListener == null) {
mImageViewClickListener = new View.OnClickListener() {
#Override
public void onClick(final View v) {
// handle the ImageView click here
...
}
}
return mImageViewClickListener;
}
So that you can arrange your code to something like this:
ImageView imageView = generateImageView();
imageView.setOnClickListener(getImageViewClickListener());
addImageViewToList(imageView);
setTag() and getTag() methods of view might help you. here I paste the link of setTag() and getTag() usage.
What is the main purpose of setTag() getTag() methods of View?

Click on Imageview and open fullscreen-image in new Activity

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.

Setting onclickListener on ImageView to view enlarged image

In my app I have a button which opens camera and captures image.When I capture the image I set it in ImageView.
I want to add a feature like whatsapp profile pics like when I click on the image it enlarges.
For that I have set an onclicklistner to the ImageView and defined a dialog.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Dialog imagedialog = new Dialog(MainActivity.this);
imagedialog.setContentView(R.layout.imagedialog);
ImageView photo = (ImageView) imagedialog.findViewById(R.id.photoenlarge);
}
});
In that dialog I have a ImageView which will show the enlarged image.
My camera code:
btncapture.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_REQUEST);
}
});
Code of onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
In the imageView.setOnClickListener method how can I set ImageView photo with the captured image??
Instead of using a dialog you can follow this guide from the android docs to do it.
What it does is having 2 imageviews on the same page, with the one showing the image at full size hidden, when clicking on the small one, the big one is shown with an animation.
You have to something like this
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Dialog imagedialog = new Dialog(MainActivity.this);
imagedialog.setContentView(R.layout.imagedialog);
ImageView photo = (ImageView) imagedialog.findViewById(R.id.photoenlarge);
// get the applied image from the imageview as a bitmap
Bitmap image=((BitmapDrawable)imageView.getDrawable()).getBitmap();
// set bitmap image to dialog image view
photo.setImageBitmap(image);
imagedialog.show();
}
});

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