Changing ImageButton margins in code - android

I have a Relative Layout with an ImageButton defined in xml. The image button has an attribute android:layout_marginBottom = "25dp".
Is it possible to change this attribute at run time, I want to move the button depending on screen size. I thought there would be a ImageButton.setMarginBottom method but it seems not. Another little inconsistency with android.

Try this:
ImageButton imgBtn = (ImageButton)findViewById(R.id.img_btn);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imgBtn.getLayoutParams();
params.setMargins(0, 0, 0, 25); //bottom margin is 25 here (change it as u wish)
imgBtn.setLayoutParams(params);

Related

Android: Imagebutton I set programatically are distored in size

this is like the x-time that I am doing this however today nothing works.
Im basically doing this:
ImageButton btnComments = new ImageButton(this);
ImageButton btngreenLikes = new ImageButton(this);
ImageButton btnblueLikes = new ImageButton(this);
btnComments.SetBackgroundResource(Resource.Drawable.comments_small);
btngreenLikes.SetBackgroundResource(Resource.Drawable.upvote_green);
btnblueLikes.SetBackgroundResource(Resource.Drawable.upvote_blue);
LinearLayout.LayoutParams lpWrap = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
btnComments.LayoutParameters = lpWrap;
btngreenLikes.LayoutParameters = lpWrap;
btnblueLikes.LayoutParameters = lpWrap;
linlayForImages.AddView(btnComments);
linlayForImages.AddView(btngreenLikes);
linlayForImages.AddView(btnblueLikes);
Set add thre imagebuttons in within my code. Give all three a background resource, then set their layout to WRAP CONTENT in HEIGHT and WIDTH.
And then add their views into my layout.
The result ist, they are everything BUT wrapped content. While the comment img is correct, th other two are distored. greenlikes is too big in total size and blue like is top big in width? I use those imagebutton resources in another activity where I set those with the xml and they are fine. So the resource is all clear. Can someone tell me what the HELL is going on here?
Update: Naming my Imagebuttons Imageviews solved this issue. Don't!

Android: How to dynamically place views with relative spacing

I am dynamically placing views into a RelativeLayout. Currently, I am doing:
layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, position, 0, 0);
relativeLayout.addView(textViewInstance, layoutParams);
where position is incremented by a fixed amount after each TextView object is added to the layout. The problem is that some of the TextViews contain long strings, which makes for awkward formatting on devices with smaller screen sizes because the string will spill into multiple lines and bleed into the whitespace.
Is there any way I can add these TextViews with spacing relative to the TextView right above it? I want to achieve something to the effect of:
layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, previousTextViewInstance.bottomPixel + 50, 0, 0);
relativeLayout.addView(textViewInstance, layoutParams);
where previousTextViewInstance will be saved after each new TextView is added.
Is this possible?
Instead of setting margin for each view, you can dynamically add each view below the previous one in relative layout.
For that, first set an id for each of your textView.
firstTextView.setId(textView_id_1);
while adding second textView, set rule for layout params as:
layoutParams.addRule(RelativeLayout.BELOW, textView_id_1);
secondTextView.setLayoutParams(layoutParams);
secondTextView.setId(textView_id_2);

Android change size programmatically

I need to change the size of the buttons programmatically.
I read some answers and I do the next to change the button size.
button.setLayoutParams(new RelativeLayout.LayoutParams(150, 50));
This code change the size correctly but the problem is that the button not stay in the position I want, it moves the button to the top left corner.
I need to change the button size but I don't want to change de position.
How can I fix this?
You have to use the existing RelativeLayout Rules as well,i.e whether the item is center aligned,align parent left etc. Or you can make use of the existing Params and modify the width and height as follows the RelativeLayout rules will not be modified.
RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.width=150;
layoutParams.height=50;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
params.width = 500;
params.height = 500;
button.setLayoutParams(params);

How to create button at run time on specified location inside frame layout android?

I am trying to create button at runtime.I am getting the coordinate and height,width of button from backend and I have to create button at same location on run time.I have used following code.
Button btn=new Button(getApplicationContext());
btn.setText("CLICK ME");
#SuppressWarnings("deprecation")
AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams( 121, 58, 50, 50);
btn.setLayoutParams(param);
mParentView.addView(btn);
My xml is
<FrameLayout
android:id="#+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ViewFlipper>
<fragment
android:id="#+id/header_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.sdei.presentationapp.activity.PresentationModeTopBar"
tools:layout="#layout/presentation_top_bar" />
</FrameLayout>
here the parentview is framelayout.
I am able to create the button but the problem is it is created always at top left corner,no matter what coordinate we pass.Please help.
Thanks in advance.
You cannot set button at your desired position in framelayout only possible in absolute layout. but you can use margin with respect to your left and top which will work like your (x, y) coordinates.
// First create your button:
Button test_button = new Button(getApplicationContext());
test_button.setText("test");
// Then create layout params for you buttons.
FrameLayout.LayoutParams lp = new LayoutParams(100, 100); // Button width and button height.
lp.leftMargin = 200; // your X coordinate.
lp.topMargin = 300; // your Y coordinate.
// Then find layout and add button on it.
FrameLayout layout = (FrameLayout) findViewById(R.id.FrameLayout1);
layout.addView(test_button, lp);
Hope this can help you.
I have not compile this code, so change as per your layout.
LinearLayout ll = new LinearLayout(this);
fl .setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setId(ok_id)
okButton.setText("some text");
See one reference link here
The absolute layout class is deprecated, you are encouraged to use Frame Layout or Relative layout instead.
The reason of that is it won’t be compatible with all the android phones as they have different screen sizes and resolutions.
Absolute layout lays widgets by specifying their exact X and Y positions. In android the origin (0,0) coordinate is located at the top left of the screen. By default, if you define any control in absolute layout without defining it’s x,y coordinates, it will be placed in the origin point at (x,y)=(0,0);
Thus your defined position for AbsoluteLayout.LayoutParams is not working properly.
As you are using FrameLayout then you can try the following way-
// declare and initialize LayoutParams for the framelayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
// decide upon the positioning of the button //
// you will likely need to use the screen size to position the
// button anywhere other than the four corners
params.setMargins(.., .., .., ..);
// use static constants from the Gravity class
params.gravity = Gravity.CENTER_HORIZONTAL;
btn.setLayoutParams(params);
mParentView.addView(btn);

How to set Margin Left ,Margin top,Margin right and Margin Bottom alignment for popup window dynamically in android

I am currently doing an android application that contains a popup window named as mypopup. It contains a Image button and four textviews. i want to align the popup window dynamically with margin top,margin bottom,margin left and margin right parameters..In my code setmargin method is not working..please anybody help me to do this...
It depends that which layout you are using. Below example places a RelativeLayout in a LinearLayout.
LinearLayout linearLayoutParent;
RelativeLayout relativeLayout;
RelativeLayout.LayoutParams margin = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
margin.setMargins(0, 0, 0, 7); //7px bottom margin
//create the linear and the relative layouts
//...add other stuff here...
// Add view with its margins
linearLayoutParent.addView(relativeLayout, margin);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0, 5, 0);

Categories

Resources