I am trying to dynamically creating imageview but it is not visible my code is:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.MultiQuesRelaLayOut);
final ImageView[] im=new ImageView[10];
for(int img=0; img<2.length; img++)
{
im[img]=new ImageView(this);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin=25;
params.topMargin=(img+1)*(35+add);
im[img].setId(img);
im[img].layout(5,5,5,5);
im[img].setVisibility(View.VISIBLE);
im[img].setBackgroundColor(color.background_dark);
im[img].setPadding(5, 15, 5, 15);
im[img].setLayoutParams(params);
layout.addView(im[img]);
im[img].setOnClickListener(ImageOnClick(im[img]));
}
How to view imageview on form by changnig it s background image or some thing like that ?
Hopes for your suggestion
Thanks
You have created the ImageView with this LayoutParams: wrap_content, wrap_content, but you have not add img to the ImageView, so, the content length is 0 for height and width.
Related
Add View dynamically in LinearLayout and after adding View change any View background on the click button.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
ImageView img1 = new ImageView(this);
img1.setLayoutParams(layoutParams);
img1.setImageBitmap(icon);
llLayout.addView(img1);
ImageView img2 = new ImageView(this);
img2.setLayoutParams(layoutParams);
img2.setImageBitmap(icon);
llLayout.addView(img2);
ImageView img3 = new ImageView(this);
img3.setLayoutParams(layoutParams);
img3.setImageBitmap(icon);
llLayout.addView(img3);
On button, click change all imageview background or particular ImageView.
Note : llLayout is my linear layout this layout adding in XML
when you are adding imageView into linear layout, at that time you are setImageBitmap to imageView.
if you want to reset Image to Imageview, you should use img1.setImageResource
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//use anyone as your requirement
img1.setBackgroundResource(); // for set background resource from drawable folder(for src)
img1.setBackground();// for set background(Drawable)(for background)
img1.setBackgroundColor(); //for set background color(for background)
}
});
I am not sure if I understand the question correctly, but If you mean change specific image background with onClick event on a button .. If you have the image reference change it .. but if you mean you are adding the image view dynamically like inside for loop and you don't have the reference you can create Arraylist and add the added images into it then loop on this arraylist to change all the images background or filter the specific image you want to change
I've been working on a system overlay and I found that moving my imageview around the screen is a hassle using WindowManager.LayoutParams x and y, so I've decided to make a RelativeLayout with the height of the device to hold my imageview. The only problem is I want to make the Layout non clickable so that the activity below it can be clicked, but not the imageview itself as it launches an activity. Is there anyway to do this? Here is my code so far
final RelativeLayout floaterLayout = new RelativeLayout(getApplicationContext());
//this layout makes the relativelayout non clickable so that the activity below it can be clicked but it doesn't allow any children to be clicked either.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.height = 2560;
params.y = 0;
floaterLayout.setBackgroundColor(Color.RED);
floaterLayout.setAlpha(0.5f);
floaterLayout.setLayoutParams(params);
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).addView(floaterLayout, params);
ImageView view = new ImageView(getApplicationContext());
Util.setImageDrawable(view, R.drawable.floater_dots);
floaterLayout.addView(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
#Nicolas you are creating a relative layout that's not added to your parent layout.Simply create a xml with a relativeLayout and an imageView. Set that imageView's onClickListener in java code. Through this the parent layout will be nonclickable and only imageview will be clickable
I have a resource png of a house linked if you want to view it. It's basically just a dark home button.
I have it included and imported as an image asset as a drawable.
I'm attempting to set it as a button, but change it to be a white color programmatically.
Here's how I'm attempting to change the color to be white:
ImageButton txtbtnAccept = new ImageButton(this);
this._surveyHomeButton = txtbtnAccept;
txtbtnAccept.setId(this.generateViewId());
txtbtnAccept.setLayoutParams(new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
layoutParams = (RelativeLayout.LayoutParams) txtbtnAccept.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.height = 200;
layoutParams.width = 200;
txtbtnAccept.setScaleType(ImageView.ScaleType.FIT_CENTER);
txtbtnAccept.setAdjustViewBounds(true);
txtbtnAccept.setLayoutParams(layoutParams);
txtbtnAccept.setBackgroundResource(0);
txtbtnAccept.setVisibility(View.GONE);
Bitmap homeImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_home);
Drawable whiteImg = new BitmapDrawable(getResources(), homeImage);
whiteImg.setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
txtbtnAccept.setImageDrawable(whiteImg);
txtbtnAccept.setImageBitmap(homeImage);
Any idea where I'm going wrong?
What you want is called tinting. Have a look at DrawableCompat.setTint(Drawable, int).
I want to add an ImageView within the onClick() of the Button. But the ImageView should be aligned to the centre of the button.
How can I do this pragmatically?
I'm creating a new instance of the ImageView to be added on top of the View that called it and it's position is equivalent of the View that called it.
How can I get the X and Y coordinates?
EDIT:
#Override
public void onClick(final View v) {
if(v.getWidth() > v.getHeight()) size = v.getHeight();
if(v.getWidth() < v.getHeight()) size = v.getWidth();
RelativeLayout.LayoutParams button = new RelativeLayout.LayoutParams(size,size);
buttonc = new ImageView(getApplicationContext());
buttonc.setBackgroundResource(R.drawable.round);
layout.addView(buttonc,button);
I'm using the above code to attempt to pragmatically set the size of the ImageView to that of the button, and it's working.
As for the coordinates, I still haven't figured it out.
To add clarity: I'm attempting to create a rounded ripple effect over the view that calls it.
I suggest a better solution create an image view before hand place it in the xml as you see fit,at first set its visibility to false .
On click switch its visibilty to true, and the image will appear
This solution is easier than what you are searching.
Feel free to ask questions
to add Imageview
#Override
public void onClick(final View v) {
//LinearLayout
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView
ImageView imageView = new ImageView(this);
//image resource
imageView.setImageResource(R.drawable.specs);
//image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
}
I am dynamically adding ImageViews to layout. Here is the code, which is doing what I want:
setContentView(R.layout.main);
RelativeLayout main = (RelativeLayout) findViewById(R.id.main_view);
ImageView smokeImage = new ImageView(this);
Drawable smoke = getResources().getDrawable(R.drawable.smoke);
smokeImage.setBackgroundDrawable(smoke);
main.addView(smokeImage, 800, 800);
How to add ImageView to specific coordinates? For example. I want it to appear on x=25px and y=43px . Are there any special functions?
The easiest way is to set margins to your image view. You can use create RelativeLayout.LayoutParams, set margins to params
params.setMargins(left, top, right, bottom);
and set params to your image view
smokeImage.setLayoutParams(params);