How to setImageResource of a dynamically declared ImageButtons? - android

I have some dynamically declared ImageButtons, these ImageButtons don't have ids' and they are declared in a LinearLayout, I want to create a method to change Images resources of these ImageButtons when called, and as I don't have a predefined ids' for these ImageButtons I'm using the function getChildAt() with the LinearLayout, but getChildAt() doesn't provide setImageResource(), it just provide setBackgroundResource() and this function doesn't make a replacement to the old Image and also doesn't fit at the old Image as it provides a background not an Image Resource, what can I do with that ?
this is my method code :
private void my_method(int drawable) {
int count = Righter.getChildCount(); // Righter is the LinearLayout
for (int i = 1; i < count; i++) {
Righter.getChildAt(i).setBackgroundResource(drawable); // here is where i change the background image
Righter.getChildAt(i).setClickable(true);
}
}

getChildAt() returns View. you need to typecast this View to ImageButton and call setImageResource() method...
ImageButton imageButton = (ImageButton) linearLayout.getChildAt(0);
imageButton.setImageResource(R.drawable.btnimage1);

try this
private void my_method(int drawable) {
int count = Righter.getChildCount(); // Righter is the LinearLayout
for (int i = 1; i < count; i++) {
((ImageButton)Righter.getChildAt(i)).setImageResource(R.drawable.btnimage1);
Righter.getChildAt(i).setClickable(true);
}
}
type cast you Righter.getChildAt(i) with ImageButton

Related

Android: How to programmatically select a group of Views and modify it?

My requirement is to apply '6' different colors to '6' ImageViews on programatically. In my Layout i can have more than six ImageViews and these colors has to be applied only to those specific ImageViews.
for (int i = 0; i < view.getChildCount(); i++) { }
But how do i identify those specific 6 ImageViews among all the views?
You can mark specific ImageViews with setTag() function (eg. imageView.setTag("Specific")) on their initialization and after that do next
for(i = 0; i < view.getChildCount(); i++){
//check for view is ImageView
if(view.getChildAt(i) instanceof ImageView){
//check for tag
if(((String) view.getChildAt(i).getTag()) == "Specific"){
//code for set color
}
}
}
Hope it helps

Alternative to adding dynamic view inside of a ViewHolder RecyclerView Adapter

I am currently adding ImageViews and setting the images within a ViewHolder. The creation time is not smooth and the first scroll through is not smooth either. Any suggestion or an alternative to having smooth dynamic views within a viewholder?
ViewHolder
class ViewHolderRecipientExpanded extends RecyclerView.ViewHolder {
private LinearLayout mFlagLayout;
//
public ViewHolderRecipientExpanded(View v) {
super(v);
mFlagLayout = (LinearLayout) v.findViewById(R.id.flagLayout);
//
}
public void initiateRecipientsDetails() {
Recipient recipient = objectList.get(getAdapterPosition());
int totalAmountOfCountries = recipient.getFlags() != null ? recipient.getFlags().size() : 0;
//
int countriesLimitedToThree = recipient.getFlags() != null ? (totalAmountOfCountries > 3 ? 3 : totalAmountOfCountries) : 0;
View imageView;
ImageView image;
Drawable drawable;
for (int i = 0; i < countriesLimitedToThree; i++) {
imageView = inflater.inflate(R.layout.view_image_flag, null);
image = (ImageView) imageView.findViewById(R.id.image);
drawable = new BitmapDrawable(context.getResources(), recipient.getFlags().get(i).getFlag());
image.setImageDrawable(drawable);
mFlagLayout.addView(imageView);
}
}
}
Load your images asynchronously, and cache them in memory when they are not in view. This will not block the UI thread when loading images, allowing the user to scroll even if the images aren't loaded yet. Though, it will still take some time for the images to actually appear.
There are also a number of great libraries that will handle this for you:
Picasso
Glide
Fresco

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.

Android - Access child of gridview that is not on screen

Is it possible to access a child view of a gridview that is not on screen and change it?
I have tried using getCount() when iterating over the gridview, but when it reaches the children that are not visible(on screen display) they lose their type (they should be ImageViews) and I can not do anything with them; I want to change their image.
An snippet of code;
GridView gridView = (GridView) findViewById(R.id.grid_view);
Integer items = gridView.getCount();
Log.d(TAG, "1)child count::"+((ViewGroup)gridView).getChildCount());
Log.d(TAG, "2)get count::"+items);
for (int i=0; i < items; ++i) {
View nextChild = ((ViewGroup)gridView).getChildAt(i);
if (nextChild instanceof ImageView) {
Log.i(TAG, i+" is imageview");
((ImageView) nextChild).setImageResource(R.drawable.arrow2);
} else {
Log.i(TAG, i+" is NOT imageview");
}
}
You should be setting the image resource in the adapter's getView() method. Then the code will run as and when they become visible, rather than trying to force it before they come into the screen.

How to get ViewFlipper's current child position

I have added a bunch of images to a ViewFlipper and now I am performing an onClick event in the flipper. For this I would like to know the current child position so that I can perform some operations in an array. Is there anyway to find out the position of the current child.
Use this to get the current Child position:
flipper.getDisplayedChild();
And this to set child number to view:
flipper.setDisplayedChild(8);
In addflipperimages(ViewFlipper flipper) method you are adding bunch of images to ViewFlipper for that you are creating imageview, set tag to imageview, set imageview clickable true then write onclick method to imageview. go through the fallowing code it may works for you
here ids[] is an array of image ids
private void addFlipperImages(ViewFlipper flipper) {
int imageCount = ids.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
for (int count = 0; count <imageCount; count++) {
ImageView imageView = new ImageView(this);
Bitmap imbm = BitmapFactory.decodeResource(this.getResources(),ids[count]);
imageView.setImageBitmap(imbm);
imageView.setLayoutParams(params);
imageView.setTag(count);
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id=(Integer) v.getTag();
Toast.makeText(ImageSliderVertical.this, id+"", Toast.LENGTH_LONG).show();
}
});
flipper.addView(imageView);
flipper.setTag(count);
}
}
Make use of indexOfChild().
Check this Post
How can I programmatically display a ViewFlipper's second child?
May this works for you.
I used this flipper.indexOfChild(flipper.getCurrentView())

Categories

Resources