I have an Imageview that when user clicks on it a dialog box opens and it is suppose to show image larger within it.
the Imageview I have is in a layout and the code is this:
ImageView image_terrain = (ImageView)findViewById(R.id.imageView2);
image_terrain.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Universitymap.class);
intent.putExtra("imageName", "sattelite");
Dialog d = new Dialog(Universitymap.this);
d.setContentView(R.layout.image_dialog);
d.show();
}
});
I use intent for sending which picture clicked by sending it's name.
now in image_dialog layout I have this code:
Intent intent = new Intent();
String fileName = intent.getExtras().getString("imageName");
loadImage(fileName);
and the function that loads image in image_dialog layout class is:
private void loadImage(String fileName){
ImageView img = (ImageView)findViewById(R.id.img_Picture);
int resID = getResources().getIdentifier(fileName, "drawable", "com.neema.smobile.Main");
img.setImageResource(resID);
}
one word = it doesn't work. I would be happy if anyone help.
Try this
int imageResource = getResources().getIdentifier(fileName, null, getPackageName());
imageview = (ImageView)findViewById(R.id.img_Picture);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
Why are you passing the filename via an Intent? If you are creating the Dialog in the same activity you could create a custom Dialog with a custom Constructor and overgive the filename.
Also i think this line doesn't make sense:
Intent intent = new Intent();
String fileName = intent.getExtras().getString("imageName");
Since you are trying to catch the String from a new Intent, instead of the Intent you sent.
Or did I missunderstood youre way?
The Intent is for starting Activities not for Dialogs :)
Edit:
class CustomDialog extends Dialog{
private String fileName;
public CustomDialog(String fileName){
this.fileName = fileName;
this.setContentView(R.layout.your_dialog_layout);
}
#Override
public void onCreate(Bundle savedInstanceState){
ImageView image = (ImageView) findById(R.id.image);
image.setBackgroundResource(....//set your Image like mentioned from kumaand using your saved fileName
}
}
Should work. For more Information you could look at http://about-android.blogspot.de/2010/02/create-custom-dialog.html
Related
I'm trying to send an image to zoom activity from my main_activity.
I have a onclick function:
case R.id.imageViewHero:
String image = ViewHolder.this.post.getImageUrl();
Intent intentv = new Intent(context, Zoom.class);
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intentv.putExtras(extras);
context.startActivity(intentv);
break;
the problem is the string image, I don't know what to do next to send it to zoom. Any ideas?
my zoom.class if needed:
public class Zoom extends Activity {
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoom);
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
ImageView imgDisplay;
Button btnClose;
imgDisplay = (ImageView) findViewById(R.id.imgDisplay);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Zoom.this.finish();
}
});
imgDisplay.setImageBitmap(bmp );
}
}
The code is correct for most part except one mistake in making the Bitmap from the image URL.
You cannot make a Bitmap image from an image URL like you have done.
Its already been answered on StackOverflow.
Check this link on how to convert an Image from an Image URL to a Bitmap - How to get bitmap from a url in android?
Basically, you are getting an Image URL from Intent which you are passing from the previous Activity, in the Zoom Activity, save it to a String variable, make Bitmap from it, set the Bitmap to the ImageView.
In onClick method, you seem to send imageUrl. However in Zoom.java you're treating it as Bitmap.
You can use a library like Picasso to populate ImageView using Image URL
I have setup 2 activities - one and two .
For activity 1, I have a EditText and a button. When user open the app, it will show Activity One(just like the screenshot) to prompt user to key in a number. For example, 1 or 2.
What I am trying to do is that: I want to display a ImageView in activity 2 when user key in a number in EditTextfollow by a click on the button in activity 1.
If user key in 1, it will display 1.png in activity 2
If user key in 2, it will display 2.png in activity 2
If user key in 3, it will display 3.png in activity 2
etc...
the image will get from drawable folder.
Please refer to this screenshot
[![enter image description here][1]][1]
I can pass the integer value through Intent from activity 1 to 2 but I can't do it for ImageView. so that means the if else loop i have already done just that the ImageView cant display.
get_image.setBackgroundResource(R.drawable.1); // here i can only key in 01 ( it will get image from Drawable folder 1.png). i cant put int value into it.
Or i shouldn't use get_image.setBackgroundResource?? Anyone can help? I stuck here for 1 day...
thanks in advance!
please check screenshot -> http://i.stack.imgur.com/53vjy.jpg
You said that you can pass integer value from activity 1 to 2 so just use that to find your image.
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) {
//set 01.png
} else {...}
I may missing somethings because i can't understand when you said that "but i cant do it for imageview".
EDIT: after your addtional code.
So you must map your integer value with your resource file name. You could not put your integer value to get_image.setBackgroundResource(R.drawable.id).
I think in your situation you should use an array just store id of resource you need int[] drawableIds = {R.drawable.01, R.drawable.02} in your Activity2
and then use like this get_image.setBackgroundResource(drawableIds[yourIntegerValue-1]) (ofcourse you should take care array out of index when you use this method).
Try below code for your solution,
For Activity One write below code to redirect on activity 2
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedNumber", editText.getEditableText.toString());
startActivity(intent);
Now In Activity 2 write below code in onCreate method
int selectedNumber = 1;
if(getIntent().getExtras() != null)
{
selectedNumber = getIntent().getExtras().getInt("SelectedNumber");
}
switch(selectedNumber)
{
case 1: // set your 01.png Image
break;
case 2: // set your 02.png Image
break;
// And so
}
Try this way might helps you.
String mDrawableName = "1"; //editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
then your Activity2 use,
setImageResource(resID);
(or)
setImageDrawable(getResources().getDrawable(resID));
Finally,
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String mDrawableName = editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Intent ii=new Intent(Activity.this, Activity1.class);
ii.putExtra("resId", resID);
startActivity(ii);
}
});
Activity2
public class Activity2 extends Activity
{
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
img = (ImageView)findViewById(R.id.img);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
int drawableId =(int) b.get("redId");
img.setImageResource(drawableId);
}
}
}
you can pass resource value in extras. try this way.
EditText edit = (EditText) findViewById(R.id.yourEdittext);
Button btn = (Button) findViewById(R.id.yourButton);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
if(edit.getText().toString().trim().equals("1")){
i.putExtra("image", R.drawable.01);
startActivity(i);
}else if(edit.getText().toString().trim().equals("2")){
i.putExtra("image", R.drawable.02);
startActivity(i);
}else{
Toast.makeText(getApplicationContext(), "Please Enter Valid Value.",
Toast.LENGTH_SHORT).show();
}
}
});
and in your ActivityTwo.java
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
imageView.setImageResource(getIntent().getIntExtra("image",0));
Happy Coding.
In activity 2's on create
get_image= (ImageView)findViewById(R.id.imageView);
then use switch case for values from intent then
switch(intentValues){
case 1:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.1));
break;
case 2:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.2));
break;
}
I want to display links which have format such as/sdcard/Pictures/ etc in textview as clickable link with highlight and underline. I often use Linkifyfor display links, however, this does not support storage links (only web, email etc).
Is there any possible way for that?
I would recommend displaying the image in an ImageView in an Activity.
You can put logic in your onClick(View v) to open the contents of the TextView in a new activity:
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowImageActivity.class);
intent.putExtra("imageLocation", v.getText());
context.startActivity(intent);
}
Then in your ShowImageActivity's onCreate() :
ImageView jpgView = (ImageView)findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/sample-1.jpg");
jpgView.setImageBitmap(bitmap);
setContentView(R.layout.main);
https://stackoverflow.com/a/9509948/5486718
If you need to you can alternatively add a custom intent to the linkify:
String newActivityURL = "content://com.example.yourapp.yourtargetactivity/";
Pattern pattern = Pattern.compile("[/sdcard/]+[[a-f][0-9]]+[.jpg]");
Linkify.addLinks(hashView, pattern, newActivityURL);
I have a listview, If the image is clicked on the listview I want it to be seen in a fullscreen
here is my code: that is when the listview is being clicked
ImageList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String Userid = ((TextView) view.findViewById(R.id.user_id)).getText().toString();
String ScrapBookId = ((TextView) view.findViewById(R.id.photo_id)).getText().toString();
// ---Starting new intent
Intent in = new Intent(getApplicationContext(),FullImageActivity.class);
in.putExtra("userid", Userid);
in.putExtra("IMAGE",ScrapBookId);
// ---starting new activity and expecting some response back
startActivityForResult(in, 100);
//Toast.makeText(getApplicationContext(), eventId, Toast.LENGTH_SHORT).show();
}
});
then it will turn into the FullImageActivity here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
int imgid = getIntent().getIntExtra("IMAGE", 0);
ImageView img = (ImageView)findViewById(R.id.imageView1);
TextView txt = (TextView)findViewById(R.id.photo_id);
img.setImageResource(imgid);
txt.setText(imgid);
}
}
but I think there is no image passed :( how will I do it? please help me :( Thank you
You have added a String extra with the key "IMAGE" in the first activity. In the second activity you are trying to obtain it with getIntExtra("IMAGE", 0). There is no int extra with this key, hence this call will return zero.
I'm not sure exactly what you are trying to do, but in any case the type of extra you are trying to obtain should match the type that you added to the intent.
There is no image passed to the new Activity. Your image identifier is a String ("ScrapBookId") but you are treating it as a resource Identifier (Integer) in the OnCreate.
Assuming your drawables are resources (which they appear to be), record the resourceId you set for each imageView as follows:
iv.setTag(R.drawable.ponies);
Then in the onItemClick, lookup the resourceId:
ImageView iv = ((ImageView) view.findViewById(R.id.imageview1));
int resId = (Integer) iv.getTag();
in = new Intent(...)
in.putExtra("IMAGE", resId);
Then in the onCreate:
resId = getIntent().getIntExtra("IMAGE", 0);
img.setImageResource(resId);
I created an ImageView and in my activity every few seconds i change the picture to a different one. I also have a button than when press, is supposed to get me the resource id of the picture currently being displayed, the problem is that everytime i do:
ImageView view = (ImageView) findViewById(R.id.imageView1);
It always gives me the resourceId defined in the xml, not the one i changed dynamically, how can i achieve this? here's the code in my activity class:
public void startImages(View v) {
...
delayedImage(0);
}
private void delayedImage(final int index) {
//stop at the 5th picture
if (index > 5) return;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
String drawableId = "picture" +index;
int resourceId = getResources().getIdentifier(drawableId, "drawable", getPackageName());
ImageView view = (ImageView) findViewById(R.id.imageView1);
view.setImageResource(resourceId);
delayedImage(index+1);
}
}, 500);
}
So every 5 second a new image is set, when i click on the button Share I want to retrieve whatever resourceId is currently in the ImageView set in the method above.
//send image to send text
public void shareImage(View v){
ImageView view = (ImageView) findViewById(R.id.imageView1);
??????
}
I checked the ImageView object while debugging and I saw someone get the int by doing this:
Field f = ImageView.class.getDeclaredField("mResource");
f.setAccessible(true);
Object out = f.get(view);
But i'm unable to get the actual R drawable name from this which I need when i'm trying to retrieve it from
String path = "android.resource://your.package.name/" +getPackageName() +"/" +???;
.. can anyone please help?