Change property of multiple image button - android

My activity has 9 image buttons, I would like to change certain properties of multiple image buttons like disable all the image buttons in a certain situation & then enable them back. I think may be i can use loop something like this....
// int [] ids = { R.id.imgBtn1, R.id.imgBtn2......, R.id.imgBtn9 };
// for (int i=0; i<=ids.length; i++){
// ids[i].setEnabled(true);
// }
Thank you all

define one list like follow:
List<ImageView> images = new ArrayList<>();
ImageView iv1 = (ImageView)findViewById(R.id.image1);
ImageView iv2 = (ImageView)findViewById(R.id.image2);
ImageView iv3 = (ImageView)findViewById(R.id.image3);
ImageView iv4 = (ImageView)findViewById(R.id.image4);
ImageView iv5 = (ImageView)findViewById(R.id.image5);
ImageView iv6 = (ImageView)findViewById(R.id.image6);
ImageView iv7 = (ImageView)findViewById(R.id.image7);
//.....
and put all in one list,
images.add(iv1);
images.add(iv2);
// add other view
then work with this list and do what you want like:
for (ImageView iv : images)
{
// your code
}

// Add views from array of ids
ArrayList<View> views = new ArrayList<>();
int [] ids = { R.id.imgBtn1, R.id.imgBtn2, R.id.imgBtn9 };
for (int i=0; i<ids.length; ++i){
views.add(findViewById(ids[i]));
}
// Loop each view and enable it
for (View view : views) {
view.setEnabled(true);
}

Related

Get Resource Id from multiple ImageViews and assign drawable to them

My app has a GridLayout and 60 ImageViews on it. I want to assign a drawable to 25 cells of grid layout. for that purpose i have an array with 25 random numbers.
then i defined a hashmap that each item is one of the image views:
private HashMap<Integer, Integer> imageViews = new HashMap<>();
imageViews.put(0, R.id.imageView1);
.
.
.
imageViews.put(59, R.id.imageView60);
and put 60 imgageviews in hashmap ... 0 is id for first imageview.
and set drawable to cells with this method:
private void setBombDrawable(){
HashMap<Integer, Integer> map = mImageViewsMap.getImageViews();
for (int tag : randomBombArray) {
int id = map.get(tag);
ImageView imageView = findViewById(id);
imageView.setImageResource(R.drawable.exploded);
}
}
code runs without problem, but if there is any simpler way for getting imageviews id or tag from grid layout instead of 60 lines of code?
Why just id when you can get the ImageView directly from the GridLayout. GridLayout is a ViewGroup, so you can loop through all of its child view.
private Map<Integer, ImageView> imageViews = new HashMap<>();
int childCount = gridLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = gridLayout.getChildAt(i);
if (v instanceof ImageView) {
map.put(i, (ImageView) v);
}
}

Declare ImageView programmatically Android

How can I declare some ImageViews programmatically in oncreate method ? I want to have something like this:
for (int i =1 ; i<=10;i++){
ImageView image+i = (ImageView)findViewById(R.id.image+i);
}
I don't know if it is possible something like this, or you have to make it imageview by imageview.
ImageView[] imageViews = new ImageView[10];
for (int i =1 ; i<= 10; i++){
int id = getResources().getIdentifier("image"+i, "id", getPackageName());
imageViews[i - 1] = (ImageView)findViewById(id);
}
getIdentifier returns the resource's id associate to the name, the first parameter, and you could use an array to store the view's reference.
you can use something like this method
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image+i = new ImageView(this);
image+i.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image+i.setMaxHeight(20);
image+i.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
go through this tutorial and this too.

Adding Dynamically ImageViews to horizontal scrollview in android

I'm trying to show images dynamically by string as ID and want to display it in Horizontal scroll-view as i have 13 cards which can't be fit on screen
Here is my code its working fine but i want it in Horizontal Scroll View
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 43, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(id);
im.setOnClickListener(this);
counter++;
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicksound);
}![enter image description here][2]
As per your requirement I will suggest you to RecyclerView which comes with library which you will have to add and use RecyclerView with horizontal orientation refer to link below.
https://developer.android.com/training/material/lists-cards.html
Or you can do it dynamically as below
here what you can do add Linear horizontal layout inside scrollview say id is imgcontainer then you can do is
create image view dynamically and add to linear layout.
for(int i=0;i<10;i++)
{
//create imageview here and setbg
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
((LinearLayout) findViewById(R.id.imgcontainer)).addView(
imageView, i);
}
int[] image_array = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image10};
LinearLayout ll = (LinearLayout) findViewById (R.id.ll_images);
for (int i=0 ; i<10; i++){
ImageView img = new ImageView (this);
img .setBackgroundResource (image_array [i]);
ll .addView(img );
}

ImageView event when in view

I have a layout like so.
HorizontalScrollView
LinearLayout (Horizontal)
ImageView (A number of times)
Is there any event/way I can tell which ImageView is visible? I'd like an event if possible so I can store the index of the current Image that's visible.
Something like
class MyViewClass
private int index;
....
imageView.onEventViewListener(...
index = currentImageIndex;
);
EDIT: My current code loading the ImageViews
OnClickListener click = new OnClickListener() {
#Override
public void onClick(View v) {
Object i = v.getTag();
smoothScrollTo((width*((Integer) i+1)), 0);
}
};
for(int i = 0;i < mImages.size();i++) {
Gallery.Image image = mImages.get(i);
ImageView iv = new ImageView(getContext());
ViewGroup.LayoutParams par = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(par);
iv.setTag(i);
iv.setOnClickListener(click);
Picasso.with(getContext())
.load("http://www.example.com/files/galleries/" + image.getGalleryId() + "/" + image.getSrc() + "_full.jpg")
.into(iv);
layout.addView(iv);
}
You can use viewpager; it will provide all the events that's required by you
You can try this widget:HorizontalListView.
Its attribute mLeftViewAdapterIndex means first Visiable item's index.
Here is the source code in github.
https://github.com/MeetMe/Android-HorizontalListView/blob/master/AndroidHorizontalListView/src/com/meetme/android/horizontallistview/HorizontalListView.java

Add images to a HorizontalScrollView

I want to create a HorizontalScrollView which reads the images from drawable folder. The name of the images are "image1" "image2" ... "image20". I donĀ“t know how I can use the numbers to read them. Here is what I have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (R.drawable.image1);
sv.addView(iv);
}
}
You can do that in two ways.
First one is to create array with the id's of images you want to use and in your for cycle, just add the images to your layout:
int[] images = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image20};
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=0 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (images[i]);
sv.addView(iv);
}
Or the second way, you can create something similar to this:
for (int i=1 ; i<=20; i++){
String uri = "drawable/image"+i;
// int imageResource = R.drawable.image1;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView iv = new ImageView (this);
iv.setBackgroundResource (imageResource);
sv.addView(iv);
}
I didn't tested the codes, but I think they should work.
If you wish to use drawables without an array list, you can do this:
getResources().getIdentifier("Name of the Drawable", "drawable", "Your Package Name");
So your code will be:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
int myImage = getResources().getIdentifier("image"+i, "drawable", "Your Package Name");
iv.setBackgroundResource(myImage);
sv.addView(iv);
}
}
A lot of examples like this show building a list of your images first. And then you can use your code and iterate through the list.
So something like
List<Drawable> imagesToAdd = Arrays.asList(R.drawable.image1,R.drawable.image2, .... R.drawable.image20);
Then you can even use the foreach loop to iterate through this.
for (Drawable image in imageToAdd) {
etc...
}

Categories

Resources