Android change size programmatically - android

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);

Related

Center view in android after changing its width properties

I'm changing the width of a grid view based on its column width and number of columns (which works):
gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);
I've tried re-centering it in the parent view but its not working, does anyone know why, its still centred as if it still has the width parameters before my dynamic change.
Set the Gravity as LayoutParams property then set that LayoutParams to your GridView as follows...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
gridView.setLayoutParams(params);
you cant center it if its bigger than its parent, which is never bigger than the screen, unless you set it manually, which will cause the system to render data outside the screen and slow you way down.....
is it expanding verticaly or horizontally or both? my recomendation would be to put it in a scrollview.

How can i move items programmatically on the screen?

I have an image with an item in the lower left. I would like at the bottom center, and not attached at the lower end but slightly raised. How can I do? Working with some of the calculations on the size of the screen? Obviously I mean in code, not xml. thanks
have a look at LayoutParams.
for exmaple
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);
you can do all differnt alignments, params, margins and padding as in xml

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);

Android: Relative layout, change X coord

I have an Activity with relative layout on whole screen. In left top corner I have button. And on button click I want move layout from 0 X coord to 100 X coord. I tried something like this in onclick method:
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
param.leftMargin = 100;
meRelativeLayout.setLayoutParams(param);
But this thing just made my relative layout width smaller, its change left margine and thats it, but I don't want to change layout width, just want to change X coord, is there any way to do that?
To me your code seems to be perfect, only the matter is of FILL_PARENT. As you used FILL_PARENT then the system will assign the space left in the display(less margin) thats why you layout became small, So Try this.
int width= mActivity.getWindowManager().getDefaultDisplay().getWidth();
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams( width, LayoutParams.FILL_PARENT);
param.leftMargin = 100;
meRelativeLayout.setLayoutParams(param);

Changing ImageButton margins in code

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);

Categories

Resources