how to convert an image from drawable file to an Image view
because this won't work
ImageView image= R.drawable.pic;
Try this
ImageView imageview = findViewById(R.id.imageView);
imageview.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.pic));
Try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fab.setImageDrawable(getResources().getDrawable(R.drawable.pic,this.getTheme()));
}else {
fab.setImageDrawable(getResources().getDrawable(R.drawable.pic));
Try this
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.pic);
or
ImageView image = new ImageView(this);
image.setImageDrawable(getResources().getDrawable(R.drawable.pic));
Related
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());
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?
I need to convert TextView into ImageView so I will be able to save the ImageView as an image in the sd card.
How do I convert TextView into ImageView?
Thanks.
(I saw the other questions but them are not realy answer my question).
Yes there is a way to do this, by two ways.
You can create an Bitmap of any View using buildDrawingCache() and getDrawingCache()
TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());
You can also check this answer for further reference.
In the other way , you can take your whole rootview as a screen shot and you can save it into disc.
This will help to achieve the above How to programatically take a screenshot on Android?
public static Bitmap convertViewToBitmap(View view)
{
view.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
I want add ImageView on ViewFlipper and try this code
ViewFlipper flipVertical = (ViewFlipper)findViewById(R.id.slideVer);
Bitmap bitmap = BitmapFactory.decodeFile("sdcard/vm/picture/vertical/VB-Logo.png");
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
flipVertical.addView(imageView);
nothing effect with that code, same as when I use
ViewFlipper flipVertical = (ViewFlipper)findViewById(R.id.slideVer);
Uri uriImage = Uri.parse("file:///sdcard/vm/picture/vertical/VB-Logo.png");
imageView.setImageURI(uriImage);
flipVertical.addView(imageView);
What should I do?? :(
You can try as below...
ViewFlipper flipVertical = (ViewFlipper)findViewById(R.id.slideVer);
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/vm/picture/vertical/VB-Logo.png");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
flipVertical.addView(imageView, params);
If you didn't add the permission to access SDCard then add these permission in the Manifest.xml file...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Please forgive me if this is a duplicate post but I tried and could not find anything on this subject.
I have a blank ImageView in a layout and now I want to put an image there dynamically. Since there is
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText("my text");
is there a way to do it for an ImageView like the way you would do it for a TextView?
ie...
ImageView image = (ImageView) v.findViewById(R.id.pPicture);
image.setImage(R.drawable.myImage); // I know this isn't correct.
Any help is much appreciated.
Try this
image.setImageResource(R.drawable.yourimage);
or
image.setImageDrawable(getResources().getDrawable(R.drawable.yourimage);
image.setImageResource(int resId)
If you want to display an image file on the phone, you can do this:
private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
If you want to display an image from your drawable resources, do this:
private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageResource(R.drawable.imageFileId);
To avoid every problem you can use the sure way
Resources res = getActivity().getResources();
Drawable drawable= res.getDrawable(R.drawable.myImage);
image.setImageDrawable(drawable);