Set visibility for a group of images - android

Let's say I have 4 images:
ImageView a = (ImageView) findViewById(R.id.a);
ImageView b = (ImageView) findViewById(R.id.b);
ImageView c = (ImageView) findViewById(R.id.c);
ImageView d = (ImageView) findViewById(R.id.d);
And then I set the visibility of those images by this:
a.setVisibility(View.INVISIBLE);
b.setVisibility(View.INVISIBLE);
c.setVisibility(View.INVISIBLE);
d.setVisibility(View.INVISIBLE);
Is there anyway I can group those 4 images and then set the visibility to that group of images?
Something like this:
images[] = {a,b,c,d};
images.setVisibility(View.INVISIBLE);
Thank you.

If you don't want to use a ViewGroup your basic idea is good. I use it often. Just do
ImageView images[] = {a,b,c,d};
for (ImageView view : images) {
view.setVisibility(View.INVISIBLE);
}

The usual approach would be to have all the views in the same ViewGroup i.e. a layout and then set the visibility of the group in order to apply it to all children. But this really depends on how the views are laid out in the first place.

You can put all those images in layout and then control visibility of that layout.

Related

how to set background to imageView by imageview casted View?

I want to drop an image over an image and I have a code in which View is type casted by ImageView with v.name 'dropped' and this view will display on other ImageView named 'droptarget' as background but cant happend, plzz tell me a solution. my code is here.:
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a target view
View view = (View) event.getLocalState();
//stop displaying the view where it was before it was dragged
view.setVisibility(View.INVISIBLE);
//view dragged item is being dropped on
ImageView dropTarget = (ImageView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
//it give me error in this line
**dropTarget.setBackgroundResource(dropped);**
You should give us more info about the code but anyway I'm going to suggest something.
I think you're calling setBackgroundResource on a null object. So you're mistake is on the next line
ImageView dropTarget = (ImageView) v;
There probably isn't a view called "v". Maybe you should change that?
I believe you are looking for ImageView.setImageResource. You can also set the backgroundImage, but best practice is to use setimageResource.

Get position of dynamic Image View

Outline
My application has a main Image View and below main view, it has dynamic horizontal imageview .
Code for dynamic image view
ImageView imageall = new ImageView(FullImage.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(175, 175);
params.setMargins(10, 0, 0, 0);
imageall.setLayoutParams(params);
imageall.setBackgroundResource(R.drawable.imageview_border);
Picasso.with(context_tab1).load("http://example.com//upload/image/" + stringList.get(in)).into(imageall);
// Adds the view to the layout
layout.addView(imageall);
imageall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer) v.getTag();
image = "http://example.com/x/upload/image/" + stringList.get(position);
Picasso.with(context_tab1).load(image).into(fullimage);
}
});
}
I populate the dynamic imageview from array list. Everything works fine till this part.
Problem
Now, On Click of the dynmic imageview must get that image view position and change as the main imageview .
This is not working for me.
For better understanding please find the image attached.
Thanks.
No need of getting position of dynamic ImageView..
While creating the dynamic ImageView you can set Ids to them and also set the onClickListener on them. Override the Onclick method, check the Id with your Ids you gave to dynamic ImageView and replace the Main view's as per the ImageView clicked..
Happy coding.. :)

android: one click listener on many imageviews in layout

I have the following scenario:
In a layout, I have imageviews aligned horizontally and vertically as shown in image.
First, which layout should be used for this purpose? RelativeLayout or FrameLayout? ListView inside the layout?
Also, instead of writing setOnClickListener for every imageview, how can I write just one click listener to get the clicked imageview?
A GridView is perfect for this. For more information on GridView, look here.
As for your onClickListener question: there is no easy way to do this, but what you can do is something like this:
Have an array containing every ImageView id like so:
public static final int[] IMAGE_VIEWS = {R.id.imageView1, R.id.imageView2, R.id.imageView3 /*etc*/}; //Make sure to list from the first imageview to the last image view in correct order
Define an onClickListener
private View.OnClickListener imageViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < IMAGE_VIEWS.length; i++) {
if (v.getId() == IMAGE_VIEWS[i]) {
doSomethingWithImageViewClick(i); //if ImageView 4 was clicked, variable 'i' will be 3 (because arrays start at index = 0).
}
}
}
}
Set the onClickListeners for all your imageViews:
final ImageView view1 = (ImageView) view.findViewById(R.id.imageView1);
view1.setOnClickListener(imageViewListener);
//etc
Use 'GridView' in the layout and use 'setOnItemClickListener' to handle click events,
You can find more details in below link
GridView.
GridView can achieve the effect. I think you can look at this Grid View.

How to show data after Intent.ACTION_VIEW?

I'm creating search for my app and one of the last things I need to do is to "extract" the data when the user clicks on search-suggestion. Ive set Intent.ACTION_VIEW and got data by intent.getData(); in my searchable activity. But what should i do now to view the objects such as TextViews and ImageView "packaged" in the data? I have no big experience with this, so could you give me any advice please?
Thanks a lot
My example of code:
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
setContentView(R.layout.activity_more);
intent.getData();
final ProgressBar progress = (ProgressBar) findViewById(R.id.more_progress);
progress.setVisibility(View.VISIBLE);
TextView about = (TextView)findViewById(R.id.more_about);
TextView animal = (TextView)findViewById(R.id.more_animal);
final ImageView imgUrl = (ImageView)findViewById(R.id.more_imgUrl);
//now do i need to set Text etc., but how?
}
Just add them to any layout in your activity, and they will be shown in it.
How to Programmatically Add Views to Views
For expample, you have a LinearLayout with id mylayout.
LinearLayout layout = (LinearLayout) findViewById("mylayout");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(myView, lp);
Alternatively, you can dispose of views and use only data from them - text from TextView, drawable from ImageView and etc.

How to retrieve an image from ImageView when Click on that image?

i have a ImageView in the layout, when i click on the image i want to get that image into a variable and replace with another image in this ImageView. please help me..
The onClick Listener will give you a View, that's the ImageView that was clicked. Cast it to an ImageView and do whatever you want with it.
in this example i have take previous image in Drawable and replace i with new image. if you set any imageview to image which stay in drawable variable(d) then use :: setBackgroundDrawable(d); is useful
public void onClick(View v){
ImageView i;
i = (ImageView) findViewById(R.id.img);
Drawable d = i.getBackground();
i.setBackgroundResource(R.id.secondImage);
}

Categories

Resources