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);
}
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'm having a problem with this code. I need to dynamically add buttons to my layout. This code works fine, with one exception. The second button sits on top of the first. This must have something to do with LayoutParams, but I'm not sure what.
private void buttonmaker (Button button)
{
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rlayout.addRule(RelativeLayout.CENTER_VERTICAL);
rlayout.addRule(RelativeLayout.ALIGN_LEFT);
rlayout.width = 100;
button.setId(Atom.count);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
atoms[id].getname();
TextView textview = (TextView)findViewById(R.id.textView2);
textview.setText(textview.getText()+String.valueOf(atoms[id].getname()));
}
});
if (Atom.count > 1) rlayout.addRule(RelativeLayout.RIGHT_OF,Atom.count-1); else rlayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setLayoutParams(rlayout);
RelativeLayout v = (RelativeLayout) findViewById(R.id.rlayout);
v.addView(button);
}
the problem is you set de button in the relativelayout, in this component the objects are being added one above the other, you try create linearlayout global with orientation vertical or horizontal depending on what you want and added buttons, and its all
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.
I have a RelativeLayout completely created within java, not using xml layout.
I have a few buttons on the left if my screen. I want to click on one button and show a default image from my res/drawable next to the button and make it disappear again on second click.
What I have tried was toggling the visibility but my onClick() raises a FATAL EXCEPTION, NullPointer Exception.
This is my code so far. Hardcoded the image shows right when I set picview.setVisibility(View.INVISIBLE); by hand. What I am doing wrong in the onClick() ?
private ImageView picview;
//*snip* loads of other code
//Show Image Button
ImageButton show_pic = new ImageButton(this);
show_pic.setBackgroundColor(Color.WHITE);
show_pic.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if(picview.getVisibility() == View.INVISIBLE)
{
picview.setVisibility(View.VISIBLE);
}
else if (picview.getVisibility() == View.VISIBLE)
{
picview.setVisibility(View.INVISIBLE);
}
}
});
params = new RelativeLayout.LayoutParams(40, 40);
params.topMargin = 10;
params.leftMargin = 10;
params.addRule(RelativeLayout.BELOW, button2_id);
rl.addView(show_pic, params);
//Imageview loaded from drawable
ImageView picview = new ImageView(this);
params = new RelativeLayout.LayoutParams(200, 400);
params.topMargin = 0;
params.leftMargin = 30;
params.addRule(RelativeLayout.RIGHT_OF, button2_id);
picview.setImageResource(R.drawable.my_image);
picview.setVisibility(View.INVISIBLE);
rl.addView(picview, params);
this.setContentView(rl);
You are accidentally creating two copies of picview. Shorten this line:
ImageView picview = new ImageView(this);
To:
picview = new ImageView(this);
(Your field variable private ImageView picview; never changed from null, so when you clicked your Button you see the NullPointerException...)
change
show_pic.setOnClickListener(new OnClickListener() to
show_pic.setOnClickListener(new View.OnClickListener()