myIntent.putExtra during listView onClick - android

Im trying to attach a picture with and Intent.putExtra, but im not really sure how to display the image when i send it to the next activity.
here is my onClick:
lv.setOnItemClickListener (new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, bring up Mockactivity.class
if(position == 1)
{
Intent myIntent = new Intent(view.getContext(), mockactivity.class);
myIntent.putExtra("myDrawable", R.drawable.mydrawable);
startActivityForResult(myIntent, 0);
}
It will click, and bring up a blank activity that i have named "mockactivity.class" if i take out the "myIntent.putExtra" line, but when that is there it does nothing.
Anyone know how to display that drawable in the next activity that im clicking in to?

Everything in the R class is an integer representing the actual resource.
In the other Activity you need to use something like...
int myDrawableId = getIntent().getIntExtra("myDrawable", -1);
In this case -1 is a default value that will be returned if the Intent doesn't contain an int extra with that name so test myDrawableId to see if it is -1 before trying to use it.
You should then be able to use myDrawableId in the same way as you would use R.drawable.mydrawable.
EDIT: Using this code...
ImageView view = (ImageView) findViewById(...);
...you are trying to find the ImageView and not the drawable that is used for its image source. You should be using...
ImageView view = (ImageView) findViewById(R.id.myImageView);
...where myImageView is the id of the ImageView in your mockactivity.xml layout file. If you do that correctly then simply do the following to set the image...
view.setImageResource(myDrawableId);

Related

How to interchange images in imageview in android studio?

If I want to change a image to another by the click of a button and then back to the previous image again by the click of the same button on imageview in android studio how to do that in short and easiest way? As I am new to it I am not familiar with all the functions of imageview.
For example:-
I wrote this code to do what I needed after a lot of failure in finding a easier way.
int i=0;
public void change(View v){
int img[] = {R.drawable.cat1,R.drawable.cat2};
ImageView cat = findViewById(R.id.imageView2);
if(i==0)
{cat.setImageResource(img[1]);
i=1;}
else {cat.setImageResource(img[0]);
i=0;}
}
Before I was trying to do something like this:-
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
if(cat.getDrawable()==R.drawable.cat2;)
{cat.setImageResource(R.drawable.cat1);}
else
{cat.setImageResource(R.drawable.cat1};
}
But it kept giving error that they have different type and I also tried some other functions named getId() but it didnt work either...
So my main objective is, is there a function through which I can campare the resource of image view directly with the image in drawable folder and how to implement it in if else or some other conditional statement?
The first approach should work, but the i value seems not tightly coupled to the ImageView. So, instead you can set a tag to the ImageView that equals to the current drawable id:
Initial tag:
ImageView cat = findViewById(R.id.imageView2);
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
And click listener callback:
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
int tag = (int) cat.getTag();
if(tag == R.drawable.cat2){
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
} else {
cat.setImageResource(R.drawable.cat2);
cat.setTag(R.drawable.cat2);
}
}
You could try StateListDrawable, LevelListDrawable, with each state/level, it will change image depend on your state/level

How will I maximize the imageView once the image is clicked on the ListView?

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 have 200 textviews and i want to know which one was pressed then how to change the text

I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}

displaying still images in drawables on text input on android/eclipse

I'm a newbie here, sorry for the dumb question. I am trying to display an image in drawables that is equal value with the text inputted in edittext.
For example I input "A" and a certain image will appear in the next activity after I click done.
My personal idea is that, this will be done in imageview, but how about if I enter "ABC" and 3 images equal to the inputted text will appear simultaneously.
I think array will be use in this problem but I have no idea how to start.
ImageView image = (ImageView) findViewById(R.id.iamgeActivity);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//my problem starts here
}
});
If I got you right..you can do it like in first activity, on clicking done button take the name of image from editText to next activity through bundle.
Intent intent= new Intent(currentactivity.this, nextactivity.class);
intent.putExtra("imagename",youreditText.getText().toString());
startActivity(intent);
In next activity take this image name from bundle and use following code to set image in your imageview dynamically:
ImageView image = (ImageView)findViewById(R.id.yourimageid);
int id = getResources().getIdentifier(imagename, "drawable", getPackageName());
image.setImageResource(id);

Replace the previous selected image with original image if one clicks on a new image

I am using grid view with ImageAdapter to display images.
I have two set of images that is mThumbIds containing original images and cThumbIds containing the selected images.
Right now when i click on an image i change the normal image with the selected image. The code is as below:
final ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//iv.setColorFilter(Color.LTGRAY);
iv.setImageResource(cThumbIds[position]);
//iv.bringToFront();
index= position;
}
});
iv.setImageResource(mThumbIds[position]);
But the problem arises when i click on the another image the other image also shows as selected. The correct way would be to show the new image as selected and remove the older one as selected .In other words the older one should revert back to original one.
Please help me on this
Thanks,
Pankaj
You need to create a variable and keep the clicked image's id in that. When the user clicks some other image, first reset the other image as per the id in the variable and then replace the variable value with the id of the currently clicked image.
I'm assuming you are using a modified copy of the ImageAdapter class in this tutorial and that the code you have posted is in the getView(int,View,ViewGroup) method of that class.
You save the index of the image that is selected but you don't save the view itself. You need to save both in order to revert the old image, something like this:
private int selectedPosition = -1;
private ImageView selectedView = null;
...
public View getView(int position, View convertView, ViewGroup parent) {
// I don't understand what this line is about??
ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
// Why not something like this??
// ImageView iv = new ImageView(mContext);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Set the selected image for the ImageView just pressed.
iv.setImageResource(cThumbIds[position]);
// Revert to the original image for the ImageView previously
// pressed.
if (selectedPosition != -1) {
selectedView.setImageResource(mThumbIds[selectedPosition]);
}
// Save the position and ImageView just pressed so it can be
// reverted next time an ImageView is pressed
selectedPosition = position;
selectedView = (ImageView) view;
}
});
iv.setImageResource(mThumbIds[position]);
return (iv);
}
I'm a bit confused about the line ImageView iv = (ImageView) v.findViewById(R.id.icon_image); though (as I mention in my code example).

Categories

Resources