What I am trying to do is simply add an imageButton view as a child of a custom view, in the top right corner. My custom view extends Webview. I have had no problem adding the imageButton to my custom view, I just can't move it to the top right corner. I need to do this programmatically, not in xml. I would never think something this simple would require me to post on here, but I really can't figure it out.
Here is what I am using to add the imageButton to the custom view currently, but it's at the top left corner:
public class customView extends WebView {
private void imageButtonInit() {
ImageButton closeImageButton = new ImageButton(getContext());
Drawable image = getResources().getDrawable(com.package.image);
closeImageButton.setBackgroundDrawable(image);
closeImageButton.setLayoutParams(new RelativeLayout.LayoutParams(25, 25));
closeImageButton.bringToFront();
customView.this.addView(closeImageButton);
}
}
What I have tried in order to move it to the top right corner:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(25, 25);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
closeImageButton.setLayoutParams(lp);
customView.this.addView(closeImageButton);
As well as:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(25, 25, Gravity.RIGHT|Gravity.TOP);
closeImageButton.setLayoutParams(lp);
customView.this.addView(closeImageButton);
All of which results in the imageButton being placed in the top left corner. This is my first post on this site so I hope the question is properly formatted. Please no xml solutions. Thanks everyone.
I think what you are doing in your other two methods is literally setting your CustomView to have those layout params. What you should try instead is set the layout params of your child using this method;
customView.this.addView(child, params);
where child is your image button and params is the layout params.
Related
I have Image in my layout and I want when user clicked on image, 4 drawable (corner button) added in corner of the image. How can I do this?
Add id field to the relative layout in the xml. Use that id in the class to create view dynamically.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.YourID);
imageView.setImageDrawable(R.drawable.image);
//Add View to Layout:
relativeLayout.addView(imageView);
well there are many ways to that.
Using layoutparams you can set layout params programatically.
like
LayoutParams param = new LayoutParam(LayoutPara.width, LayoutParam.height);
//your rules...
imageView.setlayoutParams(param);
here you can set margintop, bottom, left and right and alignParentLeft.... so on
I have a RelativeLayout in my Android App. Now I want to show an ImageView in front of that Layout. The problem is that the ImageView is not in the front, it's a bit transparent and I can see things like EditText and Button. I can't change the Layout (setContentView), because the Layout is created dynamically and after setContentView, the Controls are away.
You can add view programmatically, and in the way that it will be on the top!
create id for you top level layout
Now some code (in my case it's relative layout):
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
ImageView imageView = new ImageView(context)
Drawable rightArrowBlackDrawable = getResources().getDrawable(R.drawable.image);
imageView.setLayoutParams(getLayoutParams());
relativeLayout.addView(imageView);
imageView.bringToFront();
//here just example layout params, use yours params ;-)
private RelativeLayout.LayoutParams getLayoutParams() {
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
return layoutParams;
}
You can bring it to the front once you insert it.
imageView.bringToFront();
If the image is transparent, you can set a white background to prevent things below it from showing.
imageView.setBackgroundColor(0xFFFFFF);
I have a custom view which will be jar'ed up and added into another project. In the view I want to give an option of a button.
Here is what I have in the CustomView class.
final CustomView currentView = (CustomView) findViewById(this.getId());
RelativeLayout.LayoutParams params = (new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT ));
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
closeButton.setLayoutParams(params);
currentView.addView(closeButton);
This is all wrapped in a RelativeLayout Tag as well as the other objects of the application
Everything compiles however in the CustomView the Button is aligning left instead of right.Any Ideas???
I would guess the problem is your CustomView. It probably doesn't take the entire width of the window, and is just wide enough to fill its children (which, in your case, is the close button). Make sure your CustomView has a fill_parent horizontal layout.
Since your CustomView extends WebView, which, in turn, extends AbsoluteLayout, you can't expect it to handle RelativeLayout's parameters. Instead, it's best you put your customview and your close button inside a RelativeLayout and position them properly.
When adding your closeButton to your currentView you need to supply the LayoutParams as an argument as well in order for them to take effect.
Basically, switch
currentView.addView(closeButton);
with
currentView.addView(closeButton, params);
Since the width of your button is set to wrap_content, you could also try setting its layout_gravity to right.
params.gravity = Gravity.RIGHT;
I am new to android and stuck in a very basic problem.I am working on an application in which I need to swipe images on fling.On every image I have to add buttons dynamically.I am adding buttons using AddContentView() to add buttons.Everything is working fine but I want to set the position of buttons dynamically.I have read at many places,everyone is using addView() to add buttons and setting their positions.I have tried this
but it isn't working.Can anyone please tell me how to set the margins(position) of button using addContentView().Any help would highly be appreciated.
Setting a buttons margin using addView works for me. Be sure to pass the right LayoutParams object to the ViewGroup that should hold your button.
FrameLayout fl = new FrameLayout(context);
Button b = new Button(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
params.setMargins(top, left, bottom, right);
fl.addView(b,params);
should work.
I have a custom view (an extension of a TextView) that I want to dynamically add to my Layout (don't want to include it in the main.xml file).
The book says to fetch the RelativeLayout using findViewById() in my java code then create a new instance of my custom view, then use addView on the RelativeLayout to add the new view.
I'm not getting any errors, but when I click my button to add the new view, nothing is happening (view isn't being added). Do I need to set additional properties on my custom view (layout width, layout height for example) in order for it to be shown?
EDIT: adding code
// changed to an imageview as I thought it might be easier to see an image
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rellay);
MyCustomImageView mciv = new MyCustomImageView(null);
mciv.setId(5);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mciv.setLayoutParams(p);
mciv.setImageResource(R.drawable.someImage);
rel.Addview(mciv);
Please post your code where you add the view.
But yes, you might be missing the params for width and height. Try something like
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
txtView.setLayoutParams(p);
or what you would like the width and height to be. Also in xml layout, layout_width and layout_height are required attributes.